Skip to content

Commit

Permalink
Add support for skipping when description matches a regex pattern (#49)
Browse files Browse the repository at this point in the history
* Added skipDescriptionRegex
* Added skip description regex args
* condensed the readme example
  • Loading branch information
n8jadams authored Aug 30, 2024
1 parent 66641fe commit c1e39f0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ jobs:
issueNumber: ${{ github.event.workflow_run.pull_requests[0].number }}
```

### Optional checkboxes

Optional checkboxes can be applied with the `skipDescriptionRegex` and `skipDescriptionRegexFlags` arguments, which correspond to the first and second constructor arguments of [Javascript's RegExp class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).

Here is an example of skipping any description with including "(Optional)". (case-insensitive).

```yaml
# ...
jobs:
job1:
runs-on: ubuntu-latest
steps:
- uses: mheap/require-checklist-action@v2
with:
skipDescriptionRegex: .*\(optional\).*
skipDescriptionRegexFlags: i
```

### Inapplicable checklist items

In case there are some items that are not applicable in given checklist they can be ~stroked through~ and this action will ignore them. For example:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ inputs:
description: Do not look for checklists in comments
required: false
default: "false"
skipDescriptionRegex:
description: A regex pattern of descriptions that will be skipped if matched
required: false
default: undefined
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ async function action() {

const token = core.getInput("token");
const octokit = github.getOctokit(token);
const skipRegexPattern = core.getInput("skipDescriptionRegex");
const skipRegexFlags = core.getInput("skipDescriptionRegexFlags");
const skipDescriptionRegex = !!skipRegexPattern ? new RegExp(skipRegexPattern, skipRegexFlags) : false;

const issueNumber =
core.getInput("issueNumber") || github.context.issue?.number;
Expand Down Expand Up @@ -61,6 +64,11 @@ async function action() {
for (let item of matches) {
var is_complete = item[1] != " ";

if(skipRegexPattern && skipDescriptionRegex.test(item[2])) {
console.log("Skipping task list item: " + item[2]);
continue;
}

containsChecklist = true;

if (is_complete) {
Expand Down
23 changes: 23 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ describe("Require Checklist", () => {
"There are no incomplete task list items"
);
});

it("ignores items that match the skipDescriptionRegex + skipDescriptionRegexFlags args", async () => {
process.env.INPUT_REQUIRECHECKLIST = "true";
process.env.INPUT_SKIPDESCRIPTIONREGEX = ".*\(optional\).*";
process.env.INPUT_SKIPDESCRIPTIONREGEXFLAGS = "i";

mockIssueBody("Demo\r\n\r\n- [x] One\r\n- [x] Two\n- [x] This is (Optional) skipped");

console.log = jest.fn();

await action(tools);

expect(console.log).toBeCalledWith("Completed task list item: One");
expect(console.log).toBeCalledWith("Completed task list item: Two");
expect(console.log).toBeCalledWith("Skipping task list item: This is (Optional) skipped");

expect(console.log).toBeCalledWith(
"There are no incomplete task list items"
);

delete process.env.INPUT_SKIPDESCRIPTIONREGEX;
delete process.env.INPUT_SKIPDESCRIPTIONREGEXFLAGS;
});
});

function mockIssueBody(body, issueNumber = 17) {
Expand Down

0 comments on commit c1e39f0

Please sign in to comment.