Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display pull request head branch even the branch deleted or repository deleted #10413

Merged
merged 7 commits into from
Mar 2, 2020
Prev Previous commit
Next Next commit
retrieve sha from pull head when pull request branch deleted and fix …
…tests
  • Loading branch information
lunny committed Feb 23, 2020
commit 637a38a03820ee56ecb03a60ae9f6ae5342befd7
26 changes: 20 additions & 6 deletions modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
baseBranch *git.Branch
headBranch *git.Branch
baseCommit *git.Commit
headCommit *git.Commit
err error
)

Expand Down Expand Up @@ -99,22 +98,37 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = pr.HeadRepo.APIFormat(models.AccessModeNone)

headBranch, err = repo_module.GetBranch(pr.HeadRepo, pr.HeadBranch)
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
return nil
}
defer headGitRepo.Close()

headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
if err != nil && !git.IsErrBranchNotExist(err) {
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
return nil
}

if err == nil {
headCommit, err = headBranch.GetCommit()
if git.IsErrBranchNotExist(err) {
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
if err != nil && !git.IsErrNotExist(err) {
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
return nil
}
if err == nil {
apiPullRequest.Head.Sha = headCommitID
}
} else {
commit, err := headBranch.GetCommit()
if err != nil && !git.IsErrNotExist(err) {
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
return nil
}

if err == nil {
apiPullRequest.Head.Ref = pr.HeadBranch
apiPullRequest.Head.Sha = headCommit.ID.String()
apiPullRequest.Head.Sha = commit.ID.String()
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion modules/convert/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

func TestPullRequest_APIFormat(t *testing.T) {
//with HeadRepo
assert.NoError(t, models.PrepareTestDatabase())
headRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
assert.NoError(t, pr.LoadAttributes())
assert.NoError(t, pr.LoadIssue())
apiPullRequest := ToAPIPullRequest(pr)
assert.NotNil(t, apiPullRequest)
assert.Nil(t, apiPullRequest.Head)
assert.EqualValues(t, &structs.PRBranchInfo{
Name: "branch1",
Ref: "refs/pull/2/head",
RepoID: 1,
Repository: headRepo.APIFormat(models.AccessModeNone),
}, apiPullRequest.Head)

//withOut HeadRepo
pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
Expand Down