-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathresolve-release-source.mjs
More file actions
79 lines (73 loc) · 2.04 KB
/
Copy pathresolve-release-source.mjs
File metadata and controls
79 lines (73 loc) · 2.04 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { execFileSync, spawnSync } from "node:child_process";
import { pathToFileURL } from "node:url";
export function resolveReleaseSource({
sourceSha,
workflowRef,
repositoryPath,
}) {
if (workflowRef !== "refs/heads/main") {
throw new Error("Java publication must be dispatched from main.");
}
if (
typeof sourceSha !== "string" ||
sourceSha.length !== 40 ||
!/^[0-9a-f]{40}$/i.test(sourceSha)
) {
throw new Error("sourceSha must be a full 40-character commit SHA.");
}
const requested = sourceSha.toLowerCase();
const options = {
cwd: repositoryPath,
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
};
const type = execFileSync(
"git",
["cat-file", "-t", requested],
options,
).trim();
if (type !== "commit") {
throw new Error(`sourceSha must identify a commit, not a ${type}.`);
}
const ancestry = spawnSync(
"git",
["merge-base", "--is-ancestor", requested, "refs/remotes/origin/main"],
options,
);
if (ancestry.error) {
throw ancestry.error;
}
if (ancestry.status === 1) {
throw new Error(`Source ${requested} is not in main's history.`);
}
if (ancestry.status !== 0) {
throw new Error(
`Could not validate main's history: ${ancestry.stderr || ancestry.signal || ancestry.status}`,
);
}
return execFileSync(
"git",
["rev-parse", "--verify", `${requested}^{commit}`],
options,
).trim();
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
try {
console.log(
resolveReleaseSource({
sourceSha: process.env.REQUESTED_SOURCE,
workflowRef: process.env.WORKFLOW_REF,
repositoryPath: process.cwd(),
}),
);
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}