Skip to content

Commit 9c8d55d

Browse files
elirocawxiaoguang
andauthored
fix(pull): handle empty pull request files view to allow reviews (#37783)
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 7e43697 commit 9c8d55d

2 files changed

Lines changed: 55 additions & 35 deletions

File tree

routers/web/repo/pull.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ func indexCommit(commits []*git.Commit, commitID string) *git.Commit {
683683

684684
// ViewPullFiles render pull request changed files list page
685685
func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
686+
var err error
687+
686688
ctx.Data["PageIsPullList"] = true
687689
ctx.Data["PageIsPullFiles"] = true
688690

@@ -707,44 +709,53 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
707709

708710
headCommitID := prCompareInfo.HeadCommitID
709711
isSingleCommit := beforeCommitID == "" && afterCommitID != ""
710-
ctx.Data["IsShowingOnlySingleCommit"] = isSingleCommit
712+
// FIXME: when afterCommitID==headCommitID, isSingleCommit and isShowAllCommits can be both true, which doesn't seem right
711713
isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prCompareInfo.CompareBase) && (afterCommitID == "" || afterCommitID == headCommitID)
714+
715+
ctx.Data["IsShowingOnlySingleCommit"] = isSingleCommit
712716
ctx.Data["IsShowingAllCommits"] = isShowAllCommits
713717

714-
if afterCommitID == "" || afterCommitID == headCommitID {
715-
afterCommitID = headCommitID
716-
}
718+
// "commits list" is half-open, half-closed: (base, head]
719+
// * base commit is not in the list
720+
// * if the PR is empty, the list is also empty (head commit is not in the list)
721+
722+
afterCommitID = util.IfZero(afterCommitID, headCommitID)
717723
afterCommit := indexCommit(prCompareInfo.Commits, afterCommitID)
724+
if afterCommit == nil && afterCommitID == headCommitID {
725+
afterCommit, err = gitRepo.GetCommit(afterCommitID)
726+
if err != nil {
727+
ctx.ServerError("GetCommit(afterCommitID)", err)
728+
return
729+
}
730+
}
718731
if afterCommit == nil {
719-
ctx.HTTPError(http.StatusBadRequest, "after commit not found in PR commits")
732+
ctx.NotFound(nil)
720733
return
721734
}
722735

723736
var beforeCommit *git.Commit
724-
var err error
725-
if !isSingleCommit {
726-
if beforeCommitID == "" || beforeCommitID == prCompareInfo.CompareBase {
727-
beforeCommitID = prCompareInfo.CompareBase
728-
// merge base commit is not in the list of the pull request commits
729-
beforeCommit, err = gitRepo.GetCommit(beforeCommitID)
730-
if err != nil {
731-
ctx.ServerError("GetCommit", err)
732-
return
733-
}
734-
} else {
735-
beforeCommit = indexCommit(prCompareInfo.Commits, beforeCommitID)
736-
if beforeCommit == nil {
737-
ctx.HTTPError(http.StatusBadRequest, "before commit not found in PR commits")
738-
return
739-
}
740-
}
741-
} else {
737+
if isSingleCommit {
742738
beforeCommit, err = afterCommit.Parent(0)
743739
if err != nil {
744-
ctx.ServerError("Parent", err)
740+
ctx.ServerError("afterCommit.Parent", err)
745741
return
746742
}
747743
beforeCommitID = beforeCommit.ID.String()
744+
} else {
745+
beforeCommitID = util.IfZero(beforeCommitID, prCompareInfo.CompareBase)
746+
beforeCommit = indexCommit(prCompareInfo.Commits, beforeCommitID)
747+
if beforeCommit == nil && beforeCommitID == prCompareInfo.CompareBase {
748+
// base commit is not in the list of the pull request commits
749+
beforeCommit, err = gitRepo.GetCommit(beforeCommitID)
750+
if err != nil {
751+
ctx.ServerError("GetCommit(beforeCommitID)", err)
752+
return
753+
}
754+
}
755+
}
756+
if beforeCommit == nil {
757+
ctx.NotFound(nil)
758+
return
748759
}
749760

750761
ctx.Data["CompareInfo"] = prCompareInfo

tests/integration/pull_status_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,29 @@ func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
140140
func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
141141
onGiteaRun(t, func(t *testing.T, u *url.URL) {
142142
session := loginUser(t, "user1")
143-
testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
144-
testCreateBranch(t, session, "user1", "repo1", "branch/master", "status1", http.StatusSeeOther)
145-
req := NewRequestWithValues(t, "POST", "/user1/repo1/compare/master...status1",
146-
map[string]string{
147-
"title": "pull request from status1",
148-
},
149-
)
150-
session.MakeRequest(t, req, http.StatusOK)
151-
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
152-
resp := session.MakeRequest(t, req, http.StatusOK)
153-
doc := NewHTMLParser(t, resp.Body)
154143

144+
testCreateBranch(t, session, "user2", "repo1", "branch/master", "empty-pr-branch", http.StatusSeeOther)
145+
resp := testPullCreateDirectly(t, session, createPullRequestOptions{
146+
BaseRepoOwner: "user2",
147+
BaseRepoName: "repo1",
148+
BaseBranch: "master",
149+
HeadBranch: "empty-pr-branch",
150+
Title: "empty pr test",
151+
})
152+
prURL := test.RedirectURL(resp)
153+
154+
// check the "merge box" text
155+
req := NewRequest(t, "GET", prURL)
156+
resp = session.MakeRequest(t, req, http.StatusOK)
157+
doc := NewHTMLParser(t, resp.Body)
155158
text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
156159
assert.Contains(t, text, "This branch is already included in the target branch. There is nothing to merge.")
160+
161+
// check the "files" tab content
162+
req = NewRequest(t, "GET", prURL+"/files")
163+
resp = session.MakeRequest(t, req, http.StatusOK)
164+
doc = NewHTMLParser(t, resp.Body)
165+
assert.Equal(t, "Diff Content Not Available", strings.TrimSpace(doc.Find("#diff-container").Text()))
157166
})
158167
}
159168

0 commit comments

Comments
 (0)