-
Notifications
You must be signed in to change notification settings - Fork 1
/
jira_issues_single.go
114 lines (97 loc) · 2.87 KB
/
jira_issues_single.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
package command
import (
"context"
"github.com/slack-go/slack"
"github.com/tigerteufel85/boggy/bot"
"github.com/tigerteufel85/boggy/client"
"github.com/tigerteufel85/boggy/config"
"github.com/tigerteufel85/boggy/utils"
"gopkg.in/andygrunwald/go-jira.v1"
"strings"
)
type jiraIssuesSingle struct {
slackClient client.SlackClient
jira *jira.Client
jiraCfg config.JiraConfig
jiraReplies config.ReplyConfig
regex config.RegexConfig
}
func NewJiraIssuesSingle(slackClient client.SlackClient, jira *jira.Client, jiraCfg config.JiraConfig, jiraReplies config.ReplyConfig, regex config.RegexConfig) *jiraIssuesSingle {
return &jiraIssuesSingle{
slackClient,
jira,
jiraCfg,
jiraReplies,
regex,
}
}
func (c *jiraIssuesSingle) GetName() string {
return "jira single"
}
func (c *jiraIssuesSingle) IsValid(b *bot.Bot, command string) bool {
if !strings.HasPrefix(command, c.GetName()) {
return false
}
_, err := utils.NewJQL(c.regex, command).BuildJqlQuery(c.jiraCfg)
if err != nil {
return false
}
return true
}
func (c *jiraIssuesSingle) Execute(ctx context.Context, b *bot.Bot, eventText string, event *slack.MessageEvent, user bot.User) bool {
if !strings.HasPrefix(eventText, c.GetName()) {
return false
}
// Create JQL Query
jql := utils.NewJQL(c.regex, eventText)
query, err := jql.BuildJqlQuery(c.jiraCfg)
if err != nil {
c.slackClient.Respond(event, err.Error())
return true
}
// Search issues on JIRA
issues, searchResponse, err := c.jira.Issue.Search(query, &jira.SearchOptions{MaxResults: 50, Expand: "names"})
if err != nil {
auth, _ := c.slackClient.AuthTest()
if user.Name != auth.User {
c.slackClient.Respond(event, err.Error())
}
return true
}
if searchResponse.Total == 0 {
return true
}
// Get all fields
allFields, _, err := c.jira.Field.GetList()
if err != nil {
auth, _ := c.slackClient.AuthTest()
if user.Name != auth.User {
c.slackClient.Respond(event, err.Error())
}
return true
}
for _, issue := range issues {
customFields, _, err := c.jira.Issue.GetCustomFields(issue.ID)
if err != nil {
auth, _ := c.slackClient.AuthTest()
if user.Name != auth.User {
c.slackClient.Respond(event, err.Error())
}
return true
}
responseText := utils.NewLayout(c.regex, eventText).BuildSimpleTextResponse(c.jiraCfg, issue, customFields, allFields, jql)
c.slackClient.Respond(event, responseText)
}
return true
}
func (c *jiraIssuesSingle) GetHelp() []bot.Help {
return []bot.Help{
{
"jira issues single",
"creates a slack response for each crm sale which will start, the placeholders will be replaced respectively",
[]string{
"jira single <project:crm><issuetype:Campaign><offset-field:Start Time><offset-time:18h><jql>“Campaign Category” = Sale</jql><sort:starttime><title>A sale will start in %component% %date% at %time% : %summary%</title><time:90h>",
},
},
}
}