-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
179 lines (155 loc) · 5.98 KB
/
version.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
package jira
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"io/ioutil"
)
// VersionService handles Versions for the Jira instance / API.
//
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/version
type VersionService struct {
client *Client
}
// Version represents a single release version of a project
type Version struct {
Self string `json:"self,omitempty" structs:"self,omitempty"`
ID string `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
Description string `json:"description,omitempty" structs:"description,omitempty"`
Archived *bool `json:"archived,omitempty" structs:"archived,omitempty"`
Released *bool `json:"released,omitempty" structs:"released,omitempty"`
ReleaseDate string `json:"releaseDate,omitempty" structs:"releaseDate,omitempty"`
UserReleaseDate string `json:"userReleaseDate,omitempty" structs:"userReleaseDate,omitempty"`
ProjectID int `json:"projectId,omitempty" structs:"projectId,omitempty"` // Unlike other IDs, this is returned as a number
StartDate string `json:"startDate,omitempty" structs:"startDate,omitempty"`
}
// GetWithContext gets version info from Jira
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-id-get
func (s *VersionService) GetWithContext(ctx context.Context, versionID int) (*Version, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/2/version/%v", versionID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
version := new(Version)
resp, err := s.client.Do(req, version)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
return version, resp, nil
}
// Get wraps GetWithContext using the background context.
func (s *VersionService) Get(versionID int) (*Version, *Response, error) {
return s.GetWithContext(context.Background(), versionID)
}
// CreateWithContext creates a version in Jira.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-post
func (s *VersionService) CreateWithContext(ctx context.Context, version *Version) (*Version, *Response, error) {
apiEndpoint := "/rest/api/2/version"
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, version)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, resp, err
}
responseVersion := new(Version)
defer Cleanup(resp)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
e := fmt.Errorf("could not read the returned data")
return nil, resp, NewJiraError(resp, e)
}
err = json.Unmarshal(data, responseVersion)
if err != nil {
e := fmt.Errorf("could not unmarshall the data into struct")
return nil, resp, NewJiraError(resp, e)
}
return responseVersion, resp, nil
}
// Create wraps CreateWithContext using the background context.
func (s *VersionService) Create(version *Version) (*Version, *Response, error) {
return s.CreateWithContext(context.Background(), version)
}
// UpdateWithContext updates a version from a JSON representation.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-id-put
// Caller must close resp.Body
func (s *VersionService) UpdateWithContext(ctx context.Context, version *Version) (*Version, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/version/%v", version.ID)
req, err := s.client.NewRequestWithContext(ctx, "PUT", apiEndpoint, version)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
// This is just to follow the rest of the API's convention of returning a version.
// Returning the same pointer here is pointless, so we return a copy instead.
ret := *version
return &ret, resp, nil
}
// Update wraps UpdateWithContext using the background context.
// Caller must close resp.Body
func (s *VersionService) Update(version *Version) (*Version, *Response, error) {
return s.UpdateWithContext(context.Background(), version)
}
type RelatedIssueCounts struct {
Self string `json:"self"`
IssuesFixedCount int `json:"issuesFixedCount"`
IssuesAffectedCount int `json:"issuesAffectedCount"`
IssueCountWithCustomFieldsShowingVersion int `json:"issueCountWithCustomFieldsShowingVersion"`
}
func (s *VersionService) GetRelatedIssueCounts(versionID string, options *GetQueryOptions) (*RelatedIssueCounts, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/latest/version/%s/relatedIssueCounts", versionID)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
if options != nil {
q, err2 := query.Values(options)
if err2 != nil {
return nil, nil, err2
}
req.URL.RawQuery = q.Encode()
}
relatedissuecounts := new(RelatedIssueCounts)
resp, err := s.client.Do(req, relatedissuecounts)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return relatedissuecounts, resp, nil
}
type IssuesUnresolvedCount struct {
Self string `json:"self"`
IssuesUnresolvedCount int `json:"issuesUnresolvedCount"`
}
func (s *VersionService) GetIssuesUnresolvedCount(versionID string, options *GetQueryOptions) (*IssuesUnresolvedCount, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/latest/version/%s/unresolvedIssueCount", versionID)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
if options != nil {
q, err2 := query.Values(options)
if err2 != nil {
return nil, nil, err2
}
req.URL.RawQuery = q.Encode()
}
issuesunresolvedcount := new(IssuesUnresolvedCount)
resp, err := s.client.Do(req, issuesunresolvedcount)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return issuesunresolvedcount, resp, nil
}