11package main
22
33import (
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
1511func 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- }
0 commit comments