Allow default branch to be inferred on compare page (#17908)

* Allow default branch to be inferred

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Add test for inferred default branch

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
John Olheiser 2021-12-06 11:04:07 -06:00 committed by GitHub
parent 12a42baa5e
commit eb69c7ec8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -27,3 +27,15 @@ func TestCompareTag(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusNotFound) resp = session.MakeRequest(t, req, http.StatusNotFound)
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500") assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
} }
// Compare with inferred default branch (master)
func TestCompareDefault(t *testing.T) {
defer prepareTestEnv(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
}

View File

@ -186,6 +186,9 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
// 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch} // 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch}
// 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch} // 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch}
// 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch} // 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch}
// 4. /{:baseOwner}/{:baseRepoName}/compare/{:headBranch}
// 5. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}:{:headBranch}
// 6. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}/{:headRepoName}:{:headBranch}
// //
// Here we obtain the infoPath "{:baseBranch}...[{:headOwner}/{:headRepoName}:]{:headBranch}" as ctx.Params("*") // Here we obtain the infoPath "{:baseBranch}...[{:headOwner}/{:headRepoName}:]{:headBranch}" as ctx.Params("*")
// with the :baseRepo in ctx.Repo. // with the :baseRepo in ctx.Repo.
@ -213,10 +216,13 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
infos := strings.SplitN(infoPath, "...", 2) infos := strings.SplitN(infoPath, "...", 2)
if len(infos) != 2 { if len(infos) != 2 {
infos = []string{baseRepo.DefaultBranch, infoPath}
if strings.Contains(infoPath, "..") {
infos = strings.SplitN(infoPath, "..", 2) infos = strings.SplitN(infoPath, "..", 2)
ci.DirectComparison = true ci.DirectComparison = true
ctx.Data["PageIsComparePull"] = false ctx.Data["PageIsComparePull"] = false
} }
}
if len(infos) != 2 { if len(infos) != 2 {
log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos) log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)