Skip to content

Commit 02f05c9

Browse files
xunleiizgosalvez
andauthored
fix #4: add job.uses checks for workflow reusability (#7)
* feat: checks pinned version for job.uses * fix: checks SHA only if the step contains `uses` * Minor formatting fixes Co-authored-by: Zennon Gosalvez <[email protected]>
1 parent 8877889 commit 02f05c9

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

dist/index.js

+22-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,31 @@ async function run() {
2323
}
2424

2525
core.startGroup(workflowsPath + '/' + basename);
26-
26+
2727
for (const job in jobs) {
28+
const uses = jobs[job]['uses'];
2829
const steps = jobs[job]['steps'];
2930

30-
if (steps === undefined) {
31-
core.warning(`The "${job}" job of the "${basename}" workflow does not contain steps.`);
32-
}
33-
34-
for (const step of steps) {
35-
const uses = step['uses'];
31+
if (uses !== undefined) {
32+
if (!assertUsesSHA(uses)) {
33+
actionHasError = true;
34+
fileHasError = true;
3635

37-
if (typeof uses === 'string' && uses.includes('@')) {
38-
const version = uses.substr(uses.indexOf('@') + 1);
36+
core.error(`${uses} is not pinned to a full length commit SHA.`);
37+
}
38+
} else if (steps !== undefined) {
39+
for (const step of steps) {
40+
const uses = step['uses'];
3941

40-
if (!sha1.test(version)) {
42+
if (uses !== undefined && !assertUsesSHA(uses)) {
4143
actionHasError = true;
4244
fileHasError = true;
4345

4446
core.error(`${uses} is not pinned to a full length commit SHA.`);
4547
}
4648
}
49+
} else {
50+
core.warning(`The "${job}" job of the "${basename}" workflow does not contain steps or uses.`);
4751
}
4852
}
4953

@@ -53,7 +57,7 @@ async function run() {
5357

5458
core.endGroup();
5559
}
56-
60+
5761
if (actionHasError) {
5862
throw new Error('At least one workflow contains an unpinned GitHub Action version.');
5963
}
@@ -62,4 +66,10 @@ async function run() {
6266
}
6367
}
6468

65-
run();
69+
run();
70+
71+
function assertUsesSHA(uses) {
72+
return typeof uses === 'string' &&
73+
uses.includes('@') &&
74+
sha1.test(uses.substr(uses.indexOf('@') + 1))
75+
}

0 commit comments

Comments
 (0)