-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yaml
More file actions
83 lines (68 loc) · 2.94 KB
/
action.yaml
File metadata and controls
83 lines (68 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
name: file-changes
description: Checks git diff against a group of regexes (JSON-like input) and returns key-value pairs of results.
inputs:
before_sha:
description: The previous SHA.
required: true
current_sha:
description: The current SHA.
required: true
regex_group:
description: |
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"]}').
required: true
outputs:
results:
description: The results of the regex group.
value: ${{ steps.check.outputs.results }}
runs:
using: composite
steps:
- name: Check file changes
uses: actions/github-script@v7
id: check
with:
script: |
const { before_sha, current_sha, regex_group } = {
before_sha: '${{ inputs.before_sha }}',
current_sha: '${{ inputs.current_sha }}',
regex_group: '${{ inputs.regex_group }}'
};
console.log({before_sha, current_sha, regex_group});
async function getChangedFiles(before, current) {
let stdout = '';
const options = {
listeners: {
stdout: (data) => {
stdout += data.toString();
}
}
};
await exec.exec('git', ['diff', '--name-only', before, current], options);
return stdout.split("\n").filter(Boolean);
}
async function processRegexGroup(before, current, regexGroup) {
const parsedGroup = JSON.parse(regexGroup);
const changedFiles = await getChangedFiles(before, current);
const results = {};
for (const [key, patterns] of Object.entries(parsedGroup)) {
const positivePatterns = patterns.filter(p => !p.startsWith("!"));
const negativePatterns = patterns.filter(p => p.startsWith("!")).map(p => p.substring(1));
const positiveRegex =
positivePatterns.length > 0
? new RegExp(`(${positivePatterns.join('|')})`)
: null;
const negativeRegex =
negativePatterns.length > 0
? new RegExp(`(${negativePatterns.join('|')})`)
: null;
const matches = changedFiles.filter((file) => {
const isPositiveMatch = positiveRegex ? positiveRegex.test(file) : true;
const isNegativeMatch = negativeRegex ? negativeRegex.test(file) : false;
return isPositiveMatch && !isNegativeMatch;
});
results[key] = matches.length > 0;
}
core.setOutput('results', JSON.stringify(results));
}
await processRegexGroup(before_sha, current_sha, regex_group);