Skip to content

Commit b43bb7b

Browse files
committed
Merge branch 'main' into henrymercer/sha256
2 parents e8d3fa2 + d4b4855 commit b43bb7b

45 files changed

Lines changed: 75202 additions & 1090063 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

analyze/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ outputs:
9595
description: The ID of the uploaded SARIF file.
9696
runs:
9797
using: node24
98-
main: "../lib/analyze-action.js"
99-
post: "../lib/analyze-action-post.js"
98+
main: "../lib/analyze-entry.js"
99+
post: "../lib/analyze-post-entry.js"

autobuild/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ inputs:
1616
required: false
1717
runs:
1818
using: node24
19-
main: '../lib/autobuild-action.js'
19+
main: '../lib/autobuild-entry.js'

build.mjs

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { copyFile, rm, writeFile } from "node:fs/promises";
2-
import { dirname, join } from "node:path";
1+
import { copyFile, readFile, rm, writeFile } from "node:fs/promises";
2+
import { basename, dirname, join } from "node:path";
33
import { fileURLToPath } from "node:url";
44

55
import * as esbuild from "esbuild";
@@ -62,18 +62,123 @@ const onEndPlugin = {
6262
},
6363
};
6464

65+
/** The name of the virtual `entry-points` module. */
66+
const SHARED_ENTRYPOINT = "entry-points";
67+
68+
/**
69+
* This plugin finds all source files that contain action entry points.
70+
* It then generates the virtual `entry-points` module which imports all identifies files,
71+
* and re-exports their `runWrapper` functions with suitable aliases.
72+
* A tiny stub file is emitted for each Action entrypoint. Each stub imports the shared bundle
73+
* and calls the respective entry point.
74+
*
75+
* @type {esbuild.Plugin}
76+
*/
77+
const entryPointsPlugin = {
78+
name: "entry-points",
79+
setup(build) {
80+
const namespace = "actions";
81+
const actions = [];
82+
83+
const toPascal = (s) =>
84+
s.replace(/(^|-)([a-z0-9])/gi, (_, __, c) => c.toUpperCase());
85+
86+
// Find the source files containing action entry points.
87+
build.onStart(() => {
88+
const actionFiles = globSync("src/*-action{,-post}.ts");
89+
for (const actionFile of actionFiles) {
90+
const match = basename(actionFile).match(/(.*)-action(-post)?/);
91+
92+
if (match.length < 2) {
93+
throw new Error(`'${actionFile}' didn't match expected pattern.`);
94+
}
95+
96+
const actionName = match[1];
97+
const isPost = match[2] !== undefined;
98+
99+
actions.push({
100+
path: actionFile,
101+
name: actionName,
102+
isPost,
103+
pascalCaseName: `${toPascal(actionName)}${isPost ? "Post" : ""}Action`,
104+
});
105+
}
106+
});
107+
108+
// Resolve the virtual `entry-points` file and set the corresponding namespace.
109+
// Ideally, we'd `RegExp.escape` the entrypoint here, but that API isn't supported in Node 20.
110+
// Since we're dealing with a hardcoded string, this isn't too much of a problem.
111+
build.onResolve({ filter: new RegExp(`^${SHARED_ENTRYPOINT}$`) }, () => {
112+
return { path: SHARED_ENTRYPOINT, namespace };
113+
});
114+
115+
// Generate the virtual `entry-points` file based on the actions we discovered.
116+
// Restrict using the namespace. The path filter does not need to discriminate any further.
117+
build.onLoad({ filter: /.*/, namespace }, async () => {
118+
const wrapperTemplatePath = "entry-wrapper.js.tpl";
119+
const wrapperTemplate = await readFile(
120+
join(SRC_DIR, wrapperTemplatePath),
121+
"utf-8",
122+
);
123+
124+
const actionsSorted = actions.sort((a, b) =>
125+
a.name.localeCompare(b.name),
126+
);
127+
const imports = actionsSorted
128+
.map(
129+
(action) =>
130+
`import * as ${action.pascalCaseName} from "./src/${basename(action.path)}"`,
131+
)
132+
.join("\n");
133+
const wrappers = actionsSorted
134+
.map((action) =>
135+
wrapperTemplate.replaceAll("__ACTION__", action.pascalCaseName),
136+
)
137+
.join("\n\n");
138+
139+
return {
140+
contents: `"use strict";\n${imports}\n\n${wrappers}\n`,
141+
resolveDir: ".",
142+
loader: "ts",
143+
};
144+
});
145+
146+
// Emit entry point stubs for each action using the entry template.
147+
build.onEnd(async (result) => {
148+
// Read the entry point template.
149+
const templatePath = "action-entry.js.tpl";
150+
const template = await readFile(join(SRC_DIR, templatePath), "utf-8");
151+
152+
const makeHeader = (sourceFile) =>
153+
`// Automatically generated from '${templatePath}' for 'src/${basename(sourceFile)}'.\n\n`;
154+
155+
// Write entry point stubs for each action.
156+
for (const action of actions) {
157+
await writeFile(
158+
join(
159+
OUT_DIR,
160+
`${action.name}${action.isPost ? "-post" : ""}-entry.js`,
161+
),
162+
makeHeader(action.path) +
163+
template.replaceAll("__ACTION__", action.pascalCaseName),
164+
);
165+
}
166+
});
167+
},
168+
};
169+
65170
const context = await esbuild.context({
66171
// Include upload-lib.ts as an entry point for use in testing environments.
67-
entryPoints: globSync([
68-
`${SRC_DIR}/*-action.ts`,
69-
`${SRC_DIR}/*-action-post.ts`,
70-
"src/upload-lib.ts",
71-
]),
172+
entryPoints: [
173+
{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT },
174+
join(SRC_DIR, "upload-lib.ts"),
175+
],
72176
bundle: true,
73177
format: "cjs",
74178
outdir: OUT_DIR,
75179
platform: "node",
76-
plugins: [cleanPlugin, copyDefaultsPlugin, onEndPlugin],
180+
external: ["./entry-points"],
181+
plugins: [cleanPlugin, copyDefaultsPlugin, entryPointsPlugin, onEndPlugin],
77182
target: ["node20"],
78183
define: {
79184
__CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version),

init/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,5 @@ outputs:
171171
description: The version of the CodeQL binary used for analysis
172172
runs:
173173
using: node24
174-
main: '../lib/init-action.js'
175-
post: '../lib/init-action-post.js'
174+
main: '../lib/init-entry.js'
175+
post: '../lib/init-post-entry.js'

0 commit comments

Comments
 (0)