GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — newamp/scripts/build-provenance.mjs at main · Evilander/newamp

Expand file tree

/

build-provenance.mjs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
201 lines (185 loc) · 6.98 KB
/

build-provenance.mjs

Copy path

File metadata and controls

201 lines (185 loc) · 6.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { buildReleaseChecksums } from './release-checksums.mjs';
const scriptPath = fileURLToPath(import.meta.url);
const defaultRoot = resolve(fileURLToPath(new URL('..', import.meta.url)));
export function buildProvenancePath({ root = defaultRoot } = {}) {
return join(root, 'release', 'BUILD-PROVENANCE.json');
}
export function buildProvenance({ root = defaultRoot, version = readPackageVersion(root), now = new Date() } = {}) {
const checksums = buildReleaseChecksums({ root, version });
return {
name: 'newamp-build-provenance',
version,
createdAt: now.toISOString(),
git: gitState(root, version),
artifacts: checksums.artifacts.map((artifact) => ({
name: artifact.name,
path: artifact.path,
checksumName: artifact.checksumName,
exists: artifact.exists,
bytes: artifact.bytes ?? null,
sha256: artifact.sha256 ?? null,
})),
};
}
export function writeBuildProvenance({ root = defaultRoot, version = readPackageVersion(root), now = new Date() } = {}) {
const provenance = buildProvenance({ root, version, now });
const path = buildProvenancePath({ root });
const missingArtifacts = provenance.artifacts.filter((artifact) => !artifact.exists || !artifact.sha256);
if (missingArtifacts.length) {
throw new Error(`Cannot write build provenance; artifacts are not ready (${missingArtifacts.map((artifact) => artifact.name).join(', ')})`);
}
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, `${JSON.stringify(provenance, null, 2)}\n`, 'utf8');
return checkBuildProvenance({ root, version });
}
export function checkBuildProvenance({ root = defaultRoot, version = readPackageVersion(root) } = {}) {
const path = buildProvenancePath({ root });
if (!existsSync(path)) {
return {
name: 'build-provenance',
ok: false,
path: displayPath(root, path),
version,
git: gitState(root, version),
artifactCount: 0,
mismatches: [{ field: 'file', reason: 'missing' }],
artifactMismatches: [],
reason: 'release/BUILD-PROVENANCE.json is missing',
};
}
let actual;
try {
actual = JSON.parse(readFileSync(path, 'utf8'));
} catch (error) {
return {
name: 'build-provenance',
ok: false,
path: displayPath(root, path),
version,
git: gitState(root, version),
artifactCount: 0,
mismatches: [{ field: 'json', reason: error instanceof Error ? error.message : String(error) }],
artifactMismatches: [],
reason: 'release/BUILD-PROVENANCE.json is not valid JSON',
};
}
const expected = buildProvenance({ root, version, now: new Date(actual.createdAt || Date.now()) });
const mismatches = compareTopLevel(actual, expected);
const artifactMismatches = compareArtifacts(actual.artifacts, expected.artifacts);
const ok = mismatches.length === 0 && artifactMismatches.length === 0;
return {
name: 'build-provenance',
ok,
path: displayPath(root, path),
version,
git: expected.git,
artifactCount: expected.artifacts.length,
mismatches,
artifactMismatches,
reason: ok
? null
: `release/BUILD-PROVENANCE.json is stale or mismatched (${mismatches.length} metadata, ${artifactMismatches.length} artifacts)`,
};
}
function compareTopLevel(actual, expected) {
const mismatches = [];
for (const field of ['name', 'version']) {
if (actual?.[field] !== expected[field]) {
mismatches.push({ field, expected: expected[field], actual: actual?.[field] });
}
}
for (const field of ['mode', 'head', 'tag', 'tagHead', 'tagMatchesHead', 'clean']) {
if (actual?.git?.[field] !== expected.git[field]) {
mismatches.push({ field: `git.${field}`, expected: expected.git[field], actual: actual?.git?.[field] });
}
}
return mismatches;
}
function compareArtifacts(actualArtifacts, expectedArtifacts) {
const actual = Array.isArray(actualArtifacts) ? actualArtifacts : [];
const mismatches = expectedArtifacts.flatMap((expected) => {
const found = actual.find((artifact) => artifact.name === expected.name);
if (!found) return [{ name: expected.name, field: 'artifact', reason: 'missing from provenance' }];
const fieldMismatches = [];
for (const field of ['path', 'checksumName', 'exists', 'bytes', 'sha256']) {
if (found[field] !== expected[field]) {
fieldMismatches.push({ name: expected.name, field, expected: expected[field], actual: found[field] });
}
}
return fieldMismatches;
});
const expectedNames = new Set(expectedArtifacts.map((artifact) => artifact.name));
for (const artifact of actual) {
if (!expectedNames.has(artifact.name)) {
mismatches.push({ name: artifact.name, field: 'artifact', reason: 'unexpected in provenance' });
}
}
return mismatches;
}
function gitState(root, version) {
const mode = gitMode(root);
if (mode === 'none') {
return {
mode,
head: null,
tag: `v${version}`,
tagHead: null,
tagMatchesHead: false,
clean: null,
};
}
const head = runGit(root, ['rev-parse', 'HEAD']);
const clean = runGit(root, ['status', '--porcelain']);
const tag = `v${version}`;
const tagHead = runGit(root, ['rev-parse', '--verify', `${tag}^{}`]);
const headValue = head.status === 0 ? head.stdout.trim() : null;
const tagHeadValue = tagHead.status === 0 ? tagHead.stdout.trim() : null;
return {
mode,
head: headValue,
tag,
tagHead: tagHeadValue,
tagMatchesHead: Boolean(headValue && tagHeadValue && headValue === tagHeadValue),
clean: clean.status === 0 ? clean.stdout.trim().length === 0 : false,
};
}
function gitMode(root) {
if (existsSync(join(root, '.newamp-git'))) return 'external';
if (existsSync(join(root, '.git'))) return 'worktree';
return 'none';
}
function runGit(root, args) {
const externalGit = join(root, '.newamp-git');
const gitArgs = existsSync(externalGit)
? ['--git-dir', externalGit, '--work-tree', root, ...args]
: ['-C', root, ...args];
return spawnSync('git', gitArgs, {
cwd: root,
encoding: 'utf8',
windowsHide: true,
});
}
function readPackageVersion(root) {
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
return String(pkg.version ?? '').trim() || '0.0.0';
}
function displayPath(root, path) {
const relativePath = relative(root, path);
return relativePath && !relativePath.startsWith('..') ? relativePath.replaceAll('\\', '/') : path;
}
if (process.argv[1] && resolve(process.argv[1]) === scriptPath) {
try {
const report = process.argv.includes('--check')
? checkBuildProvenance()
: writeBuildProvenance();
console.log(JSON.stringify(report, null, 2));
process.exitCode = report.ok ? 0 : 1;
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.