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