@ -62,6 +62,8 @@ type PullRequest struct {
MergerID int64 ` xorm:"INDEX" `
MergerID int64 ` xorm:"INDEX" `
Merger * User ` xorm:"-" `
Merger * User ` xorm:"-" `
MergedUnix timeutil . TimeStamp ` xorm:"updated INDEX" `
MergedUnix timeutil . TimeStamp ` xorm:"updated INDEX" `
isHeadRepoLoaded bool ` xorm:"-" `
}
}
// MustHeadUserName returns the HeadRepo's username if failed return blank
// MustHeadUserName returns the HeadRepo's username if failed return blank
@ -74,6 +76,9 @@ func (pr *PullRequest) MustHeadUserName() string {
}
}
return ""
return ""
}
}
if pr . HeadRepo == nil {
return ""
}
return pr . HeadRepo . OwnerName
return pr . HeadRepo . OwnerName
}
}
@ -97,38 +102,55 @@ func (pr *PullRequest) LoadAttributes() error {
return pr . loadAttributes ( x )
return pr . loadAttributes ( x )
}
}
// LoadBaseRepo loads pull request base repository from database
func ( pr * PullRequest ) loadHeadRepo ( e Engine ) ( err error ) {
func ( pr * PullRequest ) LoadBaseRepo ( ) error {
if ! pr . isHeadRepoLoaded && pr . HeadRepo == nil && pr . HeadRepoID > 0 {
if pr . BaseRepo == nil {
if pr . HeadRepoID == pr . BaseRepoID {
if pr . HeadRepoID == pr . BaseRepoID && pr . HeadRepo != nil {
if pr . BaseRepo != nil {
pr . BaseRepo = pr . HeadRepo
pr . HeadRepo = pr . BaseRepo
return nil
return nil
} else if pr . Issue != nil && pr . Issue . Repo != nil {
pr . HeadRepo = pr . Issue . Repo
return nil
}
}
}
var repo Repository
if has , err := x . ID ( pr . BaseRepoID ) . Get ( & repo ) ; err != nil {
pr . HeadRepo , err = getRepositoryByID ( e , pr . HeadRepoID )
return err
if err != nil && ! IsErrRepoNotExist ( err ) { // Head repo maybe deleted, but it should still work
} else if ! has {
return fmt . Errorf ( "getRepositoryByID(head): %v" , err )
return ErrRepoNotExist { ID : pr . BaseRepoID }
}
}
pr . BaseRepo = & repo
pr . isHeadRepoLoaded = true
}
}
return nil
return nil
}
}
// LoadHeadRepo loads pull request head repository from database
// LoadHeadRepo loads the head repository
func ( pr * PullRequest ) LoadHeadRepo ( ) error {
func ( pr * PullRequest ) LoadHeadRepo ( ) error {
if pr . HeadRepo == nil {
return pr . loadHeadRepo ( x )
if pr . HeadRepoID == pr . BaseRepoID && pr . BaseRepo != nil {
}
pr . HeadRepo = pr . BaseRepo
return nil
// LoadBaseRepo loads the target repository
}
func ( pr * PullRequest ) LoadBaseRepo ( ) error {
var repo Repository
return pr . loadBaseRepo ( x )
if has , err := x . ID ( pr . HeadRepoID ) . Get ( & repo ) ; err != nil {
}
return err
} else if ! has {
func ( pr * PullRequest ) loadBaseRepo ( e Engine ) ( err error ) {
return ErrRepoNotExist { ID : pr . HeadRepoID }
if pr . BaseRepo != nil {
}
return nil
pr . HeadRepo = & repo
}
if pr . HeadRepoID == pr . BaseRepoID && pr . HeadRepo != nil {
pr . BaseRepo = pr . HeadRepo
return nil
}
if pr . Issue != nil && pr . Issue . Repo != nil {
pr . BaseRepo = pr . Issue . Repo
return nil
}
pr . BaseRepo , err = getRepositoryByID ( e , pr . BaseRepoID )
if err != nil {
return fmt . Errorf ( "GetRepositoryByID(base): %v" , err )
}
}
return nil
return nil
}
}
@ -414,32 +436,6 @@ func (pr *PullRequest) GetGitRefName() string {
return fmt . Sprintf ( "refs/pull/%d/head" , pr . Index )
return fmt . Sprintf ( "refs/pull/%d/head" , pr . Index )
}
}
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
}
// GetHeadRepo loads the head repository
func ( pr * PullRequest ) GetHeadRepo ( ) error {
return pr . getHeadRepo ( x )
}
// GetBaseRepo loads the target repository
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 == PullRequestStatusChecking
return pr . Status == PullRequestStatusChecking
@ -452,7 +448,7 @@ func (pr *PullRequest) CanAutoMerge() bool {
// GetLastCommitStatus returns the last commit status for this pull request.
// GetLastCommitStatus returns the last commit status for this pull request.
func ( pr * PullRequest ) GetLastCommitStatus ( ) ( status * CommitStatus , err error ) {
func ( pr * PullRequest ) GetLastCommitStatus ( ) ( status * CommitStatus , err error ) {
if err = pr . Get HeadRepo( ) ; err != nil {
if err = pr . Load HeadRepo( ) ; err != nil {
return nil , err
return nil , err
}
}
@ -774,7 +770,7 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string {
// IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
// IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
func ( pr * PullRequest ) IsHeadEqualWithBranch ( branchName string ) ( bool , error ) {
func ( pr * PullRequest ) IsHeadEqualWithBranch ( branchName string ) ( bool , error ) {
var err error
var err error
if err = pr . Get BaseRepo( ) ; err != nil {
if err = pr . Load BaseRepo( ) ; err != nil {
return false , err
return false , err
}
}
baseGitRepo , err := git . OpenRepository ( pr . BaseRepo . RepoPath ( ) )
baseGitRepo , err := git . OpenRepository ( pr . BaseRepo . RepoPath ( ) )
@ -786,7 +782,7 @@ func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
return false , err
return false , err
}
}
if err = pr . Get HeadRepo( ) ; err != nil {
if err = pr . Load HeadRepo( ) ; err != nil {
return false , err
return false , err
}
}
headGitRepo , err := git . OpenRepository ( pr . HeadRepo . RepoPath ( ) )
headGitRepo , err := git . OpenRepository ( pr . HeadRepo . RepoPath ( ) )