From 9557b8603aa05458a9b262d254a809d083657e62 Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Fri, 7 May 2021 17:10:05 -0400 Subject: [PATCH] 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 --- integrations/compare_test.go | 24 ++++ options/locale/locale_en-US.ini | 3 + routers/repo/compare.go | 53 ++++--- templates/repo/diff/compare.tmpl | 236 ++++++++++++++++++++----------- web_src/js/index.js | 19 ++- 5 files changed, 235 insertions(+), 100 deletions(-) create mode 100644 integrations/compare_test.go diff --git a/integrations/compare_test.go b/integrations/compare_test.go new file mode 100644 index 000000000..08468cbfd --- /dev/null +++ b/integrations/compare_test.go @@ -0,0 +1,24 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCompareTag(t *testing.T) { + defer prepareTestEnv(t)() + + session := loginUser(t, "user2") + req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1...master") + resp := session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown") + // A dropdown for both base and head. + assert.Lenf(t, selection.Nodes, 2, "The template has changed") +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 40e4e0c0b..51fa31602 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1286,6 +1286,9 @@ issues.review.resolved_by = marked this conversation as resolved issues.assignee.error = Not all assignees was added due to an unexpected error. issues.reference_issue.body = Body +compare.compare_base = base +compare.compare_head = compare + pulls.desc = Enable pull requests and code reviews. pulls.new = New Pull Request pulls.compare_changes = New Pull Request diff --git a/routers/repo/compare.go b/routers/repo/compare.go index a658374d9..d02ea0b16 100644 --- a/routers/repo/compare.go +++ b/routers/repo/compare.go @@ -391,7 +391,7 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, * if rootRepo != nil && rootRepo.ID != headRepo.ID && rootRepo.ID != baseRepo.ID { - perm, branches, err := getBranchesForRepo(ctx.User, rootRepo) + perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo) if err != nil { ctx.ServerError("GetBranchesForRepo", err) return nil, nil, nil, nil, "", "" @@ -399,19 +399,20 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, * if perm { ctx.Data["RootRepo"] = rootRepo ctx.Data["RootRepoBranches"] = branches + ctx.Data["RootRepoTags"] = tags } } // If we have a ownForkRepo and it's different from: // 1. The computed base - // 2. The computed hea + // 2. The computed head // 3. The rootRepo (if we have one) // then get the branches from it. if ownForkRepo != nil && ownForkRepo.ID != headRepo.ID && ownForkRepo.ID != baseRepo.ID && (rootRepo == nil || ownForkRepo.ID != rootRepo.ID) { - perm, branches, err := getBranchesForRepo(ctx.User, ownForkRepo) + perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo) if err != nil { ctx.ServerError("GetBranchesForRepo", err) return nil, nil, nil, nil, "", "" @@ -419,6 +420,7 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, * if perm { ctx.Data["OwnForkRepo"] = ownForkRepo ctx.Data["OwnForkRepoBranches"] = branches + ctx.Data["OwnForkRepoTags"] = tags } } @@ -572,25 +574,29 @@ func PrepareCompareDiff( return false } -func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []string, error) { +func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool, []string, []string, error) { perm, err := models.GetUserRepoPermission(repo, user) if err != nil { - return false, nil, err + return false, nil, nil, err } if !perm.CanRead(models.UnitTypeCode) { - return false, nil, nil + return false, nil, nil, nil } gitRepo, err := git.OpenRepository(repo.RepoPath()) if err != nil { - return false, nil, err + return false, nil, nil, err } defer gitRepo.Close() branches, _, err := gitRepo.GetBranches(0, 0) if err != nil { - return false, nil, err + return false, nil, nil, err } - return true, branches, nil + tags, err := gitRepo.GetTags() + if err != nil { + return false, nil, nil, err + } + return true, branches, tags, nil } // CompareDiff show different from one commit to another commit @@ -608,14 +614,29 @@ func CompareDiff(ctx *context.Context) { return } - if ctx.Data["PageIsComparePull"] == true { - headBranches, _, err := headGitRepo.GetBranches(0, 0) - if err != nil { - ctx.ServerError("GetBranches", err) - return - } - ctx.Data["HeadBranches"] = headBranches + baseGitRepo := ctx.Repo.GitRepo + baseTags, err := baseGitRepo.GetTags() + if err != nil { + ctx.ServerError("GetTags", err) + return + } + ctx.Data["Tags"] = baseTags + headBranches, _, err := headGitRepo.GetBranches(0, 0) + if err != nil { + ctx.ServerError("GetBranches", err) + return + } + ctx.Data["HeadBranches"] = headBranches + + headTags, err := headGitRepo.GetTags() + if err != nil { + ctx.ServerError("GetTags", err) + return + } + ctx.Data["HeadTags"] = headTags + + if ctx.Data["PageIsComparePull"] == true { pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch) if err != nil { if !models.IsErrPullRequestNotExist(err) { diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 124b4e5d1..7959ef7bb 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -3,105 +3,175 @@ {{template "repo/header" .}}
- {{if .PageIsComparePull}} -

