-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.js
515 lines (450 loc) · 16.5 KB
/
action.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
const core = require('@actions/core');
const github = require('@actions/github');
const octokit = require('@octokit/core');
const asana = require('asana');
const yaml = require('js-yaml');
const { Client4 } = require('@mattermost/client');
function buildAsanaClient() {
const ASANA_PAT = core.getInput('asana-pat');
return asana.Client.create({
defaultHeaders: { 'asana-enable': 'new-sections,string_ids' },
logAsanaChangeWarnings: false
}).useAccessToken(ASANA_PAT).authorize();
}
function buildGithubClient(githubPAT){
return new octokit.Octokit({
auth: githubPAT
})
}
function buildMattermostClient(){
const MATTERMOST_TOKEN = core.getInput('mattermost-token');
const MATTERMOST_URL = 'https://chat.duckduckgo.com';
const client = new Client4();
client.setUrl(MATTERMOST_URL);
client.setToken(MATTERMOST_TOKEN);
return client
}
function getArrayFromInput(input) {
return input ? input.split(',') : [];
}
function findAsanaTasks(){
const
TRIGGER_PHRASE = core.getInput('trigger-phrase'),
PULL_REQUEST = github.context.payload.pull_request,
REGEX_STRING = `${TRIGGER_PHRASE} https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+).*?`,
REGEX = new RegExp(REGEX_STRING, 'g');
console.info('looking for asana task link in body', PULL_REQUEST.body, 'regex', REGEX_STRING);
let foundTasks = [];
while((parseAsanaUrl = REGEX.exec(PULL_REQUEST.body)) !== null) {
const taskId = parseAsanaUrl.groups.task;
if (!taskId) {
core.error(`Invalid Asana task URL after trigger-phrase ${TRIGGER_PHRASE}`);
continue;
}
foundTasks.push(taskId);
}
console.info(`found ${foundTasks.length} tasksIds:`, foundTasks.join(','));
return foundTasks
}
async function createStory(client, taskId, text, isPinned) {
try {
return await client.stories.createStoryForTask(taskId, {
text: text,
is_pinned: isPinned,
});
} catch (error) {
console.error('rejecting promise', error);
}
}
async function createTaskWithComment(client, name, description, comment, projectId) {
try {
client.tasks.createTask({name: name,
notes: description,
projects: [projectId],
pretty: true})
.then((result) => {
console.log('task created', result.gid);
return createStory(client, result.gid, comment, true)
})
} catch (error) {
console.error('rejecting promise', error);
}
}
async function createIssueTask(){
const client = await buildAsanaClient();
const ISSUE = github.context.payload.issue;
const ASANA_PROJECT_ID = core.getInput('asana-project', {required: true});
console.info('creating asana task from issue', ISSUE.title);
const TASK_DESCRIPTION = `Description: ${ISSUE.body}`;
const TASK_NAME = `Github Issue: ${ISSUE.title}`;
const TASK_COMMENT = `Link to Issue: ${ISSUE.html_url}`;
return createTaskWithComment(client, TASK_NAME, TASK_DESCRIPTION, TASK_COMMENT, ASANA_PROJECT_ID)
}
async function notifyPRApproved(){
const client = await buildAsanaClient();
const
PULL_REQUEST = github.context.payload.pull_request,
TASK_COMMENT = `PR: ${PULL_REQUEST.html_url} has been approved`;
const foundTasks = findAsanaTasks()
const comments = [];
for (const taskId of foundTasks) {
const comment = createStory(client, taskId, TASK_COMMENT, false)
comments.push(comment)
}
return comments;
}
async function addTaskToAsanaProject(){
const client = await buildAsanaClient();
const projectId = core.getInput('asana-project', {required: true});
const sectionId = core.getInput('asana-section');
const taskId = core.getInput('asana-task-id', {required: true});
addTaskToProject(client, taskId, projectId, sectionId)
}
async function addTaskToProject(client, taskId, projectId, sectionId){
if (!sectionId){
console.info('adding asana task to project', projectId);
try {
return await client.tasks.addProjectForTask(taskId, {
project: projectId,
insert_after: null
});
} catch (error) {
console.error('rejecting promise', error);
}
} else {
console.info(`adding asana task to top of section ${sectionId} in project ${projectId}`);
try {
return await client.tasks.addProjectForTask(taskId, {
project: projectId
})
.then((result) => {
client.sections.addTaskForSection(sectionId, {task: taskId})
.then((result) => {
console.log(result);
});
});
} catch (error) {
console.error('rejecting promise', error);
}
}
}
async function addCommentToPRTask(){
const
PULL_REQUEST = github.context.payload.pull_request,
TASK_COMMENT = `PR: ${PULL_REQUEST.html_url}`,
isPinned = core.getInput('is-pinned') === 'true';
const client = await buildAsanaClient();
const foundTasks = findAsanaTasks()
const comments = [];
for (const taskId of foundTasks) {
const comment = createStory(client, taskId, TASK_COMMENT, isPinned)
comments.push(comment)
}
return comments;
}
async function createPullRequestTask(){
const client = await buildAsanaClient();
const PULL_REQUEST = github.context.payload.pull_request;
const ASANA_PROJECT_ID = core.getInput('asana-project', {required: true});
console.info('creating asana task from pull request', PULL_REQUEST.title);
const TASK_DESCRIPTION = `Description: ${PULL_REQUEST.body}`;
const TASK_NAME = `Community Pull Request: ${PULL_REQUEST.title}`;
const TASK_COMMENT = `Link to Pull Request: ${PULL_REQUEST.html_url}`;
return createTaskWithComment(client, TASK_NAME, TASK_DESCRIPTION, TASK_COMMENT, ASANA_PROJECT_ID)
}
async function completePRTask(){
const client = await buildAsanaClient();
const isComplete = core.getInput('is-complete') === 'true';
const foundTasks = findAsanaTasks()
const taskIds = [];
for(const taskId of foundTasks) {
console.info("marking task", taskId, isComplete ? 'complete' : 'incomplete');
try {
await client.tasks.update(taskId, {
completed: isComplete
});
} catch (error) {
console.error('rejecting promise', error);
}
taskIds.push(taskId);
}
return taskIds;
}
async function checkPRMembership(){
const
PULL_REQUEST = github.context.payload.pull_request,
ORG = PULL_REQUEST.base.repo.owner.login,
USER = PULL_REQUEST.user.login;
HEAD = PULL_REQUEST.head.user.login
console.info(`PR opened/reopened by ${USER}, checking membership in ${ORG}`);
if (HEAD === ORG){
console.log(author, `belongs to duckduckgo}`)
core.setOutput('external', false)
} else {
console.log(author, `does not belong to duckduckgo}`)
core.setOutput('external', true)
}
}
async function getLatestRepositoryRelease(){
const
GITHUB_PAT = core.getInput('github-pat', {required: true}),
githubClient = buildGithubClient(GITHUB_PAT),
ORG = core.getInput('github-org', {required: true}),
REPO = core.getInput('github-repository', {required: true});
try {
await githubClient.request('GET /repos/{owner}/{repo}/releases/latest', {
owner: ORG,
repo: REPO,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
}).then((response) => {
const version = response.data.tag_name
console.log(REPO, `latest version is ${version}`)
core.setOutput('version', version)
});
} catch (error) {
console.log(REPO, `can't find latest version ${error}`)
core.setFailed(`can't find latest version for ${REPO}`);
}
}
async function findTaskInSection(client, sectionId, name) {
let existingTaskId = "0"
try {
console.log('searching tasks in section', sectionId);
await client.tasks.getTasksForSection(sectionId).then((result) => {
const task = result.data.find(task => task.name === name);
if (!task){
console.log("task not found")
existingTaskId = "0"
} else {
console.info('task found', task.gid);
existingTaskId = task.gid
}
});
} catch (error) {
console.error('rejecting promise', error);
}
return existingTaskId
}
async function createTask(client, name, description, projectId, sectionId = '', tags = [], collaborators = []) {
const taskOpts = {
name: name,
notes: description,
projects: [projectId],
tags,
followers: collaborators,
pretty: true
};
if (sectionId != '') {
console.log('checking for duplicate task before creating a new one', name);
let existingTaskId = await findTaskInSection(client, sectionId, name)
if (existingTaskId != "0") {
console.log("task already exists, skipping")
core.setOutput('taskId', existingTaskId)
core.setOutput('duplicate', true)
return existingTaskId;
}
taskOpts.memberships = [{project: projectId, section: sectionId}];
}
console.log(`creating new task with options:='${JSON.stringify(taskOpts)}'`);
let createdTaskId = "0"
try {
await client.tasks.createTask(taskOpts)
.then((result) => {
createdTaskId = result.gid
console.log('task created', createdTaskId);
core.setOutput('taskId', createdTaskId);
core.setOutput('duplicate', false);
})
} catch (error) {
console.error('rejecting promise', error);
}
return createdTaskId
}
async function createAsanaTask(){
const client = await buildAsanaClient();
const
projectId = core.getInput('asana-project', {required: true}),
sectionId = core.getInput('asana-section'),
taskName = core.getInput('asana-task-name', {required: true}),
taskDescription = core.getInput('asana-task-description', {required: true}),
tags = getArrayFromInput(core.getInput('asana-tags')),
collaborators = getArrayFromInput(core.getInput('asana-collaborators'));
return createTask(client, taskName, taskDescription, projectId, sectionId, tags, collaborators);
}
async function addTaskPRDescription(){
const
GITHUB_PAT = core.getInput('github-pat'),
githubClient = buildGithubClient(GITHUB_PAT),
ORG = core.getInput('github-org', {required: true}),
REPO = core.getInput('github-repository', {required: true}),
PR = core.getInput('github-pr', {required: true}),
projectId = core.getInput('asana-project', {required: true}),
taskId = core.getInput('asana-task-id', {required: true});
githubClient.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner: ORG,
repo: REPO,
pull_number: PR,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
}).then((response) => {
console.log(response.data.body);
const body = response.data.body;
const asanaTaskMessage = `Task/Issue URL: https://app.asana.com/0/${projectId}/${taskId}/f`;
const updatedBody = `${asanaTaskMessage} \n\n ----- \n${body}`;
githubClient.request('PATCH /repos/{owner}/{repo}/pulls/{pull_number}', {
owner: ORG,
repo: REPO,
pull_number: PR,
body: updatedBody,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
.catch((error) => core.error(error));
});
}
async function getAsanaUserID() {
const
ghUsername = core.getInput('github-username') || github.context.payload.pull_request.user.login,
GITHUB_PAT = core.getInput('github-pat', {required: true}),
githubClient = buildGithubClient(GITHUB_PAT),
ORG = 'duckduckgo',
REPO = 'internal-github-asana-utils';
console.log(`Looking up Asana user ID for ${ghUsername}`);
try {
await githubClient.request('GET /repos/{owner}/{repo}/contents/user_map.yml', {
owner: ORG,
repo: REPO,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github.raw+json'
}
}).then((response) => {
const userMap = yaml.load(response.data);
if (ghUsername in userMap) {
core.setOutput('asanaUserId', userMap[ghUsername]);
} else {
core.setFailed(`User ${ghUsername} not found in user map`);
}
});
} catch (error) {
core.setFailed(error);
}
}
async function findAsanaTaskId(){
const foundTasks = findAsanaTasks()
if (foundTasks.length > 0) {
core.setOutput('asanaTaskId', foundTasks[0]);
} else {
core.setFailed(`Can't find an Asana task with the expected prefix`);
}
}
async function postCommentAsanaTask(){
const client = await buildAsanaClient();
const
TASK_ID = core.getInput('asana-task-id'),
TASK_COMMENT = core.getInput('asana-task-comment')
IS_PINNED = core.getInput('asana-task-comment-pinned')
const comment = createStory(client, TASK_ID, TASK_COMMENT, IS_PINNED)
if (comment != null){
console.info('Comment added to Asana task');
} else {
core.setFailed(`Can't post comment in Asana task`);
}
}
async function sendMessage(client, channelId, message) {
try {
const response = await client.createPost({
channel_id: channelId,
message: message,
});
console.log('Message sent:', response);
} catch (error) {
core.setFailed(`Error sending message`);
}
}
async function sendMattermostMessage(){
const
CHANNEL_NAME = core.getInput('mattermost-channel-name'),
MESSAGE = core.getInput('mattermost-message')
TEAM_ID = core.getInput('mattermost-team-id')
const client = buildMattermostClient()
const channel = await client.getChannelByName(TEAM_ID, CHANNEL_NAME);
if (channel) {
console.log(`Channel "${channel.id}" found.`);
await sendMessage(client, channel.id, MESSAGE);
} else {
core.setFailed(`Channel "${CHANNEL_NAME}" not found.`);
}
}
async function action() {
const ACTION = core.getInput('action', {required: true});
console.info('calling', ACTION);
switch (ACTION) {
case 'create-asana-issue-task': {
createIssueTask();
break;
}
case 'notify-pr-approved': {
notifyPRApproved();
break;
}
case 'notify-pr-merged': {
completePRTask()
break;
}
case 'check-pr-membership': {
checkPRMembership();
break;
}
case 'add-asana-comment': {
addCommentToPRTask();
break;
}
case 'add-task-asana-project': {
addTaskToAsanaProject();
break;
}
case 'create-asana-pr-task': {
createPullRequestTask();
break;
}
case 'get-latest-repo-release': {
getLatestRepositoryRelease();
break;
}
case 'create-asana-task': {
createAsanaTask();
break;
}
case 'add-task-pr-description': {
addTaskPRDescription();
break;
}
case 'get-asana-user-id': {
getAsanaUserID();
break;
}
case 'find-asana-task-id': {
findAsanaTaskId();
break;
}
case 'post-comment-asana-task': {
postCommentAsanaTask();
break;
}
case 'send-mattermost-message': {
sendMattermostMessage();
break;
}
default:
core.setFailed(`unexpected action ${ACTION}`);
}
}
module.exports = {
action,
default: action,
};