-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathvalidate-local-publication.mjs
More file actions
167 lines (155 loc) · 4.77 KB
/
Copy pathvalidate-local-publication.mjs
File metadata and controls
167 lines (155 loc) · 4.77 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import {
validateNativeClassifierJar,
validatePlaceholderJar,
} from "./validate-native-artifact.mjs";
export function validateLocalPublication({
artifactId,
repositoryPath,
repoRoot,
requireSignatures,
version,
}) {
if (
artifactId !== "copilot-sdk-java" &&
artifactId !== "copilot-sdk-java-runtime"
) {
throw new Error(`Unsupported Java SDK artifact: ${artifactId}`);
}
const classifiers =
artifactId === "copilot-sdk-java-runtime"
? [
"linux-x64",
"linux-arm64",
"linuxmusl-x64",
"win32-x64",
"win32-arm64",
"darwin-x64",
"darwin-arm64",
]
: [];
const artifactDirectory = path.join(
repositoryPath,
"com",
"github",
artifactId,
version,
);
const requiredArtifacts = [
`${artifactId}-${version}.jar`,
`${artifactId}-${version}.pom`,
`${artifactId}-${version}-sources.jar`,
`${artifactId}-${version}-javadoc.jar`,
...classifiers.map(
(classifier) => `${artifactId}-${version}-${classifier}.jar`,
),
];
const files = new Set(fs.readdirSync(artifactDirectory));
for (const artifact of requiredArtifacts) {
if (!files.has(artifact)) {
throw new Error(`Local publication is missing ${artifact}`);
}
if (requireSignatures && !files.has(`${artifact}.asc`)) {
throw new Error(
`Local release publication is missing signature ${artifact}.asc`,
);
}
}
const publishedJars = [...files]
.filter((file) => file.endsWith(".jar"))
.sort();
const expectedJars = requiredArtifacts
.filter((file) => file.endsWith(".jar"))
.sort();
if (publishedJars.join("\n") !== expectedJars.join("\n")) {
throw new Error(
`Local publication contains unexpected JARs:\n${publishedJars.join("\n")}`,
);
}
validatePublishedPom({
artifactId,
pomPath: path.join(artifactDirectory, `${artifactId}-${version}.pom`),
version,
});
validatePlaceholderJar(
path.join(artifactDirectory, `${artifactId}-${version}.jar`),
);
for (const classifier of classifiers) {
const filename = `${artifactId}-${version}-${classifier}.jar`;
validateNativeClassifierJar({
classifier,
jarPath: path.join(artifactDirectory, filename),
expectedFilename: filename,
repoRoot,
});
}
return artifactDirectory;
}
function validatePublishedPom({ artifactId, pomPath, version }) {
const pom = fs.readFileSync(pomPath, "utf8").replace(/<!--[\s\S]*?-->/g, "");
if (pom.includes("${revision}")) {
throw new Error(
`Published POM contains unresolved \${revision}: ${pomPath}`,
);
}
if (/<parent(?:\s|\/?>)/.test(pom)) {
throw new Error(`Published POM must not depend on a parent: ${pomPath}`);
}
// Maven writes the flattened project's coordinates before nested elements.
// Match that header so dependency coordinates cannot satisfy this check.
const coordinates = pom.match(
/<project\b[^>]*>\s*<modelVersion>[^<]+<\/modelVersion>\s*<groupId>([^<]+)<\/groupId>\s*<artifactId>([^<]+)<\/artifactId>\s*<version>([^<]+)<\/version>/,
);
if (!coordinates) {
throw new Error(
`Published POM is missing flattened project coordinates: ${pomPath}`,
);
}
const [, groupId, publishedArtifactId, publishedVersion] = coordinates.map(
(value) => value.trim(),
);
if (
groupId !== "com.github" ||
publishedArtifactId !== artifactId ||
publishedVersion !== version
) {
throw new Error(
`Unexpected Maven coordinates in ${pomPath}: ${groupId}:${publishedArtifactId}:${publishedVersion} (expected com.github:${artifactId}:${version})`,
);
}
}
function main() {
const [repositoryPath, artifactId, version, repoRoot, signatures] =
process.argv.slice(2);
if (!repositoryPath || !artifactId || !version || !repoRoot) {
console.error(
"Usage: node validate-local-publication.mjs <repositoryPath> <artifactId> <version> <repoRoot> [--signatures]",
);
process.exitCode = 1;
return;
}
try {
const artifactDirectory = validateLocalPublication({
artifactId,
repositoryPath,
repoRoot,
requireSignatures: signatures === "--signatures",
version,
});
console.log(`Validated local Maven publication: ${artifactDirectory}`);
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
main();
}