-
Notifications
You must be signed in to change notification settings - Fork 244
/
jira_cloud_accounts_mapping.go
301 lines (255 loc) · 11.4 KB
/
jira_cloud_accounts_mapping.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
package pagerduty
import (
"context"
"github.com/google/go-querystring/query"
)
// JiraCloudAccountsMapping establishes a connection between a PagerDuty account
// and a Jira Cloud instance, enabling integration and synchronization between
// the two platforms
type JiraCloudAccountsMapping struct {
UpdatedAt string `json:"updated_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
ID string `json:"id,omitempty"`
JiraCloudAccount JiraCloudAccount `json:"jira_cloud_account"`
PagerDutyAccount PagerDutyAccount `json:"pagerduty_account"`
}
// JiraCloudAccount describes an account from Jira
type JiraCloudAccount struct {
// The base URL of the Jira Cloud instance, used for API calls and
// constructing links
BaseURL string `json:"base_url"`
}
// PagerDutyAccount describes an account from PagerDuty
type PagerDutyAccount struct {
// The unique subdomain of the PagerDuty account, used to identify and
// access the account (e.g., acme in https://acme.pagerduty.com)
Subdomain string `json:"subdomain"`
}
// JiraCloudAccountsMappingRule configures the bidirectional synchronization
// between Jira issues and PagerDuty incidents
type JiraCloudAccountsMappingRule struct {
AccountsMapping *APIObject `json:"account_mapping,omitempty"`
AutocreateJqlDisabledReason string `json:"autocreate_jql_disabled_reason,omitempty"`
AutocreateJqlDisabledUntil string `json:"autocreate_jql_disabled_until,omitempty"`
Config JiraCloudAccountsMappingRuleConfig `json:"config"`
ID string `json:"id,omitempty"`
Name string `json:"name"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// JiraCloudAccountsMappingRuleConfig is the configuration for bidirectional
// synchronization between Jira issues and PagerDuty incidents
type JiraCloudAccountsMappingRuleConfig struct {
Jira JiraCloudSettings `json:"jira"`
Service APIObject `json:"service"`
}
// JiraCloudSettings are settings for the Jira aspect of the synchronization
type JiraCloudSettings struct {
AutocreateJQL *string `json:"autocreate_jql"`
CreateIssueOnIncidentTrigger bool `json:"create_issue_on_incident_trigger"`
CustomFields []JiraCloudCustomField `json:"custom_fields"`
IssueType JiraCloudReference `json:"issue_type"`
Priorities []JiraCloudPriority `json:"priorities"`
Project JiraCloudReference `json:"project"`
StatusMapping JiraCloudStatusMapping `json:"status_mapping"`
SyncNotesUser *UserJiraCloud `json:"sync_notes_user"`
}
// JiraCloudCustomField defines how Jira fields are populated when a Jira Issue
// is created from a PagerDuty Incident
type JiraCloudCustomField struct {
// The PagerDuty incident field from which the value will be extracted
// (only applicable if type is attribute)
//
// Allowed values:
// incident_number, incident_title, incident_description,
// incident_status, incident_created_at, incident_service,
// incident_escalation_policy, incident_impacted_services,
// incident_html_url, incident_assignees, incident_acknowledgers,
// incident_last_status_change_at, incident_last_status_change_by,
// incident_urgency, incident_priority, null
SourceIncidentField *string `json:"source_incident_field"`
// The unique identifier key of the Jira field that will be set
TargetIssueField string `json:"target_issue_field"`
// The human-readable name of the Jira field
TargetIssueFieldName string `json:"target_issue_field_name"`
// The type of the value that will be set
//
// Allowed values:
// attribute, const, jira_value
Type string `json:"type"`
// The value to be set for the Jira field (only applicable if type is
// const or jira_value)
Value interface{} `json:"value,omitempty"`
}
// JiraCloudReference is a reference pointing to a Jira Cloud object
type JiraCloudReference struct {
ID string `json:"id"`
Name string `json:"name"`
Key string `json:"key,omitempty"`
}
// JiraCloudPriority is an association between a PagerDuty incident priority
// and a Jira issue priority
type JiraCloudPriority struct {
JiraID string `json:"jira_id"`
PagerDutyID string `json:"pagerduty_id"`
}
// JiraCloudStatusMapping is an association between PagerDuty incident statuses
// to their corresponding Jira issue statuses
type JiraCloudStatusMapping struct {
Acknowledged *JiraCloudReference `json:"acknowledged"`
Resolved *JiraCloudReference `json:"resolved"`
Triggered *JiraCloudReference `json:"triggered"`
}
// UserJiraCloud is a PagerDuty user for syncing notes and comments between Jira
// issues and PagerDuty incidents. If not provided, note synchronization is
// disabled
type UserJiraCloud struct {
APIObject
Email string `json:"email,omitempty"`
}
// ListJiraCloudAccountsMappingsOptions are the options available when calling the ListJiraCloudAccountsMappings API endpoint
type ListJiraCloudAccountsMappingsOptions struct {
Limit uint `url:"limit,omitempty"`
Offset uint `url:"offset,omitempty"`
Total bool `url:"total,omitempty"`
}
// ListJiraCloudAccountsMappingsResponse is the response when calling the ListJiraCloudAccountsMappings API endpoint
type ListJiraCloudAccountsMappingsResponse struct {
APIListObject
AccountsMappings []JiraCloudAccountsMapping `json:"accounts_mappings"`
}
// ListJiraCloudAccountsMappings lists existing account mappings
func (c *Client) ListJiraCloudAccountsMappings(ctx context.Context, o ListJiraCloudAccountsMappingsOptions) (*ListJiraCloudAccountsMappingsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/integration-jira-cloud/accounts_mappings?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListJiraCloudAccountsMappingsResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetJiraCloudAccountsMapping lists existing account mappings
func (c *Client) GetJiraCloudAccountsMapping(ctx context.Context, id string) (*JiraCloudAccountsMapping, error) {
resp, err := c.get(ctx, "/integration-jira-cloud/accounts_mappings/"+id, nil)
if err != nil {
return nil, err
}
var result JiraCloudAccountsMapping
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// ListJiraCloudAccountsMappingRulesOptions are the options available when
// calling the ListJiraCloudAccountsMappingRules API endpoint
type ListJiraCloudAccountsMappingRulesOptions struct {
Limit uint `url:"limit,omitempty"`
Offset uint `url:"offset,omitempty"`
Total bool `url:"total,omitempty"`
}
// ListJiraCloudAccountsMappingRulesResponse is the response when calling the
// ListJiraCloudAccountsMappingRules API endpoint
type ListJiraCloudAccountsMappingRulesResponse struct {
APIListObject
Rules []JiraCloudAccountsMappingRule `json:"rules"`
}
// ListJiraCloudAccountsMappingRules lists existing rules for a specific account
// mapping
func (c *Client) ListJiraCloudAccountsMappingRules(ctx context.Context, id string, o ListJiraCloudAccountsMappingRulesOptions) (*ListJiraCloudAccountsMappingsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/integration-jira-cloud/accounts_mappings/"+id+"/rules?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListJiraCloudAccountsMappingsResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateJiraCloudAccountsMappingRule creates a new rule in Jira Cloud's integration
func (c *Client) CreateJiraCloudAccountsMappingRule(ctx context.Context, id string, rule JiraCloudAccountsMappingRule) (*JiraCloudAccountsMappingRule, error) {
resp, err := c.post(ctx, "/integration-jira-cloud/accounts_mappings/"+id+"/rules", rule, nil)
if err != nil {
return nil, err
}
var result JiraCloudAccountsMappingRule
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetJiraCloudAccountsMappingRule gets detailed information about an existing rule
func (c *Client) GetJiraCloudAccountsMappingRule(ctx context.Context, id, ruleID string) (*JiraCloudAccountsMappingRule, error) {
resp, err := c.get(ctx, "/integration-jira-cloud/accounts_mappings/"+id+"/rules/"+ruleID, nil)
if err != nil {
return nil, err
}
var result JiraCloudAccountsMappingRule
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// DeleteJiraCloudAccountsMappingRule deletes an existing rule in Jira Cloud's integration
func (c *Client) DeleteJiraCloudAccountsMappingRule(ctx context.Context, id, ruleID string) error {
_, err := c.delete(ctx, "/integration-jira-cloud/accounts_mappings/"+id+"/rules/"+ruleID)
return err
}
// updateJiraCloudAccountsMappingRuleBody is the body for the call to the
// UpdateJiraCloudAccountsMappingRule API endpoint
type updateJiraCloudAccountsMappingRuleBody struct {
Config updateJiraCloudAccountsMappingRuleConfig `json:"config"`
Name string `json:"name"`
}
// updateJiraCloudAccountsMappingRuleOptionsConfig is an special representation
// of a configuration used for updating a rule.
type updateJiraCloudAccountsMappingRuleConfig struct {
Jira updateJiraCloudSettings `json:"jira"`
}
// updateJiraCloudSettings are settings to update the Jira aspect of the
// synchronization
type updateJiraCloudSettings struct {
AutocreateJQL *string `json:"autocreate_jql"`
CreateIssueOnIncidentTrigger bool `json:"create_issue_on_incident_trigger"`
CustomFields []JiraCloudCustomField `json:"custom_fields"`
IssueType JiraCloudReference `json:"issue_type"`
Priorities []JiraCloudPriority `json:"priorities"`
StatusMapping JiraCloudStatusMapping `json:"status_mapping"`
SyncNotesUser *UserJiraCloud `json:"sync_notes_user"`
}
// UpdateJiraCloudAccountsMappingRule updates an existing rule in Jira Cloud's integration
func (c *Client) UpdateJiraCloudAccountsMappingRule(ctx context.Context, accountMappingID string, rule JiraCloudAccountsMappingRule) (*JiraCloudAccountsMappingRule, error) {
o := updateJiraCloudAccountsMappingRuleBody{
Config: updateJiraCloudAccountsMappingRuleConfig{
Jira: updateJiraCloudSettings{
AutocreateJQL: rule.Config.Jira.AutocreateJQL,
CreateIssueOnIncidentTrigger: rule.Config.Jira.CreateIssueOnIncidentTrigger,
CustomFields: rule.Config.Jira.CustomFields,
IssueType: rule.Config.Jira.IssueType,
Priorities: rule.Config.Jira.Priorities,
StatusMapping: rule.Config.Jira.StatusMapping,
SyncNotesUser: rule.Config.Jira.SyncNotesUser,
},
},
Name: rule.Name,
}
resp, err := c.put(ctx, "/integration-jira-cloud/accounts_mappings/"+accountMappingID+"/rules/"+rule.ID, o, nil)
if err != nil {
return nil, err
}
var result JiraCloudAccountsMappingRule
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}