Skip to content

Commit 9557b86

Browse files
authored
Add selecting tags on the compare page (#15723)
* Add selecting tags on the compare page * Remove unused condition and change indentation * Fix tag tab in dropdown to be black * Add compare tag integration test Co-authored-by: Jonathan Tran <[email protected]>
1 parent 4900881 commit 9557b86

File tree

5 files changed

+229
-94
lines changed

5 files changed

+229
-94
lines changed

integrations/compare_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCompareTag(t *testing.T) {
15+
defer prepareTestEnv(t)()
16+
17+
session := loginUser(t, "user2")
18+
req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1...master")
19+
resp := session.MakeRequest(t, req, http.StatusOK)
20+
htmlDoc := NewHTMLParser(t, resp.Body)
21+
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
22+
// A dropdown for both base and head.
23+
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
24+
}

options/locale/locale_en-US.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,9 @@ issues.review.resolved_by = marked this conversation as resolved
12861286
issues.assignee.error = Not all assignees was added due to an unexpected error.
12871287
issues.reference_issue.body = Body
12881288

1289+
compare.compare_base = base
1290+
compare.compare_head = compare
1291+
12891292
pulls.desc = Enable pull requests and code reviews.
12901293
pulls.new = New Pull Request
12911294
pulls.compare_changes = New Pull Request

routers/repo/compare.go

+37-16
Original file line numberDiff line numberDiff line change
@@ -391,34 +391,36 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
391391
if rootRepo != nil &&
392392
rootRepo.ID != headRepo.ID &&
393393
rootRepo.ID != baseRepo.ID {
394-
perm, branches, err := getBranchesForRepo(ctx.User, rootRepo)
394+
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
395395
if err != nil {
396396
ctx.ServerError("GetBranchesForRepo", err)
397397
return nil, nil, nil, nil, "", ""
398398
}
399399
if perm {
400400
ctx.Data["RootRepo"] = rootRepo
401401
ctx.Data["RootRepoBranches"] = branches
402+
ctx.Data["RootRepoTags"] = tags
402403
}
403404
}
404405

405406
// If we have a ownForkRepo and it's different from:
406407
// 1. The computed base
407-
// 2. The computed hea
408+
// 2. The computed head
408409
// 3. The rootRepo (if we have one)
409410
// then get the branches from it.
410411
if ownForkRepo != nil &&
411412
ownForkRepo.ID != headRepo.ID &&
412413
ownForkRepo.ID != baseRepo.ID &&
413414
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
414-
perm, branches, err := getBranchesForRepo(ctx.User, ownForkRepo)
415+
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
415416
if err != nil {
416417
ctx.ServerError("GetBranchesForRepo", err)
417418
return nil, nil, nil, nil, "", ""
418419
}
419420
if perm {
420421
ctx.Data["OwnForkRepo"] = ownForkRepo
421422
ctx.Data["OwnForkRepoBranches"] = branches
423+
ctx.Data["OwnForkRepoTags"] = tags
422424
}
423425
}
424426

@@ -572,25 +574,29 @@ func PrepareCompareDiff(
572574
return false
573575
}
574576

575-
func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []string, error) {
577+
func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool, []string, []string, error) {
576578
perm, err := models.GetUserRepoPermission(repo, user)
577579
if err != nil {
578-
return false, nil, err
580+
return false, nil, nil, err
579581
}
580582
if !perm.CanRead(models.UnitTypeCode) {
581-
return false, nil, nil
583+
return false, nil, nil, nil
582584
}
583585
gitRepo, err := git.OpenRepository(repo.RepoPath())
584586
if err != nil {
585-
return false, nil, err
587+
return false, nil, nil, err
586588
}
587589
defer gitRepo.Close()
588590

589591
branches, _, err := gitRepo.GetBranches(0, 0)
590592
if err != nil {
591-
return false, nil, err
593+
return false, nil, nil, err
592594
}
593-
return true, branches, nil
595+
tags, err := gitRepo.GetTags()
596+
if err != nil {
597+
return false, nil, nil, err
598+
}
599+
return true, branches, tags, nil
594600
}
595601

596602
// CompareDiff show different from one commit to another commit
@@ -608,14 +614,29 @@ func CompareDiff(ctx *context.Context) {
608614
return
609615
}
610616

611-
if ctx.Data["PageIsComparePull"] == true {
612-
headBranches, _, err := headGitRepo.GetBranches(0, 0)
613-
if err != nil {
614-
ctx.ServerError("GetBranches", err)
615-
return
616-
}
617-
ctx.Data["HeadBranches"] = headBranches
617+
baseGitRepo := ctx.Repo.GitRepo
618+
baseTags, err := baseGitRepo.GetTags()
619+
if err != nil {
620+
ctx.ServerError("GetTags", err)
621+
return
622+
}
623+
ctx.Data["Tags"] = baseTags
618624

625+
headBranches, _, err := headGitRepo.GetBranches(0, 0)
626+
if err != nil {
627+
ctx.ServerError("GetBranches", err)
628+
return
629+
}
630+
ctx.Data["HeadBranches"] = headBranches
631+
632+
headTags, err := headGitRepo.GetTags()
633+
if err != nil {
634+
ctx.ServerError("GetTags", err)
635+
return
636+
}
637+
ctx.Data["HeadTags"] = headTags
638+
639+
if ctx.Data["PageIsComparePull"] == true {
619640
pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
620641
if err != nil {
621642
if !models.IsErrPullRequestNotExist(err) {

0 commit comments

Comments
 (0)