Skip to content

Commit f2aa09c

Browse files
committed
feat(tasks): optional tasks support! fixes #21
1 parent 8faaa0c commit f2aa09c

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ By default, we mark the check as in_progress until all tasks pass and then it ma
5959
Tasks that contain "POST-MERGE" or "N/A" in all caps are skipped. This is useful for tasks that are not applicable to the PR, or tasks that are only applicable after the PR is merged.
6060
This was inspired by [another project here](https://github.com/Shopify/task-list-checker/tree/main?tab=readme-ov-file#in-a-pull-request).
6161

62+
## Optional tasks
63+
64+
Tasks that contain "OPTIONAL" in all caps are also skipped unless checked, they are also added to an "(+X optional)" text in the check. This is useful for tasks that are not required to be completed before the PR can be merged.
65+
6266
## TODO
6367

6468
- [x] ~~unit tests & travis CI~~

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,21 @@ module.exports = (app) => {
9494
});
9595
}
9696

97+
// optional addon text
98+
let optionalText = '';
99+
if (outstandingTasks.optionalRemaining > 0) {
100+
optionalText = ' (+' + outstandingTasks.optionalRemaining + ' optional)';
101+
}
102+
97103
let check = {
98104
name: 'task-list-completed',
99105
head_branch: '',
100106
head_sha: pr.head.sha,
101107
started_at: (new Date).toISOString(),
102108
status: 'in_progress',
103109
output: {
104-
title: (outstandingTasks.total - outstandingTasks.remaining) + ' / ' + outstandingTasks.total + ' tasks completed',
105-
summary: outstandingTasks.remaining + ' task' + (outstandingTasks.remaining > 1 ? 's' : '') + ' still to be completed',
110+
title: (outstandingTasks.total - outstandingTasks.remaining) + ' / ' + outstandingTasks.total + ' tasks completed' + optionalText,
111+
summary: outstandingTasks.remaining + ' task' + (outstandingTasks.remaining > 1 ? 's' : '') + ' still to be completed' + optionalText,
106112
text: 'We check if any task lists need completing before you can merge this PR'
107113
},
108114
request: {
@@ -116,7 +122,7 @@ module.exports = (app) => {
116122
check.status = 'completed';
117123
check.conclusion = 'success';
118124
check.completed_at = (new Date).toISOString();
119-
check.output.summary = 'All tasks have been completed';
125+
check.output.summary = 'All tasks have been completed' + optionalText;
120126
};
121127

122128
log(pr, 'Complete and sending back to GitHub');

src/check-outstanding-tasks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ module.exports = function (body) {
1919
});
2020
// and filter down to just the task list items
2121
let listItems = allTokens.filter(token => token.type === 'list_item');
22+
let optionalItems = listItems.filter(item => item.text.indexOf('OPTIONAL') !== -1);
2223

2324
// filter out skippable items, case sensitive
2425
let skippable = [
2526
'POST-MERGE',
2627
'N/A',
28+
'OPTIONAL', // this is a special case, we want to count these items but not include them in the remaining count
2729
];
2830
listItems = listItems.filter(item => {
29-
return ! skippable.some(skip => item.text.indexOf(skip) !== -1);
31+
return ! skippable.some(skip => item.text.indexOf(skip) !== -1) || item.text.indexOf('OPTIONAL') !== -1 && item.checked === true;
3032
});
3133

3234
// return counts of task list items and how many are left to be completed
3335
return {
3436
total: listItems.filter(item => item.checked !== undefined).length,
35-
remaining: listItems.filter(item => item.checked === false).length
37+
remaining: listItems.filter(item => item.checked === false).length,
38+
optionalRemaining: optionalItems.filter(item => item.checked === false).length
3639
};
3740
};

tests/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ Hello World
7575
expect(results.total).toBe(3);
7676
expect(results.remaining).toBe(2);
7777
});
78+
79+
test('Test optional items', () => {
80+
let markdown = `
81+
Hello World
82+
- normal
83+
- [x] OPTIONAL: one
84+
- [ ] OPTIONAL: two
85+
- [x] this is not an optional test
86+
`;
87+
let results = checkOutstandingTasks(markdown);
88+
expect(results.total).toBe(2);
89+
expect(results.remaining).toBe(0);
90+
expect(results.optionalRemaining).toBe(1);
91+
});

0 commit comments

Comments
 (0)