[BugFix] [API] Pull.API.Convert: Only try to get HeadBranch if HeadRepo exist (#10029) (#10088)

This commit is contained in:
6543 2020-02-01 02:49:45 +01:00 committed by GitHub
parent 912ce27421
commit f5727d83dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -420,7 +420,7 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
err error err error
) )
if err = pr.Issue.loadRepo(e); err != nil { if err = pr.Issue.loadRepo(e); err != nil {
log.Error("loadRepo[%d]: %v", pr.ID, err) log.Error("pr.Issue.loadRepo[%d]: %v", pr.ID, err)
return nil return nil
} }
apiIssue := pr.Issue.apiFormat(e) apiIssue := pr.Issue.apiFormat(e)
@ -431,19 +431,14 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
return nil return nil
} }
} }
if pr.HeadRepo == nil { if pr.HeadRepoID != 0 && pr.HeadRepo == nil {
pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID) pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
if err != nil { if err != nil && !IsErrRepoNotExist(err) {
log.Error("GetRepositoryById[%d]: %v", pr.ID, err) log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
return nil return nil
} }
} }
if err = pr.Issue.loadRepo(e); err != nil {
log.Error("pr.Issue.loadRepo[%d]: %v", pr.ID, err)
return nil
}
apiPullRequest := &api.PullRequest{ apiPullRequest := &api.PullRequest{
ID: pr.ID, ID: pr.ID,
URL: pr.Issue.HTMLURL(), URL: pr.Issue.HTMLURL(),
@ -495,33 +490,41 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
apiPullRequest.Base = apiBaseBranchInfo apiPullRequest.Base = apiBaseBranchInfo
} }
headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch) if pr.HeadRepo != nil {
if err != nil { headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch)
if git.IsErrBranchNotExist(err) {
apiPullRequest.Head = nil
} else {
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
return nil
}
} else {
apiHeadBranchInfo := &api.PRBranchInfo{
Name: pr.HeadBranch,
Ref: pr.HeadBranch,
RepoID: pr.HeadRepoID,
Repository: pr.HeadRepo.innerAPIFormat(e, AccessModeNone, false),
}
headCommit, err = headBranch.GetCommit()
if err != nil { if err != nil {
if git.IsErrNotExist(err) { if git.IsErrBranchNotExist(err) {
apiHeadBranchInfo.Sha = "" apiPullRequest.Head = nil
} else { } else {
log.Error("GetCommit[%s]: %v", headBranch.Name, err) log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
return nil return nil
} }
} else { } else {
apiHeadBranchInfo.Sha = headCommit.ID.String() apiHeadBranchInfo := &api.PRBranchInfo{
Name: pr.HeadBranch,
Ref: pr.HeadBranch,
RepoID: pr.HeadRepoID,
Repository: pr.HeadRepo.innerAPIFormat(e, AccessModeNone, false),
}
headCommit, err = headBranch.GetCommit()
if err != nil {
if git.IsErrNotExist(err) {
apiHeadBranchInfo.Sha = ""
} else {
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
return nil
}
} else {
apiHeadBranchInfo.Sha = headCommit.ID.String()
}
apiPullRequest.Head = apiHeadBranchInfo
}
} else {
apiPullRequest.Head = &api.PRBranchInfo{
Name: pr.HeadBranch,
Ref: fmt.Sprintf("refs/pull/%d/head", pr.Index),
RepoID: -1,
} }
apiPullRequest.Head = apiHeadBranchInfo
} }
if pr.Status != PullRequestStatusChecking { if pr.Status != PullRequestStatusChecking {