-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.js
76 lines (70 loc) · 2.52 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const Asana = require('asana');
async function asanaOperations(
asanaPAT,
targets,
taskId,
taskComment
) {
try {
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = asanaPAT;
let tasksApiInstance = new Asana.TasksApi();
let sectionsApiInstance = new Asana.SectionsApi();
let storiesApiInstance = new Asana.StoriesApi();
const task = await tasksApiInstance.getTask(taskId);
targets.forEach(async target => {
let targetProject = task.data.projects.find(project => project.name === target.project);
if (targetProject) {
let targetSection = await sectionsApiInstance.getSectionsForProject(targetProject.gid)
.then(sections => sections.data.find(section => section.name === target.section));
if (targetSection) {
await sectionsApiInstance.addTaskForSection(targetSection.gid, { body: { data: { task: taskId } } });
core.info(`Moved to: ${target.project}/${target.section}`);
} else {
core.error(`Asana section ${target.section} not found.`);
}
} else {
core.info(`This task does not exist in "${target.project}" project`);
}
});
if (taskComment) {
await storiesApiInstance.createStoryForTask({ data: { text: taskComment } }, taskId)
core.info('Added the pull request link to the Asana task.');
}
} catch (ex) {
console.error(ex.value);
}
}
try {
const ASANA_PAT = core.getInput('asana-pat'),
TARGETS = core.getInput('targets'),
TRIGGER_PHRASE = core.getInput('trigger-phrase'),
TASK_COMMENT = core.getInput('task-comment'),
PULL_REQUEST = github.context.payload.pull_request,
REGEX = new RegExp(
`${TRIGGER_PHRASE} *\\[(.*?)\\]\\(https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+).*?\\)`,
'g'
);
let taskComment = null,
targets = TARGETS? JSON.parse(TARGETS) : [],
parseAsanaURL = null;
if (!ASANA_PAT){
throw({message: 'ASANA PAT Not Found!'});
}
if (TASK_COMMENT) {
taskComment = `${TASK_COMMENT} ${PULL_REQUEST.html_url}`;
}
while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) {
let taskId = parseAsanaURL.groups.task;
if (taskId) {
asanaOperations(ASANA_PAT, targets, taskId, taskComment);
} else {
core.info(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`);
}
}
} catch (error) {
core.error(error.message);
}