/
jira-client.go
154 lines (144 loc) · 4.07 KB
/
jira-client.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
package jira_client
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"github.com/NielsGaljaard/jira-rss-client/internal/domain"
"go.uber.org/zap"
)
const apiPath = "/rest/api/2/search"
type Config struct {
TicketsPerJQL uint32
AppUserName string
AppPassword string
AppBaseURL string
}
type Client struct {
config *Config
logger *zap.Logger
}
type TicketRequest struct {
JQL string `json:"jql"`
StartAt uint32 `json:"startAt"`
MaxResults uint32 `json:"maxResults"`
Fields []string `json:"fields"`
}
type apiResponse struct {
Tickets []ticketResponse `json:"issues"`
}
type ticketResponse struct {
Id string `json:"id"`
Key string `json:"key"`
Link string `json:"self"`
Fields ticketFields `json:"fields"`
}
type ticketFields struct {
Summary string `json:"summary"`
Assignee ticketAssignee `json:"assignee"`
Priority ticketPriority `json:"priority"`
Reporter ticketReporter `json:"reporter"`
IssueType ticketIssueType `json:"issuetype"`
Status ticketStatus `json:"status"`
}
type ticketStatus struct {
Name string `json:"name"`
}
type ticketAssignee struct {
DisplayName string `json:"displayName"`
}
type ticketPriority struct {
Name string `json:"name"`
}
type ticketReporter struct {
DisplayName string `json:"displayName"`
}
type ticketIssueType struct {
Name string `json:"name"`
}
func New(config *Config, logger *zap.Logger) (*Client, error) {
return &Client{
config: config,
logger: logger,
}, nil
}
func (c *Client) DoJQL(JQL string) (*[]domain.Ticket, error) {
bytes, err := c.doJQLRequest(JQL)
if err != nil {
return nil, err
}
responses, err := c.parseFromJson(bytes)
if err != nil {
return nil, err
}
tickets := c.createTicketsFromAPIResponse(responses)
return tickets, nil
}
func (c *Client) parseFromJson(responseBody []byte) (*[]ticketResponse, error) {
var response apiResponse
err := json.Unmarshal(responseBody, &response)
if err != nil {
c.logger.Error("error unmarshalling JSON", zap.Error(err))
c.logger.Info("failed response body", zap.String("body", string(responseBody)))
return nil, err
}
c.logger.Debug("request got", zap.Int("nr_tickets", len(response.Tickets)))
return &response.Tickets, nil
}
func (c *Client) doJQLRequest(JQL string) ([]byte, error) {
apiUrl, err := url.Parse(c.config.AppBaseURL + apiPath)
if err != nil {
return nil, err
}
c.logger.Debug("doing request for", zap.String("url", apiUrl.String()))
requestBody := &TicketRequest{
JQL: JQL,
StartAt: 0,
MaxResults: c.config.TicketsPerJQL,
Fields: []string{"*navigable"},
}
requestByes, err := json.Marshal(requestBody)
if err != nil {
c.logger.Error("failed to marshall request body")
return nil, err
}
req, err := http.NewRequest("POST", apiUrl.String(), bytes.NewReader(requestByes))
req.Header.Set("Content-Type", "application/json")
if err != nil {
c.logger.Error("cant create request", zap.Error(err))
return nil, err
}
req.SetBasicAuth(c.config.AppUserName, c.config.AppPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode > 299 || resp.StatusCode < 200 {
body, err := io.ReadAll(resp.Body)
if err != nil {
c.logger.Error("Api error with body", zap.String("body", string(body)))
return nil, errors.New("API ERROR")
}
c.logger.Error("API error", zap.Error(err), zap.Int("statusCode", resp.StatusCode))
return nil, errors.New("API ERROR")
}
body, err := io.ReadAll(resp.Body)
return body, err
}
func (c *Client) createTicketsFromAPIResponse(t *[]ticketResponse) *[]domain.Ticket {
tickets := make([]domain.Ticket, len(*t))
for i, ticket := range *t {
tickets[i].Id = ticket.Id
tickets[i].Key = ticket.Key
tickets[i].Link = ticket.Link
tickets[i].Assignee = ticket.Fields.Assignee.DisplayName
tickets[i].Reporter = ticket.Fields.Reporter.DisplayName
tickets[i].Priority = ticket.Fields.Priority.Name
tickets[i].IssueType = ticket.Fields.IssueType.Name
tickets[i].Status = ticket.Fields.Status.Name
tickets[i].Summary = ticket.Fields.Summary
}
return &tickets
}