@mark1russell7/workflow-github 0.0.0-bootstrap.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -1
- package/build/index.d.ts +207 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1728 -0
- package/build/index.js.map +1 -0
- package/package.json +42 -6
- package/src/expression.ts +374 -0
- package/src/index.ts +30 -0
- package/src/inject.ts +307 -0
- package/src/language.ts +142 -0
- package/src/yaml.ts +670 -0
package/src/inject.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { isRecord } from "@mark1russell7/coeval";
|
|
2
|
+
import { type EditResult, type WorkflowIssue, getIn, setIn } from "@mark1russell7/workflow";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The script injection rule of GitHub. GitHub puts the value of each `${{ }}` into the text of a script before the
|
|
6
|
+
* shell reads it. A value that a person outside the repository writes (the title of an issue, the name of a branch)
|
|
7
|
+
* can thus become a command. The safe form reads the value from an environment variable.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
type Key = string | number;
|
|
11
|
+
type Rec = Record<string, unknown>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The reads that are safe in a script: numbers, hashes, ids and names that GitHub limits to safe characters. Each other
|
|
15
|
+
* read is not safe. The list has full paths, because a person can write any key under `github.event.inputs`.
|
|
16
|
+
*/
|
|
17
|
+
const TRUSTED_READS: ReadonlySet<string> = new Set([
|
|
18
|
+
"github.sha",
|
|
19
|
+
"github.run_id",
|
|
20
|
+
"github.run_number",
|
|
21
|
+
"github.run_attempt",
|
|
22
|
+
"github.repository",
|
|
23
|
+
"github.repository_id",
|
|
24
|
+
"github.repository_owner",
|
|
25
|
+
"github.repository_owner_id",
|
|
26
|
+
"github.event_name",
|
|
27
|
+
"github.workflow_sha",
|
|
28
|
+
"github.job",
|
|
29
|
+
"github.workspace",
|
|
30
|
+
"github.action_path",
|
|
31
|
+
"github.server_url",
|
|
32
|
+
"github.api_url",
|
|
33
|
+
"github.graphql_url",
|
|
34
|
+
"github.retention_days",
|
|
35
|
+
"github.actor",
|
|
36
|
+
"github.actor_id",
|
|
37
|
+
"github.triggering_actor",
|
|
38
|
+
"github.event.number",
|
|
39
|
+
"github.event.issue.number",
|
|
40
|
+
"github.event.pull_request.number",
|
|
41
|
+
"github.event.pull_request.head.sha",
|
|
42
|
+
"github.event.pull_request.base.sha",
|
|
43
|
+
"github.event.before",
|
|
44
|
+
"github.event.after",
|
|
45
|
+
"runner.os",
|
|
46
|
+
"runner.arch",
|
|
47
|
+
"runner.temp",
|
|
48
|
+
"runner.tool_cache",
|
|
49
|
+
"strategy.job-index",
|
|
50
|
+
"strategy.job-total",
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
/** The trust of a read in a script: safe, written by a person outside the repository, or not known. */
|
|
54
|
+
export type ReadTrust = "trusted" | "outside" | "unknown";
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* This function gives the trust of a reference in a script. A read of the list `TRUSTED_READS` is safe. A read of
|
|
58
|
+
* `github.event` or `github.head_ref` comes from outside. Each other read is not known: an input, the output of a step,
|
|
59
|
+
* a value of `env` or the name of a branch.
|
|
60
|
+
*
|
|
61
|
+
* @param path - The path of the reference, for example `["names", "github", "event", "issue", "title"]`.
|
|
62
|
+
* @returns The trust.
|
|
63
|
+
*/
|
|
64
|
+
export function readTrust(path: readonly unknown[]): ReadTrust {
|
|
65
|
+
if (path[0] === "names" && TRUSTED_READS.has(path.slice(1).map(String).join("."))) return "trusted";
|
|
66
|
+
if (path[0] === "names" && path[1] === "github" && (path[2] === "event" || path[2] === "head_ref")) return "outside";
|
|
67
|
+
return "unknown";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** This function gives the references in a document that are not safe in a script. */
|
|
71
|
+
function unsafeRefs(doc: unknown, out: unknown[][] = []): unknown[][] {
|
|
72
|
+
if (Array.isArray(doc)) for (const item of doc) unsafeRefs(item, out);
|
|
73
|
+
else if (isRecord(doc)) {
|
|
74
|
+
const record = doc as Rec;
|
|
75
|
+
if (record["type"] === "ref" && Array.isArray(record["value"])) {
|
|
76
|
+
if (readTrust(record["value"]) !== "trusted") out.push(record["value"]);
|
|
77
|
+
} else for (const value of Object.values(record)) unsafeRefs(value, out);
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The language of a script: a POSIX shell, PowerShell or JavaScript. */
|
|
83
|
+
type ScriptLanguage = "sh" | "pwsh" | "js";
|
|
84
|
+
|
|
85
|
+
/** A place where text becomes code: the script of a step, and its language or the reason why it has no safe form. */
|
|
86
|
+
interface ScriptPlace {
|
|
87
|
+
readonly at: readonly Key[];
|
|
88
|
+
readonly script: unknown;
|
|
89
|
+
readonly language: ScriptLanguage | { readonly why: string };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const SHELL_LANGUAGES: Readonly<Record<string, ScriptLanguage>> = { bash: "sh", sh: "sh", pwsh: "pwsh", powershell: "pwsh" };
|
|
93
|
+
|
|
94
|
+
/** The parts of each language that the quote scanner does not read. Before such a part, the move refuses. */
|
|
95
|
+
const UNREAD_PARTS: Readonly<Record<ScriptLanguage, RegExp>> = {
|
|
96
|
+
sh: /<<|\$'|`|\$\(/,
|
|
97
|
+
pwsh: /@'|@"|\$\(|<#/,
|
|
98
|
+
js: /\$\{/,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* This function gives the open quote at the end of a text, or `""` outside a quote and in a comment. A part that the
|
|
103
|
+
* scanner does not read gives a refusal: a heredoc, a `$'...'` text, a command in a text, or a regular expression.
|
|
104
|
+
*/
|
|
105
|
+
function openQuote(language: ScriptLanguage, text: string): string | { readonly why: string } {
|
|
106
|
+
if (UNREAD_PARTS[language].test(text)) {
|
|
107
|
+
return { why: "The script has a part before the value that the move does not read: a heredoc, a command in a text or a nested template. Move the value by hand." };
|
|
108
|
+
}
|
|
109
|
+
const quotes = language === "js" ? "'\"`" : "'\"";
|
|
110
|
+
const escape = language === "pwsh" ? "`" : "\\";
|
|
111
|
+
let open = "";
|
|
112
|
+
for (let i = 0; i < text.length; i++) {
|
|
113
|
+
const char = text[i] as string;
|
|
114
|
+
if (open === "") {
|
|
115
|
+
const comment = language === "js" ? (text.startsWith("//", i) ? "\n" : text.startsWith("/*", i) ? "*/" : undefined) : char === "#" && (i === 0 || /\s/.test(text[i - 1] as string)) ? "\n" : undefined;
|
|
116
|
+
if (comment !== undefined) {
|
|
117
|
+
const end = text.indexOf(comment, i + 2);
|
|
118
|
+
if (end < 0) return "";
|
|
119
|
+
i = end + comment.length - 1;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (language === "js" && char === "/") return { why: "The script has a / before the value. It can be a regular expression, which the move does not read. Move the value by hand." };
|
|
123
|
+
if (char === escape) i++;
|
|
124
|
+
else if (quotes.includes(char)) open = char;
|
|
125
|
+
} else if (char === escape && (open !== "'" || language === "js")) i++;
|
|
126
|
+
else if (char === open) open = "";
|
|
127
|
+
}
|
|
128
|
+
return open;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* This function gives the code that reads a variable at a place in a script, by the open quote there. A shell reads
|
|
133
|
+
* the variable in double quotes, thus the shell does not split the value. The result `undefined` means no safe form.
|
|
134
|
+
*/
|
|
135
|
+
function readAt(language: ScriptLanguage, quote: string, name: string): string | undefined {
|
|
136
|
+
if (language === "sh") return quote === '"' ? `\${${name}}` : quote === "'" ? `'"\${${name}}"'` : `"\${${name}}"`;
|
|
137
|
+
if (language === "pwsh") return quote === '"' ? `\${env:${name}}` : quote === "'" ? undefined : `"\${env:${name}}"`;
|
|
138
|
+
if (quote === "`") return `\${process.env.${name}}`;
|
|
139
|
+
return quote === "" ? `process.env.${name}` : `${quote} + process.env.${name} + ${quote}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** This function gives the shell of a step: its own, the default of its job or of the workflow, or the default of the runner. */
|
|
143
|
+
function shellOf(doc: unknown, job: unknown, step: unknown): string | undefined {
|
|
144
|
+
const named = [getIn(step, ["value", "do", "value", "shell"]), getIn(job, ["value", "layers", "github", "keys", "defaults", "run", "shell"]), getIn(doc, ["value", "layers", "github", "keys", "defaults", "run", "shell"])].find(
|
|
145
|
+
(shell) => shell !== undefined,
|
|
146
|
+
);
|
|
147
|
+
if (named !== undefined) return typeof named === "string" ? (named.trim().split(/\s+/)[0] ?? "") : undefined;
|
|
148
|
+
const runsOn = getIn(job, ["value", "layers", "github", "keys", "runs-on"]);
|
|
149
|
+
if (typeof runsOn !== "string" || runsOn.includes("${{")) return undefined;
|
|
150
|
+
return /windows/i.test(runsOn) ? "pwsh" : "bash";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** This function gives each script of a workflow: the `run` of a step, and the `script` input of `actions/github-script`. */
|
|
154
|
+
function scriptPlaces(doc: unknown): ScriptPlace[] {
|
|
155
|
+
const places: ScriptPlace[] = [];
|
|
156
|
+
const jobs = getIn(doc, ["value", "jobs"]);
|
|
157
|
+
if (!isRecord(jobs)) return places;
|
|
158
|
+
for (const [id, job] of Object.entries(jobs as Rec)) {
|
|
159
|
+
const steps = getIn(job, ["value", "steps"]);
|
|
160
|
+
if (!Array.isArray(steps)) continue;
|
|
161
|
+
steps.forEach((step, index) => {
|
|
162
|
+
const at: Key[] = ["value", "jobs", id, "value", "steps", index];
|
|
163
|
+
const kind = getIn(step, ["value", "do", "type"]);
|
|
164
|
+
const call = Array.isArray(kind) ? kind[1] : undefined;
|
|
165
|
+
if (call === "run") {
|
|
166
|
+
const shell = shellOf(doc, job, step);
|
|
167
|
+
const language =
|
|
168
|
+
shell === undefined
|
|
169
|
+
? { why: "Set the shell of the step: the runner of the job is not a fixed text." }
|
|
170
|
+
: (SHELL_LANGUAGES[shell] ?? { why: `The move knows no safe form of an environment variable in the shell ${shell}.` });
|
|
171
|
+
places.push({ at: [...at, "value", "do", "value", "script"], script: getIn(step, ["value", "do", "value", "script"]), language });
|
|
172
|
+
} else if (call === "action") {
|
|
173
|
+
const uses = getIn(step, ["value", "do", "value", "uses"]);
|
|
174
|
+
if (typeof uses === "string" && uses.startsWith("actions/github-script@")) {
|
|
175
|
+
places.push({ at: [...at, "value", "do", "value", "with", "script"], script: getIn(step, ["value", "do", "value", "with", "script"]), language: "js" });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return places;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const refPathText = (path: readonly unknown[]): string => path.slice(1).map(String).join(".");
|
|
184
|
+
const samePath = (a: readonly Key[], b: readonly Key[]): boolean => a.length === b.length && a.every((key, index) => key === b[index]);
|
|
185
|
+
|
|
186
|
+
/** This function tells if a script has a text part with `${{`: an expression that no reader read. */
|
|
187
|
+
function hasRawExpression(script: unknown): boolean {
|
|
188
|
+
if (typeof script === "string") return script.includes("${{");
|
|
189
|
+
if (isRecord(script) && (script as Rec)["type"] === "template" && Array.isArray((script as Rec)["value"])) {
|
|
190
|
+
return ((script as Rec)["value"] as unknown[]).some((part) => typeof part === "string" && part.includes("${{"));
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* This function finds each script that reads an unsafe value with `${{ }}`. A read from outside the repository
|
|
197
|
+
* (`github.event`, `github.head_ref`) is an error. A read that is not known is a warning. Each problem has the code
|
|
198
|
+
* `script-injection`, and `moveToEnv` corrects it.
|
|
199
|
+
*
|
|
200
|
+
* A script with a text `${{` that no reader read is also an error, with the code `raw-expression`. The export
|
|
201
|
+
* writes it as an expression, but the check did not see it. A typed script with an open `${{` gives this error.
|
|
202
|
+
*
|
|
203
|
+
* @param doc - The workflow document.
|
|
204
|
+
* @returns The problems.
|
|
205
|
+
*/
|
|
206
|
+
export function scriptInjections(doc: unknown): WorkflowIssue[] {
|
|
207
|
+
const places = scriptPlaces(doc);
|
|
208
|
+
const issues: WorkflowIssue[] = places.flatMap((place) => {
|
|
209
|
+
const refs = unsafeRefs(place.script);
|
|
210
|
+
if (refs.length === 0) return [];
|
|
211
|
+
const outside = refs.some((ref) => readTrust(ref) === "outside");
|
|
212
|
+
const names = [...new Set(refs.map(refPathText))].join(", ");
|
|
213
|
+
const issue: WorkflowIssue = {
|
|
214
|
+
level: outside ? "error" : "warning",
|
|
215
|
+
code: "script-injection",
|
|
216
|
+
at: place.at,
|
|
217
|
+
message: outside
|
|
218
|
+
? `The script reads ${names} with \${{ }}. A person outside the repository can write this value as a command. Move the value to env.`
|
|
219
|
+
: `The script reads ${names} with \${{ }}. If a person outside the repository sets this value, it becomes a command. Move the value to env.`,
|
|
220
|
+
};
|
|
221
|
+
return [issue];
|
|
222
|
+
});
|
|
223
|
+
for (const place of places.filter((item) => hasRawExpression(item.script))) {
|
|
224
|
+
issues.push({
|
|
225
|
+
level: "error",
|
|
226
|
+
code: "raw-expression",
|
|
227
|
+
at: place.at,
|
|
228
|
+
message: "This text has ${{ that no reader read. The export writes it as an expression that the check did not see. Correct the expression.",
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return issues;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const RESERVED = /^(GITHUB_|RUNNER_|ACTIONS_|CI$|HOME$|PATH$|[0-9])/;
|
|
235
|
+
|
|
236
|
+
/** This function gives the name of a variable for a reference: its last two keys, in upper case, not taken and not reserved. */
|
|
237
|
+
function variableName(path: readonly unknown[], taken: ReadonlySet<string>): string {
|
|
238
|
+
const keys = path.slice(2).filter((key): key is string => typeof key === "string" && key !== "event" && key !== "outputs");
|
|
239
|
+
let base = (keys.slice(-2).join("_") || "VALUE").toUpperCase().replace(/[^A-Z0-9_]/g, "_");
|
|
240
|
+
if (RESERVED.test(base)) base = `V_${base}`;
|
|
241
|
+
if (!taken.has(base)) return base;
|
|
242
|
+
for (let n = 2; ; n++) if (!taken.has(`${base}_${String(n)}`)) return `${base}_${String(n)}`;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const keysAt = (doc: unknown, at: readonly Key[]): string[] => {
|
|
246
|
+
const value = getIn(doc, at);
|
|
247
|
+
return isRecord(value) ? Object.keys(value) : [];
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* This function moves each unsafe value of a script to `env` of its step. The script then reads the variable, and
|
|
252
|
+
* the shell does not read the value as code. The same value gets one variable. A new name is not a name of `env` of
|
|
253
|
+
* the job or of the workflow, thus the move hides no value.
|
|
254
|
+
*
|
|
255
|
+
* @param doc - The workflow document.
|
|
256
|
+
* @param at - The JSON path of the script, as the problem of `scriptInjections` gives it.
|
|
257
|
+
* @returns The new document, or the reason of a refusal.
|
|
258
|
+
*/
|
|
259
|
+
export function moveToEnv(doc: unknown, at: readonly Key[]): EditResult {
|
|
260
|
+
const place = scriptPlaces(doc).find((item) => samePath(item.at, at));
|
|
261
|
+
if (place === undefined) return { ok: false, why: "This path is not a script of a step." };
|
|
262
|
+
const language = place.language;
|
|
263
|
+
if (typeof language !== "string") return { ok: false, why: language.why };
|
|
264
|
+
const stepAt = at.slice(0, at.indexOf("steps") + 2);
|
|
265
|
+
const envAt = [...stepAt, "value", "env"];
|
|
266
|
+
const env: Rec = { ...(isRecord(getIn(doc, envAt)) ? (getIn(doc, envAt) as Rec) : {}) };
|
|
267
|
+
const byDoc = new Map<string, string>();
|
|
268
|
+
for (const [name, value] of Object.entries(env)) byDoc.set(JSON.stringify(value), name);
|
|
269
|
+
const taken = new Set([...Object.keys(env), ...keysAt(doc, [...at.slice(0, 4), "env"]), ...keysAt(doc, ["value", "env"])]);
|
|
270
|
+
let before = "";
|
|
271
|
+
let refused: string | undefined;
|
|
272
|
+
const replace = (part: unknown): unknown => {
|
|
273
|
+
const refs = unsafeRefs(part);
|
|
274
|
+
if (refs.length === 0) {
|
|
275
|
+
before += "\u0000";
|
|
276
|
+
return part;
|
|
277
|
+
}
|
|
278
|
+
const quote = openQuote(language, before);
|
|
279
|
+
const key = JSON.stringify(part);
|
|
280
|
+
const name = byDoc.get(key) ?? variableName(refs[0] as unknown[], taken);
|
|
281
|
+
const code = typeof quote === "string" ? readAt(language, quote, name) : undefined;
|
|
282
|
+
if (code === undefined) {
|
|
283
|
+
refused ??= typeof quote === "string" ? "The value is in a quote that does not read variables. Close the quote around the value first." : quote.why;
|
|
284
|
+
return part;
|
|
285
|
+
}
|
|
286
|
+
if (!byDoc.has(key)) {
|
|
287
|
+
taken.add(name);
|
|
288
|
+
byDoc.set(key, name);
|
|
289
|
+
env[name] = part;
|
|
290
|
+
}
|
|
291
|
+
before += "\u0000";
|
|
292
|
+
return code;
|
|
293
|
+
};
|
|
294
|
+
const script = place.script;
|
|
295
|
+
let next: unknown;
|
|
296
|
+
if (isRecord(script) && (script as Rec)["type"] === "template" && Array.isArray((script as Rec)["value"])) {
|
|
297
|
+
const parts = ((script as Rec)["value"] as unknown[]).map((part) => {
|
|
298
|
+
if (typeof part !== "string") return replace(part);
|
|
299
|
+
before += part;
|
|
300
|
+
return part;
|
|
301
|
+
});
|
|
302
|
+
next = parts.every((part) => typeof part === "string") ? parts.join("") : { type: "template", value: parts };
|
|
303
|
+
} else next = replace(script);
|
|
304
|
+
if (refused !== undefined) return { ok: false, why: refused };
|
|
305
|
+
if (next === script) return { ok: true, doc };
|
|
306
|
+
return { ok: true, doc: setIn(setIn(doc, at, next), envAt, env) };
|
|
307
|
+
}
|
package/src/language.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Evaluator,
|
|
3
|
+
type FormulaProfile,
|
|
4
|
+
type Language,
|
|
5
|
+
type Machine,
|
|
6
|
+
type Namespace,
|
|
7
|
+
type OpFn,
|
|
8
|
+
type PartShape as Shape,
|
|
9
|
+
type Signature,
|
|
10
|
+
defineLanguage,
|
|
11
|
+
fail,
|
|
12
|
+
isRecord,
|
|
13
|
+
} from "@mark1russell7/coeval";
|
|
14
|
+
import { type RefChoice, WORKFLOW_OPS, isSecretRef, workflowMachineWith } from "@mark1russell7/workflow";
|
|
15
|
+
|
|
16
|
+
/** The version of the GitHub add-on language. */
|
|
17
|
+
export const GITHUB_VERSION = "0.1.0";
|
|
18
|
+
|
|
19
|
+
const text: Shape = { shape: "text" };
|
|
20
|
+
const term: Shape = { shape: "term", of: "lit" };
|
|
21
|
+
const terms: Shape = { shape: "map", item: term };
|
|
22
|
+
|
|
23
|
+
/** This function stops an operation that only a GitHub runner can evaluate. */
|
|
24
|
+
const hostOnly = (name: string): OpFn => () => {
|
|
25
|
+
throw new TypeError(`${name} reads the state of a GitHub run. Only GitHub evaluates it.`);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The operations of GitHub beside the operations of the workflow language. They are an index into a value, the filter `.*`
|
|
30
|
+
* and the functions that read the state of a run on GitHub.
|
|
31
|
+
*/
|
|
32
|
+
const OPS: Readonly<Record<string, OpFn>> = {
|
|
33
|
+
at: ((...args: unknown[]) => {
|
|
34
|
+
const [value, key] = args;
|
|
35
|
+
if (isSecretRef(value)) throw new TypeError("The operation at cannot read a secret.");
|
|
36
|
+
if (Array.isArray(value) && typeof key === "number") return value[key] ?? null;
|
|
37
|
+
if (isRecord(value) && (typeof key === "string" || typeof key === "number")) {
|
|
38
|
+
// GitHub reads the keys of an object without case.
|
|
39
|
+
const name = String(key).toLowerCase();
|
|
40
|
+
const found = Object.keys(value).find((own) => own.toLowerCase() === name);
|
|
41
|
+
return found === undefined ? null : value[found];
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}) as OpFn,
|
|
45
|
+
pluck: ((...args: unknown[]) => {
|
|
46
|
+
const [value, ...path] = args;
|
|
47
|
+
const items = Array.isArray(value) ? value : isRecord(value) ? Object.values(value) : [];
|
|
48
|
+
return items.map((item) => path.reduce<unknown>((at, key) => (isRecord(at) ? (at[String(key)] ?? null) : null), item));
|
|
49
|
+
}) as OpFn,
|
|
50
|
+
hashFiles: hostOnly("hashFiles"),
|
|
51
|
+
success: hostOnly("success"),
|
|
52
|
+
always: hostOnly("always"),
|
|
53
|
+
cancelled: hostOnly("cancelled"),
|
|
54
|
+
failure: hostOnly("failure"),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const SIGS: Readonly<Record<string, Signature>> = {
|
|
58
|
+
at: { params: ["unknown", "unknown"], to: "unknown" },
|
|
59
|
+
pluck: { params: ["unknown"], rest: "unknown", to: "unknown" },
|
|
60
|
+
hashFiles: { params: [], rest: "text", to: "text" },
|
|
61
|
+
success: { params: [], to: "bool" },
|
|
62
|
+
always: { params: [], to: "bool" },
|
|
63
|
+
cancelled: { params: [], to: "bool" },
|
|
64
|
+
failure: { params: [], to: "bool" },
|
|
65
|
+
format: { params: ["text"], rest: "unknown", to: "text" },
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** The formula profile of GitHub: the operations of the workflow language and the operations of GitHub. */
|
|
69
|
+
export const GITHUB_OPS: FormulaProfile = {
|
|
70
|
+
ops: { ...WORKFLOW_OPS.ops, ...OPS },
|
|
71
|
+
...(WORKFLOW_OPS.lazy === undefined ? {} : { lazy: WORKFLOW_OPS.lazy }),
|
|
72
|
+
sigs: { ...WORKFLOW_OPS.sigs, ...SIGS },
|
|
73
|
+
pure: WORKFLOW_OPS.pure === true ? true : [...(WORKFLOW_OPS.pure ?? []), "at", "pluck"],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The namespace `github`: the calls that only GitHub runs. The runner of the core runs only procedure calls, thus an
|
|
78
|
+
* evaluation of these kinds stops with `#CALC!`.
|
|
79
|
+
*/
|
|
80
|
+
const GITHUB: Namespace = {
|
|
81
|
+
kinds: {
|
|
82
|
+
action: {
|
|
83
|
+
verbs: { eval: "github.host" },
|
|
84
|
+
parts: { shape: "record", fields: { uses: text, with: terms }, required: ["uses"], closed: true },
|
|
85
|
+
summary: "A step that runs an action of GitHub, with its inputs.",
|
|
86
|
+
},
|
|
87
|
+
run: {
|
|
88
|
+
verbs: { eval: "github.host" },
|
|
89
|
+
parts: { shape: "record", fields: { script: term, shell: term, cwd: term }, closed: true },
|
|
90
|
+
summary: "A step that runs a script in the shell of a GitHub runner.",
|
|
91
|
+
},
|
|
92
|
+
workflow: {
|
|
93
|
+
verbs: { eval: "github.host" },
|
|
94
|
+
parts: { shape: "record", fields: { uses: text, with: terms, secrets: terms, inherit: { shape: "none" } }, required: ["uses"], closed: true },
|
|
95
|
+
summary: "A job that calls a reusable workflow of GitHub, with its inputs and secrets.",
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const EVALUATORS: Readonly<Record<string, Evaluator>> = {
|
|
101
|
+
"github.host": () => fail("#CALC!", "Only GitHub runs this step."),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/** The GitHub add-on language: the namespace `github`. */
|
|
105
|
+
export const GITHUB_LANGUAGE: Language = defineLanguage({
|
|
106
|
+
name: "workflow-github",
|
|
107
|
+
version: GITHUB_VERSION,
|
|
108
|
+
world: { kinds: { github: GITHUB } },
|
|
109
|
+
code: { evaluators: EVALUATORS },
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* This function gives the machine of a GitHub workflow: the workflow language with the GitHub profile and the
|
|
114
|
+
* namespace `github`.
|
|
115
|
+
*
|
|
116
|
+
* @param more - The languages of other add-ons, in order.
|
|
117
|
+
* @returns The machine.
|
|
118
|
+
*/
|
|
119
|
+
export function githubMachine(...more: readonly Language[]): Machine {
|
|
120
|
+
return workflowMachineWith(GITHUB_OPS, GITHUB_LANGUAGE, ...more);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The contexts of GitHub that an expression can read, beside the names of the workflow language. */
|
|
124
|
+
export const GITHUB_NAMES: readonly string[] = ["github", "vars", "runner", "strategy"];
|
|
125
|
+
|
|
126
|
+
/** The call namespaces of GitHub: an input of such a call can hold a secret. */
|
|
127
|
+
export const GITHUB_CALLS: readonly string[] = ["github"];
|
|
128
|
+
|
|
129
|
+
/** The values of the GitHub contexts that a builder offers at each position, as reference documents. */
|
|
130
|
+
export const GITHUB_CONTEXT_CHOICES: readonly RefChoice[] = [
|
|
131
|
+
["github", "event_name"],
|
|
132
|
+
["github", "ref"],
|
|
133
|
+
["github", "ref_name"],
|
|
134
|
+
["github", "sha"],
|
|
135
|
+
["github", "actor"],
|
|
136
|
+
["github", "repository"],
|
|
137
|
+
["github", "run_id"],
|
|
138
|
+
["github", "event", "pull_request", "number"],
|
|
139
|
+
["github", "workspace"],
|
|
140
|
+
["runner", "os"],
|
|
141
|
+
["runner", "temp"],
|
|
142
|
+
].map((path) => ({ group: String(path[0]), label: path.join("."), doc: { type: "ref", value: ["names", ...path] } }));
|