-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaddSecurity.js
52 lines (46 loc) · 1.27 KB
/
addSecurity.js
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
import * as fs from "fs/promises";
let workflow = `
on: push
name: Security
jobs:
ensure-pinned-actions:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Ensure SHA pinned actions
uses: zgosalvez/github-actions-ensure-sha-pinned-actions@v3
`.trim();
export default async function (filename, allowed, log) {
const debug = log.extend("add-security");
debug(`Adding security workflow to ${filename}`);
// Check if the file already exists
try {
await fs.stat(filename);
throw new Error(`File ${filename} already exists.`);
} catch (err) {
if (err.code === "ENOENT") {
debug(`File ${filename} does not exist, creating.`);
} else {
throw err;
}
}
// Append any allow-listed repositories to the workflow
if (allowed.length) {
debug(`Adding allow-listed repositories to the workflow: ${allowed}`);
workflow += `
with:
allowlist: |
`.trimEnd();
allowed.forEach((repo) => {
if (repo.endsWith("/*")) {
repo = repo.slice(0, -1); // Remove the trailing *
}
workflow += `\n ${repo}`;
});
}
workflow += "\n";
// Write the workflow to the file
await fs.writeFile(filename, workflow);
return true;
}