Skip to content

Commit edf591f

Browse files
feat: add mentioned users and issues to annotations
1 parent 92eda2f commit edf591f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pkg/analyzer/commit_analyzer.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ import (
88
)
99

1010
var (
11-
CAVERSION = "dev"
12-
commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`)
13-
breakingPattern = regexp.MustCompile("BREAKING CHANGES?")
11+
CAVERSION = "dev"
12+
commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`)
13+
breakingPattern = regexp.MustCompile("BREAKING CHANGES?")
14+
mentionedIssuesPattern = regexp.MustCompile(`#(\d+)`)
15+
mentionedUsersPattern = regexp.MustCompile(`(?i)@([a-z\d]([a-z\d]|-[a-z\d])+)`)
1416
)
1517

18+
func extractMentions(re *regexp.Regexp, s string) string {
19+
ret := make([]string, 0)
20+
for _, m := range re.FindAllStringSubmatch(s, -1) {
21+
ret = append(ret, m[1])
22+
}
23+
return strings.Join(ret, ",")
24+
}
25+
1626
type DefaultCommitAnalyzer struct{}
1727

1828
func (da *DefaultCommitAnalyzer) Init(m map[string]string) error {
@@ -34,6 +44,9 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit
3444
Change: &semrel.Change{},
3545
Annotations: rawCommit.Annotations,
3646
}
47+
c.Annotations["mentioned_issues"] = extractMentions(mentionedIssuesPattern, rawCommit.RawMessage)
48+
c.Annotations["mentioned_users"] = extractMentions(mentionedUsersPattern, rawCommit.RawMessage)
49+
3750
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
3851
if len(found) < 1 {
3952
return c

pkg/analyzer/commit_analyzer_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ func createRawCommit(sha, message string) *semrel.RawCommit {
2626
SHA: sha,
2727
RawMessage: message,
2828
Annotations: map[string]string{
29-
"author_name": "test",
30-
"my-annotation": "true",
29+
"author_name": "test",
3130
},
3231
}
3332
}
3433

3534
func TestAnnotations(t *testing.T) {
3635
defaultAnalyzer := &DefaultCommitAnalyzer{}
37-
rawCommit := createRawCommit("a", "feat: new feature")
36+
rawCommit := createRawCommit("a", "fix: bug #123 and #243\nthanks @Test-user for providing this fix\n\nCloses #22")
3837
commit := defaultAnalyzer.analyzeSingleCommit(rawCommit)
3938
require.Equal(t, rawCommit.SHA, commit.SHA)
4039
require.Equal(t, rawCommit.RawMessage, strings.Join(commit.Raw, "\n"))
41-
require.Equal(t, rawCommit.Annotations, commit.Annotations)
40+
require.Equal(t, "test", commit.Annotations["author_name"])
41+
require.Equal(t, "123,243,22", commit.Annotations["mentioned_issues"])
42+
require.Equal(t, "Test-user", commit.Annotations["mentioned_users"])
4243
}
4344

4445
func TestDefaultAnalyzer(t *testing.T) {

0 commit comments

Comments
 (0)