From f5727d83ddad7890965549d4e3ed8146c9e207c7 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 1 Feb 2020 02:49:45 +0100 Subject: [PATCH] [BugFix] [API] Pull.API.Convert: Only try to get HeadBranch if HeadRepo exist (#10029) (#10088) --- models/pull.go | 61 ++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/models/pull.go b/models/pull.go index c39ceb6165..9df5b90fc4 100644 --- a/models/pull.go +++ b/models/pull.go @@ -420,7 +420,7 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest { err error ) 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 } apiIssue := pr.Issue.apiFormat(e) @@ -431,19 +431,14 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest { return nil } } - if pr.HeadRepo == nil { + if pr.HeadRepoID != 0 && pr.HeadRepo == nil { pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID) - if err != nil { + if err != nil && !IsErrRepoNotExist(err) { log.Error("GetRepositoryById[%d]: %v", pr.ID, err) 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{ ID: pr.ID, URL: pr.Issue.HTMLURL(), @@ -495,33 +490,41 @@ func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest { apiPullRequest.Base = apiBaseBranchInfo } - headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch) - if err != nil { - 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 pr.HeadRepo != nil { + headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch) if err != nil { - if git.IsErrNotExist(err) { - apiHeadBranchInfo.Sha = "" + if git.IsErrBranchNotExist(err) { + apiPullRequest.Head = nil } else { - log.Error("GetCommit[%s]: %v", headBranch.Name, err) + log.Error("GetBranch[%s]: %v", pr.HeadBranch, err) return nil } } 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 {