Repository navigation
Expand file tree
/
Copy pathgithub-actions-runtime.test.js
More file actions
103 lines (89 loc) · 3.39 KB
/
Copy pathgithub-actions-runtime.test.js
File metadata and controls
103 lines (89 loc) · 3.39 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const repoRoot = path.resolve(__dirname, "../..");
const workflowsDir = path.join(repoRoot, ".github/workflows");
const approvedNode24Actions = new Set([
"actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9",
"actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0",
"actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131",
"actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71",
"actions/setup-node@820762786026740c76f36085b0efc47a31fe5020",
"actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a",
]);
function pinnedExternalActions(workflow, workflowName) {
const actions = [];
for (const [index, line] of workflow.split("\n").entries()) {
const usesKey = /(?:^|[{,])[ \t]*(?:-[ \t]*)?(?:"uses"|'uses'|uses)[ \t]*:/g;
const explicitUsesKey = /^[ \t]*\?[ \t]*(?:"uses"|'uses'|uses)[ \t]*$/;
if (!usesKey.test(line) && !explicitUsesKey.test(line)) continue;
const canonical = line.match(
/^[ \t]*(?:-[ \t]*)?uses:[ \t]+([^\s#]+)(?:[ \t]+#.*)?$/,
);
assert.ok(
canonical,
`${workflowName}:${index + 1} must use canonical "uses: owner/action@commit" syntax`,
);
const action = canonical[1];
if (action.startsWith("./") || action.startsWith("docker://")) continue;
const separator = action.lastIndexOf("@");
const ref = separator === -1 ? "" : action.slice(separator + 1);
assert.match(
ref,
/^[0-9a-f]{40}$/,
`${workflowName}:${index + 1}: ${action} must use a full commit SHA`,
);
actions.push(action);
}
return actions;
}
test("pinning validation rejects tags, branches, and noncanonical YAML keys", () => {
const validSha = "9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0";
assert.deepEqual(
pinnedExternalActions(` - uses: actions/checkout@${validSha}`, "fixture.yml"),
[`actions/checkout@${validSha}`],
);
for (const invalid of [
" - uses: actions/checkout@v7",
" - uses: actions/checkout@main",
" - uses : actions/checkout@v7",
" - \"uses\": actions/checkout@v7",
" - {'uses': actions/checkout@v7}",
" ? uses\n : actions/checkout@v7",
]) {
assert.throws(
() => pinnedExternalActions(invalid, "fixture.yml"),
/full commit SHA|canonical/,
);
}
});
test("workflows pin external actions and use approved Node 24 first-party actions", () => {
const workflowNames = fs
.readdirSync(workflowsDir)
.filter((name) => name.endsWith(".yml") || name.endsWith(".yaml"))
.sort();
const unapproved = [];
let actionCount = 0;
for (const workflowName of workflowNames) {
const workflow = fs.readFileSync(
path.join(workflowsDir, workflowName),
"utf8",
);
assert.doesNotMatch(
workflow,
/FORCE_JAVASCRIPT_ACTIONS_TO_NODE24/,
`${workflowName} must not force an action onto a runtime it does not declare`,
);
for (const action of pinnedExternalActions(workflow, workflowName)) {
if (action.startsWith("actions/")) {
actionCount += 1;
if (!approvedNode24Actions.has(action)) {
unapproved.push(`${workflowName}: ${action}`);
}
}
}
}
assert.ok(actionCount > 0, "expected to find first-party JavaScript actions");
assert.deepEqual(unapproved, []);
});