From c1e39f0e3ec3af21516d5cecf4d8ab2b61ac213a Mon Sep 17 00:00:00 2001 From: Nate Adams Date: Fri, 30 Aug 2024 10:51:29 -0600 Subject: [PATCH] Add support for skipping when description matches a regex pattern (#49) * Added skipDescriptionRegex * Added skip description regex args * condensed the readme example --- README.md | 19 +++++++++++++++++++ action.yml | 4 ++++ index.js | 8 ++++++++ index.test.js | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 6c1eac4..3788516 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/action.yml b/action.yml index 58b609b..b66c149 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/index.js b/index.js index c8f159a..1d47e69 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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) { diff --git a/index.test.js b/index.test.js index a749621..89e6927 100644 --- a/index.test.js +++ b/index.test.js @@ -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) {