Skip to content

Commit bc26354

Browse files
refactor: dry up sending Slack messages
1 parent 32d083b commit bc26354

File tree

7 files changed

+79
-134
lines changed

7 files changed

+79
-134
lines changed

cmd/doctext/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package main
33
import (
44
"context"
55
"log"
6-
"net/http"
76
"os"
87
"strings"
98
"sync"
109

1110
jira "github.com/andygrunwald/go-jira"
1211
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
1312
"github.com/shiftstack/bugwatcher/pkg/query"
13+
"github.com/shiftstack/bugwatcher/pkg/slack"
1414
)
1515

1616
const queryTriaged = query.ShiftStack + `AND status in ("Release Pending", Verified, ON_QA) AND "Release Note Text" is EMPTY`
@@ -43,8 +43,8 @@ func main() {
4343
gotErrors bool
4444
wg sync.WaitGroup
4545
)
46-
slackClient := &http.Client{}
47-
issues := make(map[string][]jira.Issue)
46+
slackClient := slack.New()
47+
issuesNeedingAttention := make(map[string][]jira.Issue)
4848
for issue := range query.SearchIssues(ctx, jiraClient, queryTriaged) {
4949
wg.Add(1)
5050
found++
@@ -71,19 +71,19 @@ func main() {
7171
} else {
7272
assignee = issue.Fields.Assignee.Name
7373
}
74-
issues[assignee] = append(issues[assignee], issue)
74+
issuesNeedingAttention[assignee] = append(issuesNeedingAttention[assignee], issue)
7575

7676
}
7777
}(issue)
7878
}
7979
wg.Wait()
8080

