-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnudge.go
128 lines (109 loc) · 2.85 KB
/
nudge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package nudge
import (
"context"
"fmt"
"log"
"github.com/mterwill/jira-slack-nudge/pkg/jira"
"github.com/mterwill/jira-slack-nudge/pkg/slack"
)
const (
maxIssues = 10
defaultConfigURL = "https://github.com/mterwill/jira-slack-nudge"
)
// Config is a 1:1 JIRA:Slack integration
type Config struct {
PrefixMessage string `yaml:"prefix_message"`
SlackWebhookURL string `yaml:"slack_webhook_url"`
JQL string `yaml:"jql"`
SkipSendEmpty bool `yaml:"skip_send_empty"`
}
// Nudge stores global JIRA config
type Nudge struct {
jiraServer string
jiraUsername string
jiraPassword string
configURL string
}
// New creates a new Nudge
func New(jiraServer, jiraUsername, jiraPassword, configURL string) *Nudge {
if configURL == "" {
configURL = defaultConfigURL
}
return &Nudge{jiraServer, jiraUsername, jiraPassword, configURL}
}
// Run finds JIRA issues matching a given JQL and post the result to Slack.
func (n *Nudge) Run(ctx context.Context, c *Config) error {
j := jira.New(n.jiraServer, n.jiraUsername, n.jiraPassword)
resp, err := j.FindIssues(ctx, c.JQL)
if err != nil {
return err
}
if len(resp.Issues) == 0 && c.SkipSendEmpty {
log.Printf("Found no results for JQL `%s` and Config.SkipSendEmpty is on.", c.JQL)
return nil
}
var blocks []*slack.Block
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeSection,
Text: &slack.Text{
Type: slack.BlockTextTypeMarkdown,
Text: c.PrefixMessage,
},
})
if len(resp.Issues) == 0 {
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeSection,
Text: &slack.Text{
Type: slack.BlockTextTypeMarkdown,
Text: "_No issues found matching filter._",
},
})
} else {
for i, issue := range resp.Issues {
if i > maxIssues {
break
}
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeSection,
Text: &slack.Text{
Type: slack.BlockTextTypeMarkdown,
Text: fmt.Sprintf(
"• *<%s|%s>* %s (status: `%s`, assignee: `%s`)",
j.LinkForIssue(&issue),
issue.Key,
issue.Fields.Summary,
issue.Fields.Status.Name,
issue.Assignee(),
),
},
})
}
}
if resp.Total > maxIssues {
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeSection,
Text: &slack.Text{
Type: slack.BlockTextTypeMarkdown,
Text: fmt.Sprintf("_showing %d of %d total results._", maxIssues, resp.Total),
},
})
}
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeDivider,
})
blocks = append(blocks, &slack.Block{
Type: slack.BlockTypeSection,
Text: &slack.Text{
Type: slack.BlockTextTypeMarkdown,
Text: fmt.Sprintf("This message was generated by <%s|jira-slack-nudge> using the JQL: ```%s```", n.configURL, c.JQL),
},
})
s := slack.New(c.SlackWebhookURL)
err = s.PostMessage(ctx, &slack.Message{
Blocks: blocks,
})
if err != nil {
return err
}
return nil
}