forked from everphone-gmbh/github-asana-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
126 lines (115 loc) · 3.72 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
const core = require('@actions/core');
const github = require('@actions/github');
const Asana = require('asana');
async function findComment(client, taskId, commentId) {
let stories;
try {
stories = await client.stories.getStoriesForTask(taskId);
} catch (error) {
throw error;
}
return stories.data.find(story => story.text.indexOf(commentId) !== -1);
}
async function addComment(client, taskId, commentId, text, isPinned) {
if (commentId) {
text += '\n' + commentId;
}
try {
console.log('adding comment to task id ' + taskId, JSON.stringify({
data: {
html_text: text,
is_pinned: isPinned,
}
}, null, 2));
const story = await client.stories.createStoryForTask({
data: {
html_text: `<body>${text}</body>`,
is_pinned: isPinned,
}
}, taskId);
console.log('added comment', JSON.stringify(story, null, 2));
return story.data;
} catch (error) {
console.error('rejecting promise', error);
}
}
async function buildClient(asanaPAT) {
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = asanaPAT;
return {
tasks: new Asana.TasksApi(),
stories: new Asana.StoriesApi(),
};
}
async function action() {
const
ASANA_PAT = core.getInput('asana-pat', { required: true }),
ACTION = core.getInput('action', { required: true }),
TRIGGER_PHRASE = core.getInput('trigger-phrase') || '',
PULL_REQUEST = github.context.payload.pull_request,
REGEX_STRING = `${TRIGGER_PHRASE}(?:\s*)https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+)`,
REGEX = new RegExp(REGEX_STRING, 'g')
;
console.log('pull_request', PULL_REQUEST);
const client = await buildClient(ASANA_PAT);
if (client === null) {
throw new Error('client authorization failed');
}
console.info('looking in body', PULL_REQUEST.body, 'regex', REGEX_STRING);
let foundAsanaTasks = [];
while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) {
const taskId = parseAsanaURL.groups.task;
if (!taskId) {
core.error(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`);
continue;
}
foundAsanaTasks.push(taskId);
}
console.info(`found ${foundAsanaTasks.length} taskIds:`, foundAsanaTasks.join(','));
console.info('calling', ACTION);
switch (ACTION) {
case 'add-comment': {
const commentId = core.getInput('comment-id'),
htmlText = core.getInput('text', { required: true }),
isPinned = core.getInput('is-pinned') === 'true';
const comments = [];
for (const taskId of foundAsanaTasks) {
if (commentId) {
const comment = await findComment(client, taskId, commentId);
if (comment) {
console.info('found existing comment', comment.gid);
continue;
}
}
const comment = await addComment(client, taskId, commentId, htmlText, isPinned);
comments.push(comment);
}
return comments;
}
case 'remove-comment': {
const commentId = core.getInput('comment-id', { required: true });
const removedCommentIds = [];
for (const taskId of foundAsanaTasks) {
const comment = await findComment(client, taskId, commentId);
if (comment) {
console.info('removing comment', comment.gid);
try {
await client.stories.deleteStory(comment.gid);
} catch (error) {
console.error('rejecting promise', error);
}
removedCommentIds.push(comment.gid);
}
}
return removedCommentIds;
}
default:
core.setFailed('unexpected action ${ACTION}');
}
}
module.exports = {
action,
default: action,
buildClient: buildClient
};