-
Notifications
You must be signed in to change notification settings - Fork 13
/
jira.go
190 lines (175 loc) · 4.4 KB
/
jira.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
package issues
import (
"bytes"
"fmt"
"net/http"
"time"
"github.com/andygrunwald/go-jira"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
type JiraService struct {
JiraClient *jira.Client
ServerURL string
Project string
}
func CreateJiraIssueProvider(serverURL, username, apiToken, project string, batchMode bool) (IssueProvider, error) {
if serverURL == "" {
return nil, fmt.Errorf("no JIRA server URL for server")
}
var httpClient *http.Client
if apiToken != "" {
tp := jira.BasicAuthTransport{
Username: username,
Password: apiToken,
}
httpClient = tp.Client()
if batchMode {
log.Logger().Infof("Using JIRA server %s user name %s and an API token", serverURL, username)
}
} else {
if batchMode {
log.Logger().Warnf("No authentication found for JIRA server %s so using anonymous access", serverURL)
}
}
jiraClient, _ := jira.NewClient(httpClient, serverURL)
return &JiraService{
JiraClient: jiraClient,
ServerURL: serverURL,
Project: project,
}, nil
}
func (i *JiraService) GetIssue(key string) (*scm.Issue, error) {
issue, _, err := i.JiraClient.Issue.Get(key, nil)
if err != nil {
return nil, err
}
return i.jiraToGitIssue(issue), nil
}
func (i *JiraService) SearchIssues(query string) ([]*scm.Issue, error) {
jql := "project = " + i.Project + " AND status NOT IN (Closed, Resolved)"
if query != "" {
jql += " AND text ~ " + query
}
var answer []*scm.Issue
issues, _, err := i.JiraClient.Issue.Search(jql, nil)
if err != nil {
return answer, err
}
for _, issue := range issues {
iss := issue
answer = append(answer, i.jiraToGitIssue(&iss))
}
return answer, nil
}
func (i *JiraService) SearchIssuesClosedSince(_ time.Time) ([]*scm.Issue, error) {
log.Logger().Warn("TODO SearchIssuesClosedSince() not yet implemented for JIRA")
return nil, nil
}
func (i *JiraService) CreateIssue(issue *scm.Issue) (*scm.Issue, error) {
project, _, err := i.JiraClient.Project.Get(i.Project)
if err != nil {
return nil, errors.Wrapf(err, "could not find project %s", i.Project)
}
ji := i.gitToJiraIssue(issue)
issueTypes := project.IssueTypes
if len(issueTypes) > 0 {
it := issueTypes[0]
ji.Fields.Type.Name = it.Name
}
created, resp, err := i.JiraClient.Issue.Create(ji)
if err != nil {
buf := new(bytes.Buffer)
if resp != nil {
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
}
msg := buf.String()
return nil, errors.Wrapf(err, "failed to create issue: %s", msg)
}
return i.jiraToGitIssue(created), nil
}
func (i *JiraService) CreateIssueComment(_ string, _ string) error {
return fmt.Errorf("TODO")
}
func (i *JiraService) IssueURL(key string) string {
return stringhelpers.UrlJoin(i.ServerURL, "browse", key)
}
func (i *JiraService) jiraToGitIssue(issue *jira.Issue) *scm.Issue {
answer := &scm.Issue{}
key := issue.Key
// TODO
//answer.Key = key
answer.Link = i.IssueURL(key)
fields := issue.Fields
if fields != nil {
answer.Title = fields.Summary
answer.Body = fields.Description
/// TODO
//answer.Labels = gits.ToGitLabels(fields.Labels)
// TODO
//answer.ClosedAt = jiraTimeToTimeP(fields.Resolutiondate)
user := jiraUserToGitUser(fields.Reporter)
if user != nil {
answer.Author = *user
}
assignee := jiraUserToGitUser(fields.Assignee)
if assignee != nil {
answer.Assignees = []scm.User{*assignee}
}
}
return answer
}
func jiraUserToGitUser(user *jira.User) *scm.User {
if user == nil {
return nil
}
return &scm.User{
Avatar: jiraAvatarUrl(user),
Name: user.Name,
Login: user.Key,
Email: user.EmailAddress,
}
}
func jiraAvatarUrl(user *jira.User) string {
answer := ""
if user != nil {
av := user.AvatarUrls
answer = av.Four8X48
if answer == "" {
answer = av.Three2X32
}
if answer == "" {
answer = av.Two4X24
}
if answer == "" {
answer = av.One6X16
}
}
return answer
}
func (i *JiraService) gitToJiraIssue(issue *scm.Issue) *jira.Issue {
answer := &jira.Issue{
Fields: &jira.IssueFields{
Project: jira.Project{
Key: i.Project,
},
Summary: issue.Title,
Description: issue.Body,
Type: jira.IssueType{
Name: "Bug",
},
},
}
return answer
}
func (i *JiraService) ServerName() string {
return i.ServerURL
}
func (i *JiraService) HomeURL() string {
return stringhelpers.UrlJoin(i.ServerURL, "browse", i.Project)
}