]> source.dussan.org Git - gitea.git/commitdiff
Fix regex for issues in commit messages (#7444)
authormrsdizzie <info@mrsdizzie.com>
Sun, 14 Jul 2019 14:48:51 +0000 (10:48 -0400)
committerLunny Xiao <xiaolunwen@gmail.com>
Sun, 14 Jul 2019 14:48:51 +0000 (22:48 +0800)
* Fix regex for issues in commit messages

Use same regex as markup for matching in commits.

Fixes #7438

* make fmt

models/action.go
models/action_test.go

index 21b008e3be83de03b92ef4e14de3faf9afda5b69..a092f564bfdc999ca1ec46a7e9291e25bc7265ed 100644 (file)
@@ -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
index 53a3202894e2ee66bf212700f7e3a0fc4c133167..da50ebce809d849217ac3c7305899634c40634ed 100644 (file)
@@ -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)