From 12fddd6d8fb5f0d1d88d279e4a1af37b9511664e Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Fri, 12 Jul 2019 12:18:02 -0400 Subject: [PATCH 1/2] Fix regex for issues in commit messages Use same regex as markup for matching in commits. Fixes #7438 --- models/action.go | 3 ++- models/action_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/models/action.go b/models/action.go index 21b008e3be83d..a092f564bfdc9 100644 --- a/models/action.go +++ b/models/action.go @@ -65,6 +65,7 @@ var ( ) 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|$))` func assembleKeywordsPattern(words []string) string { return fmt.Sprintf(`(?i)(?:%s)(?::?) %s`, strings.Join(words, "|"), issueRefRegexpStr) @@ -73,7 +74,7 @@ func assembleKeywordsPattern(words []string) string { func init() { issueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueCloseKeywords)) issueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueReopenKeywords)) - issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStr) + issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStrNoKeyword) } // Action represents user operation type and other information to diff --git a/models/action_test.go b/models/action_test.go index 53a3202894e2e..0660c14cd1854 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -155,6 +155,26 @@ func TestPushCommits_AvatarLink(t *testing.T) { pushCommits.AvatarLink("nonexistent@example.com")) } +func TestRegExp_issueReferenceKeywordsPat(t *testing.T) { + trueTestCases := []string{ + "#2", + "[#2]", + "please see go-gitea/gitea#5", + } + falseTestCases := []string{ + "kb#2", + "#2xy", + } + + for _, testCase := range trueTestCases { + assert.True(t, issueReferenceKeywordsPat.MatchString(testCase)) + } + for _, testCase := range falseTestCases { + assert.False(t, issueReferenceKeywordsPat.MatchString(testCase)) + } +} + + func Test_getIssueFromRef(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) From e348ac74b14716f8b33c6793ae116706d7013834 Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Fri, 12 Jul 2019 12:36:18 -0400 Subject: [PATCH 2/2] make fmt --- models/action_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/models/action_test.go b/models/action_test.go index 0660c14cd1854..da50ebce809d8 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -174,7 +174,6 @@ func TestRegExp_issueReferenceKeywordsPat(t *testing.T) { } } - func Test_getIssueFromRef(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)