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

Fix ref links in issue overviews for tags #8742

Merged
merged 28 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e882d55
Properly generate ref URLs
vijfhoek Oct 30, 2019
1e76a26
Fix formatting and create migration
vijfhoek Oct 30, 2019
daf590d
Add copyright head to utils_test
vijfhoek Oct 30, 2019
543b287
Use a raw query for the ref migration
vijfhoek Oct 30, 2019
dfaca34
Remove semicolon
vijfhoek Oct 30, 2019
7df5048
Quote column and table names in migration SQL
vijfhoek Oct 30, 2019
4e607fc
Change || to CONCAT, since MSSQL does not support ||
vijfhoek Oct 30, 2019
dbdc39f
Make migration engine aware
vijfhoek Oct 31, 2019
2840c20
Add missing import
vijfhoek Oct 31, 2019
59b3a3c
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek Oct 31, 2019
e69f3a9
Move ref EndName and URL to the issue service
vijfhoek Nov 3, 2019
8f28995
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek Nov 3, 2019
0d076ed
Fix tests
vijfhoek Nov 3, 2019
2eebad7
Add test for commit refs
vijfhoek Nov 3, 2019
c543c03
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek Nov 3, 2019
28d8d8e
Update issue.go
vijfhoek Nov 4, 2019
77b5f43
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek Nov 18, 2019
e5b047a
Merge branch 'related-link-tags' of github.com:SijmenSchoon/gitea int…
vijfhoek Nov 18, 2019
d719cd6
Use the right command for building JavaScript bundles
vijfhoek Nov 18, 2019
ee3b910
Prepare for merge
vijfhoek May 10, 2020
88c20ea
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek May 10, 2020
190c4f2
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek May 10, 2020
85bbb9b
Check for refs/* before prepending in migration
vijfhoek May 11, 2020
a6f5850
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek May 11, 2020
16d4358
Merge branch 'master' of https://github.com/go-gitea/gitea into relat…
vijfhoek May 12, 2020
0cc3313
Merge branch 'master' into related-link-tags
techknowlogick May 14, 2020
a071c26
Update services/issue/issue_test.go
techknowlogick May 14, 2020
a16f5e8
Update modules/git/utils_test.go
techknowlogick May 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ var migrations = []Migration{
NewMigration("Add template options to repository", addTemplateToRepo),
// v108 -> v109
NewMigration("Add comment_id on table notification", addCommentIDOnNotification),
// v109 -> v110
NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs),
}

// Migrate database to current version
Expand Down
25 changes: 25 additions & 0 deletions models/migrations/v109.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2019 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 migrations

import (
"code.gitea.io/gitea/modules/setting"

"xorm.io/xorm"
)

func prependRefsHeadsToIssueRefs(x *xorm.Engine) error {
var query string

switch {
case setting.Database.UseMSSQL:
query = "UPDATE `issue` SET `ref` = 'refs/heads/' + `ref` WHERE `ref` IS NOT NULL AND `ref` <> ''"
default:
query = "UPDATE `issue` SET `ref` = 'refs/heads/' || `ref` WHERE `ref` IS NOT NULL AND `ref` <> ''"
}

_, err := x.Exec(query)
return err
}
13 changes: 13 additions & 0 deletions modules/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func RefEndName(refStr string) string {
return refStr
}

// RefURL returns the absolute URL for a ref in a repository
func RefURL(repoURL, ref string) string {
refName := RefEndName(ref)
switch {
case strings.HasPrefix(ref, BranchPrefix):
return repoURL + "/src/branch/" + refName
case strings.HasPrefix(ref, TagPrefix):
return repoURL + "/src/tag/" + refName
default:
return repoURL + "/src/commit/" + refName
}
}

// ParseBool returns the boolean value represented by the string as per git's git_config_bool
// true will be returned for the result if the string is empty, but valid will be false.
// "true", "yes", "on" are all true, true
Expand Down
31 changes: 31 additions & 0 deletions modules/git/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2019 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 git

import (
"testing"

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

func TestRefEndName(t *testing.T) {
// Test branch names (with and without slash).
assert.Equal(t, "foo", RefEndName("refs/heads/foo"))
assert.Equal(t, "feature/foo", RefEndName("refs/heads/feature/foo"))

// Test tag names (with and without slash).
assert.Equal(t, "foo", RefEndName("refs/tags/foo"))
assert.Equal(t, "release/foo", RefEndName("refs/tags/release/foo"))

// Test commit hashes.
assert.Equal(t, "c0ffee", RefEndName("c0ffee"))
}

func TestRefURL(t *testing.T) {
repoURL := "/user/repo"
assert.Equal(t, repoURL+"/src/branch/foo", RefURL(repoURL, "refs/heads/foo"))
assert.Equal(t, repoURL+"/src/tag/foo", RefURL(repoURL, "refs/tags/foo"))
assert.Equal(t, repoURL+"/src/commit/c0ffee", RefURL(repoURL, "c0ffee"))
}
10 changes: 2 additions & 8 deletions modules/webhook/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,9 @@ func SlackLinkFormatter(url string, text string) string {

// SlackLinkToRef slack-formatter link to a repo ref
func SlackLinkToRef(repoURL, ref string) string {
url := git.RefURL(repoURL, ref)
refName := git.RefEndName(ref)
switch {
case strings.HasPrefix(ref, git.BranchPrefix):
return SlackLinkFormatter(repoURL+"/src/branch/"+refName, refName)
case strings.HasPrefix(ref, git.TagPrefix):
return SlackLinkFormatter(repoURL+"/src/tag/"+refName, refName)
default:
return SlackLinkFormatter(repoURL+"/src/commit/"+refName, refName)
}
return SlackLinkFormatter(url, refName)
}

func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
}

ctx.Data["IssueRefEndNames"], ctx.Data["IssueRefURLs"] =
issue_service.GetRefEndNamesAndURLs(issues, ctx.Repo.RepoLink)

ctx.Data["IssueStats"] = issueStats
ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
ctx.Data["ViewType"] = viewType
Expand Down Expand Up @@ -963,6 +966,7 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["IsRepoAdmin"] = ctx.IsSigned && (ctx.Repo.IsAdmin() || ctx.User.IsAdmin)
ctx.Data["IsRepoIssuesWriter"] = ctx.IsSigned && (ctx.Repo.CanWrite(models.UnitTypeIssues) || ctx.User.IsAdmin)
ctx.Data["LockReasons"] = setting.Repository.Issue.LockReasons
ctx.Data["RefEndName"] = git.RefEndName(issue.Ref)
ctx.HTML(200, tplIssueView)
}

Expand Down
4 changes: 4 additions & 0 deletions routers/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
issue_service "code.gitea.io/gitea/services/issue"

"github.com/keybase/go-crypto/openpgp"
"github.com/keybase/go-crypto/openpgp/armor"
Expand Down Expand Up @@ -360,6 +361,9 @@ func Issues(ctx *context.Context) {
total = int(issueStats.ClosedCount)
}

ctx.Data["IssueRefEndNames"], ctx.Data["IssueRefURLs"] =
issue_service.GetRefEndNamesAndURLs(issues, ctx.Query("RepoLink"))

ctx.Data["Issues"] = issues
ctx.Data["CommitStatus"] = commitStatus
ctx.Data["Repos"] = showRepos
Expand Down
16 changes: 16 additions & 0 deletions services/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package issue

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/util"
)

// NewIssue creates new issue with labels for repository.
Expand Down Expand Up @@ -128,3 +130,17 @@ func AddAssigneeIfNotAssigned(issue *models.Issue, doer *models.User, assigneeID

return nil
}

// GetRefEndNamesAndURLs retrieves the ref end names (e.g. refs/heads/branch-name -> branch-name)
// and their respective URLs.
func GetRefEndNamesAndURLs(issues []*models.Issue, repoLink string) (map[int64]string, map[int64]string) {
var issueRefEndNames = make(map[int64]string, len(issues))
var issueRefURLs = make(map[int64]string, len(issues))
for _, issue := range issues {
if issue.Ref != "" {
issueRefEndNames[issue.ID] = git.RefEndName(issue.Ref)
issueRefURLs[issue.ID] = git.RefURL(repoLink, util.PathEscapeSegments(issue.Ref))
}
}
return issueRefEndNames, issueRefURLs
}
30 changes: 30 additions & 0 deletions services/issue/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2019 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 issue

import (
"testing"

"code.gitea.io/gitea/models"

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

func TestGetRefEndNamesAndURLs(t *testing.T) {
issues := []*models.Issue{
{ID: 1, Ref: "refs/heads/branch1"},
{ID: 2, Ref: "refs/tags/tag1"},
{ID: 3, Ref: "c0ffee"},
}
repoLink := "/foo/bar"

endNames, urls := GetRefEndNamesAndURLs(issues, repoLink)
assert.EqualValues(t, map[int64]string{1: "branch1", 2: "tag1", 3: "c0ffee"}, endNames)
assert.EqualValues(t, map[int64]string{
1: repoLink + "/src/branch/branch1",
2: repoLink + "/src/tag/tag1",
3: repoLink + "/src/commit/c0ffee",
}, urls)
}
8 changes: 4 additions & 4 deletions templates/repo/issue/branch_selector_field.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<input id="ref_selector" name="ref" type="hidden" value="{{.Issue.Ref}}">
<div class="ui {{if .ReadOnly}}disabled{{end}} floating filter select-branch dropdown" data-no-results="{{.i18n.Tr "repo.pulls.no_results"}}">
<div class="ui basic small button">
<span class="text branch-name">{{if .Issue.Ref}}{{.Issue.Ref}}{{else}}{{.i18n.Tr "repo.issues.no_ref"}}{{end}}</span>
<span class="text branch-name">{{if .Issue.Ref}}{{$.RefEndName}}{{else}}{{.i18n.Tr "repo.issues.no_ref"}}{{end}}</span>
<i class="dropdown icon"></i>
</div>
<div class="menu">
Expand All @@ -28,16 +28,16 @@
</div>
<div id="branch-list" class="scrolling menu reference-list-menu">
{{range .Branches}}
<div class="item" data-id="{{.}}" data-id-selector="#ref_selector">{{.}}</div>
<div class="item" data-id="refs/heads/{{.}}" data-name="{{.}}" data-id-selector="#ref_selector">{{.}}</div>
{{end}}
</div>
<div id="tag-list" class="scrolling menu reference-list-menu" style="display: none">
{{range .Tags}}
<div class="item" data-id="{{.}}" data-id-selector="#ref_selector">{{.}}</div>
<div class="item" data-id="refs/tags/{{.}}" data-name="tags/{{.}}" data-id-selector="#ref_selector">{{.}}</div>
{{end}}
</div>
</div>
</div>

<div class="ui divider"></div>
{{end}}
{{end}}
4 changes: 2 additions & 2 deletions templates/repo/issue/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@
</a>
{{end}}
{{if .Ref}}
<a class="ref" href="{{$.RepoLink}}/src/branch/{{.Ref | PathEscapeSegments}}">
<span class="octicon octicon-git-branch"></span> {{.Ref}}
<a class="ref" href="{{index $.IssueRefURLs .ID}}">
<span class="octicon octicon-git-branch"></span> {{index $.IssueRefEndNames .ID}}
</a>
{{end}}
{{$tasks := .GetTasks}}
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/milestone_issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@
{{$.i18n.Tr .GetLastEventLabelFake $timeStr (.Poster.GetDisplayName|Escape) | Safe}}
{{end}}
{{if .Ref}}
<a class="ref" href="{{$.RepoLink}}/src/branch/{{.Ref}}">
<span class="octicon octicon-git-branch"></span> {{.Ref}}
<a class="ref" href="{{index $.IssueRefURLs .ID}}">
<span class="octicon octicon-git-branch"></span> {{index $.IssueRefEndNames .ID}}
</a>
{{end}}
{{$tasks := .GetTasks}}
Expand Down
8 changes: 4 additions & 4 deletions templates/user/dashboard/issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
{{ $timeStr:= TimeSinceUnix .CreatedUnix $.Lang }}
<li class="item">
<div class="ui label">{{if not $.RepoID}}{{.Repo.FullName}}{{end}}#{{.Index}}</div>
<a class="title has-emoji" href="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Name}}/issues/{{.Index}}">{{.Title}}</a>
<a class="title has-emoji" href="{{.Repo.Link}}/issues/{{.Index}}">{{.Title}}</a>

{{if .IsPull }}
{{if (index $.CommitStatus .PullRequest.ID)}}
Expand Down Expand Up @@ -105,13 +105,13 @@
{{$.i18n.Tr .GetLastEventLabelFake $timeStr (.Poster.GetDisplayName|Escape) | Safe}}
{{end}}
{{if .Milestone}}
<a class="milestone" href="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Name}}{{$.Link}}?q={{$.Keyword}}&type={{$.ViewType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{.Milestone.ID}}&assignee={{$.AssigneeID}}">
<a class="milestone" href="{{.Repo.Link}}{{$.Link}}?q={{$.Keyword}}&type={{$.ViewType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{.Milestone.ID}}&assignee={{$.AssigneeID}}">
<span class="octicon octicon-milestone"></span> {{.Milestone.Name}}
</a>
{{end}}
{{if .Ref}}
<a class="ref" href="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Name}}/src/branch/{{.Ref}}">
<span class="octicon octicon-git-branch"></span> {{.Ref}}
<a class="ref" href="{{index $.IssueRefURLs .ID}}">
<span class="octicon octicon-git-branch"></span> {{index $.IssueRefEndNames .ID}}
</a>
{{end}}
{{range .Assignees}}
Expand Down
5 changes: 2 additions & 3 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ function initBranchSelector() {
const $selectBranch = $('.ui.select-branch');
const $branchMenu = $selectBranch.find('.reference-list-menu');
$branchMenu.find('.item:not(.no-select)').click(function () {
const selectedValue = $(this).data('id');
$($(this).data('id-selector')).val(selectedValue);
$selectBranch.find('.ui .branch-name').text(selectedValue);
$($(this).data('id-selector')).val($(this).data('id'));
$selectBranch.find('.ui .branch-name').text($(this).data('name'));
});
$selectBranch.find('.reference.column').click(function () {
$selectBranch.find('.scrolling.reference-list-menu').css('display', 'none');
Expand Down