@skillstech/thunderlang 0.5.10 → 0.5.12
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/CHANGELOG.md +450 -0
- package/dist/core.cjs +30434 -7577
- package/dist/index.cjs +28576 -5328
- package/dist/learn.cjs +24201 -5367
- package/package.json +12 -3
- package/src/bicep.mjs +167 -0
- package/src/cli.mjs +169 -3
- package/src/commands.mjs +464 -0
- package/src/core.d.ts +12 -4
- package/src/core.mjs +12 -4
- package/src/emit.mjs +1 -1
- package/src/executed.d.mts +53 -0
- package/src/executed.mjs +465 -0
- package/src/faithful.mjs +137 -0
- package/src/file-formats.mjs +35 -0
- package/src/hcl.mjs +94 -0
- package/src/index.d.ts +7 -1
- package/src/index.mjs +6 -1
- package/src/infra-bicep.mjs +312 -0
- package/src/infra-cloud.mjs +348 -0
- package/src/infra-compose.mjs +315 -0
- package/src/infra-containers.mjs +100 -0
- package/src/infra-docker.mjs +267 -0
- package/src/infra-expr.mjs +509 -0
- package/src/infra-gha.mjs +350 -0
- package/src/infra-k8s.mjs +657 -0
- package/src/infra-terraform.mjs +499 -0
- package/src/infra-words.mjs +811 -0
- package/src/libraries.mjs +202 -0
- package/src/lift.mjs +49 -6
- package/src/meaning.d.mts +89 -0
- package/src/meaning.mjs +2544 -0
- package/src/model.d.mts +65 -14
- package/src/model.mjs +496 -80
- package/src/powershell.mjs +1361 -0
- package/src/read-repo-fs.mjs +29 -7
- package/src/read-repo.d.mts +5 -1
- package/src/read-repo.mjs +33 -7
- package/src/read.d.mts +21 -1
- package/src/read.mjs +1017 -42
- package/src/review.d.mts +35 -0
- package/src/review.mjs +671 -0
- package/src/rlang.mjs +1084 -0
- package/src/round-trip.d.mts +12 -0
- package/src/round-trip.mjs +174 -0
- package/src/runners/thunder-generate.mjs +56 -0
- package/src/runners/thunder-trace.cjs +284 -0
- package/src/runners/thunder_trace.py +599 -0
- package/src/running.d.mts +104 -0
- package/src/running.mjs +787 -0
- package/src/same.d.mts +1 -0
- package/src/same.mjs +24 -3
- package/src/shell.mjs +1212 -0
- package/src/sql.mjs +3 -2
- package/src/steps.d.mts +6 -1
- package/src/steps.mjs +1448 -86
- package/src/system.d.mts +12 -0
- package/src/system.mjs +845 -0
- package/src/things-langs.mjs +275 -0
- package/src/thunderize.mjs +3 -3
- package/src/visual.d.mts +5 -5
- package/src/visual.mjs +24 -3
- package/src/write-steps.d.mts +64 -0
- package/src/write-steps.mjs +1608 -0
- package/src/write.mjs +2 -1
- package/src/yaml.mjs +403 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// GitHub Actions workflows in Thunder: how an app is built, tested, and released. Each job is an Action whose badge
|
|
2
|
+
// says WHEN it runs ("When someone pushes to main", "On a schedule: every day at 02:00 UTC"), its steps in words:
|
|
3
|
+
// well-known actions ("Gets the code", "Sets up Node 22"), `run:` commands one per step, `if:` as a question, `needs:`
|
|
4
|
+
// as its order, and `exit 1` as a Stop. `permissions:` become Permissions ("This workflow can read the code"),
|
|
5
|
+
// secrets are named and never shown, and values worked out when it runs say so. Deterministic, browser-safe.
|
|
6
|
+
import { parseYaml, get, entry, text, list } from './yaml.mjs';
|
|
7
|
+
import { shellSteps, conditionQuestion, cronWords, secretsIn, sayExpressions, hasExpression, expressionsIn, valueWords, listWords, orWords, cap, lc, SECRET_NAME } from './infra-words.mjs';
|
|
8
|
+
|
|
9
|
+
const words = (s) => String(s).replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/[_-]+/g, ' ').toLowerCase().trim();
|
|
10
|
+
const texts = (n) => list(n).map(text).filter((x) => x != null);
|
|
11
|
+
|
|
12
|
+
/** Every scalar under a node, with its line (for finding secrets and expressions). */
|
|
13
|
+
function scalars(n, out = []) {
|
|
14
|
+
if (!n) return out;
|
|
15
|
+
if (n.t === 'map') for (const e of n.entries) { out.push({ v: e.key, line: e.line }); scalars(e.value, out); }
|
|
16
|
+
else if (n.t === 'seq') for (const x of n.items) scalars(x, out);
|
|
17
|
+
else if (n.v != null) out.push({ v: String(n.v), line: n.line, node: n });
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
/** The last line a node covers (a block scalar covers all of its lines). */
|
|
21
|
+
function lastLine(n) {
|
|
22
|
+
if (!n) return 0;
|
|
23
|
+
if (n.t === 'map') return Math.max(n.line, ...n.entries.map((e) => Math.max(e.line, lastLine(e.value))));
|
|
24
|
+
if (n.t === 'seq') return Math.max(n.line, ...n.items.map(lastLine));
|
|
25
|
+
if (n.block) return (n.valueLine ?? n.line) + Math.max(0, String(n.v).replace(/\n+$/, '').split('\n').length - 1);
|
|
26
|
+
return n.line;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ── When it runs ───────────────────────────────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
const PR_TYPES = { opened: 'opened', synchronize: 'updated', reopened: 'reopened', closed: 'closed', edited: 'edited', labeled: 'labeled', unlabeled: 'unlabeled', ready_for_review: 'marked ready for review', review_requested: 'given a reviewer', assigned: 'assigned', converted_to_draft: 'turned into a draft' };
|
|
32
|
+
const OTHER_EVENTS = {
|
|
33
|
+
workflow_dispatch: 'someone starts it by hand', workflow_call: 'another workflow calls it', issue_comment: 'someone comments on an issue or pull request',
|
|
34
|
+
issues: 'something happens to an issue', merge_group: 'a pull request joins the merge queue', repository_dispatch: 'another system asks it to run',
|
|
35
|
+
pull_request_review: 'someone reviews a pull request', pull_request_review_comment: 'someone comments on a pull request review', create: 'a branch or tag is made',
|
|
36
|
+
delete: 'a branch or tag is deleted', deployment: 'a deployment is made', deployment_status: "a deployment's status changes", fork: 'someone forks the repository',
|
|
37
|
+
watch: 'someone stars the repository', page_build: 'GitHub Pages is built', label: 'a label changes', milestone: 'a milestone changes', discussion: 'something happens to a discussion',
|
|
38
|
+
check_run: 'a check runs', check_suite: 'a group of checks runs', status: "a commit's status changes", registry_package: 'a package is published or updated', public: 'the repository is made public',
|
|
39
|
+
};
|
|
40
|
+
const pathsWords = (cfg) => {
|
|
41
|
+
const paths = texts(get(cfg, 'paths'));
|
|
42
|
+
const ignore = texts(get(cfg, 'paths-ignore'));
|
|
43
|
+
if (paths.length) return paths.length <= 3 ? ` and changes ${orWords(paths)}` : ' and changes certain files';
|
|
44
|
+
if (ignore.length) return ' (unless it changes only files it ignores)';
|
|
45
|
+
return '';
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function eventPhrase(ev, cfg) {
|
|
49
|
+
if (ev === 'push') {
|
|
50
|
+
const branches = texts(get(cfg, 'branches')), tags = texts(get(cfg, 'tags')), notBranches = texts(get(cfg, 'branches-ignore'));
|
|
51
|
+
const parts = [];
|
|
52
|
+
if (branches.length) parts.push(orWords(branches));
|
|
53
|
+
if (tags.length) parts.push(`a tag like ${orWords(tags)}`);
|
|
54
|
+
const base = parts.length ? `someone pushes to ${parts.join(' or ')}` : notBranches.length ? `someone pushes to any branch except ${orWords(notBranches)}` : 'someone pushes';
|
|
55
|
+
return base.replace('pushes to a tag', 'pushes a tag') + pathsWords(cfg);
|
|
56
|
+
}
|
|
57
|
+
if (ev === 'pull_request' || ev === 'pull_request_target') {
|
|
58
|
+
const types = texts(get(cfg, 'types'));
|
|
59
|
+
const branches = texts(get(cfg, 'branches'));
|
|
60
|
+
const what = types.length ? orWords([...new Set(types.map((t) => PR_TYPES[t] ?? words(t)))]) : 'opened or updated';
|
|
61
|
+
return `a pull request${branches.length ? ` to ${orWords(branches)}` : ''} is ${what}${pathsWords(cfg)}${ev === 'pull_request_target' ? " (it runs with this repository's own access, even for pull requests from forks)" : ''}`;
|
|
62
|
+
}
|
|
63
|
+
if (ev === 'release') {
|
|
64
|
+
const types = texts(get(cfg, 'types'));
|
|
65
|
+
return types.length ? `a release is ${orWords(types.map((t) => words(t)))}` : 'something happens to a release';
|
|
66
|
+
}
|
|
67
|
+
if (ev === 'workflow_run') {
|
|
68
|
+
const names = texts(get(cfg, 'workflows'));
|
|
69
|
+
const types = texts(get(cfg, 'types'));
|
|
70
|
+
return `the ${orWords(names.map((n) => `"${n}"`)) || 'other'} workflow ${types.length && types.every((t) => t === 'requested') ? 'starts' : 'finishes'}`;
|
|
71
|
+
}
|
|
72
|
+
return OTHER_EVENTS[ev] ?? `the ${words(ev)} event happens`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The workflow's triggers in words, and the list of events. */
|
|
76
|
+
export function triggerWords(onNode) {
|
|
77
|
+
const events = [];
|
|
78
|
+
if (onNode?.t === 'str') events.push({ ev: onNode.v, cfg: null, line: onNode.line });
|
|
79
|
+
else if (onNode?.t === 'seq') for (const x of onNode.items) events.push({ ev: text(x), cfg: null, line: x.line });
|
|
80
|
+
else if (onNode?.t === 'map') for (const e of onNode.entries) events.push({ ev: e.key, cfg: e.value, line: e.line });
|
|
81
|
+
const phrases = [];
|
|
82
|
+
for (const { ev, cfg } of events) {
|
|
83
|
+
if (ev === 'schedule') {
|
|
84
|
+
for (const s of list(cfg)) { const c = text(get(s, 'cron')); if (c) { const w = cronWords(c); phrases.push(w ? `on a schedule: ${w}` : `on a schedule (cron ${c})`); } }
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
phrases.push(eventPhrase(ev, cfg));
|
|
88
|
+
}
|
|
89
|
+
if (!phrases.length) return { when: 'When nothing (it has no triggers)', events };
|
|
90
|
+
const first = phrases[0].startsWith('on a schedule') ? cap(phrases[0]) : `When ${phrases[0]}`;
|
|
91
|
+
const comma = phrases.length > 2 || phrases.some((p) => / or | and /.test(p));
|
|
92
|
+
const when = phrases.length === 1 ? first : comma ? `${first}, or ${phrases.slice(1).join(', or ')}` : `${first} or ${phrases[1]}`;
|
|
93
|
+
return { when, events };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── Permissions ───────────────────────────────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
const SCOPES = {
|
|
99
|
+
contents: 'the code', packages: 'packages', 'pull-requests': 'pull requests', issues: 'issues', actions: 'workflow runs', checks: 'checks',
|
|
100
|
+
deployments: 'deployments', statuses: 'commit statuses', 'security-events': 'security alerts', pages: 'GitHub Pages', attestations: 'attestations',
|
|
101
|
+
discussions: 'discussions', 'repository-projects': 'projects', models: 'GitHub Models',
|
|
102
|
+
};
|
|
103
|
+
function permissionsOf(n, who, file, keyLine) {
|
|
104
|
+
const out = [];
|
|
105
|
+
const add = (can, on, line, extra = {}) => out.push({ id: `${file}#permission#${line}#${out.length}`, who, can, on, wide: false, file, line, says: `${who} ${can === 'not use' ? 'cannot use' : `can ${can}`} ${on}${extra.wide ? ' (wide)' : ''}`, ...extra });
|
|
106
|
+
if (n?.t === 'str') {
|
|
107
|
+
const v = String(n.v ?? '').trim();
|
|
108
|
+
if (v === 'write-all') add('change', 'everything in the repository', n.line, { wide: true, why: 'write-all lets it change every part of the repository: code, packages, releases, settings for workflows, and more.' });
|
|
109
|
+
else if (v === 'read-all') add('read', 'everything in the repository', n.line);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
if (n?.t === 'map') {
|
|
113
|
+
if (!n.entries.length) add('not use', 'anything in the repository', keyLine);
|
|
114
|
+
for (const e of n.entries) {
|
|
115
|
+
const level = text(e.value);
|
|
116
|
+
if (e.key === 'id-token') { if (level === 'write') add('ask for', 'an identity token (to sign in to other services without a stored password)', e.line); continue; }
|
|
117
|
+
add(level === 'write' ? 'write' : level === 'read' ? 'read' : 'not use', SCOPES[e.key] ?? words(e.key), e.line);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ── Well-known actions in words ────────────────────────────────────────────────────────────────────────
|
|
124
|
+
|
|
125
|
+
/** A version given to a setup action: "22", or "(the node for this run)" when it is worked out when it runs. */
|
|
126
|
+
const ver = (v) => {
|
|
127
|
+
if (v == null || v === '') return '';
|
|
128
|
+
if (hasExpression(v)) { const x = expressionsIn(v); return x.length === 1 ? ` (${valueWords(x[0].expr) ?? 'its version is worked out when it runs'})` : ' (its version is worked out when it runs)'; }
|
|
129
|
+
return ` ${v}`;
|
|
130
|
+
};
|
|
131
|
+
const SETUP = { 'setup-node': 'Node', 'setup-python': 'Python', 'setup-java': 'Java', 'setup-go': 'Go', 'setup-dotnet': '.NET', 'setup-ruby': 'Ruby' };
|
|
132
|
+
|
|
133
|
+
function actionWords(uses, w) {
|
|
134
|
+
const u = String(uses).trim();
|
|
135
|
+
if (u.startsWith('./')) return `Uses the action in ${u.slice(2)}`;
|
|
136
|
+
if (u.startsWith('docker://')) return `Runs the image ${u.slice(9)}`;
|
|
137
|
+
const repo = u.split('@')[0];
|
|
138
|
+
const [owner, name = '', ...sub] = repo.split('/');
|
|
139
|
+
const path = sub.join('/');
|
|
140
|
+
const key = `${owner}/${name}`.toLowerCase();
|
|
141
|
+
const W = (k) => (w[k] != null ? sayExpressions(String(w[k])) : null);
|
|
142
|
+
if (key === 'actions/checkout') {
|
|
143
|
+
const bits = [];
|
|
144
|
+
if (w.repository) bits.push(`of ${W('repository')}`);
|
|
145
|
+
if (w.ref) bits.push(`at ${W('ref')}`);
|
|
146
|
+
if (String(w['fetch-depth']) === '0') bits.push('with its whole history');
|
|
147
|
+
return `Gets the code${bits.length ? ` ${bits.join(' ')}` : ''}`;
|
|
148
|
+
}
|
|
149
|
+
const setupName = /^actions\/(setup-\w+)$/.exec(key)?.[1] ?? (key === 'ruby/setup-ruby' ? 'setup-ruby' : null);
|
|
150
|
+
if (setupName && SETUP[setupName]) {
|
|
151
|
+
const lang = SETUP[setupName];
|
|
152
|
+
const vkey = Object.keys(w).find((k) => /-version$/.test(k) && k !== 'check-latest');
|
|
153
|
+
const vfile = Object.keys(w).find((k) => /-version-file$/.test(k));
|
|
154
|
+
let s = `Sets up ${lang}${vkey ? ver(String(w[vkey])) : vfile ? ` (the version in ${w[vfile]})` : ''}`;
|
|
155
|
+
if (setupName === 'setup-java' && w.distribution) s += ` (${w.distribution})`;
|
|
156
|
+
if (w['registry-url']) s += /registry\.npmjs\.org/.test(String(w['registry-url'])) ? ', ready to publish to npm' : `, ready to publish to ${W('registry-url')}`;
|
|
157
|
+
if (w.cache) s += ', reusing saved downloads';
|
|
158
|
+
return s;
|
|
159
|
+
}
|
|
160
|
+
const known = {
|
|
161
|
+
'pnpm/action-setup': () => `Sets up pnpm${ver(w.version)}`, 'oven-sh/setup-bun': () => `Sets up Bun${ver(w['bun-version'])}`, 'denoland/setup-deno': () => 'Sets up Deno',
|
|
162
|
+
'hashicorp/setup-terraform': () => 'Sets up Terraform', 'azure/setup-kubectl': () => 'Sets up kubectl', 'azure/setup-helm': () => 'Sets up Helm', 'astral-sh/setup-uv': () => 'Sets up uv',
|
|
163
|
+
'dtolnay/rust-toolchain': () => `Sets up Rust${u.includes('@') && !/^[0-9a-f]{40}$/.test(u.split('@')[1]) ? ` ${u.split('@')[1]}` : ''}`,
|
|
164
|
+
'docker/setup-buildx-action': () => 'Sets up Docker Buildx (for building images)', 'docker/setup-qemu-action': () => 'Sets up QEMU (for building images for other processors)',
|
|
165
|
+
'actions/cache': () => (path === 'save' ? `Saves files to reuse next time${w.path ? ` (${W('path')})` : ''}` : `Reuses saved files${w.path ? ` (${W('path')})` : ''}`),
|
|
166
|
+
'actions/upload-artifact': () => `Saves ${w.path ? W('path') : 'files'}${w.name ? ` as "${W('name')}"` : ''} for later jobs and people to download`,
|
|
167
|
+
'actions/download-artifact': () => `Gets the saved files${w.name ? ` "${W('name')}"` : ''}`,
|
|
168
|
+
'docker/login-action': () => `Signs in to ${w.registry ? W('registry') : 'Docker Hub'}`,
|
|
169
|
+
'docker/build-push-action': () => `${String(w.push) === 'true' ? 'Builds and pushes the image' : hasExpression(w.push) ? 'Builds the image (and pushes it when that is worked out to be true)' : 'Builds the image'}${w.tags && !hasExpression(w.tags) ? ` ${listWords(String(w.tags).split(/[\n,]/).map((t) => t.trim()).filter(Boolean))}` : ''}`,
|
|
170
|
+
'docker/metadata-action': () => "Works out the image's names and labels",
|
|
171
|
+
'azure/login': () => 'Signs in to Azure', 'azure/webapps-deploy': () => `Deploys the app to Azure${w['app-name'] ? ` (${W('app-name')})` : ''}`, 'azure/k8s-deploy': () => 'Deploys to the Kubernetes cluster',
|
|
172
|
+
'azure/arm-deploy': () => 'Deploys an Azure template', 'aws-actions/configure-aws-credentials': () => 'Signs in to AWS', 'aws-actions/amazon-ecr-login': () => 'Signs in to the Amazon image registry',
|
|
173
|
+
'google-github-actions/auth': () => 'Signs in to Google Cloud', 'actions/github-script': () => 'Runs a small script that uses GitHub',
|
|
174
|
+
'softprops/action-gh-release': () => 'Makes a GitHub release', 'actions/create-release': () => 'Makes a GitHub release',
|
|
175
|
+
'github/codeql-action': () => (path === 'init' ? 'Gets the code scanner ready' : path === 'analyze' ? 'Scans the code for security problems' : path === 'upload-sarif' ? 'Uploads security scan results' : path === 'autobuild' ? 'Builds the code for the scanner' : 'Uses the code scanner'),
|
|
176
|
+
'actions/labeler': () => 'Labels pull requests by the files they change', 'actions/stale': () => 'Marks old issues and pull requests as stale',
|
|
177
|
+
'actions/configure-pages': () => 'Gets GitHub Pages ready', 'actions/upload-pages-artifact': () => 'Saves the site for GitHub Pages', 'actions/deploy-pages': () => 'Publishes the site to GitHub Pages',
|
|
178
|
+
'peaceiris/actions-gh-pages': () => 'Publishes the site to GitHub Pages', 'codecov/codecov-action': () => 'Sends test coverage to Codecov',
|
|
179
|
+
'actions/dependency-review-action': () => 'Checks new dependencies for known problems',
|
|
180
|
+
}[key];
|
|
181
|
+
if (known) return known();
|
|
182
|
+
return `Uses ${repo}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── A workflow file ────────────────────────────────────────────────────────────────────────────────────
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Read a GitHub Actions workflow: jobs as items (kind 'function' with a 'When' route), plus permissions, settings
|
|
189
|
+
* (secrets named only), and facts. Throws on YAML Thunder cannot read, so the file is listed as not read.
|
|
190
|
+
*/
|
|
191
|
+
export function readWorkflow(src, file = 'workflow.yml') {
|
|
192
|
+
const root = parseYaml(src);
|
|
193
|
+
if (!root || root.t !== 'map' || !get(root, 'jobs')) throw new Error('This is not a GitHub Actions workflow: it has no jobs.');
|
|
194
|
+
const name = text(get(root, 'name'));
|
|
195
|
+
const onEntry = entry(root, 'on') ?? entry(root, 'true');
|
|
196
|
+
const { when, events } = triggerWords(onEntry?.value);
|
|
197
|
+
const evNames = events.map((e) => e.ev);
|
|
198
|
+
const permissions = [], settings = [], facts = [], links = [];
|
|
199
|
+
const fact = (kind, says, line, extra = {}) => facts.push({ id: `${file}#${kind}#${line}`, kind, says, file, line, ...extra });
|
|
200
|
+
const setting = (sname, source, line, secret = source === 'secret' || SECRET_NAME.test(sname)) => { if (!settings.some((x) => x.name === sname && x.source === source)) settings.push({ name: sname, source, secret, file, line, ...(source === 'computed' ? { computed: true } : {}) }); };
|
|
201
|
+
|
|
202
|
+
// Settings: inputs given when it starts, secrets and variables it names, and environment values.
|
|
203
|
+
for (const ev of ['workflow_dispatch', 'workflow_call']) {
|
|
204
|
+
const cfg = get(onEntry?.value, ev);
|
|
205
|
+
for (const e of get(cfg, 'inputs')?.entries ?? []) setting(e.key, 'parameter', e.line, false);
|
|
206
|
+
for (const e of get(cfg, 'secrets')?.entries ?? []) setting(e.key, 'secret', e.line, true);
|
|
207
|
+
}
|
|
208
|
+
for (const s of scalars(root)) {
|
|
209
|
+
for (const sec of secretsIn(s.v)) setting(sec, 'secret', s.line, true);
|
|
210
|
+
for (const x of expressionsIn(s.v)) { const m = /^vars\.([\w-]+)$/.exec(x.expr); if (m) setting(m[1], 'variable', s.line, false); }
|
|
211
|
+
}
|
|
212
|
+
const envSettings = (envNode, where) => {
|
|
213
|
+
for (const e of envNode?.t === 'map' ? envNode.entries : []) {
|
|
214
|
+
const v = text(e.value) ?? '';
|
|
215
|
+
const secret = secretsIn(v).length > 0;
|
|
216
|
+
setting(e.key, secret ? 'secret' : hasExpression(v) ? 'computed' : 'literal', e.line, secret || SECRET_NAME.test(e.key));
|
|
217
|
+
if (!secret && !hasExpression(v) && v && SECRET_NAME.test(e.key) && !/^(true|false|\d+)$/i.test(v)) fact('secret-in-plain-env', `${where} sets ${e.key} to a value written in the workflow file, not taken from a secret.`, e.line);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
envSettings(get(root, 'env'), 'The workflow');
|
|
221
|
+
|
|
222
|
+
const wfPerm = entry(root, 'permissions');
|
|
223
|
+
if (wfPerm) permissions.push(...permissionsOf(wfPerm.value, 'This workflow', file, wfPerm.line));
|
|
224
|
+
|
|
225
|
+
// A pull_request_target or workflow_run trigger runs with the repository's access for code anyone can send.
|
|
226
|
+
const untrusted = evNames.filter((e) => e === 'pull_request_target' || e === 'workflow_run');
|
|
227
|
+
let checksOutHead = null;
|
|
228
|
+
|
|
229
|
+
const jobsNode = get(root, 'jobs');
|
|
230
|
+
// A job's own name, unless part of it is worked out when it runs ("Test on Node ${{ matrix.node }}"): then its id.
|
|
231
|
+
const jobLabel = new Map((jobsNode.entries ?? []).map((j) => { const n = text(get(j.value, 'name')); return [j.key, n && !hasExpression(n) ? n : cap(words(j.key))]; }));
|
|
232
|
+
const items = [];
|
|
233
|
+
for (const j of jobsNode.entries ?? []) {
|
|
234
|
+
const job = j.value;
|
|
235
|
+
const label = jobLabel.get(j.key);
|
|
236
|
+
const steps = [];
|
|
237
|
+
const needs = texts(get(job, 'needs'));
|
|
238
|
+
if (needs.length) {
|
|
239
|
+
steps.push({ kind: 'call', says: `After ${listWords(needs.map((n) => `the ${jobLabel.get(n) ?? n} job`))} ${needs.length === 1 ? 'finishes' : 'finish'}`, code: `needs: ${needs.join(', ')}`, line: entry(job, 'needs').line });
|
|
240
|
+
for (const n of needs) { const other = jobsNode.entries.find((x) => x.key === n); if (other) links.push({ from: `${j.key}:${j.line}`, to: `${n}:${other.line}`, kind: 'uses', file, line: entry(job, 'needs').line }); }
|
|
241
|
+
}
|
|
242
|
+
const jp = entry(job, 'permissions');
|
|
243
|
+
if (jp) permissions.push(...permissionsOf(jp.value, `The ${label} job`, file, jp.line));
|
|
244
|
+
envSettings(get(job, 'env'), `The ${label} job`);
|
|
245
|
+
const env = get(job, 'environment');
|
|
246
|
+
const envName = env?.t === 'map' ? text(get(env, 'name')) : text(env);
|
|
247
|
+
const gate = [];
|
|
248
|
+
if (envName) gate.push({ kind: 'call', says: `Deploys to the ${sayExpressions(envName)} environment (it may need someone's approval first)`, code: `environment: ${envName}`, line: entry(job, 'environment').line });
|
|
249
|
+
const runsOn = get(job, 'runs-on');
|
|
250
|
+
const body = [];
|
|
251
|
+
if (runsOn) {
|
|
252
|
+
const ro = runsOn.t === 'str' ? text(runsOn) : runsOn.t === 'seq' ? texts(runsOn).join(', ') : text(get(runsOn, 'group')) ?? 'a chosen computer';
|
|
253
|
+
const roWords = hasExpression(ro) && expressionsIn(ro).length === 1 ? valueWords(expressionsIn(ro)[0].expr) ?? 'a computer worked out when it runs' : sayExpressions(ro);
|
|
254
|
+
body.push({ kind: 'call', says: `Runs on ${roWords}`, code: `runs-on: ${ro}`, line: entry(job, 'runs-on').line });
|
|
255
|
+
}
|
|
256
|
+
const container = get(job, 'container');
|
|
257
|
+
const image = container?.t === 'map' ? text(get(container, 'image')) : text(container);
|
|
258
|
+
if (image) body.push({ kind: 'call', says: `Runs inside the image ${sayExpressions(image)}`, code: `container: ${image}`, line: entry(job, 'container').line });
|
|
259
|
+
for (const sv of get(job, 'services')?.entries ?? []) body.push({ kind: 'call', says: `Starts ${sv.key} alongside it${text(get(sv.value, 'image')) ? ` (the image ${sayExpressions(text(get(sv.value, 'image')))})` : ''}`, code: `services: ${sv.key}`, line: sv.line });
|
|
260
|
+
const reusable = text(get(job, 'uses'));
|
|
261
|
+
if (reusable) {
|
|
262
|
+
const inherit = text(get(job, 'secrets')) === 'inherit';
|
|
263
|
+
body.push({ kind: 'call', says: `Runs the workflow ${reusable}${inherit ? ' (and gives it all of this workflow\'s secrets)' : ''}`, code: `uses: ${reusable}`, line: entry(job, 'uses').line });
|
|
264
|
+
}
|
|
265
|
+
// Steps, with each `if:` asked as a question; neighbours that share a question share one branch.
|
|
266
|
+
const stepNodes = list(get(job, 'steps'));
|
|
267
|
+
let open = null;
|
|
268
|
+
for (const sn of stepNodes) {
|
|
269
|
+
envSettings(get(sn, 'env'), 'A step');
|
|
270
|
+
const got = stepOf(sn, { fact, file, untrusted, onHead: (line) => { checksOutHead ??= line; } });
|
|
271
|
+
const cond = text(get(sn, 'if'));
|
|
272
|
+
if (!cond) { open = null; body.push(...got); continue; }
|
|
273
|
+
const q = conditionQuestion(cond);
|
|
274
|
+
if (q.always) { for (const g of got) g.says += ' (even if an earlier step failed)'; open = null; body.push(...got); continue; }
|
|
275
|
+
if (q.evenAfterFailure) for (const g of got) g.says += ' (even if an earlier step failed)';
|
|
276
|
+
if (open && open.cond === cond) { open.step.children.push(...got); continue; }
|
|
277
|
+
const step = { kind: 'branch', says: q.question, code: `if: ${cond}`, line: entry(sn, 'if').line, children: got };
|
|
278
|
+
open = { cond, step };
|
|
279
|
+
body.push(step);
|
|
280
|
+
}
|
|
281
|
+
// A matrix runs the job once for each combination.
|
|
282
|
+
const matrix = get(get(job, 'strategy'), 'matrix');
|
|
283
|
+
let main = [...gate, ...body];
|
|
284
|
+
if (matrix) {
|
|
285
|
+
const dims = matrix.t === 'map' ? matrix.entries.filter((e) => e.key !== 'include' && e.key !== 'exclude') : [];
|
|
286
|
+
const dimWords = dims.map((d) => `${words(d.key)} (${d.value.t === 'seq' ? listWords(texts(d.value).map(sayExpressions)) : 'worked out when it runs'})`);
|
|
287
|
+
const extra = list(get(matrix, 'include')).length;
|
|
288
|
+
const says = matrix.t !== 'map' ? 'For each combination in the matrix (worked out when it runs)' : dims.length === 1 ? `For each ${dimWords[0]}` : dims.length ? `For each combination of ${listWords(dimWords)}` : (() => { const keys = [...new Set(list(get(matrix, 'include')).flatMap((m) => (m.t === 'map' ? m.entries.map((e) => words(e.key)) : [])))]; return keys.length ? `For each ${listWords(keys)} listed` : 'For each combination it lists'; })();
|
|
289
|
+
main = [{ kind: 'loop', says: `${says}${extra && dims.length ? `, plus ${extra} more` : ''}`, code: 'strategy: matrix', line: entry(get(job, 'strategy'), 'matrix').line, children: body }];
|
|
290
|
+
main = [...gate, ...main];
|
|
291
|
+
}
|
|
292
|
+
const jobIf = text(get(job, 'if'));
|
|
293
|
+
if (jobIf) {
|
|
294
|
+
const q = conditionQuestion(jobIf);
|
|
295
|
+
if (q.always) steps.push(...main.map((m, k) => (k === 0 ? { ...m, says: `${m.says} (even if an earlier job failed)` } : m)));
|
|
296
|
+
else steps.push({ kind: 'branch', says: q.question, code: `if: ${jobIf}`, line: entry(job, 'if').line, children: main });
|
|
297
|
+
} else steps.push(...main);
|
|
298
|
+
if (String(text(get(job, 'continue-on-error'))) === 'true') steps.push({ kind: 'call', says: 'If this job fails, the workflow carries on anyway', code: 'continue-on-error: true', line: entry(job, 'continue-on-error').line });
|
|
299
|
+
const flat = (xs) => xs.flatMap((x) => [x, ...flat(x.children ?? []), ...flat(x.otherwise ?? [])]);
|
|
300
|
+
const all = flat(steps);
|
|
301
|
+
const readable = all.filter((x) => x.kind !== 'other').length;
|
|
302
|
+
const [method, ...rest] = when.split(' ');
|
|
303
|
+
items.push({
|
|
304
|
+
id: `${j.key}:${j.line}`, name: j.key, label, title: when, when, line: j.line, endLine: lastLine(job), route: { method, path: rest.join(' ') },
|
|
305
|
+
inputs: [], returns: null, provenance: 'inferred-from-code', kind: 'function', status: 'signature-only', steps, stepsMore: 0,
|
|
306
|
+
stepsReadable: all.length ? Math.round((readable / all.length) * 100) / 100 : 0,
|
|
307
|
+
says: `The ${label} job runs ${lc(when)}.`, headline: `${label}: ${lc(when)}.`, guards: [], unread: [],
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
for (const p of permissions) if (p.wide) fact('write-all-permissions', `${p.who} has write-all permissions: it can change everything in the repository.`, p.line);
|
|
312
|
+
if (untrusted.length) {
|
|
313
|
+
const secrets = settings.filter((s) => s.source === 'secret' && s.name !== 'GITHUB_TOKEN').map((s) => s.name);
|
|
314
|
+
if (checksOutHead || secrets.length) {
|
|
315
|
+
const why = [checksOutHead && "it gets the pull request's own code", secrets.length && `it uses the secret${secrets.length === 1 ? '' : 's'} ${listWords(secrets)}`].filter(Boolean).join(' and ');
|
|
316
|
+
fact('untrusted-trigger-with-secrets', `It runs on ${untrusted.join(' and ')}, with this repository's access, for changes anyone can send, and ${why}.`, onEntry?.line ?? 1);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return { items, things: [], permissions, settings, facts, links, purpose: name ?? null };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** One workflow step as Thunder steps. */
|
|
323
|
+
function stepOf(sn, ctx) {
|
|
324
|
+
const uses = text(get(sn, 'uses'));
|
|
325
|
+
const runNode = get(sn, 'run');
|
|
326
|
+
const stepName = text(get(sn, 'name'));
|
|
327
|
+
const withNode = get(sn, 'with');
|
|
328
|
+
const w = Object.fromEntries((withNode?.entries ?? []).map((e) => [e.key, text(e.value)]));
|
|
329
|
+
const secrets = [...new Set([...scalars(get(sn, 'env')), ...scalars(withNode), ...scalars(runNode)].flatMap((s) => secretsIn(s.v)))];
|
|
330
|
+
const line = (entry(sn, 'uses') ?? entry(sn, 'run') ?? { line: sn.line }).line;
|
|
331
|
+
let out;
|
|
332
|
+
if (uses) {
|
|
333
|
+
out = [{ kind: 'call', says: actionWords(uses, w), code: `uses: ${uses}`, line }];
|
|
334
|
+
const at = uses.indexOf('@');
|
|
335
|
+
if (!uses.startsWith('./') && !uses.startsWith('docker://') && at > 0 && !/^[0-9a-f]{40}$/.test(uses.slice(at + 1))) {
|
|
336
|
+
const firstParty = /^(actions|github)\//i.test(uses);
|
|
337
|
+
ctx.fact('action-pinned-by-tag', `It uses ${uses.slice(0, at)} by the tag or branch "${uses.slice(at + 1)}", not by a commit, so what runs can change without this file changing.${firstParty ? ' (An action made by GitHub itself.)' : ''}`, line, { firstParty });
|
|
338
|
+
}
|
|
339
|
+
if (/^actions\/checkout@/i.test(uses) && /github\.(event\.pull_request\.head|head_ref|event\.workflow_run\.head)/.test(w.ref ?? '')) ctx.onHead(line);
|
|
340
|
+
} else if (runNode) {
|
|
341
|
+
const firstLine = runNode.block ? runNode.valueLine ?? runNode.line + 1 : runNode.line;
|
|
342
|
+
const inner = shellSteps(String(runNode.v ?? ''), firstLine);
|
|
343
|
+
const say = (xs) => { for (const x of xs) { x.says = sayExpressions(x.says); if (x.children) say(x.children); if (x.otherwise) say(x.otherwise); } };
|
|
344
|
+
say(inner);
|
|
345
|
+
out = stepName && inner.length > 1 ? [{ kind: 'call', says: `${sayExpressions(stepName)}:`, code: `run: (${inner.length} commands)`, line: entry(sn, 'name')?.line ?? line, children: inner }] : inner.length ? inner : [{ kind: 'call', says: 'Runs nothing', code: 'run:', line }];
|
|
346
|
+
} else out = [{ kind: 'other', says: stepName ?? 'A step Thunder cannot read yet', code: stepName ?? 'step', line: sn.line }];
|
|
347
|
+
if (secrets.length) out[0].says += ` (uses the secret${secrets.length === 1 ? '' : 's'} ${listWords(secrets)})`;
|
|
348
|
+
if (String(text(get(sn, 'continue-on-error'))) === 'true') for (const o of out) o.says += ' (a failure here does not stop the job)';
|
|
349
|
+
return out;
|
|
350
|
+
}
|