Skip to content
Snippets Groups Projects
Commit edf591f4 authored by Christoph Witzko's avatar Christoph Witzko
Browse files

feat: add mentioned users and issues to annotations

parent 92eda2f3
No related branches found
No related tags found
No related merge requests found
...@@ -11,8 +11,18 @@ var ( ...@@ -11,8 +11,18 @@ var (
CAVERSION = "dev" CAVERSION = "dev"
commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`) commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`)
breakingPattern = regexp.MustCompile("BREAKING CHANGES?") 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{} type DefaultCommitAnalyzer struct{}
func (da *DefaultCommitAnalyzer) Init(m map[string]string) error { func (da *DefaultCommitAnalyzer) Init(m map[string]string) error {
...@@ -34,6 +44,9 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit ...@@ -34,6 +44,9 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit
Change: &semrel.Change{}, Change: &semrel.Change{},
Annotations: rawCommit.Annotations, 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) found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
if len(found) < 1 { if len(found) < 1 {
return c return c
......
...@@ -27,18 +27,19 @@ func createRawCommit(sha, message string) *semrel.RawCommit { ...@@ -27,18 +27,19 @@ func createRawCommit(sha, message string) *semrel.RawCommit {
RawMessage: message, RawMessage: message,
Annotations: map[string]string{ Annotations: map[string]string{
"author_name": "test", "author_name": "test",
"my-annotation": "true",
}, },
} }
} }
func TestAnnotations(t *testing.T) { func TestAnnotations(t *testing.T) {
defaultAnalyzer := &DefaultCommitAnalyzer{} 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) commit := defaultAnalyzer.analyzeSingleCommit(rawCommit)
require.Equal(t, rawCommit.SHA, commit.SHA) require.Equal(t, rawCommit.SHA, commit.SHA)
require.Equal(t, rawCommit.RawMessage, strings.Join(commit.Raw, "\n")) 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) { func TestDefaultAnalyzer(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment