-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
executable file
·74 lines (65 loc) · 2.01 KB
/
main.ts
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
#!/usr/bin/env -S deno run -A
import * as core from "npm:@actions/core";
import { readFile, writeFile } from "node:fs/promises";
import process from "node:process";
import { glob } from "npm:glob";
import { $ } from "npm:zx";
const path = core.getInput("path");
process.chdir(path);
$.cwd = process.cwd();
const allFeatures = await Promise.all(
(
await glob("src/*/devcontainer-feature.json")
).map((f) => readFile(f, "utf8").then((x) => JSON.parse(x)))
);
console.log("all-features", allFeatures)
core.setOutput("all-features", JSON.stringify(allFeatures));
const eventName = process.env.GITHUB_EVENT_NAME;
const event = JSON.parse(process.env.GITHUB_EVENT);
console.log("event", event);
// BEFORE
let baseRef = core.getInput("base_ref");
if (!baseRef) {
if (eventName === "push") {
baseRef = event.before;
} else if (eventName === "pull_request") {
baseRef = event.pull_request.base.sha;
}
}
console.log("before", baseRef);
// AFTER
let ref = core.getInput("ref");
if (!ref) {
if (eventName === "push") {
ref = event.after;
} else if (eventName === "pull_request") {
ref = event.pull_request.head.sha;
}
}
console.log("after", ref);
let changedFeatures: any[];
if (baseRef) {
const changedFiles = (await $`git diff --name-only ${baseRef} ${ref}`)
.toString()
.trim()
.split(/\r?\n/g);
console.log(changedFiles);
const changedIds = [...new Set(changedFiles
.map((x) => x.match(/(?:src|test)\/(.*?)\//)?.[1])
.filter((x) => x))].filter(x => allFeatures.map(y => y.id).includes(x));
changedFeatures = (
await Promise.all(
changedIds.map((x) => readFile(`src/${x}/devcontainer-feature.json`, "utf8"))
)
)
.map((x) => JSON.parse(x))
} else {
changedFeatures = [];
}
console.log("changed-features", changedFeatures);
core.setOutput("changed-features", JSON.stringify(changedFeatures));
if (changedFeatures.length) {
core.setOutput("relevant-features", JSON.stringify(changedFeatures))
} else {
core.setOutput("relevant-features", JSON.stringify(allFeatures))
}