Skip to content

refactor: dry up sending Slack messages #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/doctext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"context"
"log"
"net/http"
"os"
"strings"
"sync"

jira "github.com/andygrunwald/go-jira"
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

const queryTriaged = query.ShiftStack + `AND status in ("Release Pending", Verified, ON_QA) AND "Release Note Text" is EMPTY`
Expand Down Expand Up @@ -43,8 +43,8 @@ func main() {
gotErrors bool
wg sync.WaitGroup
)
slackClient := &http.Client{}
issues := make(map[string][]jira.Issue)
slackClient := slack.New()
issuesNeedingAttention := make(map[string][]jira.Issue)
for issue := range query.SearchIssues(ctx, jiraClient, queryTriaged) {
wg.Add(1)
found++
Expand All @@ -71,19 +71,19 @@ func main() {
} else {
assignee = issue.Fields.Assignee.Name
}
issues[assignee] = append(issues[assignee], issue)
issuesNeedingAttention[assignee] = append(issuesNeedingAttention[assignee], issue)

}
}(issue)
}
wg.Wait()

for assignee, issue := range issues {
for assignee, issues := range issuesNeedingAttention {
teamMember, ok := team[assignee]
if !ok {
teamMember = team["team"]
}
if err := notify(SLACK_HOOK, slackClient, issue, teamMember); err != nil {
if err := slackClient.Send(SLACK_HOOK, notification(issues, teamMember)); err != nil {
gotErrors = true
log.Print(err)
return
Expand Down
43 changes: 3 additions & 40 deletions cmd/doctext/notify.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

jira "github.com/andygrunwald/go-jira"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

func notification(issues []jira.Issue, assignee TeamMember) string {
Expand All @@ -23,41 +19,8 @@ func notification(issues []jira.Issue, assignee TeamMember) string {
var notification strings.Builder
notification.WriteString(slackId + " please check the Release Note Text of these bugs:")
for _, issue := range issues {
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
notification.WriteByte(' ')
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
}
return notification.String()
}

func notify(slackHook string, slackClient *http.Client, issues []jira.Issue, assignee TeamMember) error {
var msg bytes.Buffer
err := json.NewEncoder(&msg).Encode(struct {
LinkNames bool `json:"link_names"`
Text string `json:"text"`
}{
LinkNames: true,
Text: notification(issues, assignee),
})
if err != nil {
return fmt.Errorf("error while preparing the Slack notification for %s: %w", assignee.SlackId, err)
}

res, err := slackClient.Post(
slackHook,
"application/JSON",
&msg,
)
if err != nil {
return fmt.Errorf("error while sending a Slack notification for %s: %w", assignee, err)
}

io.Copy(io.Discard, res.Body)
res.Body.Close()

switch res.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
default:
return fmt.Errorf("unexpected status code %q while sending a Slack notification for %s", res.Status, assignee)
}

return nil
}
6 changes: 3 additions & 3 deletions cmd/pretriage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"log"
"net/http"
"os"
"strings"
"sync"
Expand All @@ -12,6 +11,7 @@ import (
jira "github.com/andygrunwald/go-jira"
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

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

slackClient := &http.Client{}
slackClient := slack.New()
now := time.Now()

log.Print("Running the actual triage assignment...")
Expand Down Expand Up @@ -128,7 +128,7 @@ func main() {
return
}

if err := notify(SLACK_HOOK, slackClient, issue, assignee); err != nil {
if err := slackClient.Send(SLACK_HOOK, notification(issue, assignee)); err != nil {
gotErrors = true
log.Print(err)
return
Expand Down
45 changes: 4 additions & 41 deletions cmd/pretriage/notify.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

jira "github.com/andygrunwald/go-jira"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

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

var notification strings.Builder
notification.WriteString(slackId + " you have been assigned triage of this bug:")
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
notification.WriteString(slackId)
notification.WriteString(" you have been assigned triage of this bug: ")
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
return notification.String()
}

func notify(slackHook string, slackClient *http.Client, issue jira.Issue, assignee TeamMember) error {
var msg bytes.Buffer
err := json.NewEncoder(&msg).Encode(struct {
LinkNames bool `json:"link_names"`
Text string `json:"text"`
}{
LinkNames: true,
Text: notification(issue, assignee),
})
if err != nil {
return fmt.Errorf("error while preparing the Slack notification for bug %s: %w", issue.Key, err)
}

res, err := slackClient.Post(
slackHook,
"application/JSON",
&msg,
)
if err != nil {
return fmt.Errorf("error while sending a Slack notification for bug %s: %w", issue.Key, err)
}

io.Copy(io.Discard, res.Body)
res.Body.Close()

switch res.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
default:
return fmt.Errorf("unexpected status code %q while sending a Slack notification for bug %s", res.Status, issue.Key)
}

return nil
}
6 changes: 3 additions & 3 deletions cmd/triage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"log"
"net/http"
"os"
"strings"
"sync"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/shiftstack/bugwatcher/cmd/triage/tasker"
"github.com/shiftstack/bugwatcher/pkg/jiraclient"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

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

if err := notify(SLACK_HOOK, slackClient, issues, teamMember); err != nil {
if err := slackClient.Send(SLACK_HOOK, notification(issues, teamMember)); err != nil {
gotErrors = true
log.Print(err)
return
Expand Down
46 changes: 5 additions & 41 deletions cmd/triage/notify.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

jira "github.com/andygrunwald/go-jira"
"github.com/shiftstack/bugwatcher/pkg/query"
"github.com/shiftstack/bugwatcher/pkg/slack"
)

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

var notification strings.Builder
notification.WriteString(slackId + " please triage these bugs:")
notification.WriteString(slackId)
notification.WriteString(" please triage these bugs:")
for _, issue := range issues {
notification.WriteString(fmt.Sprintf(" <%s|%s>", query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
notification.WriteByte(' ')
notification.WriteString(slack.Link(query.JiraBaseURL+"browse/"+issue.Key, issue.Key))
}
return notification.String()
}

func notify(slackHook string, slackClient *http.Client, issues []jira.Issue, assignee TeamMember) error {
var msg bytes.Buffer
err := json.NewEncoder(&msg).Encode(struct {
LinkNames bool `json:"link_names"`
Text string `json:"text"`
}{
LinkNames: true,
Text: notification(issues, assignee),
})
if err != nil {
return fmt.Errorf("error while preparing the Slack notification for %s: %w", assignee.SlackId, err)
}

res, err := slackClient.Post(
slackHook,
"application/JSON",
&msg,
)
if err != nil {
return fmt.Errorf("error while sending a Slack notification for %s: %w", assignee.SlackId, err)
}

io.Copy(io.Discard, res.Body)
res.Body.Close()

switch res.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
default:
return fmt.Errorf("unexpected status code %q while sending a Slack notification for %s", res.Status, assignee.SlackId)
}

return nil
}
55 changes: 55 additions & 0 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package slack

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)

type Client struct {
httpClient *http.Client
}

func New() Client {
return Client{httpClient: &http.Client{}}
}

func (c Client) Send(slackHook string, text string) error {
var msg bytes.Buffer
err := json.NewEncoder(&msg).Encode(struct {
LinkNames bool `json:"link_names"`
Text string `json:"text"`
}{
LinkNames: true,
Text: text,
})
if err != nil {
return fmt.Errorf("error marshalling the message payload: %w", err)
}

res, err := c.httpClient.Post(
slackHook,
"application/JSON",
&msg,
)
if err != nil {
return fmt.Errorf("error sending the message: %w", err)
}

io.Copy(io.Discard, res.Body)
res.Body.Close()

switch res.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
default:
return fmt.Errorf("unexpected status code %q sending the message:", res.Status)
}

return nil
}

func Link(text, url string) string {
return "<" + text + "|" + url + ">"
}