Skip to content

Commit

Permalink
Fix bug with regex not matching without a trailing space.
Browse files Browse the repository at this point in the history
Fixes hattan#5
  • Loading branch information
William Wilkinson committed Nov 15, 2023
1 parent 70d4b06 commit b5548df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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/[email protected]
Expand All @@ -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
30 changes: 14 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const core = require('@actions/core')
const github = require('@actions/github');
const context = github.context;
Expand Down Expand Up @@ -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<len;i++){
let match = matches[i];
return matches.some((match) => {
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;
}
Expand All @@ -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){
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b5548df

Please sign in to comment.