Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Fix no mention sent on comment
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Jul 2, 2018
1 parent aa36959 commit 9253bda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
14 changes: 9 additions & 5 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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, ", ")
Expand Down
35 changes: 32 additions & 3 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}},
},
Expand Down

0 comments on commit 9253bda

Please sign in to comment.