feat: highlight issue references with : (#8101) (#8404)

* feat: highlight issue references with :

e.g. #1287: my commit msg
e.g. ABC-1234: my commit msg

* ref: update model regex to consistent with issueNumericPattern

* test: check highlight issue with : in commits messages
This commit is contained in:
6543 2019-10-07 05:41:46 +02:00 committed by techknowlogick
parent b0dcf417ea
commit c9b8c12bc9
4 changed files with 11 additions and 3 deletions

View File

@ -65,7 +65,7 @@ var (
) )
const issueRefRegexpStr = `(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)+` const issueRefRegexpStr = `(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)+`
const issueRefRegexpStrNoKeyword = `(?:\s|^|\(|\[)(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))` const issueRefRegexpStrNoKeyword = `(?:\s|^|\(|\[)(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`
func assembleKeywordsPattern(words []string) string { func assembleKeywordsPattern(words []string) string {
return fmt.Sprintf(`(?i)(?:%s)(?::?) %s`, strings.Join(words, "|"), issueRefRegexpStr) return fmt.Sprintf(`(?i)(?:%s)(?::?) %s`, strings.Join(words, "|"), issueRefRegexpStr)

View File

@ -160,6 +160,7 @@ func TestRegExp_issueReferenceKeywordsPat(t *testing.T) {
"#2", "#2",
"[#2]", "[#2]",
"please see go-gitea/gitea#5", "please see go-gitea/gitea#5",
"#2:",
} }
falseTestCases := []string{ falseTestCases := []string{
"kb#2", "kb#2",

View File

@ -38,9 +38,9 @@ var (
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`) mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`)
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287 // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`) issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234 // issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|\.(\s|$))`) issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$))`)
// crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository // crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
// e.g. gogits/gogs#12345 // e.g. gogits/gogs#12345
crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`) crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)

View File

@ -118,6 +118,10 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243) test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
test("(#4)(#5)", "(%s)(%s)", 4, 5) test("(#4)(#5)", "(%s)(%s)", 4, 5)
test("#1 (#4321) test", "%s (%s) test", 1, 4321) test("#1 (#4321) test", "%s (%s) test", 1, 4321)
// should render with :
test("#1234: test", "%s: test", 1234)
test("wow (#54321: test)", "wow (%s: test)", 54321)
} }
func TestRender_IssueIndexPattern3(t *testing.T) { func TestRender_IssueIndexPattern3(t *testing.T) {
@ -237,6 +241,8 @@ func TestRegExp_issueNumericPattern(t *testing.T) {
"#0", "#0",
"#1234567890987654321", "#1234567890987654321",
" #12", " #12",
"#12:",
"ref: #12: msg",
} }
falseTestCases := []string{ falseTestCases := []string{
"# 1234", "# 1234",
@ -354,6 +360,7 @@ func TestRegExp_issueAlphanumericPattern(t *testing.T) {
"ABC-123.", "ABC-123.",
"(ABC-123)", "(ABC-123)",
"[ABC-123]", "[ABC-123]",
"ABC-123:",
} }
falseTestCases := []string{ falseTestCases := []string{
"RC-08", "RC-08",