diff --git a/pkg/analyzer/commit_analyzer.go b/pkg/analyzer/commit_analyzer.go index 3178641..8b1121e 100644 --- a/pkg/analyzer/commit_analyzer.go +++ b/pkg/analyzer/commit_analyzer.go @@ -8,11 +8,21 @@ import ( ) var ( - CAVERSION = "dev" - commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`) - breakingPattern = regexp.MustCompile("BREAKING CHANGES?") + CAVERSION = "dev" + commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`) + breakingPattern = regexp.MustCompile("BREAKING CHANGES?") + mentionedIssuesPattern = regexp.MustCompile(`#(\d+)`) + mentionedUsersPattern = regexp.MustCompile(`(?i)@([a-z\d]([a-z\d]|-[a-z\d])+)`) ) +func extractMentions(re *regexp.Regexp, s string) string { + ret := make([]string, 0) + for _, m := range re.FindAllStringSubmatch(s, -1) { + ret = append(ret, m[1]) + } + return strings.Join(ret, ",") +} + type DefaultCommitAnalyzer struct{} func (da *DefaultCommitAnalyzer) Init(m map[string]string) error { @@ -34,6 +44,9 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit Change: &semrel.Change{}, Annotations: rawCommit.Annotations, } + c.Annotations["mentioned_issues"] = extractMentions(mentionedIssuesPattern, rawCommit.RawMessage) + c.Annotations["mentioned_users"] = extractMentions(mentionedUsersPattern, rawCommit.RawMessage) + found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1) if len(found) < 1 { return c diff --git a/pkg/analyzer/commit_analyzer_test.go b/pkg/analyzer/commit_analyzer_test.go index 17229ae..d08276f 100644 --- a/pkg/analyzer/commit_analyzer_test.go +++ b/pkg/analyzer/commit_analyzer_test.go @@ -26,19 +26,20 @@ func createRawCommit(sha, message string) *semrel.RawCommit { SHA: sha, RawMessage: message, Annotations: map[string]string{ - "author_name": "test", - "my-annotation": "true", + "author_name": "test", }, } } func TestAnnotations(t *testing.T) { defaultAnalyzer := &DefaultCommitAnalyzer{} - rawCommit := createRawCommit("a", "feat: new feature") + rawCommit := createRawCommit("a", "fix: bug #123 and #243\nthanks @Test-user for providing this fix\n\nCloses #22") commit := defaultAnalyzer.analyzeSingleCommit(rawCommit) require.Equal(t, rawCommit.SHA, commit.SHA) require.Equal(t, rawCommit.RawMessage, strings.Join(commit.Raw, "\n")) - require.Equal(t, rawCommit.Annotations, commit.Annotations) + require.Equal(t, "test", commit.Annotations["author_name"]) + require.Equal(t, "123,243,22", commit.Annotations["mentioned_issues"]) + require.Equal(t, "Test-user", commit.Annotations["mentioned_users"]) } func TestDefaultAnalyzer(t *testing.T) {