diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index 621ba4f70..6bbf1e183 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -576,6 +576,9 @@ settings.external_wiki_url_desc = Visitors will be redirected to URL when they c settings.issues_desc = Enable builtin lightweight issue tracker settings.use_external_issue_tracker = Use external issue tracker settings.tracker_url_format = External Issue Tracker URL Format +settings.tracker_issue_style = External Issue Tracker Naming Style: +settings.tracker_issue_style.numeric = Numeric (1234) +settings.tracker_issue_style.alphanumeric = Alphanumeric (ABC-123, DEFG-234, ...) settings.tracker_url_format_desc = You can use placeholder {user} {repo} {index} for user name, repository name and issue index. settings.pulls_desc = Enable pull requests to accept public contributions settings.danger_zone = Danger Zone diff --git a/models/repo.go b/models/repo.go index 91dd9acf3..8820293e5 100644 --- a/models/repo.go +++ b/models/repo.go @@ -170,6 +170,7 @@ type Repository struct { EnableIssues bool `xorm:"NOT NULL DEFAULT true"` EnableExternalTracker bool ExternalTrackerFormat string + ExternalTrackerStyle string ExternalMetas map[string]string `xorm:"-"` EnablePulls bool `xorm:"NOT NULL DEFAULT true"` @@ -254,6 +255,13 @@ func (repo *Repository) ComposeMetas() map[string]string { "user": repo.MustOwner().Name, "repo": repo.Name, } + switch repo.ExternalTrackerStyle { + case markdown.ISSUE_NAME_STYLE_ALPHANUMERIC: + repo.ExternalMetas["style"] = markdown.ISSUE_NAME_STYLE_ALPHANUMERIC + default: + repo.ExternalMetas["style"] = markdown.ISSUE_NAME_STYLE_NUMERIC + } + } return repo.ExternalMetas } diff --git a/models/repo_test.go b/models/repo_test.go new file mode 100644 index 000000000..4a4203ba0 --- /dev/null +++ b/models/repo_test.go @@ -0,0 +1,62 @@ +package models_test + +import ( + "testing" + . "github.com/smartystreets/goconvey/convey" + . "github.com/gogits/gogs/models" + + "github.com/gogits/gogs/modules/markdown" +) + +func TestRepo(t *testing.T) { + Convey("The metas map", t, func () { + var repo = new(Repository) + repo.Name = "testrepo" + repo.Owner = new(User) + repo.Owner.Name = "testuser" + repo.ExternalTrackerFormat = "https://someurl.com/{user}/{repo}/{issue}" + + Convey("When no external tracker is configured", func () { + Convey("It should be nil", func () { + repo.EnableExternalTracker = false + So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil)) + }) + Convey("It should be nil even if other settings are present", func () { + repo.EnableExternalTracker = false + repo.ExternalTrackerFormat = "http://someurl.com/{user}/{repo}/{issue}" + repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_NUMERIC + So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil)) + }) + }) + + Convey("When an external issue tracker is configured", func () { + repo.EnableExternalTracker = true + Convey("It should default to numeric issue style", func () { + metas := repo.ComposeMetas() + So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_NUMERIC) + }) + Convey("It should pass through numeric issue style setting", func () { + repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_NUMERIC + metas := repo.ComposeMetas() + So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_NUMERIC) + }) + Convey("It should pass through alphanumeric issue style setting", func () { + repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_ALPHANUMERIC + metas := repo.ComposeMetas() + So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_ALPHANUMERIC) + }) + Convey("It should contain the user name", func () { + metas := repo.ComposeMetas() + So(metas["user"], ShouldEqual, "testuser") + }) + Convey("It should contain the repo name", func () { + metas := repo.ComposeMetas() + So(metas["repo"], ShouldEqual, "testrepo") + }) + Convey("It should contain the URL format", func () { + metas := repo.ComposeMetas() + So(metas["format"], ShouldEqual, "https://someurl.com/{user}/{repo}/{issue}") + }) + }) + }) +} diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 333f468a1..aa74ab3b9 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -96,6 +96,7 @@ type RepoSettingForm struct { EnableIssues bool EnableExternalTracker bool TrackerURLFormat string + TrackerIssueStyle string EnablePulls bool } diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 862f7d253..899d87745 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -22,6 +22,11 @@ import ( "github.com/gogits/gogs/modules/setting" ) +const ( + ISSUE_NAME_STYLE_NUMERIC = "numeric" + ISSUE_NAME_STYLE_ALPHANUMERIC = "alphanumeric" +) + var Sanitizer = bluemonday.UGCPolicy() // BuildSanitizer initializes sanitizer with allowed attributes based on settings. @@ -79,8 +84,10 @@ var ( // IssueFullPattern matches link to an issue with or without trailing hash, // e.g. https://try.gogs.io/gogs/gogs/issues/4#issue-685 IssueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) - // IssueIndexPattern matches string that references to an issue, e.g. #1287 - IssueIndexPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`) + // IssueNumericPattern matches string that references to a numeric issue, e.g. #1287 + IssueNumericPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`) + // IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234 + IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`) // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`) @@ -208,22 +215,30 @@ func cutoutVerbosePrefix(prefix string) string { // RenderIssueIndexPattern renders issue indexes to corresponding links. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte { urlPrefix = cutoutVerbosePrefix(urlPrefix) - ms := IssueIndexPattern.FindAll(rawBytes, -1) + + pattern := IssueNumericPattern + if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC { + pattern = IssueAlphanumericPattern + } + + ms := pattern.FindAll(rawBytes, -1) for _, m := range ms { - var space string - if m[0] != '#' { - space = string(m[0]) - m = m[1:] + if m[0] == ' ' || m[0] == '(' { + m = m[1:] // ignore leading space or opening parentheses } + var link string if metas == nil { - rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(`%s%s`, - space, urlPrefix, m[1:], m)), 1) + link = fmt.Sprintf(`%s`, urlPrefix, m[1:], m) } else { // Support for external issue tracker - metas["index"] = string(m[1:]) - rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(`%s%s`, - space, com.Expand(metas["format"], metas), m)), 1) + if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC { + metas["index"] = string(m) + } else { + metas["index"] = string(m[1:]) + } + link = fmt.Sprintf(`%s`, com.Expand(metas["format"], metas), m) } + rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1) } return rawBytes } diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go new file mode 100644 index 000000000..f39565b81 --- /dev/null +++ b/modules/markdown/markdown_test.go @@ -0,0 +1,236 @@ +package markdown_test + +import ( + "testing" + . "github.com/smartystreets/goconvey/convey" + . "github.com/gogits/gogs/modules/markdown" + + "github.com/gogits/gogs/modules/setting" +) + +func TestMarkdown(t *testing.T) { + var ( + urlPrefix = "/prefix" + metas map[string]string = nil + ) + setting.AppSubUrlDepth = 0 + + Convey("Rendering an issue link", t, func () { + Convey("To the internal issue tracker", func () { + Convey("It should not render anything when there are no issues", func () { + testCases := []string { + "", + "this is a test", + "test 123 123 1234", + "#", + "# # #", + "# 123", + "#abcd", + "##1234", + "test#1234", + "#1234test", + " test #1234test", + } + + for i := 0; i < len(testCases); i++ { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i]) + } + }) + Convey("It should render freestanding issue mentions", func () { + testCases := []string { + "#1234 test", "#1234 test", + "test #1234 issue", "test #1234 issue", + "test issue #1234", "test issue #1234", + "#5 test", "#5 test", + "test #5 issue", "test #5 issue", + "test issue #5", "test issue #5", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should not render issue without leading space", func () { + input := []byte("test#54321 issue") + expected := "test#54321 issue" + So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected) + }) + Convey("It should not render issue without trailing space", func () { + input := []byte("test #54321issue") + expected := "test #54321issue" + So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected) + }) + Convey("It should render issue in parentheses", func () { + testCases := []string { + "(#54321 issue)", "(#54321 issue)", + "test (#54321) issue", "test (#54321) issue", + "test (#54321 extra) issue", "test (#54321 extra) issue", + "test (#54321 issue)", "test (#54321 issue)", + "test (#54321)", "test (#54321)", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should render multiple issues in the same line", func () { + testCases := []string { + "#54321 #1243", "#54321 #1243", + "test #54321 #1243", "test #54321 #1243", + "(#54321 #1243)", "(#54321 #1243)", + "(#54321)(#1243)", "(#54321)(#1243)", + "text #54321 test #1243 issue", "text #54321 test #1243 issue", + "#1 (#4321) test", "#1 (#4321) test", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + }) + Convey("To an external issue tracker with numeric style", func () { + metas = make(map[string]string) + metas["format"] = "https://someurl.com/{user}/{repo}/{index}" + metas["user"] = "someuser" + metas["repo"] = "somerepo" + metas["style"] = ISSUE_NAME_STYLE_NUMERIC + + Convey("should not render anything when there are no issues", func () { + testCases := []string { + "this is a test", + "test 123 123 1234", + "#", + "# # #", + "# 123", + "#abcd", + } + + for i := 0; i < len(testCases); i++ { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i]) + } + }) + Convey("It should render freestanding issue", func () { + testCases := []string { + "#1234 test", "#1234 test", + "test #1234 issue", "test #1234 issue", + "test issue #1234", "test issue #1234", + "#5 test", "#5 test", + "test #5 issue", "test #5 issue", + "test issue #5", "test issue #5", + } + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should not render issue mention without leading space", func () { + input := []byte("test#54321 issue") + expected := "test#54321 issue" + So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected) + }) + Convey("It should not render issue mention without trailing space", func () { + input := []byte("test #54321issue") + expected := "test #54321issue" + So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected) + }) + Convey("It should render mention in parentheses", func () { + testCases := []string { + "(#54321 issue)", "(#54321 issue)", + "test (#54321) issue", "test (#54321) issue", + "test (#54321 extra) issue", "test (#54321 extra) issue", + "test (#54321 issue)", "test (#54321 issue)", + "test (#54321)", "test (#54321)", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should render multiple mentions in the same line", func () { + testCases := []string { + "#54321 #1243", "#54321 #1243", + "test #54321 #1243", "test #54321 #1243", + "(#54321 #1243)", "(#54321 #1243)", + "(#54321)(#1243)", "(#54321)(#1243)", + "text #54321 test #1243 issue", "text #54321 test #1243 issue", + "#1 (#4321) test", "#1 (#4321) test", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + }) + Convey("To an external issue tracker with alphanumeric style", func () { + metas = make(map[string]string) + metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}" + metas["user"] = "someuser" + metas["repo"] = "somerepo" + metas["style"] = ISSUE_NAME_STYLE_ALPHANUMERIC + Convey("It should not render anything when there are no issues", func () { + testCases := []string { + "", + "this is a test", + "test 123 123 1234", + "#", + "##1234", + "# 123", + "#abcd", + "test #123", + "abc-1234", // issue prefix must be capital + "ABc-1234", // issue prefix must be _all_ capital + "ABCDEFGHIJK-1234", // the limit is 10 characters in the prefix + "ABC1234", // dash is required + "test ABC- test", // number is required + "test -1234 test", // prefix is required + "testABC-123 test", // leading space is required + "test ABC-123test", // trailing space is required + "ABC-0123", // no leading zero + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i]) + } + }) + Convey("It should render freestanding issue", func () { + testCases := []string { + "OTT-1234 test", "OTT-1234 test", + "test T-12 issue", "test T-12 issue", + "test issue ABCDEFGHIJ-1234567890", "test issue ABCDEFGHIJ-1234567890", + "A-1 test", "A-1 test", + "test ZED-1 issue", "test ZED-1 issue", + "test issue DEED-7154", "test issue DEED-7154", + } + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should render mention in parentheses", func () { + testCases := []string { + "(ABG-124 issue)", "(ABG-124 issue)", + "test (ABG-124) issue", "test (ABG-124) issue", + "test (ABG-124 extra) issue", "test (ABG-124 extra) issue", + "test (ABG-124 issue)", "test (ABG-124 issue)", + "test (ABG-124)", "test (ABG-124)", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + Convey("It should render multiple mentions in the same line", func () { + testCases := []string { + "ABG-124 OTT-4321", "ABG-124 OTT-4321", + "test ABG-124 OTT-4321", "test ABG-124 OTT-4321", + "(ABG-124 OTT-4321)", "(ABG-124 OTT-4321)", + "(ABG-124)(OTT-4321)", "(ABG-124)(OTT-4321)", + "text ABG-124 test OTT-4321 issue", "text ABG-124 test OTT-4321 issue", + "A-1 (RRE-345) test", "A-1 (RRE-345) test", + } + + for i := 0; i < len(testCases); i += 2 { + So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1]) + } + }) + }) + }) +} diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 678572242..99f9b8af8 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -130,6 +130,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { repo.EnableIssues = form.EnableIssues repo.EnableExternalTracker = form.EnableExternalTracker repo.ExternalTrackerFormat = form.TrackerURLFormat + repo.ExternalTrackerStyle = form.TrackerIssueStyle repo.EnablePulls = form.EnablePulls if err := models.UpdateRepository(repo, false); err != nil { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 75cfd34ad..182ef7dfa 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -116,6 +116,21 @@

{{.i18n.Tr "repo.settings.tracker_url_format_desc" | Str2html}}

+
+ +
+
+ + +
+
+
+
+ + +
+
+
{{if .Repository.CanEnablePulls}}