Skip to content

Commit 8693e54

Browse files
guillep2ktechknowlogick
authored andcommitted
Backport: Enable punctuations ending mentions (#8889) (#8894)
* Enable punctuations ending mentions * Improve tests
1 parent b27cac0 commit 8693e54

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

modules/references/references.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
// TODO: fix invalid linking issue
2727

2828
// mentionPattern matches all mentions in the form of "@user"
29-
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`)
29+
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`)
3030
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287
3131
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
3232
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234

modules/references/references_test.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -204,32 +204,57 @@ func TestFindAllIssueReferences(t *testing.T) {
204204
}
205205

206206
func TestRegExp_mentionPattern(t *testing.T) {
207-
trueTestCases := []string{
208-
"@Unknwon",
209-
"@ANT_123",
210-
"@xxx-DiN0-z-A..uru..s-xxx",
211-
" @lol ",
212-
" @Te-st",
213-
"(@gitea)",
214-
"[@gitea]",
207+
trueTestCases := []struct {
208+
pat string
209+
exp string
210+
}{
211+
{"@Unknwon", "@Unknwon"},
212+
{"@ANT_123", "@ANT_123"},
213+
{"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"},
214+
{" @lol ", "@lol"},
215+
{" @Te-st", "@Te-st"},
216+
{"(@gitea)", "@gitea"},
217+
{"[@gitea]", "@gitea"},
218+
{"@gitea! this", "@gitea"},
219+
{"@gitea? this", "@gitea"},
220+
{"@gitea. this", "@gitea"},
221+
{"@gitea, this", "@gitea"},
222+
{"@gitea; this", "@gitea"},
223+
{"@gitea!\nthis", "@gitea"},
224+
{"\n@gitea?\nthis", "@gitea"},
225+
{"\t@gitea.\nthis", "@gitea"},
226+
{"@gitea,\nthis", "@gitea"},
227+
{"@gitea;\nthis", "@gitea"},
228+
{"@gitea!", "@gitea"},
229+
{"@gitea?", "@gitea"},
230+
{"@gitea.", "@gitea"},
231+
{"@gitea,", "@gitea"},
232+
{"@gitea;", "@gitea"},
215233
}
216234
falseTestCases := []string{
217235
"@ 0",
218236
"@ ",
219237
"@",
220238
"",
221239
"ABC",
240+
"@.ABC",
222241
"/home/gitea/@gitea",
223242
"\"@gitea\"",
243+
"@@gitea",
244+
"@gitea!this",
245+
"@gitea?this",
246+
"@gitea,this",
247+
"@gitea;this",
224248
}
225249

226250
for _, testCase := range trueTestCases {
227-
res := mentionPattern.MatchString(testCase)
228-
assert.True(t, res)
251+
found := mentionPattern.FindStringSubmatch(testCase.pat)
252+
assert.Len(t, found, 2)
253+
assert.Equal(t, testCase.exp, found[1])
229254
}
230255
for _, testCase := range falseTestCases {
231256
res := mentionPattern.MatchString(testCase)
232-
assert.False(t, res)
257+
assert.False(t, res, "[%s] should be false", testCase)
233258
}
234259
}
235260

0 commit comments

Comments
 (0)