Skip to content

Commit 38bbc70

Browse files
committed
handle os and arch being switched beginning with ZLS 0.15.0
1 parent d80f8da commit 38bbc70

File tree

5 files changed

+161
-55
lines changed

5 files changed

+161
-55
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ The body is a JSON object with the following fields:
471471

472472
The key of `artifacts` is the file name of a release artifact with the following format:
473473

474-
`zls-${OS}-${ARCH}-${ZLS_VERSION}.(tar.xz|tar.gz|zip)` (Example: `zls-linux-x86_64-0.1.0.tar.xz`)
474+
`zls-${TARGET}-${ZLS_VERSION}.(tar.xz|tar.gz|zip)` (Example: `zls-linux-x86_64-0.1.0.tar.xz`)
475475

476476
Artifacts that target windows must be `.zip` files. All other non windows targets must include `.tar.xz` **and** `.tar.gz`.
477477

@@ -491,5 +491,5 @@ npm run dev
491491
git clone https://github.com/zigtools/zls
492492
cd zls
493493
zig build release
494-
ZLS_WORKER_ENDPOINT=http://localhost:8787 zig run .github/workflows/publish_release.zig
494+
zig run .github/workflows/prepare_release_payload.zig | curl --request POST --user admin:amogus --header "Content-Type: application/json" --data @- http://localhost:8787/v1/zls/publish
495495
```

src/publish.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from "node:assert";
22
import { Buffer } from "node:buffer";
33
import {
4-
ArtifactEntry,
54
D2JsonData,
65
Extension,
76
ReleaseArtifact,
87
VersionCompatibility,
98
ZLSIndex,
109
artifactsToRecord,
10+
targetFormat,
1111
} from "./shared";
1212
import { SemanticVersion } from "./semantic-version";
1313

@@ -283,23 +283,40 @@ export async function handlePublish(
283283
status: 400, // Bad Request
284284
});
285285
}
286+
assert.strictEqual(
287+
key,
288+
`zls-${match[1]}-${match[2]}-${match[3]}.${match[4]}`,
289+
);
286290

287-
const os = match[1];
288-
const arch = match[2];
289-
const version = match[3];
291+
const rawVersion = match[3];
290292
const extension = match[4] as Extension;
291293

292-
assert.strictEqual(key, `zls-${os}-${arch}-${version}.${extension}`);
293-
294-
if (SemanticVersion.parse(version) === null) {
294+
const version = SemanticVersion.parse(rawVersion);
295+
if (version === null) {
295296
return new Response(
296-
`artifact '${key}' has an invalid version '${version}'!`,
297+
`artifact '${key}' has an invalid version '${rawVersion}'!`,
297298
{
298299
status: 400, // Bad Request
299300
},
300301
);
301302
}
302303

304+
const osArchOrderSwapVersion = SemanticVersion.parse("0.15.0");
305+
assert(osArchOrderSwapVersion != null);
306+
307+
let os;
308+
let arch;
309+
switch (targetFormat(version)) {
310+
case "arch-os":
311+
arch = match[1];
312+
os = match[2];
313+
break;
314+
case "os-arch":
315+
os = match[1];
316+
arch = match[2];
317+
break;
318+
}
319+
303320
if (size == 0) {
304321
return new Response(`artifact '${key}' can't be empty!`, {
305322
status: 400, // Bad Request
@@ -322,7 +339,7 @@ export async function handlePublish(
322339
releaseArtifacts.push({
323340
os: os,
324341
arch: arch,
325-
version: version,
342+
version: rawVersion,
326343
extension: extension,
327344
fileShasum: shasum,
328345
fileSize: size,

src/shared.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from "node:assert";
2+
import { Order, SemanticVersion } from "./semantic-version";
23

34
export interface D2JsonData {
45
date: number;
@@ -63,6 +64,18 @@ export interface SQLiteQueryPlanRow {
6364
detail: string;
6465
}
6566

67+
export function targetFormat(version: SemanticVersion): "arch-os" | "os-arch" {
68+
const osArchOrderSwapVersion = SemanticVersion.parse("0.15.0");
69+
assert(osArchOrderSwapVersion != null);
70+
switch (SemanticVersion.order(version, osArchOrderSwapVersion)) {
71+
case Order.lt:
72+
return "os-arch";
73+
case Order.eq:
74+
case Order.gt:
75+
return "arch-os";
76+
}
77+
}
78+
6679
export function artifactsToRecord(
6780
R2_PUBLIC_URL: string,
6881
artifacts: ReleaseArtifact[],
@@ -77,10 +90,24 @@ export function artifactsToRecord(
7790
case "zip":
7891
break;
7992
}
93+
94+
const version = SemanticVersion.parse(artifact.version);
95+
assert(version != null);
96+
97+
let targetString;
98+
switch (targetFormat(version)) {
99+
case "arch-os":
100+
targetString = `${artifact.arch}-${artifact.os}`;
101+
break;
102+
case "os-arch":
103+
targetString = `${artifact.os}-${artifact.arch}`;
104+
break;
105+
}
106+
80107
assert(!(`${artifact.arch}-${artifact.os}` in targets));
81108
assert.strictEqual(artifact.fileShasum.length, 64);
82109
targets[`${artifact.arch}-${artifact.os}`] = {
83-
tarball: `${R2_PUBLIC_URL}/zls-${artifact.os}-${artifact.arch}-${artifact.version}.${artifact.extension}`,
110+
tarball: `${R2_PUBLIC_URL}/zls-${targetString}-${artifact.version}.${artifact.extension}`,
84111
shasum: artifact.fileShasum,
85112
size: artifact.fileSize.toString(),
86113
};

test/publish.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,29 @@ describe("/v1/zls/publish", () => {
802802
});
803803
});
804804

805+
test("publish build new target format (0.15.0 or later)", async () => {
806+
const response = await sendPublish({
807+
zlsVersion: "0.15.0",
808+
zigVersion: "0.15.0",
809+
artifacts: {
810+
"zls-x86_64-linux-0.15.0.tar.xz": {
811+
shasum: "a".repeat(64),
812+
size: 1,
813+
},
814+
"zls-x86_64-linux-0.15.0.tar.gz": {
815+
shasum: "b".repeat(64),
816+
size: 2,
817+
},
818+
"zls-aarch64-windows-0.15.0.zip": {
819+
shasum: "c".repeat(64),
820+
size: 3,
821+
},
822+
},
823+
});
824+
expect(await response.text()).toBe("");
825+
expect(response.status).toBe(200);
826+
});
827+
805828
test("disallow publishing a failed tagged release", async () => {
806829
const response = await sendPublish({
807830
zlsVersion: "0.1.0",

test/select-zls-version.test.ts

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { env, SELF } from "cloudflare:test";
22
import assert from "node:assert";
3-
import { describe, test, expect, beforeEach, beforeAll } from "vitest";
3+
import { describe, test, expect, beforeAll } from "vitest";
44
import {
55
D2JsonData,
66
ReleaseArtifact,
@@ -15,44 +15,14 @@ import {
1515
} from "../src/select-zls-version";
1616
import { SemanticVersion } from "../src/semantic-version";
1717

18-
const defaultArtifacts: ReleaseArtifact[] = [
19-
{
20-
arch: "x86_64",
21-
os: "linux",
22-
version: "0.11.0",
23-
extension: "tar.xz",
24-
fileShasum:
25-
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
26-
fileSize: 12,
27-
},
28-
{
29-
arch: "x86_64",
30-
os: "linux",
31-
version: "0.11.0",
32-
extension: "tar.gz",
33-
fileShasum:
34-
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
35-
fileSize: 12,
36-
},
37-
{
38-
arch: "aarch64",
39-
os: "windows",
40-
version: "0.11.0",
41-
extension: "zip",
42-
fileShasum:
43-
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
44-
fileSize: 12,
45-
},
46-
];
47-
4818
const samples: D2JsonData[] = [
4919
{
5020
date: 0,
5121
zlsVersion: "0.9.0-dev.3+aaaaaaaaa",
5222
zigVersion: "0.9.0-dev.20+aaaaaaaaa",
5323
minimumBuildZigVersion: "0.9.0-dev.25+aaaaaaaaa",
5424
minimumRuntimeZigVersion: "0.9.0-dev.15+aaaaaaaaa",
55-
artifacts: defaultArtifacts,
25+
artifacts: createExampleArtifacts("0.9.0-dev.3+aaaaaaaaa"),
5626
testedZigVersions: {
5727
"0.9.0-dev.20+aaaaaaaaa": VersionCompatibility.Full,
5828
"0.9.0-dev.25+aaaaaaaaa": VersionCompatibility.Full,
@@ -65,7 +35,7 @@ const samples: D2JsonData[] = [
6535
zigVersion: "0.11.0",
6636
minimumBuildZigVersion: "0.11.0",
6737
minimumRuntimeZigVersion: "0.11.0",
68-
artifacts: defaultArtifacts,
38+
artifacts: createExampleArtifacts("0.11.0"),
6939
testedZigVersions: { "0.11.0": VersionCompatibility.Full },
7040
},
7141
{
@@ -74,7 +44,7 @@ const samples: D2JsonData[] = [
7444
zigVersion: "0.11.0",
7545
minimumBuildZigVersion: "0.11.0",
7646
minimumRuntimeZigVersion: "0.11.0",
77-
artifacts: defaultArtifacts,
47+
artifacts: createExampleArtifacts("0.12.0-dev.1+aaaaaaaaa"),
7848
testedZigVersions: {
7949
"0.11.0": VersionCompatibility.Full,
8050
"0.12.0-dev.2+aaaaaaaaa": VersionCompatibility.Full,
@@ -89,7 +59,7 @@ const samples: D2JsonData[] = [
8959
zigVersion: "0.12.0-dev.7+aaaaaaaaa",
9060
minimumBuildZigVersion: "0.11.0",
9161
minimumRuntimeZigVersion: "0.12.0-dev.7+aaaaaaaaa",
92-
artifacts: defaultArtifacts,
62+
artifacts: createExampleArtifacts("0.12.0-dev.2+aaaaaaaaa"),
9363
testedZigVersions: {
9464
"0.12.0-dev.7+aaaaaaaaa": VersionCompatibility.Full,
9565
"0.12.0-dev.8+aaaaaaaaa": VersionCompatibility.Full,
@@ -103,7 +73,7 @@ const samples: D2JsonData[] = [
10373
zigVersion: "0.12.0-dev.17+aaaaaaaaa",
10474
minimumBuildZigVersion: "0.11.0",
10575
minimumRuntimeZigVersion: "0.12.0-dev.14+aaaaaaaaa",
106-
artifacts: defaultArtifacts,
76+
artifacts: createExampleArtifacts("0.12.0-dev.3+aaaaaaaaa"),
10777
testedZigVersions: {
10878
"0.12.0-dev.17+aaaaaaaaa": VersionCompatibility.Full,
10979
},
@@ -114,7 +84,7 @@ const samples: D2JsonData[] = [
11484
zigVersion: "0.12.0",
11585
minimumBuildZigVersion: "0.12.0",
11686
minimumRuntimeZigVersion: "0.12.0",
117-
artifacts: defaultArtifacts,
87+
artifacts: createExampleArtifacts("0.12.0"),
11888
testedZigVersions: {
11989
"0.12.0": VersionCompatibility.Full,
12090
"0.12.1": VersionCompatibility.Full,
@@ -127,7 +97,7 @@ const samples: D2JsonData[] = [
12797
zigVersion: "0.12.0",
12898
minimumBuildZigVersion: "0.12.0",
12999
minimumRuntimeZigVersion: "0.12.0",
130-
artifacts: defaultArtifacts,
100+
artifacts: createExampleArtifacts("0.12.1"),
131101
testedZigVersions: { "0.12.0": VersionCompatibility.Full },
132102
},
133103
{
@@ -136,7 +106,7 @@ const samples: D2JsonData[] = [
136106
zigVersion: "0.13.0",
137107
minimumBuildZigVersion: "0.13.0",
138108
minimumRuntimeZigVersion: "0.13.0",
139-
artifacts: defaultArtifacts,
109+
artifacts: createExampleArtifacts("0.13.0"),
140110
testedZigVersions: {
141111
"0.13.0": VersionCompatibility.Full,
142112
"0.14.0-dev.2+aaaaaaaaa": VersionCompatibility.Full,
@@ -145,9 +115,41 @@ const samples: D2JsonData[] = [
145115
},
146116
];
147117

148-
async function populateDatabase(): Promise<void> {
149-
shuffleArray(samples);
150-
const statements = samples.map((sample) => {
118+
function createExampleArtifacts(version: string): ReleaseArtifact[] {
119+
return [
120+
{
121+
arch: "x86_64",
122+
os: "linux",
123+
version: version,
124+
extension: "tar.xz",
125+
fileShasum:
126+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
127+
fileSize: 12,
128+
},
129+
{
130+
arch: "x86_64",
131+
os: "linux",
132+
version: version,
133+
extension: "tar.gz",
134+
fileShasum:
135+
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
136+
fileSize: 12,
137+
},
138+
{
139+
arch: "aarch64",
140+
os: "windows",
141+
version: version,
142+
extension: "zip",
143+
fileShasum:
144+
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
145+
fileSize: 12,
146+
},
147+
];
148+
}
149+
150+
async function populateDatabase(entries: D2JsonData[]): Promise<void> {
151+
shuffleArray(entries);
152+
const statements = entries.map((sample) => {
151153
const zlsVersion = SemanticVersion.parse(sample.zlsVersion);
152154
assert(zlsVersion !== null);
153155

@@ -294,7 +296,9 @@ describe("/v1/zls/select-version", () => {
294296
});
295297

296298
describe("test on sample database", () => {
297-
beforeAll(populateDatabase);
299+
beforeAll(async () => {
300+
await populateDatabase(samples);
301+
});
298302

299303
test("search for with Version 0.11.0", async () => {
300304
const response = await selectZLSVersion(
@@ -446,6 +450,41 @@ describe("/v1/zls/select-version", () => {
446450
);
447451
});
448452

453+
test("target string has changed with begining with 0.15.0", async () => {
454+
await populateDatabase([
455+
{
456+
date: 0,
457+
zlsVersion: "0.15.0",
458+
zigVersion: "0.15.0",
459+
minimumBuildZigVersion: "0.15.0",
460+
minimumRuntimeZigVersion: "0.15.0",
461+
artifacts: createExampleArtifacts("0.15.0"),
462+
testedZigVersions: {
463+
"0.15.0": VersionCompatibility.Full,
464+
},
465+
},
466+
]);
467+
468+
const response = await selectZLSVersion(
469+
"0.15.0",
470+
VersionCompatibility.Full,
471+
);
472+
473+
expect(response).not.toHaveProperty("message");
474+
assert(!("message" in response));
475+
476+
expect(response).toHaveProperty("x86_64-linux");
477+
expect(response).not.toHaveProperty("linux-x86_64");
478+
479+
const artifactEntry = response["x86_64-linux"];
480+
expect(artifactEntry).toHaveProperty("tarball");
481+
assert(typeof artifactEntry == "object");
482+
483+
expect(artifactEntry.tarball).toBe(
484+
`${env.R2_PUBLIC_URL}/zls-x86_64-linux-0.15.0.tar.xz`,
485+
);
486+
});
487+
449488
test("explain query plan when searching all tagged releases", async () => {
450489
const response = await env.ZIGTOOLS_DB.prepare(
451490
"EXPLAIN QUERY PLAN SELECT ZLSVersion, JsonData FROM ZLSReleases WHERE IsRelease = 1 ORDER BY ZLSVersionMajor DESC, ZLSVersionMinor DESC, ZLSVersionPatch DESC",

0 commit comments

Comments
 (0)