-
Notifications
You must be signed in to change notification settings - Fork 128
/
issue_parser.go
132 lines (109 loc) Β· 3.38 KB
/
issue_parser.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
// See License for license information.
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
package main
import (
"fmt"
"regexp"
jira "github.com/andygrunwald/go-jira"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
)
var jiraLinkWithTextRegex = regexp.MustCompile(`\[([^\[]+)\|([^\]]+)\]`)
func parseJiraLinksToMarkdown(text string) string {
return jiraLinkWithTextRegex.ReplaceAllString(text, "[${1}](${2})")
}
func mdKeySummaryLink(issue *jira.Issue, instance Instance) string {
return fmt.Sprintf("[%s: %s (%s)](%s%s)", issue.Key, issue.Fields.Summary, issue.Fields.Status.Name, instance.GetJiraBaseURL(), "/browse/"+issue.Key)
}
func reporterSummary(reporter *jira.User) string {
avatarLink := fmt.Sprintf("![avatar](%s =30x30)", reporter.AvatarUrls.One6X16)
reporterSummary := avatarLink + " " + reporter.Name
return reporterSummary
}
func getActions(instanceID types.ID, client Client, issue *jira.Issue) ([]*model.PostAction, error) {
var actions []*model.PostAction
ctx := map[string]interface{}{
"issue_key": issue.ID,
"instance_id": instanceID.String(),
}
integration := &model.PostActionIntegration{
URL: fmt.Sprintf("/plugins/%s%s%s", manifest.Id, routeAPI, routeIssueTransition),
Context: ctx,
}
var options []*model.PostActionOptions
transitions, err := client.GetTransitions(issue.Key)
if err != nil {
return actions, err
}
// Remove current issue status from possible transitions
for _, transition := range transitions {
if transition.Name != issue.Fields.Status.Name {
options = append(options, &model.PostActionOptions{
Text: transition.Name,
Value: transition.To.Name,
})
}
}
actions = append(actions, &model.PostAction{
Name: "Transition issue",
Type: "select",
Options: options,
Integration: integration,
})
actions = append(actions, &model.PostAction{
Name: "Share publicly",
Type: "button",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("/plugins/%s%s%s", manifest.Id, routeAPI, routeSharePublicly),
Context: ctx,
},
})
return actions, nil
}
func asSlackAttachment(instance Instance, client Client, issue *jira.Issue, showActions bool) ([]*model.SlackAttachment, error) {
text := mdKeySummaryLink(issue, instance)
desc := truncate(issue.Fields.Description, 3000)
desc = parseJiraLinksToMarkdown(desc)
if desc != "" {
text += "\n\n" + desc + "\n"
}
var fields []*model.SlackAttachmentField
if issue.Fields.Assignee != nil {
fields = append(fields, &model.SlackAttachmentField{
Title: "Assignee",
Value: issue.Fields.Assignee.DisplayName,
Short: true,
})
}
if issue.Fields.Priority != nil {
fields = append(fields, &model.SlackAttachmentField{
Title: "Priority",
Value: issue.Fields.Priority.Name,
Short: true,
})
}
if issue.Fields.Reporter != nil {
fields = append(fields, &model.SlackAttachmentField{
Title: "Reporter",
Value: reporterSummary(issue.Fields.Reporter),
Short: true,
})
}
var actions []*model.PostAction
var err error
if showActions {
actions, err = getActions(instance.GetID(), client, issue)
if err != nil {
return []*model.SlackAttachment{}, err
}
}
return []*model.SlackAttachment{
{
// TODO is this supposed to be themed?
Color: "#95b7d0",
Text: text,
Fields: fields,
Actions: actions,
},
}, nil
}