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

Commit

Permalink
Add notification if issue or comment has mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Jul 2, 2018
1 parent d6f2013 commit 168abf4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ https://jira-to-slack.example.com/?webhook=https://hooks.slack.com/xxx&username=
```


## How it works

The server sends a message to the Slack channel on the following triggers:

- Someone created an issue.
- Someone commented to an issue.
- Someone assigned an issue.
- Someone updated summary or description of an issue.
- Someone deleted an issue.

If the issue or comment has mentions (i.e. `@foo` or `[~foo]`), the server appends them to the title of message for Slack notification.


## Contribution

This is an open source software licensed under Apache License 2.0.
Expand Down
55 changes: 38 additions & 17 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package formatter

import (
"fmt"
"regexp"
"strings"

"github.com/int128/jira-to-slack/jira"
"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 All @@ -22,7 +26,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
switch {
case event.IsIssueCreated():
return &message.Message{
Text: f.title(event, "created"),
Text: f.title(event, "created", f.mentions(event.Issue.Fields.Description)),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -32,7 +36,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
case event.IsIssueCommented():
return &message.Message{
Text: f.title(event, "commented to"),
Text: f.title(event, "commented to", f.mentions(event.Comment.Body)),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -42,7 +46,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
case event.IsIssueAssigned():
return &message.Message{
Text: f.title(event, "assigned"),
Text: f.title(event, "assigned", f.mentions(event.Issue.Fields.Description)),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -52,7 +56,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
case event.IsIssueFieldUpdated("summary"):
return &message.Message{
Text: f.title(event, "updated"),
Text: f.title(event, "updated", ""),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -61,7 +65,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
case event.IsIssueFieldUpdated("description"):
return &message.Message{
Text: f.title(event, "updated"),
Text: f.title(event, "updated", f.mentions(event.Issue.Fields.Description)),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -71,7 +75,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
case event.IsIssueDeleted():
return &message.Message{
Text: f.title(event, "deleted"),
Text: f.title(event, "deleted", ""),
Attachments: message.Attachments{{
Title: event.Issue.FormatKeyAndSummary(),
TitleLink: event.Issue.GetURL(),
Expand All @@ -83,18 +87,35 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message
}
}

func (f *Formatter) title(event *jira.Event, verb string) string {
return fmt.Sprintf("%s %s %s:",
f.Dialect.Mention(event.User.Name),
verb,
f.issue(event.Issue))
}

func (f *Formatter) issue(issue *jira.Issue) string {
// title returns a message title for the JIRA event.
func (f *Formatter) title(event *jira.Event, verb string, additionalMentions string) string {
switch {
case issue.Fields.Assignee == nil:
return "the issue"
case event.Issue.Fields.Assignee == nil:
return fmt.Sprintf("%s %s the issue: %s",
f.Dialect.Mention(event.User.Name),
verb,
additionalMentions)
default:
return fmt.Sprintf("the issue (assigned to %s)", f.Dialect.Mention(issue.Fields.Assignee.Name))
return fmt.Sprintf("%s %s the issue (assigned to %s): %s",
f.Dialect.Mention(event.User.Name),
verb,
f.Dialect.Mention(event.Issue.Fields.Assignee.Name),
additionalMentions)
}
}

// mentions returns all mentions in the text.
func (f *Formatter) mentions(text string) string {
all := jiraMention.FindAllStringSubmatch(text, -1)
if all == nil {
return ""
}
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)))
}
}
return strings.Join(mentions, ", ")
}

0 comments on commit 168abf4

Please sign in to comment.