-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJiraWebhook.js
468 lines (369 loc) · 11.8 KB
/
JiraWebhook.js
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
var JiraWebhookProcessor = Class.create();
JiraWebhookProcessor.prototype = Object.extendsObject(WebhookProcessor, {
process: function() {
this.debug = false;
if (this.debug) {
gs.info("JiraWebhookProcessor received webhook payload:\n" + global.JSON.stringify(this.data));
}
switch(this.data.webhookEvent) {
case "project_created":
case "project_updated":
this.update_project();
break;
case "project_deleted":
this.delete_project();
break;
case "jira:issue_created":
case "jira:issue_updated":
this.update_issue();
break;
case "jira:issue_deleted":
this.delete_issue();
break;
case "sprint_created":
case "sprint_updated":
case "sprint_started":
case "sprint_closed":
this.update_sprint();
break;
case "sprint_deleted":
this.delete_sprint();
break;
case "jira:worklog_updated":
this.update_worklog();
break;
case "worklog_deleted":
this.delete_worklog();
break;
default:
break;
}
},
/* JIRA Project => x_snc_ippy_jira_projects *********************************************************
webhookEvent = ['project_created', 'project_updated', 'project_deleted']
Notes:
- 'description' is not sent in webhook create or update events from JIRA
- project_deleted does not cascade events on child objects, clean up will be needed
***************************************************************************************************** */
update_project: function() {
key = this.data.project.key;
id = this.data.project.id;
url = this.data.project.self;
name = this.data.project.name;
email = this.data.project.projectLead.emailAddress;
host = this.get_uri_host(url);
ownerSysId = this.get_sn_user(email);
if (!ownerSysId) {
gs.error("Failed to locate user with email address '" + email +
"' for JIRA project '" + key + "' (" + host + ")");
}
rec = new GlideRecord('x_snc_ippy_jira_projects');
rec.addQuery('url', url);
rec.query();
rec.next();
rec.owner = ownerSysId;
rec.name = name;
rec.key = key;
rec.id = id;
rec.host = host;
rec.url = url;
rec.state = "Active";
rec.update();
return;
},
delete_project: function() {
key = this.data.project.key;
id = this.data.project.id;
url = this.data.project.self;
rec = new GlideRecord('x_snc_ippy_jira_projects');
rec.addQuery('url', url);
rec.deleteMultiple();
return;
},
/* JIRA Issue => x_snc_ippy_jira_issues *************************************************************
webhookEvent = ['issue_created', 'issue_updated', 'issue_deleted']
Notes:
***************************************************************************************************** */
update_issue: function() {
issue = this.data.issue;
key = issue.key;
id = issue.id;
url = issue.self;
host = this.get_uri_host(url);
type = issue.fields.issuetype.name;
desc = issue.fields.description;
summary = issue.fields.summary;
// Match JIRA users to SN Users (sys_user) by emailAddress
creatorEmail = issue.fields.creator.emailAddress;
reporterEmail = null;
assigneeEmail = null;
if (issue.fields.reporter != null) {
reporterEmail = issue.fields.reporter.emailAddress;
}
if (issue.fields.assignee != null) {
assigneeEmail = issue.fields.assignee.emailAddress;
}
creatorSysId = this.get_sn_user(creatorEmail);
reporterSysId = this.get_sn_user(reporterEmail);
assigneeSysId = this.get_sn_user(assigneeEmail);
// Project reference to x_snc_ippy_jira_projects
projectSysId = this.get_sn_project(issue.fields.project.self);
/* Sprint reference to x_snc_ippy_jira_sprints
Issues have a customfield reference to the sprint, which seems to be
a toString() invocation on a java object. Avoiding the assumption that
the same customfield_1001 is used consistently across versions and instances
of JIRA and assuming boardId + id is sufficiently unique across multiple JIRA
instances */
sprintSysId = '';
for (var property in issue.fields) {
if (property.match(/customfield_\d{1,}/i) &&
issue.fields[property] instanceof Array &&
issue.fields[property].length > 0) {
for (var i=0; i<issue.fields[property].length; i++) {
var value = issue.fields[property][i];
if (value instanceof String && value.match(/^com\.atlassian\.greenhopper\.service\.sprint\.Sprint/i)) {
// state
matches = value.match(/state=(.*?),/);
state = matches[1];
// sprintId
matches = value.match(/id=(\d{1,}),/);
sprintId = matches[1];
// BoardId (rapidViewId?)
matches = value.match(/rapidViewId=(\d{1,}),/);
boardId = matches[1];
if (state != "CLOSED") {
sprintSysId = this.get_sn_sprint(sprintId, boardId);
}
}
}
}
}
// Now that we know the relationship of a Sprint to Project, patch it. **hack**
if (!gs.nil(sprintSysId)) {
this.patch_project_sprint(projectSysId, sprintSysId);
}
// Priority
priority = issue.fields.priority.name;
status = issue.fields.status.name;
if (!creatorSysId) {
gs.error("Failed to locate creator with email address '" + creatorEmail +
"' for JIRA issue '" + key + "' (" + host + ")");
}
rec = new GlideRecord('x_snc_ippy_jira_issues');
rec.addQuery('url', url);
rec.query();
rec.next();
rec.creator = creatorSysId;
rec.reporter = reporterSysId;
rec.assignee = assigneeSysId;
rec.project = projectSysId;
rec.sprint = sprintSysId;
rec.id = id;
rec.url = url;
rec.key = key;
rec.host = host;
rec.type = type;
rec.summary = summary;
rec.priority = priority;
rec.status = status;
rec.description = desc;
rec.update();
return;
},
delete_issue: function() {
key = this.data.issue.key;
id = this.data.issue.id;
url = this.data.issue.self;
rec = new GlideRecord('x_snc_ippy_jira_issues');
rec.addQuery('url', url);
rec.deleteMultiple();
return;
},
/* JIRA Sprint => x_snc_ippy_jira_sprints ***********************************************************
webhookEvent = ['sprint_updated', 'sprint_created', 'sprint_closed', 'sprint_started']
Notes:
- Sprint hooks do not show relationship to a project; hack we'll wait for an issue hook to
link them
- Case where sprint records remain when they are created and a project is deleted before
an isuse is linked to the sprint
- No 'self' in sprint_deleted event, this could cause issues with multiple JIRA instances
***************************************************************************************************** */
update_sprint: function() {
id = this.data.sprint.id;
url = this.data.sprint.self;
state = this.data.sprint.state;
name = this.data.sprint.name;
boardId = this.data.sprint.originBoardId;
host = this.get_uri_host(url);
rec = new GlideRecord('x_snc_ippy_jira_sprints');
rec.addQuery('url', url);
rec.query();
rec.next();
rec.name = name;
rec.id = id;
rec.host = host;
rec.url = url;
rec.state = state;
rec.boardid = boardId;
rec.update();
return;
},
delete_sprint: function() {
// boardId + id is the 'best' option for unique match across multiple instances of JIRA
id = this.data.sprint.id;
boardId = this.data.sprint.originBoardId;
name = this.data.sprint.name;
rec = new GlideRecord('x_snc_ippy_jira_sprints');
rec.addQuery('boardid', boardId);
rec.addQuery('id', id);
rec.deleteMultiple();
return;
},
/* JIRA Worklog => x_snc_ippy_jira_worklogs *********************************************************
webhookEvent = ['jira:worklog_updated', 'worklog_deleted']
Notes:
- Using jira:worklog_updated event to relate worklog to issue
- 'worklog' data structure implies maxResult = 20, meaning likely only see recent 20 worklogs,
should be fine for POC logic
***************************************************************************************************** */
update_worklog: function() {
issueId = this.data.issue.id;
issueUrl = this.data.issue.self;
issueKey = this.data.issue.key;
worklog = this.data.issue.fields.worklog;
// Iterate worklogs
for (var i=0; i<worklog.worklogs.length; i++) {
thisLog = worklog.worklogs[i];
var url = thisLog.self;
var id = thisLog.id;
var comment = thisLog.comment;
var created = thisLog.created;
var updated = thisLog.updated;
var started = thisLog.started;
var timeSpent = thisLog.timeSpent;
var timeSpentSeconds = thisLog.timeSpentSeconds;
var authorSysId = this.get_sn_user(thisLog.author.emailAddress);
var issueSysId = this.get_sn_jira_record(issueUrl, 'x_snc_ippy_jira_issues');
var updateAuthorSysId = this.get_sn_user(thisLog.updateAuthor.emailAddress);
rec = new GlideRecord('x_snc_ippy_jira_worklogs');
rec.addQuery('url', url);
rec.query();
rec.next();
rec.url = url;
rec.id = id;
rec.comment = comment;
rec.created = this.ISODateTimeToGlideDateTime(created);
rec.updated = this.ISODateTimeToGlideDateTime(updated);
rec.started = this.ISODateTimeToGlideDateTime(started);
rec.issue = issueSysId;
rec.author = authorSysId;
rec.update_author = updateAuthorSysId;
rec.time_spent = timeSpent;
rec.time_spent_secs = timeSpentSeconds;
rec.update();
}
return;
},
delete_worklog: function() {
id = this.data.worklog.id;
url = this.data.worklog.self;
rec = new GlideRecord('x_snc_ippy_jira_worklogs');
rec.addQuery('url', url);
rec.deleteMultiple();
return;
},
get_sn_user: function(email) {
if (gs.nil(email)) {
return null;
}
user = new GlideRecord('sys_user');
user.addQuery('email', email);
user.query();
if (user.next()) {
return user.sys_id;
} else {
return null;
}
},
get_uri_host: function(uri) {
hostname = null;
if (!gs.nil(uri)) {
matches = uri.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
hostname = matches && matches[1];
}
return hostname;
},
get_sn_project: function(uri) {
rec = new GlideRecord('x_snc_ippy_jira_projects');
rec.addQuery('url', uri);
rec.query();
if (rec.next()) {
return rec.sys_id;
} else {
return null;
}
},
get_sn_jira_record: function(uri, table) {
rec = new GlideRecord(table);
rec.addQuery('url', uri);
rec.query();
if (rec.next()) {
return rec.sys_id;
} else {
return null;
}
},
get_sn_sprint: function(id, boardId) {
rec = new GlideRecord('x_snc_ippy_jira_sprints');
rec.addQuery('id', id);
rec.addQuery('boardid', boardId);
rec.query();
if (rec.next()) {
return rec.sys_id;
} else {
return null;
}
},
patch_project_sprint: function(projectSysId, sprintSysId) {
rec = new GlideRecord('x_snc_ippy_jira_sprints');
if (rec.get(sprintSysId)) {
rec.project = projectSysId;
rec.update();
} else {
gs.error("Unable to locate record '" + sprintSysId + "' in x_snc_ippy_jira_sprints");
}
return;
},
/* function : ISODateTimeToGlideDateTime()
Convert ISO8601 DateTime string to GlideDateTime object
*/
ISODateTimeToGlideDateTime: function(s) {
if ( (s == '') || (s == null) ) {
return null;
}
// GlideDateTime constructor expects date format: 'YY-MM-DD HH::MM::SS'
parts = s.split(/[-TZ:+]/g);
if (parts.length < 6) {
return null;
}
dts = '';
dts += parts[0] + "-" + parts[1] + "-" + parts[2];
dts += ' ';
// Handle sub seconds
sec = parts[5].split(/\./);
dts += parts[3] + ":" + parts[4] + ":" + sec[0];
// Calculate timezone offset in seconds
sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+';
if ( (parts[6] != null) && (parts[7] != null) ) {
offset = parseInt(parts[6] * 3600 + parseInt(parts[7] * 60));
offset = 0 + (sign == '-' ? -1 * offset : offset);
} else {
offset = 0;
}
// GlideDateTime object
gdt = new GlideDateTime(dts);
gdt.addSeconds(offset);
return gdt;
},
type: 'JiraWebhookProcessor'
});