-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
106 lines (88 loc) · 2.93 KB
/
index.ts
File metadata and controls
106 lines (88 loc) · 2.93 KB
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
import { getInput, info, error } from '@actions/core';
import { context } from '@actions/github';
import { Client } from 'asana';
const ASANA_TASK_LINK_REGEX = /https:\/\/app\.asana\.com\/(?:\d+\/(?:home\/\d+\/|\d+\/)|\d+\/\d+\/project\/\d+\/task\/)(?<taskId>\d+)/ig;
async function addComment(client: Client, taskId: string, comment: string): Promise<void> {
await client.tasks.addComment(taskId, {text: comment});
info(`Added the GitHub link to the Asana task: ${taskId}`);
}
function getPreviousText(): {text?: string} {
if (context.payload.action === 'edited') {
const text = context.payload.changes?.body?.from;
if (text) {
return {text};
}
}
return {};
}
function getCurrentTextAndUrl(): {url: string, text: string} {
const pullRequest = context.payload.pull_request;
const issue = context.payload.issue
const comment = context.payload.comment;
if (pullRequest) {
info(`Extracting information from PR: ${pullRequest.html_url}`);
return {
url: pullRequest.html_url,
text: pullRequest.body,
}
} else if (issue && comment) {
info(`Extracting information from issue: ${issue.html_url}`);
return {
url: comment.html_url,
text: comment.body,
}
}
throw new Error('Must be used on pull_request and issue_comment events only');
}
function extractTasks(text: string): Set<string> {
ASANA_TASK_LINK_REGEX.lastIndex = 0;
const tasks = new Set<string>();
let rawParseUrl: RegExpExecArray | null;
while ((rawParseUrl = ASANA_TASK_LINK_REGEX.exec(text)) !== null) {
tasks.add(rawParseUrl.groups.taskId);
}
return tasks;
}
function difference(setA: Set<string>, setB: Set<string>): Set<string> {
let diff = new Set<string>(setA);
for (let elem of setB) {
diff.delete(elem);
}
return diff;
}
async function main() {
const personalAccessToken = getInput('asana-pat');
if (!personalAccessToken) {
throw new Error('Asana personal access token (asana-pat) not specified');
}
const { url, text } = getCurrentTextAndUrl();
const currentTasks = extractTasks(text);
const { text: previous } = getPreviousText();
const previousTasks = (previous && extractTasks(previous)) ?? new Set<string>();
const tasks = difference(currentTasks, previousTasks);
const deduped = currentTasks.size - tasks.size;
if (deduped > 0) {
info(`Deduplicated ${deduped} tasks`);
}
if (tasks.size === 0) {
info('No new Asana tasks referenced. Done.');
return;
}
const options = {
defaultHeaders: { 'asana-enable': 'string_ids' },
logAsanaChangeWarnings: false,
};
const client = Client.create(options).useAccessToken(personalAccessToken);
const commentPrefix = getInput('comment-prefix') || `${context.payload.sender.login} referenced in: `;
const comment = `${commentPrefix}${url}`;
for (const taskId of tasks) {
await addComment(client, taskId, comment);
}
}
(async () => {
try {
await main();
} catch (err) {
error(err);
}
})();