Skip to content

Commit fb0c562

Browse files
mrsdizzielunny
authored andcommitted
Fix regex for issues in commit messages (#7444)
* Fix regex for issues in commit messages Use same regex as markup for matching in commits. Fixes #7438 * make fmt
1 parent 97078d1 commit fb0c562

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

models/action.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
)
6666

6767
const issueRefRegexpStr = `(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)+`
68+
const issueRefRegexpStrNoKeyword = `(?:\s|^|\(|\[)(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`
6869

6970
func assembleKeywordsPattern(words []string) string {
7071
return fmt.Sprintf(`(?i)(?:%s)(?::?) %s`, strings.Join(words, "|"), issueRefRegexpStr)
@@ -73,7 +74,7 @@ func assembleKeywordsPattern(words []string) string {
7374
func init() {
7475
issueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueCloseKeywords))
7576
issueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueReopenKeywords))
76-
issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStr)
77+
issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStrNoKeyword)
7778
}
7879

7980
// Action represents user operation type and other information to

models/action_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ func TestPushCommits_AvatarLink(t *testing.T) {
155155
pushCommits.AvatarLink("nonexistent@example.com"))
156156
}
157157

158+
func TestRegExp_issueReferenceKeywordsPat(t *testing.T) {
159+
trueTestCases := []string{
160+
"#2",
161+
"[#2]",
162+
"please see go-gitea/gitea#5",
163+
}
164+
falseTestCases := []string{
165+
"kb#2",
166+
"#2xy",
167+
}
168+
169+
for _, testCase := range trueTestCases {
170+
assert.True(t, issueReferenceKeywordsPat.MatchString(testCase))
171+
}
172+
for _, testCase := range falseTestCases {
173+
assert.False(t, issueReferenceKeywordsPat.MatchString(testCase))
174+
}
175+
}
176+
158177
func Test_getIssueFromRef(t *testing.T) {
159178
assert.NoError(t, PrepareTestDatabase())
160179
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)

0 commit comments

Comments
 (0)