Skip to content

Commit dad3d0a

Browse files
committed
feat: use nodejs for regex match
1 parent a6fc2ab commit dad3d0a

1 file changed

Lines changed: 60 additions & 59 deletions

File tree

action.yaml

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,70 @@ inputs:
1414
A JSON-like string of regex groups (e.g., '{"deps": ["**", "!cypress/e2e/dynamic/**/*.ts", "!cypress/e2e/web/**/*.ts"], "dynamic": ["cypress/e2e/dynamic/**/*.cy.ts"], "web": ["cypress/e2e/web/**/*.cy.ts"]}').
1515
required: true
1616

17+
outputs:
18+
results:
19+
description: The results of the regex group.
20+
value: ${{ steps.check.outputs.results }}
21+
1722
runs:
1823
using: composite
1924
steps:
20-
- name: Run diff regex check
21-
shell: bash
22-
run: |
23-
#!/bin/bash
24-
25-
check_diff_regex() {
26-
local before_sha="$1"
27-
local current_sha="$2"
28-
local regex="$3"
29-
local result
30-
31-
result=$(git diff --name-only "$before_sha" "$current_sha" | grep -E "$regex")
32-
33-
if [[ -n "$result" ]]; then
34-
echo "true"
35-
else
36-
echo "false"
37-
fi
38-
}
39-
40-
process_regex_group() {
41-
local before_sha="$1"
42-
local current_sha="$2"
43-
local regex_group="$3"
25+
- name: Check file changes
26+
uses: actions/github-script@v7
27+
id: check
28+
with:
29+
script: |
30+
const { before_sha, current_sha, regex_group } = {
31+
before_sha: '${{ inputs.before_sha }}',
32+
current_sha: '${{ inputs.current_sha }}',
33+
regex_group: '${{ inputs.regex_group }}'
34+
};
4435
45-
# Parse JSON-like input using jq (install jq if not already present)
46-
if ! command -v jq &> /dev/null; then
47-
echo "jq could not be found. Install it and try again."
48-
exit 1
49-
fi
50-
51-
for key in $(echo "$regex_group" | jq -r 'keys[]'); do
52-
local regex_array=$(echo "$regex_group" | jq -r ".$key | join(\",\")")
53-
local combined_regex=""
54-
local negative_regex=""
55-
local positive_regex=""
56-
57-
for regex in $(echo "$regex_array" | tr "," "\n"); do
58-
if [[ "$regex" == \!* ]]; then
59-
negative_regex+="(?!$(echo $regex | sed 's/!//'))"
60-
else
61-
positive_regex+="($regex)|"
62-
fi
63-
done
36+
console.log({before_sha, current_sha, regex_group});
37+
38+
async function getChangedFiles(before, current) {
39+
let stdout = '';
40+
const options = {
41+
listeners: {
42+
stdout: (data) => {
43+
stdout += data.toString();
44+
}
45+
}
46+
};
6447
65-
positive_regex=${positive_regex%|}
48+
await exec.exec('git', ['diff', '--name-only', before, current], options);
49+
return stdout.split("\n").filter(Boolean);
50+
}
51+
52+
async function processRegexGroup(before, current, regexGroup) {
53+
const parsedGroup = JSON.parse(regexGroup);
54+
const changedFiles = await getChangedFiles(before, current);
55+
const results = {};
6656
67-
if [[ -n "$negative_regex" ]]; then
68-
if [[ -n "$positive_regex" ]]; then
69-
combined_regex="$negative_regex($positive_regex)"
70-
else
71-
combined_regex="$negative_regex.*"
72-
fi
73-
else
74-
combined_regex="$positive_regex"
75-
fi
76-
77-
local result=$(check_diff_regex "$before_sha" "$current_sha" "$combined_regex")
78-
echo "$key=$result" >> $GITHUB_OUTPUT
79-
done
80-
}
57+
for (const [key, patterns] of Object.entries(parsedGroup)) {
58+
const positivePatterns = patterns.filter(p => !p.startsWith("!"));
59+
const negativePatterns = patterns.filter(p => p.startsWith("!")).map(p => p.substring(1));
60+
61+
const positiveRegex =
62+
positivePatterns.length > 0
63+
? new RegExp(`(${positivePatterns.join('|')})`)
64+
: null;
8165
82-
process_regex_group "${{ inputs.before_sha }}" "${{ inputs.current_sha }}" "${{ inputs.regex_group }}"
66+
const negativeRegex =
67+
negativePatterns.length > 0
68+
? new RegExp(`(${negativePatterns.join('|')})`)
69+
: null;
70+
71+
const matches = changedFiles.filter((file) => {
72+
const isPositiveMatch = positiveRegex ? positiveRegex.test(file) : true;
73+
const isNegativeMatch = negativeRegex ? negativeRegex.test(file) : false;
74+
return isPositiveMatch && !isNegativeMatch;
75+
});
76+
77+
results[key] = matches.length > 0;
78+
}
79+
80+
core.setOutput('results', JSON.stringify(results));
81+
}
82+
83+
await processRegexGroup(before_sha, current_sha, regex_group);

0 commit comments

Comments
 (0)