-
Notifications
You must be signed in to change notification settings - Fork 42
/
search.go
287 lines (251 loc) · 7.08 KB
/
search.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package jira
import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/andygrunwald/go-jira"
"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/config"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/client"
"github.com/slack-go/slack"
)
type jiraCommand struct {
jira *jira.Client
slackClient client.SlackClient
config *config.Jira
}
const (
formatLink = "link"
)
var ticketRe = regexp.MustCompile(`^(\w+)-(\d+)$`)
// newJiraCommand search for a Jira ticket id or a JQL
func newJiraCommand(jiraClient *jira.Client, slackClient client.SlackClient, cfg *config.Jira) bot.Command {
return &jiraCommand{jiraClient, slackClient, cfg}
}
func (c *jiraCommand) GetMatcher() matcher.Matcher {
matchers := []matcher.Matcher{
matcher.NewRegexpMatcher("jira (?P<action>link) (?P<text>.*)", c.run),
matcher.NewRegexpMatcher("(?P<action>jira|issue|jql) (?P<text>.*)", c.run),
matcher.NewRegexpMatcher(
fmt.Sprintf(
"%s\\/browse\\/(?P<text>.*)",
regexp.QuoteMeta(strings.TrimRight(c.config.Host, "/")),
),
c.run,
),
}
if c.config.Project != "" {
// short alias for "PRO-123"
matchers = append(matchers, matcher.NewRegexpMatcher(
fmt.Sprintf(
`%s-(?P<text>\d+)`,
regexp.QuoteMeta(c.config.Project),
),
c.run,
))
}
return matcher.NewGroupMatcher(matchers...)
}
func (c *jiraCommand) run(match matcher.Result, message msg.Message) {
eventText := match.GetString("text")
ticketNumber := c.getTicketNumber(eventText)
action := match.GetString("action")
if ticketNumber != "" {
issue, _, err := c.jira.Issue.Get(ticketNumber, nil)
if err != nil {
c.slackClient.SendMessage(message, err.Error())
return
}
c.sendTicket(message, issue, action)
return
}
// search by JQL
defaultProject := c.config.Project
var jql string
if action == "jql" {
jql = match.GetString("text")
if !strings.Contains(jql, "project") {
// search in default project
jql = fmt.Sprintf("project = %s AND %s", defaultProject, jql)
}
} else {
// search in default project
jql = fmt.Sprintf("project = %s AND text ~ '%s' ORDER BY priority DESC", defaultProject, eventText)
}
c.jqlList(message, jql)
}
// "1234" -> PROJ-1234
// "FOO-1234" -> "FOO-1234"
// dsadsad -> ""
func (c *jiraCommand) getTicketNumber(eventText string) string {
var ticketNumber string
if _, err := strconv.Atoi(eventText); err == nil {
ticketNumber = c.config.Project + "-" + eventText
} else if ticketRe.MatchString(eventText) {
ticketNumber = eventText
}
return ticketNumber
}
func (c *jiraCommand) sendTicket(ref msg.Ref, issue *jira.Issue, format string) {
if format == formatLink {
text := fmt.Sprintf("<%s|%s: %s>", getTicketURL(c.config, *issue), issue.Key, issue.Fields.Summary)
c.slackClient.SendMessage(ref, text)
return
}
fields := []slack.AttachmentField{
{
Title: "Name",
Value: fmt.Sprintf("%s: %s", getFormattedURL(c.config, *issue), issue.Fields.Summary),
},
{
Title: "Priority",
Value: c.getField("Priority", issue.Fields.Priority.Name),
Short: true,
},
{
Title: "Type",
Value: c.getField("Type", issue.Fields.Type.Name),
Short: true,
},
}
if issue.Fields.Assignee != nil {
fields = append(fields, slack.AttachmentField{
Title: "Assignee",
Value: fmt.Sprintf(
"<%sissues/?jql=assignee=\"%s\" AND status != \"Closed\"|%s>",
c.config.Host,
issue.Fields.Assignee.Name,
issue.Fields.Assignee.DisplayName,
),
Short: true,
})
}
fields = append(fields, slack.AttachmentField{
Title: "Status",
Value: issue.Fields.Status.Name,
Short: true,
})
if len(issue.Fields.Components) > 0 {
var components []string
for _, component := range issue.Fields.Components {
components = append(components, component.Name)
}
fields = append(fields, slack.AttachmentField{
Title: "Components",
Value: strings.Join(components, ", "),
Short: true,
})
}
fields = append(fields, slack.AttachmentField{
Title: "Description",
Value: convertMarkdown(issue.Fields.Description),
Short: false,
})
if len(issue.Fields.Labels) > 0 {
fields = append(fields, slack.AttachmentField{
Title: "Labels",
Value: strings.Join(issue.Fields.Labels, ", "),
Short: false,
})
}
attachment := slack.Attachment{
Color: "#D3D3D3",
Fields: fields,
MarkdownIn: []string{
"text", "fields",
},
Actions: []slack.AttachmentAction{
client.GetSlackLink("Open in Jira", getTicketURL(c.config, *issue)),
},
}
c.slackClient.SendMessage(ref, "", slack.MsgOptionAttachments(attachment))
}
// get the name of the field and the mapped icon (if configured)
func (c *jiraCommand) getField(fieldType string, name string) string {
for _, field := range c.config.Fields {
if field.Name == fieldType {
if icon, ok := field.Icons[name]; ok {
return fmt.Sprintf("%s %s", name, icon)
}
return name + " :question:" // todo const
}
}
return name
}
func (c *jiraCommand) jqlList(message msg.Message, jql string) {
search := &url.URL{Path: jql}
tickets, _, err := c.jira.Issue.Search(jql, nil)
if err != nil {
c.slackClient.SendMessage(message, err.Error())
return
}
listText := ""
for _, ticket := range tickets {
if ticket.Fields == nil {
continue
}
listText += fmt.Sprintf(
"%s %s%s - %s (%s - %s)\n",
getFormattedURL(c.config, ticket),
idToIcon(ticket.Fields.Priority),
c.getField("Type", ticket.Fields.Type.Name),
ticket.Fields.Summary,
ticket.Fields.Status.Name,
getAssignee(ticket.Fields.Assignee),
)
}
// add button which leads to search
searchLink := fmt.Sprintf("%sissues/?jql=%s", c.config.Host, search.String())
attachment := slack.Attachment{}
attachment.Title = fmt.Sprintf("I found <%s|%d matching ticket(s)>.\n", searchLink, len(tickets))
attachment.Text = listText
attachment.Actions = []slack.AttachmentAction{
client.GetSlackLink("Search in Jira", searchLink),
}
c.slackClient.SendMessage(message, "", slack.MsgOptionAttachments(attachment))
}
func getAssignee(user *jira.User) string {
if user == nil {
return "unassigned"
}
return user.Name
}
func (c *jiraCommand) GetHelp() []bot.Help {
return []bot.Help{
{
Command: "jira <ticket|search>",
Description: "list jira ticket information or performs jira searches. It uses the configured jira project by default to display/search tickets",
Category: category,
Examples: []string{
"issue 43234",
fmt.Sprintf("%s/browse/PROJ-1234", strings.TrimRight(c.config.Host, "/")),
"issue PROJ-23123",
"issue \"second city\"",
},
},
{
Command: "jql <query>",
Description: "list jira tickets based on the given JQL",
Category: category,
Examples: []string{
"jql status=\"In Progress\"",
},
},
}
}
func (c *jiraCommand) GetTemplateFunction() template.FuncMap {
return template.FuncMap{
"jiraTicket": func(ticketId string) *jira.Issue {
issue, _, _ := c.jira.Issue.Get(ticketId, nil)
return issue
},
"jiraTicketUrl": func(ticketId string) string {
return fmt.Sprintf("%sbrowse/%s", c.config.Host, ticketId)
},
}
}