Skip to content

Commit 4bcea86

Browse files
committed
improve regex, add link-required
1 parent 570885a commit 4bcea86

File tree

3 files changed

+108
-38
lines changed

3 files changed

+108
-38
lines changed

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ This action integrates asana with github.
1212

1313
### `asana-pat`
1414

15-
**Required** Your public access token of asana, you can find it in [asana docs](https://developers.asana.com/docs/#authentication-basics).
15+
**Required** Your public access token of asana, you can generate one [here](https://app.asana.com/0/developer-console).
16+
17+
### `github-token`
18+
19+
**Required** A github auth token (used to set statuses)
1620

1721
### `trigger-phrase`
1822

19-
**Required** Prefix before the task i.e ASANA TASK: https://app.asana.com/1/2/3/.
23+
**Optional** Prefix before the task i.e ASANA TASK: https://app.asana.com/1/2/3/.
2024

2125
### `task-comment`
2226

@@ -30,14 +34,49 @@ targets: '[{"project": "Backlog", "section": "Development Done"}, {"project": "C
3034
```
3135
if you don't want to move task omit `targets`.
3236

37+
### `link-required`
38+
39+
**Optional** When set to true will fail pull requests without an asana link
3340

3441
## Example usage
3542

3643
```yaml
37-
uses: https://github.com/insurify/[email protected]
38-
with:
39-
asana-pat: 'Your PAT'
40-
task-comment: 'View Pull Request Here: '
41-
trigger-phrase: 'Asana Task:'
42-
targets: '[{"project": "Backlog", "section": "Development Done"}, {"project": "Current Sprint", "section": "In Review"}]'
44+
name: Mark asana task as done
45+
46+
on:
47+
pull_request:
48+
types: [closed]
49+
50+
jobs:
51+
sync:
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: everphone-gmbh/[email protected]
55+
if: github.event.pull_request.merged
56+
with:
57+
asana-pat: ${{ secrets.ASANA_PAT }}
58+
targets: '[{"project": "Engineering scrum", "section": "Done"}]'
59+
link-required: false
60+
github-token: ${{ secrets.GITHUB_TOKEN }}
61+
```
62+
63+
```yaml
64+
name: Add asana link
65+
66+
on:
67+
pull_request:
68+
types: [opened, edited, labeled, unlabeled]
69+
70+
jobs:
71+
sync:
72+
runs-on: ubuntu-latest
73+
steps:
74+
- uses: everphone-gmbh/[email protected]
75+
with:
76+
asana-pat: ${{ secrets.ASANA_PAT }}
77+
task-comment: 'View Pull Request Here: '
78+
# if the branch is labeled or named a hotfix, skip this check
79+
link-required: ${{ !contains(github.event.pull_request.labels.*.name, 'hotfix') && !startsWith(github.event.pull_request.title,'hotfix/') }}
80+
github-token: ${{ secrets.GITHUB_TOKEN }}
81+
4382
```

action.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'Asana Git'
1+
name: 'Asana Git with enforcement'
22
description: 'Asana Github Actions'
33
inputs:
44
asana-pat:
@@ -12,6 +12,13 @@ inputs:
1212
required: false
1313
trigger-phrase:
1414
description: 'Prefix before the task i.e ASANA TASK: https://app.asana.com/1/2/3'
15+
required: false
16+
link-required:
17+
description: 'When set to true requires an asana link in the body'
18+
required: false
19+
default: false
20+
github-token:
21+
description: 'your github auth token'
1522
required: true
1623
branding:
1724
icon: 'chevron-right'

index.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,63 @@ async function asanaOperations(
3838
});
3939
core.info('Added the pull request link to the Asana task.');
4040
}
41-
} catch (ex) {
42-
console.error(ex.value);
41+
} catch (error) {
42+
core.error(error.value);
4343
}
4444
}
4545

46-
try {
47-
const ASANA_PAT = core.getInput('asana-pat'),
48-
TARGETS = core.getInput('targets'),
49-
TRIGGER_PHRASE = core.getInput('trigger-phrase'),
50-
TASK_COMMENT = core.getInput('task-comment'),
51-
PULL_REQUEST = github.context.payload.pull_request,
52-
REGEX_STRING = `\\*\\*${TRIGGER_PHRASE}\\*\\* \\[(.*?)\\]\\(https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+).*?\\)`,
53-
REGEX = new RegExp(REGEX_STRING,'g');
54-
core.info(`Regex: ${REGEX_STRING}`);
55-
core.info(`pr body: ${PULL_REQUEST.body}`);
56-
let taskComment = null,
57-
targets = TARGETS? JSON.parse(TARGETS) : [],
58-
parseAsanaURL = null;
46+
async function run() {
47+
try {
48+
const GITHUB_TOKEN = core.getInput('github-token', {required: true});
49+
ASANA_PAT = core.getInput('asana-pat', {required: true}),
50+
TARGETS = core.getInput('targets'),
51+
TRIGGER_PHRASE = core.getInput('trigger-phrase'),
52+
TASK_COMMENT = core.getInput('task-comment'),
53+
PULL_REQUEST = github.context.payload.pull_request,
54+
REGEX_STRING = `${TRIGGER_PHRASE}(?:\s*)https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+)`,
55+
REGEX = new RegExp(REGEX_STRING,'g'),
56+
// all inputs are strings
57+
LINK_REQUIRED = core.getInput('link-required') === 'true'
58+
;
5959

60-
if (!ASANA_PAT){
61-
throw({message: 'ASANA PAT Not Found!'});
62-
}
63-
if (TASK_COMMENT) {
64-
taskComment = `${TASK_COMMENT} ${PULL_REQUEST.html_url}`;
65-
}
66-
while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) {
67-
let taskId = parseAsanaURL.groups.task;
68-
if (taskId) {
69-
asanaOperations(ASANA_PAT, targets, taskId, taskComment);
70-
} else {
71-
core.info(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`);
60+
const octokit = new github.GitHub(GITHUB_TOKEN);
61+
62+
core.debug(`Regex: ${REGEX_STRING}`);
63+
// contains the markdown version of the PR
64+
core.debug(`pr body: ${PULL_REQUEST.body}`);
65+
66+
let taskComment = null,
67+
targets = TARGETS ? JSON.parse(TARGETS) : [],
68+
parseAsanaURL = null;
69+
70+
if (TASK_COMMENT) {
71+
taskComment = `${TASK_COMMENT} ${PULL_REQUEST.html_url}`;
7272
}
73+
74+
let foundAsanaTasks = [];
75+
while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) {
76+
let taskId = parseAsanaURL.groups.task;
77+
if (taskId) {
78+
foundAsanaLink = true;
79+
asanaOperations(ASANA_PAT, targets, taskId, taskComment);
80+
foundAsanaTasks.push(taskId);
81+
} else {
82+
core.error(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`);
83+
}
84+
}
85+
86+
const statusState = (!LINK_REQUIRED || foundAsanaTasks.length > 0) ? 'success' : 'error';
87+
core.info(`setting ${statusState} for ${github.context.payload.pull_request.head.sha}`);
88+
octokit.repos.createStatus({
89+
...github.context.repo,
90+
'context': 'asana-link-presence',
91+
'state': statusState,
92+
'description': 'asana link not found',
93+
'sha': github.context.payload.pull_request.head.sha,
94+
});
95+
} catch (error) {
96+
core.setFailed(error.message);
7397
}
74-
} catch (error) {
75-
core.error(error.message);
7698
}
99+
100+
run()

0 commit comments

Comments
 (0)