Add support for commit cross references (#22645)

Fixes #22628

This PR adds cross references for commits by using the format
`owner/repo@commit` . References are rendered like
[go-gitea/lgtm@6fe88302](#dummy).

---------

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
KN4CK3R 2023-01-30 02:50:01 +01:00 committed by GitHub
parent 3ff5a6a365
commit d0d257b243
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 0 deletions

View file

@ -303,6 +303,67 @@ func TestFindAllMentions(t *testing.T) {
}, res)
}
func TestFindRenderizableCommitCrossReference(t *testing.T) {
cases := []struct {
Input string
Expected *RenderizableReference
}{
{
Input: "",
Expected: nil,
},
{
Input: "test",
Expected: nil,
},
{
Input: "go-gitea/gitea@test",
Expected: nil,
},
{
Input: "go-gitea/gitea@ab1234",
Expected: nil,
},
{
Input: "go-gitea/gitea@abcd1234",
Expected: &RenderizableReference{
Owner: "go-gitea",
Name: "gitea",
CommitSha: "abcd1234",
RefLocation: &RefSpan{Start: 0, End: 23},
},
},
{
Input: "go-gitea/gitea@abcd1234abcd1234abcd1234abcd1234abcd1234",
Expected: &RenderizableReference{
Owner: "go-gitea",
Name: "gitea",
CommitSha: "abcd1234abcd1234abcd1234abcd1234abcd1234",
RefLocation: &RefSpan{Start: 0, End: 55},
},
},
{
Input: "go-gitea/gitea@abcd1234abcd1234abcd1234abcd1234abcd12340", // longer than 40 characters
Expected: nil,
},
{
Input: "test go-gitea/gitea@abcd1234 test",
Expected: &RenderizableReference{
Owner: "go-gitea",
Name: "gitea",
CommitSha: "abcd1234",
RefLocation: &RefSpan{Start: 4, End: 29},
},
},
}
for _, c := range cases {
found, ref := FindRenderizableCommitCrossReference(c.Input)
assert.Equal(t, ref != nil, found)
assert.Equal(t, c.Expected, ref)
}
}
func TestRegExp_mentionPattern(t *testing.T) {
trueTestCases := []struct {
pat string