diff --git a/formatter/formatter.go b/formatter/formatter.go index 5875816..50d3e12 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -9,8 +9,6 @@ import ( "github.com/int128/jira-to-slack/message" ) -var jiraMention = regexp.MustCompile(`\[~(\w+)\]|@(\w+)`) - // Formatter performs JIRA and Slack message conversion. type Formatter struct { Dialect message.Dialect @@ -104,6 +102,8 @@ func (f *Formatter) title(event *jira.Event, verb string, additionalMentions str } } +var jiraMention = regexp.MustCompile(`\[~(\w+)\]|@(\w+)`) + // mentions returns all mentions in the text. func (f *Formatter) mentions(text string) string { all := jiraMention.FindAllStringSubmatch(text, -1) @@ -112,9 +112,13 @@ func (f *Formatter) mentions(text string) string { } mentions := make([]string, 0) for _, m := range all { - if len(m) == 1 { - name := m[1] - mentions = append(mentions, fmt.Sprintf("%s", f.Dialect.Mention(name))) + switch { + case len(m) == 0: + // something wrong + case m[2] != "": + mentions = append(mentions, f.Dialect.Mention(m[2])) + case m[1] != "": + mentions = append(mentions, f.Dialect.Mention(m[1])) } } return strings.Join(mentions, ", ") diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index de56f8e..3e3ebf5 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -12,9 +12,38 @@ import ( "github.com/int128/jira-to-slack/jira" ) +func TestMention(t *testing.T) { + matrix := []struct { + source string + expected string + }{ + {"foo", ""}, + {"[~bob]", "<@bob>"}, + {"[~bob] hello", "<@bob>"}, + {"hello [~bob]", "<@bob>"}, + {"[@bob]", "<@bob>"}, + {"[@bob] hello", "<@bob>"}, + {"hello [@bob]", "<@bob>"}, + {"@bob", "<@bob>"}, + {"@bob hello", "<@bob>"}, + {"hello @bob", "<@bob>"}, + } + formatter := &Formatter{&message.SlackDialect{}} + for i := 0; i < len(matrix); i++ { + m := matrix[i] + t.Run(m.source, func(t *testing.T) { + actual := formatter.mentions(m.source) + if m.expected != actual { + t.Errorf("mention(%s) wants %s but %s", m.source, m.expected, actual) + } + }) + } +} + const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.` +const elitDuis = "[~bob]\r\n\r\nElit duis tristique sollicitudin nibh sit amet. Pharetra pharetra massa massa ultricies mi quis hendrerit dolor. Adipiscing elit duis tristique sollicitudin nibh sit amet commodo. Velit laoreet id donec ultrices tincidunt arcu non sodales neque. Faucibus vitae aliquet nec ullamcorper. Lobortis elementum nibh tellus molestie nunc non blandit massa. Eu lobortis elementum nibh tellus. Pharetra convallis posuere morbi leo urna molestie at elementum eu. Arcu odio ut sem nulla pharetra diam. Placerat orci nulla pellentesque dignissim enim sit. Enim ut tellus elementum sagittis.\r\n\r\n " -func TestFormatJIRAEventToSlackMessage(t *testing.T) { +func TestJIRAEventToSlackMessage(t *testing.T) { matrix := []struct { source string expected *message.Message @@ -61,11 +90,11 @@ func TestFormatJIRAEventToSlackMessage(t *testing.T) { { source: "../testdata/issue_updated_commented.json", expected: &message.Message{ - Text: "<@alice> commented the issue: <@bob>", + Text: "<@alice> commented to the issue: <@bob>", Attachments: message.Attachments{{ Title: "TEST-4: Lorem Ipsum", TitleLink: "https://jira.example.com/browse/TEST-4", - Text: loremIpsum, + Text: elitDuis, Timestamp: 1519993498, }}, },