Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -210,6 +210,8 @@ var migrations = []Migration{
NewMigration("Add Branch Protection Block Outdated Branch", addBlockOnOutdatedBranch),
// v138 -> v139
NewMigration("Add ResolveDoerID to Comment table", addResolveDoerIDCommentColumn),
// v139 -> v140
NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs),
}

// GetCurrentDBVersion returns the current db version
Expand Down
25 changes: 25 additions & 0 deletions models/migrations/v139.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` <> '' AND `ref` NOT LIKE 'refs/%'"
default:
query = "UPDATE `issue` SET `ref` = 'refs/heads/' || `ref` WHERE `ref` IS NOT NULL AND `ref` <> '' AND `ref` NOT LIKE 'refs/%'"
}

_, 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
}
}

// SplitRefName splits a full refname to reftype and simple refname
func SplitRefName(refStr string) (string, string) {
if strings.HasPrefix(refStr, BranchPrefix) {
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 2020 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 @@ -93,15 +93,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
4 changes: 4 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,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["ApprovalCounts"] = func(issueID int64, typ string) int64 {
counts, ok := approvalCounts[issueID]
if !ok || len(counts) == 0 {
Expand Down Expand Up @@ -1127,6 +1130,7 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
ctx.Data["IsRepoAdmin"] = ctx.IsSigned && (ctx.Repo.IsAdmin() || 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 @@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
issue_service "code.gitea.io/gitea/services/issue"
pull_service "code.gitea.io/gitea/services/pull"

"github.com/keybase/go-crypto/openpgp"
Expand Down Expand Up @@ -624,6 +625,9 @@ func Issues(ctx *context.Context) {
totalIssues = int(allIssueStats.ClosedCount)
}

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

ctx.Data["Issues"] = issues
ctx.Data["ApprovalCounts"] = func(issueID int64, typ string) int64 {
counts, ok := approvalCounts[issueID]
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 2020 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)
}
6 changes: 3 additions & 3 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,12 +28,12 @@
</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>
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
</a>
{{end}}
{{if .Ref}}
<a class="ref" href="{{$.RepoLink}}/src/branch/{{.Ref | PathEscapeSegments}}">
{{svg "octicon-git-branch" 16}} {{.Ref}}
<a class="ref" href="{{index $.IssueRefURLs .ID}}">
{{svg "octicon-git-branch" 16}} {{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 @@ -218,8 +218,8 @@
{{end}}

{{if .Ref}}
<a class="ref" href="{{$.RepoLink}}/src/branch/{{.Ref}}">
{{svg "octicon-git-branch" 16}} {{.Ref}}
<a class="ref" href="{{index $.IssueRefURLs .ID}}">
{{svg "octicon-git-branch" 16}} {{index $.IssueRefEndNames .ID}}
</a>
{{end}}
{{$tasks := .GetTasks}}
Expand Down
4 changes: 2 additions & 2 deletions templates/user/dashboard/issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
</a>
{{end}}
{{if .Ref}}
<a class="ref" href="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Name}}/src/branch/{{.Ref}}">
{{svg "octicon-git-branch" 16}} {{.Ref}}
<a class="ref" href="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Name}}{{index $.IssueRefURLs .ID}}">
{{svg "octicon-git-branch" 16}} {{index $.IssueRefEndNames .ID}}
</a>
{{end}}
{{range .Assignees}}
Expand Down
7 changes: 3 additions & 4 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ function initEditForm() {
function initBranchSelector() {
const $selectBranch = $('.ui.select-branch');
const $branchMenu = $selectBranch.find('.reference-list-menu');
$branchMenu.find('.item:not(.no-select)').on('click', function () {
const selectedValue = $(this).data('id');
$($(this).data('id-selector')).val(selectedValue);
$selectBranch.find('.ui .branch-name').text(selectedValue);
$branchMenu.find('.item:not(.no-select)').click(function () {
$($(this).data('id-selector')).val($(this).data('id'));
$selectBranch.find('.ui .branch-name').text($(this).data('name'));
});
$selectBranch.find('.reference.column').on('click', function () {
$selectBranch.find('.scrolling.reference-list-menu').css('display', 'none');
Expand Down