Skip to content

Commit c24069e

Browse files
committed
Changed to a single regex
On the regex you can specify multiple conditions, so there is no reason to split by comma, which could be a character from the regex.
1 parent 462f977 commit c24069e

File tree

7 files changed

+87
-91
lines changed

7 files changed

+87
-91
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![CodeQL](https://github.com/zumba/regex-match-commenter-action/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/zumba/regex-match-commenter-action/actions/workflows/codeql-analysis.yml)
77
[![Coverage](./badges/coverage.svg)](./badges/coverage.svg)
88

9-
This GitHub Action searches for specified regular expression patterns in the changes of a pull request. If matches are found, it can optionally mark the pull request for changes and add inline comments. If no matches are found, a comment is added to the pull request.
9+
This GitHub Action searches for specified regular expression pattern in the changes of a pull request. If matches are found, it can optionally mark the pull request for changes and add inline comments. If no matches are found, a comment is added to the pull request.
1010

1111
Some use cases are for detecting PII changes on the code. For example, you can monitor if the words `email`, `phone`, `street`, `password`, etc. are part of the changes.
1212
The match uses regex, so you can also look for variables such as `\w+@\w+.\w+` to look for an actual e-mail address.
@@ -17,9 +17,9 @@ The match uses regex, so you can also look for variables such as `\w+@\w+.\w+` t
1717

1818
**Required** GitHub token for authentication. Typically, this is the GitHub Actions token.
1919

20-
### `regex_patterns`
20+
### `regex_pattern`
2121

22-
**Required** A comma-separated list of regular expression patterns to search for in the pull request diff.
22+
**Required** A regular expression pattern to search for in the pull request diff.
2323

2424
### `diff_scope`
2525

@@ -59,7 +59,7 @@ jobs:
5959
uses: zumba/regex-match-commenter-action@v1
6060
with:
6161
github_token: ${{ secrets.GITHUB_TOKEN }}
62-
regex_patterns: 'email'
62+
regex_pattern: 'email|phone'
6363
diff_scope: 'both'
6464
mark_changes_requested: false
6565
match_found_message: 'Attention needed.'

__tests__/main.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe('action', () => {
3030
switch (name) {
3131
case 'github_token':
3232
return 'gh-token'
33-
case 'regex_patterns':
34-
return 'pattern1,pattern2'
33+
case 'regex_pattern':
34+
return 'pattern[12]'
3535
default:
3636
return ''
3737
}

action.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'Regex Match Commenter Action'
2-
description: 'Searches for regex patterns in PR diffs and comments on them'
2+
description: 'Searches for regex pattern in PR diffs and comments on them'
33
author: 'Zumba'
44

55
branding:
@@ -11,8 +11,8 @@ inputs:
1111
description: 'GitHub token'
1212
required: true
1313

14-
regex_patterns:
15-
description: 'List of regex patterns to search for'
14+
regex_pattern:
15+
description: 'Javascript regex pattern to search for'
1616
required: true
1717

1818
diff_scope:

dist/index.js

Lines changed: 35 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "regex-match-commenter-action",
3-
"description": "Searches for regex patterns in PR diffs and comments on them",
3+
"description": "Searches for regex pattern in PR diffs and comments on them",
44
"version": "0.0.0",
55
"author": "",
66
"private": true,

src/main.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ enum DiffScope {
1414
export async function run(): Promise<void> {
1515
try {
1616
const token: string = core.getInput('github_token', { required: true })
17-
const regexPatterns: string[] = core
18-
.getInput('regex_patterns', { required: true })
19-
.split(',')
17+
const regexPattern: string = core.getInput('regex_pattern', {
18+
required: true
19+
})
2020
const diffScope: DiffScope = (core.getInput('diff_scope') ||
2121
'both') as DiffScope
2222
const markChangesRequested: boolean =
@@ -98,43 +98,41 @@ export async function run(): Promise<void> {
9898
((diffScope === DiffScope.BOTH || diffScope === DiffScope.REMOVED) &&
9999
line.startsWith('-'))
100100
) {
101-
for (const pattern of regexPatterns) {
102-
const regex = new RegExp(pattern)
103-
if (regex.test(line)) {
104-
// Before posting a new comment, check if it already exists
105-
const isDuplicate = existingComments.data.some(
106-
comment => comment.body === matchFoundMessage
107-
)
108-
109-
if (!isDuplicate) {
110-
core.debug(`Match found`)
111-
foundMatches = true
112-
const side = line.startsWith('+') ? 'RIGHT' : 'LEFT'
113-
const requestParams = {
114-
owner,
115-
repo,
116-
pull_number: pullRequestNumber,
117-
body: matchFoundMessage,
118-
commit_id: context.payload.pull_request.head.sha,
119-
path: currentFile,
120-
side,
121-
line: side === 'LEFT' ? oldLineNumber : newLineNumber
122-
}
123-
core.debug(JSON.stringify(requestParams))
101+
const regex = new RegExp(regexPattern)
102+
if (regex.test(line)) {
103+
// Before posting a new comment, check if it already exists
104+
const isDuplicate = existingComments.data.some(
105+
comment => comment.body === matchFoundMessage
106+
)
124107

125-
await octokit.rest.pulls.createReviewComment({
126-
owner,
127-
repo,
128-
pull_number: pullRequestNumber,
129-
body: matchFoundMessage,
130-
commit_id: context.payload.pull_request.head.sha,
131-
path: currentFile,
132-
side,
133-
line: side === 'LEFT' ? oldLineNumber : newLineNumber
134-
})
135-
} else {
136-
core.debug(`Match found but already commented`)
108+
if (!isDuplicate) {
109+
core.debug(`Match found`)
110+
foundMatches = true
111+
const side = line.startsWith('+') ? 'RIGHT' : 'LEFT'
112+
const requestParams = {
113+
owner,
114+
repo,
115+
pull_number: pullRequestNumber,
116+
body: matchFoundMessage,
117+
commit_id: context.payload.pull_request.head.sha,
118+
path: currentFile,
119+
side,
120+
line: side === 'LEFT' ? oldLineNumber : newLineNumber
137121
}
122+
core.debug(JSON.stringify(requestParams))
123+
124+
await octokit.rest.pulls.createReviewComment({
125+
owner,
126+
repo,
127+
pull_number: pullRequestNumber,
128+
body: matchFoundMessage,
129+
commit_id: context.payload.pull_request.head.sha,
130+
path: currentFile,
131+
side,
132+
line: side === 'LEFT' ? oldLineNumber : newLineNumber
133+
})
134+
} else {
135+
core.debug(`Match found but already commented`)
138136
}
139137
}
140138
}

0 commit comments

Comments
 (0)