From b5548dff2e8bbb9b8836474d1ee019166eae4a28 Mon Sep 17 00:00:00 2001 From: William Wilkinson Date: Thu, 26 Oct 2023 09:32:53 -0500 Subject: [PATCH] Fix bug with regex not matching without a trailing space. Fixes #5 --- README.md | 8 ++------ src/index.js | 30 ++++++++++++++---------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c664003..d67b905 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Verify Linked Issue Action -A GitHub action that verifies your pull request contains a reference to an issue. +A GitHub action that verifies your pull request contains a reference to an issue. On a PR that does not include a linked issue or reference to an issue in the body, the check should fail and a comment will be added to the PR. @@ -46,7 +46,7 @@ If you want a more complex message, consider using a static template file. (Supp There are two options when using template files: * Option 1) Default File Path: Add a file to .github called VERIFY_PR_COMMENT_TEMPLATE.md. The content of this file will be used as the fail comment in the PR. -* Option 2) Speciy a filename input with the path to a template file. +* Option 2) Speciy a filename input with the path to a template file. ```yaml - name: Verify Linked Issue uses: hattan/verify-linked-issue-action@v1.1.5 @@ -64,10 +64,6 @@ There are two options when using template files: ![Failed Build log](images/failed1.png "Failed Build log") ## Known Issues -* There must be a space after the issue number (ie "#12 " not "#12".) This is due to the way the RegEx is structured and will be resolved in a future release. - * The Issue reference by # needs to be in the body, we don't currently look in the title. That is a future enhancement. v1 - - diff --git a/src/index.js b/src/index.js index 059e514..c90b235 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ - + const core = require('@actions/core') const github = require('@actions/github'); const context = github.context; @@ -28,29 +28,27 @@ async function checkBodyForValidIssue(context, github){ return false; } core.debug(`Checking PR Body: "${body}"`) - const re = /#(.*?)[\s]/g; + const re = /\B#\d+\b/g; const matches = body.match(re); core.debug(`regex matches: ${matches}`) if(matches){ - for(let i=0,len=matches.length;i { let issueId = match.replace('#','').trim(); core.debug(`verifying match is a valid issue issueId: ${issueId}`) - try{ - let issue = await octokit.rest.issues.get({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issueId, - }); + octokit.rest.issues.get({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueId, + }).then((issue) => { if(issue){ core.debug(`Found issue in PR Body ${issueId}`); return true; } - } - catch{ + return false; + }).catch(() => { core.debug(`#${issueId} is not a valid issue.`); - } - } + }); + }) } return false; } @@ -60,7 +58,7 @@ async function checkEventsListForConnectedEvent(context, github){ let pull = await octokit.rest.issues.listEvents({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: context.payload.pull_request.number + issue_number: context.payload.pull_request.number }); if(pull.data){ @@ -116,7 +114,7 @@ async function run() { core.debug('Starting Linked Issue Verification!'); await verifyLinkedIssue(); - + } catch (err) { core.error(`Error verifying linked issue.`) core.error(err)