Skip to content

Commit

Permalink
feat: add mentioned users and issues to annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Jan 14, 2023
1 parent 92eda2f commit edf591f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 16 additions & 3 deletions pkg/analyzer/commit_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions pkg/analyzer/commit_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit edf591f

Please sign in to comment.