- {{if and $.IsSigned (not .Repository.IsArchived)}} - {{.i18n.Tr "repo.pulls.compare_changes"}} -
{{.i18n.Tr "repo.pulls.compare_changes_desc"}}
- {{ else }} - {{.i18n.Tr "action.compare_commits_general"}} - {{ end }} -

- {{ $BaseCompareName := $.BaseName -}} - {{- $HeadCompareName := $.HeadRepo.OwnerName -}} - {{- if and (eq $.BaseName $.HeadRepo.OwnerName) (ne $.Repository.Name $.HeadRepo.Name) -}} +

+ {{if and $.PageIsComparePull $.IsSigned (not .Repository.IsArchived)}} + {{.i18n.Tr "repo.pulls.compare_changes"}} +
{{.i18n.Tr "repo.pulls.compare_changes_desc"}}
+ {{ else }} + {{.i18n.Tr "action.compare_commits_general"}} + {{ end }} +

+ {{ $BaseCompareName := $.BaseName -}} + {{- $HeadCompareName := $.HeadRepo.OwnerName -}} + {{- if and (eq $.BaseName $.HeadRepo.OwnerName) (ne $.Repository.Name $.HeadRepo.Name) -}} + {{- $HeadCompareName = printf "%s/%s" $.HeadRepo.OwnerName $.HeadRepo.Name -}} + {{- end -}} + {{- $OwnForkCompareName := "" -}} + {{- if .OwnForkRepo -}} + {{- $OwnForkCompareName = .OwnForkRepo.OwnerName -}} + {{- end -}} + {{- $RootRepoCompareName := "" -}} + {{- if .RootRepo -}} + {{- $RootRepoCompareName = .RootRepo.OwnerName -}} + {{- if eq $.HeadRepo.OwnerName .RootRepo.OwnerName -}} {{- $HeadCompareName = printf "%s/%s" $.HeadRepo.OwnerName $.HeadRepo.Name -}} {{- end -}} - {{- $OwnForkCompareName := "" -}} {{- if .OwnForkRepo -}} - {{- $OwnForkCompareName = .OwnForkRepo.OwnerName -}} - {{- end -}} - {{- $RootRepoCompareName := "" -}} - {{- if .RootRepo -}} - {{- $RootRepoCompareName = .RootRepo.OwnerName -}} - {{- if eq $.HeadRepo.OwnerName .RootRepo.OwnerName -}} - {{- $HeadCompareName = printf "%s/%s" $.HeadRepo.OwnerName $.HeadRepo.Name -}} - {{- end -}} - {{- if .OwnForkRepo -}} - {{- if eq $.OwnForkRepo.OwnerName .RootRepo.OwnerName -}} - {{- $OwnForkRepoCompareName = printf "%s/%s" $.OwnForkRepo.OwnerName $.OwnForkRepo.Name -}} - {{- end -}} + {{- if eq $.OwnForkRepo.OwnerName .RootRepo.OwnerName -}} + {{- $OwnForkRepoCompareName = printf "%s/%s" $.OwnForkRepo.OwnerName $.OwnForkRepo.Name -}} {{- end -}} {{- end -}} -
- {{svg "octicon-git-compare"}} -