81-
for assignee, issue := range issues {
81+
for assignee, issues := range issuesNeedingAttention {
8282
teamMember, ok := team[assignee]
8383
if !ok {
8484
teamMember = team["team"]
8585
}
86-
if err := notify(SLACK_HOOK, slackClient, issue, teamMember); err != nil {
86+
if err := slackClient.Send(SLACK_HOOK, notification(issues, teamMember)); err != nil {
8787
gotErrors = true
8888
log.Print(err)
8989
return

cmd/doctext/notify.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"fmt"
7-
"io"
8-
"net/http"
94
"strings"
105

116
jira "github.com/andygrunwald/go-jira"
127
"github.com/shiftstack/bugwatcher/pkg/query"
8+
"github.com/shiftstack/bugwatcher/pkg/slack"
139
)
1410

1511
func notification(issues []jira.Issue, assignee TeamMember) string {
@@ -23,41 +19,8 @@ func notification(issues []jira.Issue, assignee TeamMember) string {
2319
var notification strings.Builder
2420
notification.WriteString(slackId + " please check the Release Note Text of these bugs:")
2521
for _, issue := range issues {
26-
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
22+
notification.WriteByte(' ')
23+
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
2724
}
2825
return notification.String()
2926
}
30-
31-
func notify(slackHook string, slackClient *http.Client, issues []jira.Issue, assignee TeamMember) error {
32-
var msg bytes.Buffer
33-
err := json.NewEncoder(&msg).Encode(struct {
34-
LinkNames bool `json:"link_names"`
35-
Text string `json:"text"`
36-
}{
37-
LinkNames: true,
38-
Text: notification(issues, assignee),
39-
})
40-
if err != nil {
41-
return fmt.Errorf("error while preparing the Slack notification for %s: %w", assignee.SlackId, err)
42-
}
43-
44-
res, err := slackClient.Post(
45-
slackHook,
46-
"application/JSON",
47-
&msg,
48-
)
49-
if err != nil {
50-
return fmt.Errorf("error while sending a Slack notification for %s: %w", assignee, err)
51-
}
52-
53-
io.Copy(io.Discard, res.Body)
54-
res.Body.Close()
55-
56-
switch res.StatusCode {
57-
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
58-
default:
59-
return fmt.Errorf("unexpected status code %q while sending a Slack notification for %s", res.Status, assignee)
60-
}
61-
62-
return nil
63-
}

cmd/pretriage/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"log"
6-
"net/http"
76
"os"
87
"strings"
98
"sync"
@@ -12,6 +11,7 @@ import (
1211
jira "github.com/andygrunwald/go-jira"
1312
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
1413
"github.com/shiftstack/bugwatcher/pkg/query"
14+
"github.com/shiftstack/bugwatcher/pkg/slack"
1515
)
1616

1717
const queryUntriaged = query.ShiftStack + `AND ( assignee is EMPTY OR assignee = shiftstack ) AND (labels not in ("Triaged") OR labels is EMPTY)`
@@ -87,7 +87,7 @@ func main() {
8787
os.Exit(1)
8888
}
8989

90-
slackClient := &http.Client{}
90+
slackClient := slack.New()
9191
now := time.Now()
9292

9393
log.Print("Running the actual triage assignment...")
@@ -128,7 +128,7 @@ func main() {
128128
return
129129
}
130130

131-
if err := notify(SLACK_HOOK, slackClient, issue, assignee); err != nil {
131+
if err := slackClient.Send(SLACK_HOOK, notification(issue, assignee)); err != nil {
132132
gotErrors = true
133133
log.Print(err)
134134
return

cmd/pretriage/notify.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"fmt"
7-
"io"
8-
"net/http"
94
"strings"
105

116
jira "github.com/andygrunwald/go-jira"
127
"github.com/shiftstack/bugwatcher/pkg/query"
8+
"github.com/shiftstack/bugwatcher/pkg/slack"
139
)
1410

1511
func notification(issue jira.Issue, assignee TeamMember) string {
@@ -21,41 +17,8 @@ func notification(issue jira.Issue, assignee TeamMember) string {
2117
}
2218

2319
var notification strings.Builder
24-
notification.WriteString(slackId + " you have been assigned triage of this bug:")
25-
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
20+
notification.WriteString(slackId)
21+
notification.WriteString(" you have been assigned triage of this bug: ")
22+
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
2623
return notification.String()
2724
}
28-
29-
func notify(slackHook string, slackClient *http.Client, issue jira.Issue, assignee TeamMember) error {
30-
var msg bytes.Buffer
31-
err := json.NewEncoder(&msg).Encode(struct {
32-
LinkNames bool `json:"link_names"`
33-
Text string `json:"text"`
34-
}{
35-
LinkNames: true,
36-
Text: notification(issue, assignee),
37-
})
38-
if err != nil {
39-
return fmt.Errorf("error while preparing the Slack notification for bug %s: %w", issue.Key, err)
40-
}
41-
42-
res, err := slackClient.Post(
43-
slackHook,
44-
"application/JSON",
45-
&msg,
46-
)
47-
if err != nil {
48-
return fmt.Errorf("error while sending a Slack notification for bug %s: %w", issue.Key, err)
49-
}
50-
51-
io.Copy(io.Discard, res.Body)
52-
res.Body.Close()
53-
54-
switch res.StatusCode {
55-
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
56-
default:
57-
return fmt.Errorf("unexpected status code %q while sending a Slack notification for bug %s", res.Status, issue.Key)
58-
}
59-
60-
return nil
61-
}

cmd/triage/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"log"
6-
"net/http"
76
"os"
87
"strings"
98
"sync"
@@ -12,6 +11,7 @@ import (
1211
"github.com/shiftstack/bugwatcher/cmd/triage/tasker"
1312
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
1413
"github.com/shiftstack/bugwatcher/pkg/query"
14+
"github.com/shiftstack/bugwatcher/pkg/slack"
1515
)
1616

1717
const queryUntriaged = query.ShiftStack + `AND (labels not in ("Triaged") OR labels is EMPTY) AND "Need Info From" is EMPTY`
@@ -40,7 +40,7 @@ func main() {
4040
gotErrors bool
4141
wg sync.WaitGroup
4242
)
43-
slackClient := &http.Client{}
43+
slackClient := slack.New()
4444
issuesByAssignee := new(tasker.Tasker)
4545
for issue := range query.SearchIssues(ctx, jiraClient, queryUntriaged) {
4646
wg.Add(1)
@@ -72,7 +72,7 @@ func main() {
7272
teamMember = team["team"]
7373
}
7474

75-
if err := notify(SLACK_HOOK, slackClient, issues, teamMember); err != nil {
75+
if err := slackClient.Send(SLACK_HOOK, notification(issues, teamMember)); err != nil {
7676
gotErrors = true
7777
log.Print(err)
7878
return

cmd/triage/notify.go

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"fmt"
7-
"io"
8-
"net/http"
94
"strings"
105

116
jira "github.com/andygrunwald/go-jira"
127
"github.com/shiftstack/bugwatcher/pkg/query"
8+
"github.com/shiftstack/bugwatcher/pkg/slack"
139
)
1410

1511
func notification(issues []jira.Issue, assignee TeamMember) string {
@@ -21,43 +17,11 @@ func notification(issues []jira.Issue, assignee TeamMember) string {
2117
}
2218

2319
var notification strings.Builder
24-
notification.WriteString(slackId + " please triage these bugs:")
20+
notification.WriteString(slackId)
21+
notification.WriteString(" please triage these bugs:")
2522
for _, issue := range issues {
26-
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
23+
notification.WriteByte(' ')
24+
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
2725
}
2826
return notification.String()
2927
}
30-
31-
func notify(slackHook string, slackClient *http.Client, issues []jira.Issue, assignee TeamMember) error {
32-
var msg bytes.Buffer
33-
err := json.NewEncoder(&msg).Encode(struct {
34-
LinkNames bool `json:"link_names"`
35-
Text string `json:"text"`
36-
}{
37-
LinkNames: true,
38-
Text: notification(issues, assignee),
39-
})
40-
if err != nil {
41-
return fmt.Errorf("error while preparing the Slack notification for %s: %w", assignee.SlackId, err)
42-
}
43-
44-
res, err := slackClient.Post(
45-
slackHook,
46-
"application/JSON",
47-
&msg,
48-
)
49-
if err != nil {
50-
return fmt.Errorf("error while sending a Slack notification for %s: %w", assignee.SlackId, err)
51-
}
52-
53-
io.Copy(io.Discard, res.Body)
54-
res.Body.Close()
55-
56-
switch res.StatusCode {
57-
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
58-
default:
59-
return fmt.Errorf("unexpected status code %q while sending a Slack notification for %s", res.Status, assignee.SlackId)
60-
}
61-
62-
return nil
63-
}

pkg/slack/slack.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package slack
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
)
10+
11+
type Client struct {
12+
httpClient *http.Client
13+
}
14+
15+
func New() Client {
16+
return Client{httpClient: &http.Client{}}
17+
}
18+
19+
func (c Client) Send(slackHook string, text string) error {
20+
var msg bytes.Buffer
21+
err := json.NewEncoder(&msg).Encode(struct {
22+
LinkNames bool `json:"link_names"`
23+
Text string `json:"text"`
24+
}{
25+
LinkNames: true,
26+
Text: text,
27+
})
28+
if err != nil {
29+
return fmt.Errorf("error marshalling the message payload: %w", err)
30+
}
31+
32+
res, err := c.httpClient.Post(
33+
slackHook,
34+
"application/JSON",
35+
&msg,
36+
)
37+
if err != nil {
38+
return fmt.Errorf("error sending the message: %w", err)
39+
}
40+
41+
io.Copy(io.Discard, res.Body)
42+
res.Body.Close()
43+
44+
switch res.StatusCode {
45+
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
46+
default:
47+
return fmt.Errorf("unexpected status code %q sending the message:", res.Status)
48+
}
49+
50+
return nil
51+
}
52+
53+
func Link(text, url string) string {
54+
return "<" + text + "|" + url + ">"
55+
}

0 commit comments

Comments
 (0)