forked from andygrunwald/go-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organization.go
387 lines (311 loc) · 14 KB
/
organization.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package jira
import (
"context"
"fmt"
)
// OrganizationService handles Organizations for the Jira instance / API.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/
type OrganizationService struct {
client *Client
}
// OrganizationCreationDTO is DTO for creat organization API
type OrganizationCreationDTO struct {
Name string `json:"name,omitempty" structs:"name,omitempty"`
}
// SelfLink Stores REST API URL to the organization.
type SelfLink struct {
Self string `json:"self,omitempty" structs:"self,omitempty"`
}
// Organization contains Organization data
type Organization struct {
ID string `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
Links *SelfLink `json:"_links,omitempty" structs:"_links,omitempty"`
}
// OrganizationUsersDTO contains organization user ids
type OrganizationUsersDTO struct {
AccountIds []string `json:"accountIds,omitempty" structs:"accountIds,omitempty"`
}
// PagedDTO is response of a paged list
type PagedDTO struct {
Size int `json:"size,omitempty" structs:"size,omitempty"`
Start int `json:"start,omitempty" structs:"start,omitempty"`
Limit int `limit:"size,omitempty" structs:"limit,omitempty"`
IsLastPage bool `json:"isLastPage,omitempty" structs:"isLastPage,omitempty"`
Values []interface{} `values:"isLastPage,omitempty" structs:"values,omitempty"`
Expands []string `json:"_expands,omitempty" structs:"_expands,omitempty"`
}
// PropertyKey contains Property key details.
type PropertyKey struct {
Self string `json:"self,omitempty" structs:"self,omitempty"`
Key string `json:"key,omitempty" structs:"key,omitempty"`
}
// PropertyKeys contains an array of PropertyKey
type PropertyKeys struct {
Keys []PropertyKey `json:"keys,omitempty" structs:"keys,omitempty"`
}
// GetAllOrganizationsWithContext returns a list of organizations in
// the Jira Service Management instance.
// Use this method when you want to present a list
// of organizations or want to locate an organization
// by name.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-group-organization
func (s *OrganizationService) GetAllOrganizationsWithContext(ctx context.Context, start int, limit int, accountID string) (*PagedDTO, *Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization?start=%d&limit=%d", start, limit)
if accountID != "" {
apiEndPoint += fmt.Sprintf("&accountId=%s", accountID)
}
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
v := new(PagedDTO)
resp, err := s.client.Do(req, v)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return v, resp, nil
}
// GetAllOrganizations wraps GetAllOrganizationsWithContext using the background context.
func (s *OrganizationService) GetAllOrganizations(start int, limit int, accountID string) (*PagedDTO, *Response, error) {
return s.GetAllOrganizationsWithContext(context.Background(), start, limit, accountID)
}
// CreateOrganizationWithContext creates an organization by
// passing the name of the organization.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-post
func (s *OrganizationService) CreateOrganizationWithContext(ctx context.Context, name string) (*Organization, *Response, error) {
apiEndPoint := "rest/servicedeskapi/organization"
organization := OrganizationCreationDTO{
Name: name,
}
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndPoint, organization)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
o := new(Organization)
resp, err := s.client.Do(req, &o)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return o, resp, nil
}
// CreateOrganization wraps CreateOrganizationWithContext using the background context.
func (s *OrganizationService) CreateOrganization(name string) (*Organization, *Response, error) {
return s.CreateOrganizationWithContext(context.Background(), name)
}
// GetOrganizationWithContext returns details of an
// organization. Use this method to get organization
// details whenever your application component is
// passed an organization ID but needs to display
// other organization details.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-get
func (s *OrganizationService) GetOrganizationWithContext(ctx context.Context, organizationID int) (*Organization, *Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d", organizationID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
o := new(Organization)
resp, err := s.client.Do(req, &o)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return o, resp, nil
}
// GetOrganization wraps GetOrganizationWithContext using the background context.
func (s *OrganizationService) GetOrganization(organizationID int) (*Organization, *Response, error) {
return s.GetOrganizationWithContext(context.Background(), organizationID)
}
// DeleteOrganizationWithContext deletes an organization. Note that
// the organization is deleted regardless
// of other associations it may have.
// For example, associations with service desks.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-delete
func (s *OrganizationService) DeleteOrganizationWithContext(ctx context.Context, organizationID int) (*Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d", organizationID)
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndPoint, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return resp, jerr
}
return resp, nil
}
// DeleteOrganization wraps DeleteOrganizationWithContext using the background context.
func (s *OrganizationService) DeleteOrganization(organizationID int) (*Response, error) {
return s.DeleteOrganizationWithContext(context.Background(), organizationID)
}
// GetPropertiesKeysWithContext returns the keys of
// all properties for an organization. Use this resource
// when you need to find out what additional properties
// items have been added to an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-property-get
func (s *OrganizationService) GetPropertiesKeysWithContext(ctx context.Context, organizationID int) (*PropertyKeys, *Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/property", organizationID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
pk := new(PropertyKeys)
resp, err := s.client.Do(req, &pk)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return pk, resp, nil
}
// GetPropertiesKeys wraps GetPropertiesKeysWithContext using the background context.
func (s *OrganizationService) GetPropertiesKeys(organizationID int) (*PropertyKeys, *Response, error) {
return s.GetPropertiesKeysWithContext(context.Background(), organizationID)
}
// GetPropertyWithContext returns the value of a property
// from an organization. Use this method to obtain the JSON
// content for an organization's property.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-property-propertykey-get
func (s *OrganizationService) GetPropertyWithContext(ctx context.Context, organizationID int, propertyKey string) (*EntityProperty, *Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/property/%s", organizationID, propertyKey)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
ep := new(EntityProperty)
resp, err := s.client.Do(req, &ep)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return ep, resp, nil
}
// GetProperty wraps GetPropertyWithContext using the background context.
func (s *OrganizationService) GetProperty(organizationID int, propertyKey string) (*EntityProperty, *Response, error) {
return s.GetPropertyWithContext(context.Background(), organizationID, propertyKey)
}
// SetPropertyWithContext sets the value of a
// property for an organization. Use this
// resource to store custom data against an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-property-propertykey-put
func (s *OrganizationService) SetPropertyWithContext(ctx context.Context, organizationID int, propertyKey string) (*Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/property/%s", organizationID, propertyKey)
req, err := s.client.NewRequestWithContext(ctx, "PUT", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return resp, jerr
}
return resp, nil
}
// SetProperty wraps SetPropertyWithContext using the background context.
func (s *OrganizationService) SetProperty(organizationID int, propertyKey string) (*Response, error) {
return s.SetPropertyWithContext(context.Background(), organizationID, propertyKey)
}
// DeletePropertyWithContext removes a property from an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-property-propertykey-delete
func (s *OrganizationService) DeletePropertyWithContext(ctx context.Context, organizationID int, propertyKey string) (*Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/property/%s", organizationID, propertyKey)
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return resp, jerr
}
return resp, nil
}
// DeleteProperty wraps DeletePropertyWithContext using the background context.
func (s *OrganizationService) DeleteProperty(organizationID int, propertyKey string) (*Response, error) {
return s.DeletePropertyWithContext(context.Background(), organizationID, propertyKey)
}
// GetUsersWithContext returns all the users
// associated with an organization. Use this
// method where you want to provide a list of
// users for an organization or determine if
// a user is associated with an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-user-get
func (s *OrganizationService) GetUsersWithContext(ctx context.Context, organizationID int, start int, limit int) (*PagedDTO, *Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/user?start=%d&limit=%d", organizationID, start, limit)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, nil, err
}
users := new(PagedDTO)
resp, err := s.client.Do(req, &users)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return users, resp, nil
}
// GetUsers wraps GetUsersWithContext using the background context.
func (s *OrganizationService) GetUsers(organizationID int, start int, limit int) (*PagedDTO, *Response, error) {
return s.GetUsersWithContext(context.Background(), organizationID, start, limit)
}
// AddUsersWithContext adds users to an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-user-post
func (s *OrganizationService) AddUsersWithContext(ctx context.Context, organizationID int, users OrganizationUsersDTO) (*Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/user", organizationID)
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndPoint, users)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return resp, jerr
}
return resp, nil
}
// AddUsers wraps AddUsersWithContext using the background context.
func (s *OrganizationService) AddUsers(organizationID int, users OrganizationUsersDTO) (*Response, error) {
return s.AddUsersWithContext(context.Background(), organizationID, users)
}
// RemoveUsersWithContext removes users from an organization.
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-organization/#api-rest-servicedeskapi-organization-organizationid-user-delete
func (s *OrganizationService) RemoveUsersWithContext(ctx context.Context, organizationID int, users OrganizationUsersDTO) (*Response, error) {
apiEndPoint := fmt.Sprintf("rest/servicedeskapi/organization/%d/user", organizationID)
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndPoint, nil)
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return resp, jerr
}
return resp, nil
}
// RemoveUsers wraps RemoveUsersWithContext using the background context.
func (s *OrganizationService) RemoveUsers(organizationID int, users OrganizationUsersDTO) (*Response, error) {
return s.RemoveUsersWithContext(context.Background(), organizationID, users)
}