diff options
author | mrsdizzie <info@mrsdizzie.com> | 2019-07-14 16:05:59 -0400 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-07-14 21:05:59 +0100 |
commit | b45f9260bfa56127943992513fe5be3e52c11b7f (patch) | |
tree | 2b4895370207472703c5300b6fb5e818df84dda1 | |
parent | ee1a8d7b4190e2640429d6d106a3a5a695af342f (diff) | |
download | gitea-b45f9260bfa56127943992513fe5be3e52c11b7f.tar.gz gitea-b45f9260bfa56127943992513fe5be3e52c11b7f.zip |
Fix regex for issues in commit messages (#7444) (#7466)
* Fix regex for issues in commit messages
Use same regex as markup for matching in commits.
Fixes #7438
* make fmt
-rw-r--r-- | models/action.go | 3 | ||||
-rw-r--r-- | models/action_test.go | 19 |
2 files changed, 21 insertions, 1 deletions
diff --git a/models/action.go b/models/action.go index 21b008e3be..a092f564bf 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 53a3202894..da50ebce80 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -155,6 +155,25 @@ 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) |