@ -85,6 +85,20 @@ func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
// Note: don't try to get Issue because will end up recursive querying.
func ( pr * PullRequest ) loadAttributes ( e Engine ) ( err error ) {
if pr . HeadRepo == nil {
pr . HeadRepo , err = getRepositoryByID ( e , pr . HeadRepoID )
if err != nil && ! IsErrRepoNotExist ( err ) {
return fmt . Errorf ( "getRepositoryByID.(HeadRepo) [%d]: %v" , pr . HeadRepoID , err )
}
}
if pr . BaseRepo == nil {
pr . BaseRepo , err = getRepositoryByID ( e , pr . BaseRepoID )
if err != nil {
return fmt . Errorf ( "getRepositoryByID.(BaseRepo) [%d]: %v" , pr . BaseRepoID , err )
}
}
if pr . HasMerged && pr . Merger == nil {
pr . Merger , err = getUserByID ( e , pr . MergerID )
if IsErrUserNotExist ( err ) {
@ -112,11 +126,19 @@ func (pr *PullRequest) LoadIssue() (err error) {
}
// This method assumes following fields have been assigned with valid values:
// Required - Issue
// Required - HeadRepo
// Required - BaseRepo
// Optional - Merger
// Required - Issue, BaseRepo
// Optional - HeadRepo, Merger
func ( pr * PullRequest ) APIFormat ( ) * api . PullRequest {
// In case of head repo has been deleted.
var apiHeadRepo * api . Repository
if pr . HeadRepo == nil {
apiHeadRepo = & api . Repository {
Name : "deleted" ,
}
} else {
apiHeadRepo = pr . HeadRepo . APIFormat ( nil )
}
apiIssue := pr . Issue . APIFormat ( )
apiPullRequest := & api . PullRequest {
ID : pr . ID ,
@ -130,7 +152,7 @@ func (pr *PullRequest) APIFormat() *api.PullRequest {
State : apiIssue . State ,
Comments : apiIssue . Comments ,
HeadBranch : pr . HeadBranch ,
HeadRepo : pr . HeadRepo . APIFormat ( nil ) ,
HeadRepo : apiHeadRepo ,
BaseBranch : pr . BaseBranch ,
BaseRepo : pr . BaseRepo . APIFormat ( nil ) ,
HTMLURL : pr . Issue . HTMLURL ( ) ,
@ -150,30 +172,6 @@ func (pr *PullRequest) APIFormat() *api.PullRequest {
return apiPullRequest
}
func ( pr * PullRequest ) getHeadRepo ( e Engine ) ( err error ) {
pr . HeadRepo , err = getRepositoryByID ( e , pr . HeadRepoID )
if err != nil && ! IsErrRepoNotExist ( err ) {
return fmt . Errorf ( "getRepositoryByID(head): %v" , err )
}
return nil
}
func ( pr * PullRequest ) GetHeadRepo ( ) error {
return pr . getHeadRepo ( x )
}
func ( pr * PullRequest ) GetBaseRepo ( ) ( err error ) {
if pr . BaseRepo != nil {
return nil
}
pr . BaseRepo , err = GetRepositoryByID ( pr . BaseRepoID )
if err != nil {
return fmt . Errorf ( "GetRepositoryByID(base): %v" , err )
}
return nil
}
// IsChecking returns true if this pull request is still checking conflict.
func ( pr * PullRequest ) IsChecking ( ) bool {
return pr . Status == PULL_REQUEST_STATUS_CHECKING
@ -187,12 +185,6 @@ func (pr *PullRequest) CanAutoMerge() bool {
// Merge merges pull request to base repository.
// FIXME: add repoWorkingPull make sure two merges does not happen at same time.
func ( pr * PullRequest ) Merge ( doer * User , baseGitRepo * git . Repository ) ( err error ) {
if err = pr . GetHeadRepo ( ) ; err != nil {
return fmt . Errorf ( "GetHeadRepo: %v" , err )
} else if err = pr . GetBaseRepo ( ) ; err != nil {
return fmt . Errorf ( "GetBaseRepo: %v" , err )
}
defer func ( ) {
go HookQueue . Add ( pr . BaseRepo . ID )
go AddTestPullRequestTask ( doer , pr . BaseRepo . ID , pr . BaseBranch , false )
@ -548,17 +540,11 @@ func (pr *PullRequest) UpdateCols(cols ...string) error {
// UpdatePatch generates and saves a new patch.
func ( pr * PullRequest ) UpdatePatch ( ) ( err error ) {
if err = pr . GetHeadRepo ( ) ; err != nil {
return fmt . Errorf ( "GetHeadRepo: %v" , err )
} else if pr . HeadRepo == nil {
if pr . HeadRepo == nil {
log . Trace ( "PullRequest[%d].UpdatePatch: ignored cruppted data" , pr . ID )
return nil
}
if err = pr . GetBaseRepo ( ) ; err != nil {
return fmt . Errorf ( "GetBaseRepo: %v" , err )
}
headGitRepo , err := git . OpenRepository ( pr . HeadRepo . RepoPath ( ) )
if err != nil {
return fmt . Errorf ( "OpenRepository: %v" , err )
@ -763,8 +749,8 @@ func TestPullRequests() {
func ( idx int , bean interface { } ) error {
pr := bean . ( * PullRequest )
if err := pr . GetBaseRepo ( ) ; err != nil {
log . Error ( 3 , "GetBaseRepo : %v" , err )
if err := pr . LoadAttributes ( ) ; err != nil {
log . Error ( 3 , "LoadAttributes : %v" , err )
return nil
}