Skip to content

Commit 029c5ce

Browse files
authored
Merge pull request #2 from insurify/add_target_project
Find correct project in asana-github integration action
2 parents e7eb105 + e93f34b commit 029c5ce

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ This action integrates asana with github.
2222

2323
**Optional** If any comment is provided, the action will add a comment to the specified asana task with the text & pull request link.
2424

25-
### `target-section`
25+
### `targets`
2626

27-
**Optional** Add/Move the task to the provided section i.e `merged`, `review`.
27+
**Optional** JSON array of objects having project and section where to move current task. Move task only if it exists in target project. e.g
28+
```yaml
29+
targets: '[{"project": "Backlog", "section": "Development Done"}, {"project": "Current Sprint", "section": "In Review"}]'
30+
```
31+
if you don't want to move task omit `targets`.
2832

2933

3034
## Example usage
3135

3236
```yaml
33-
uses: https://github.com/insurify/github-actions@master
37+
uses: https://github.com/insurify/github-actions@v2.0.0
3438
with:
3539
asana-pat: 'Your PAT'
36-
target-section: 'In Review'
3740
task-comment: 'View Pull Request Here: '
3841
trigger-phrase: 'Asana Task:'
42+
targets: '[{"project": "Backlog", "section": "Development Done"}, {"project": "Current Sprint", "section": "In Review"}]'
3943
```

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ inputs:
77
task-comment:
88
description: 'Provide text, which will add a comment with the pull request link to the asana task.'
99
required: false
10-
target-section:
11-
description: 'Provide the name of an Asana projects section or column where you want to move the task.'
10+
targets:
11+
description: 'JSON array of objects having project and section where to move current task. Move task only if it exists in target project.'
1212
required: false
1313
trigger-phrase:
1414
description: 'Prefix before the task i.e ASANA TASK: https://app.asana.com/1/2/3'

index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,34 @@ const asana = require('asana');
44

55
async function asanaOperations(
66
asanaPAT,
7-
projectId,
7+
targets,
88
taskId,
9-
sectionName,
109
taskComment
1110
) {
1211
try {
1312
const client = asana.Client.create({
1413
defaultHeaders: { 'asana-enable': 'new-sections,string_ids' },
1514
logAsanaChangeWarnings: false
1615
}).useAccessToken(asanaPAT);
17-
if (sectionName) {
18-
let project = await client.sections.findByProject(projectId);
19-
if (project) {
20-
let requiredSection = project.find(data => data.name === sectionName);
21-
if (requiredSection) {
22-
await client.sections.addTask(requiredSection.gid, { task: taskId });
23-
core.info('Moved to: ' + requiredSection.name);
16+
17+
const task = await client.tasks.findById(taskId);
18+
19+
targets.forEach(async target => {
20+
let targetProject = task.projects.find(project => project.name === target.project);
21+
if (targetProject) {
22+
let targetSection = await client.sections.findByProject(targetProject.gid)
23+
.then(sections => sections.find(section => section.name === target.section));
24+
if (targetSection) {
25+
await client.sections.addTask(targetSection.gid, { task: taskId });
26+
core.info(`Moved to: ${target.project}/${target.section}`);
2427
} else {
25-
core.error('Asana section ' + sectionName + ' not found.');
28+
core.error(`Asana section ${target.section} not found.`);
2629
}
2730
} else {
28-
core.error('Asana project with id ' + projectId + ' not found.');
31+
core.info(`This task does not exist in "${target.project}" project`);
2932
}
30-
}
33+
});
34+
3135
if (taskComment) {
3236
await client.tasks.addComment(taskId, {
3337
text: taskComment
@@ -41,7 +45,7 @@ async function asanaOperations(
4145

4246
try {
4347
const ASANA_PAT = core.getInput('asana-pat'),
44-
SECTION_NAME = core.getInput('target-section'),
48+
TARGETS = core.getInput('targets'),
4549
TRIGGER_PHRASE = core.getInput('trigger-phrase'),
4650
TASK_COMMENT = core.getInput('task-comment'),
4751
PULL_REQUEST = github.context.payload.pull_request,
@@ -50,21 +54,21 @@ try {
5054
'g'
5155
);
5256
let taskComment = null,
57+
targets = TARGETS? JSON.parse(TARGETS) : [],
5358
parseAsanaURL = null;
5459

5560
if (!ASANA_PAT){
56-
throw({message: "ASANA PAT Not Found!"});
61+
throw({message: 'ASANA PAT Not Found!'});
5762
}
5863
if (TASK_COMMENT) {
5964
taskComment = `${TASK_COMMENT} ${PULL_REQUEST.html_url}`;
6065
}
6166
while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) {
62-
let projectId = parseAsanaURL.groups.project,
63-
taskId = parseAsanaURL.groups.task;
64-
if (projectId && taskId) {
65-
asanaOperations(ASANA_PAT, projectId, taskId, SECTION_NAME, taskComment);
67+
let taskId = parseAsanaURL.groups.task;
68+
if (taskId) {
69+
asanaOperations(ASANA_PAT, targets, taskId, taskComment);
6670
} else {
67-
core.info('Invalid Asana task URL after the trigger phrase' + TRIGGER_PHRASE);
71+
core.info(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`);
6872
}
6973
}
7074
} catch (error) {

0 commit comments

Comments
 (0)