-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration.js
372 lines (343 loc) · 12.2 KB
/
integration.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
'use strict';
const _ = require('lodash');
const { get, filter, flow, split, map, trim, compact, uniq, size, some } = require('lodash/fp');
const async = require('async');
const crypto = require('crypto');
const { getStatusesForProject } = require('./src/get-statuses-for-project');
const { getIssueById } = require('./src/get-issue-by-id');
const { searchIssues } = require('./src/search-issues');
const { createResultObject } = require('./src/create-result-object');
const { getCommentsByIssueId } = require('./src/get-comments-by-issue-id');
const { downloadIconByUrl } = require('./src/download-icon-by-url');
const { updateIssueTransition } = require('./src/update-issue-transition');
const { getBulkPermissions } = require('./src/get-bulk-permissions');
const { addCommentToIssue } = require('./src/add-comment-to-issue');
const { getProjects } = require('./src/get-projects.js');
const { getIssueTypesByProject } = require('./src/get-issue-types-by-project.js');
const { addIntegrationDataToIssue } = require('./src/add-integration-data-to-issue');
const { addIssue } = require('./src/add-issue.js');
const { setLogger } = require('./src/logger');
const { getIssueFields } = require('./src/get-issue-fields');
let log = null;
// Project and Task icons require authentication to display so we fetch them and store them here
// This way we only fetch the icon the first time we see it. The cache is keyed on the URL of the
// icon and the value is the icon data base64 encoded.
const iconCache = {};
function startup(logger) {
log = logger;
setLogger(logger);
}
const splitCommaSeparatedUserOption = (key, options) => flow(get(key), split(','), map(trim), compact, uniq)(options);
async function doLookup(entities, options, cb) {
let lookupResults = [];
const ignoreEntities = splitCommaSeparatedUserOption('ignoreList', options);
const ignoreEntitiesRegex = splitCommaSeparatedUserOption('ignoreListRegex', options);
const entitiesWithoutIgnored =
size(ignoreEntities) || size(ignoreEntitiesRegex)
? filter((entity) => {
const entityNotCaughtByRegex = !some((regexStr) => {
try {
return entity.value.match(new RegExp(regexStr, 'i'));
} catch (_) {
return false;
}
}, ignoreEntitiesRegex);
return !ignoreEntities.includes(entity.value) && entityNotCaughtByRegex;
}, entities)
: entities;
log.trace({ entities, entitiesWithoutIgnored }, 'doLookup');
try {
await async.each(entitiesWithoutIgnored, async (entityObj, next) => {
let apiResponse;
if (entityObj.type === 'custom' && entityObj.types.indexOf('custom.jira') >= 0) {
apiResponse = await getIssueById(entityObj.value.trim(), options);
} else {
apiResponse = await searchIssues(entityObj, options);
}
const resultObject = createResultObject(entityObj, apiResponse, options);
if (resultObject.data === null && options.enableCreatingIssues.value === 'enabledAlways') {
// no result
resultObject.requireOtherData = true;
resultObject.data = {
summary: ['Create Issue'],
details: {
noResults: true
}
};
}
lookupResults.push(resultObject);
});
} catch (error) {
return cb(error);
}
log.trace({ lookupResults }, 'doLookup results');
cb(null, lookupResults);
}
function computeMd5(data) {
return crypto.createHash('md5').update(data).digest('hex');
}
/**
* Issues have icons for their project icon and issue type icon. These icons are not publicly accessible so if
* we want to display them we have to retrieve them using an authenticated GET request. In order to prevent us from
* fetching the same icon over and over (since they rarely change), we maintain a local cache of the icons keyed on
* the MD5 of the icon URL. The reason we use a MD5 is so that we can use the `get` helper in the template to fetch
* the icon data. The `get` helper does not work with `urls` as keys due to special characters in the url.
*
* Since the MD5 is something we generate, we need to add the MD5 value to the `issuetype` and `project` objects.
* We call this the `__iconMd5` property.
*
* This method does mutate the passed in issues to add this `__iconMd5` property.
*
* Note that this method will only fetch icons that we haven't already fetched in previous lookups. Icons are
* stored in the global `iconCache` variable.
*
* Icons are fetched and converted into base64 strings that we can pass to the template.
*
* @param issues
* @param options
* @returns {Promise<{}>}
*/
async function getRequiredIcons(issues, options) {
// enrich with icon data as needed
// Project Icon Path: issue.fields.project.avatarUrls.24x24
// Issue Type Icon: issue.fields.issuetype.iconUrl
const iconUrlMap = new Map();
issues.forEach((issue) => {
const projectIconUrl = _.get(issue, 'fields.project.avatarUrls.24x24', null);
const issueIconUrl = _.get(issue, 'fields.issuetype.iconUrl', null);
if (projectIconUrl !== null) {
const projectIconMd5 = computeMd5(projectIconUrl);
issue.fields.project.__iconMd5 = projectIconMd5;
iconUrlMap.set(projectIconMd5, projectIconUrl);
}
if (issueIconUrl !== null) {
const issueIconMd5 = computeMd5(issueIconUrl);
issue.fields.issuetype.__iconMd5 = issueIconMd5;
iconUrlMap.set(issueIconMd5, issueIconUrl);
}
});
// For icons that are yet cached, fetch the icon, then add it to the list of requireIcons
// that will be sent to the template for rendering.
const requiredIcons = {};
for await (let [md5, url] of iconUrlMap) {
if (!iconCache[url]) {
iconCache[url] = await downloadIconByUrl(url, options);
}
requiredIcons[md5] = iconCache[url];
}
return requiredIcons;
}
async function onDetails(resultObject, options, cb) {
if (resultObject.data.details.noResults) {
return cb(null, resultObject.data);
}
try {
resultObject.data.details.issues = await getCommentsAndAddToIssues(resultObject.data.details.issues, options);
resultObject.data.details.icons = await getRequiredIcons(resultObject.data.details.issues, options);
resultObject.data.details.permissions = await getBulkPermissions(
resultObject.data.details.issues.map((issue) => issue.id),
options
);
log.trace({ resultObject }, 'onDetails result');
cb(null, resultObject.data);
} catch (e) {
log.error(e, 'onDetails Error');
cb(errorToPojo(e, 'Error fetching comments'));
}
}
function errorToPojo(err, detail) {
return err instanceof Error
? {
...err,
name: err.name,
message: err.message,
stack: err.stack,
detail: err.message ? err.message : err.detail ? err.detail : detail
}
: err;
}
async function getCommentsAndAddToIssues(issues, options) {
await async.eachLimit(issues, 10, async (issue) => {
const body = await getCommentsByIssueId(issue.id, options);
issue.comments = body.comments;
});
log.trace({ issues }, 'Issues with comments');
return issues;
}
async function onMessage(payload, options, cb) {
log.trace({ payload }, 'onMessage');
switch (payload.action) {
case 'UPDATE_TRANSITION':
try {
if (!options.enableUpdatingStatus) {
return cb({
error: 'Updating Issue status is disabled for this integration'
});
}
await updateIssueTransition(payload.issueId, payload.transitionId, options);
const body = await getIssueById(payload.issueId, options);
const status = body.issues[0].fields.status;
cb(null, {
status
});
} catch (e) {
log.error(e, 'Error updating transitions');
cb(errorToPojo(e, 'Error updating transitions'));
}
break;
case 'ADD_COMMENT':
try {
if (!options.enableAddingComments) {
return cb({
error: 'Adding comments is disabled for this integration'
});
}
let newComments = [];
if (payload.includeIntegrationData) {
newComments = await addIntegrationDataToIssue(
payload.issueId,
payload.comment,
payload.entity,
payload.username,
payload.email,
payload.integrationData,
payload.annotations,
options
);
} else {
let newComment = await addCommentToIssue(payload.issueId, payload.comment, options);
newComments.push(newComment);
}
cb(null, {
comments: newComments
});
} catch (e) {
log.error(e, 'Error adding comment');
cb(errorToPojo(e, 'Error adding comment'));
}
break;
case 'GET_PROJECTS':
try {
if (options.enableCreatingIssues.value === 'disabled') {
return cb({
error: 'Adding issues is disabled for this integration'
});
}
const projects = await getProjects(options);
cb(null, {
projects
});
} catch (e) {
log.error(e, 'Error getting Projects');
cb(errorToPojo(e, 'Error getting Projects'));
}
break;
case 'GET_ISSUE_TYPES':
try {
if (options.enableCreatingIssues.value === 'disabled') {
return cb({
error: 'Adding issues is disabled for this integration'
});
}
const issueTypes = await getIssueTypesByProject(payload.projectId, options);
cb(null, {
issueTypes
});
} catch (e) {
log.error(e, 'Error getting issue types');
cb(errorToPojo(e, 'Error getting issue types'));
}
break;
case 'ADD_ISSUE':
try {
if (options.enableCreatingIssues.value === 'disabled') {
return cb({
error: 'Adding issues is disabled for this integration'
});
}
const issue = await addIssue(payload.projectId, payload.issueType, payload.fields, options);
if (payload.includeIntegrationData) {
// No comment content is provided in the payload when adding data to a new issue
const comment = '';
await addIntegrationDataToIssue(
issue.id,
comment,
payload.entity,
payload.username,
payload.email,
payload.integrationData,
payload.annotations,
options
);
}
cb(null, {
issue
});
} catch (e) {
log.error(e, 'Error creating issue');
cb(errorToPojo(e, 'Error creating issue'));
}
break;
case 'GET_ISSUE_FIELDS':
try {
if (options.enableCreatingIssues.value === 'disabled') {
return cb({
error: 'Adding issues is disabled for this integration'
});
}
const issueFields = await getIssueFields(payload.projectId, payload.issueType, options);
cb(null, {
issueFields
});
} catch (e) {
log.error(e, 'Error getting issue field meta');
cb(errorToPojo(e, 'Error getting issue field meta'));
}
break;
default:
cb({
error: 'Invalid Action Provided'
});
}
}
function validateOptions(userOptions, cb) {
let errors = [];
if (
typeof userOptions.apiKey.value !== 'string' ||
(typeof userOptions.apiKey.value === 'string' && userOptions.apiKey.value.length === 0)
) {
errors.push({
key: 'apiKey',
message: 'You must provide your Jira API key or Password'
});
}
if (
typeof userOptions.baseUrl.value !== 'string' ||
(typeof userOptions.baseUrl.value === 'string' && userOptions.baseUrl.value.length === 0)
) {
errors.push({
key: 'baseUrl',
message: 'You must provide a Jira API URL'
});
}
if (typeof userOptions.baseUrl.value === 'string' && userOptions.baseUrl.value.endsWith('/')) {
errors.push({
key: 'baseUrl',
message: 'Jira API URL cannot end with a trailing slash ("/")'
});
}
if (typeof userOptions.baseAppUrl.value === 'string' && userOptions.baseAppUrl.value.endsWith('/')) {
errors.push({
key: 'baseAppUrl',
message: 'Jira Application URL cannot end with a trailing slash ("/")'
});
}
cb(null, errors);
}
module.exports = {
doLookup,
startup,
validateOptions,
onDetails,
onMessage
};