Skip to content

Commit

Permalink
don't override files in R2 when republishing same ZLS version
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed May 26, 2024
1 parent 3e98e48 commit c56e508
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,18 @@ export async function handlePublish(
}
}

if (!zlsVersion.isRelease && zlsVersion.commitHeight !== undefined) {
let skipArtifactUpload;

if (zlsVersion.isRelease) {
const result = await env.ZIGTOOLS_DB.prepare(
"SELECT ZLSVersion FROM ZLSReleases WHERE ZLSVersion = ?1",
)
.bind(zlsVersionString)
.first<{ ZLSVersion: string }>();

skipArtifactUpload = result !== null;
} else {
assert(zlsVersion.commitHeight !== undefined);
const result = await env.ZIGTOOLS_DB.prepare(
"SELECT ZLSVersion FROM ZLSReleases WHERE IsRelease = 0 AND ZLSVersionMajor = ?1 AND ZLSVersionMinor = ?2 AND ZLSVersionPatch = ?3 AND ZLSVersionBuildID = ?4",
)
Expand All @@ -438,6 +449,8 @@ export async function handlePublish(
)
.first<{ ZLSVersion: string }>();

skipArtifactUpload = result !== null;

if (result !== null && zlsVersionString !== result.ZLSVersion) {
return new Response(
`ZLS version is '${zlsVersionString}' can't be published because ZLS '${result.ZLSVersion}' has already been published!`,
Expand Down Expand Up @@ -472,6 +485,8 @@ export async function handlePublish(
),
]);

if (skipArtifactUpload) return new Response();

const promises: Promise<R2Object>[] = [];

for (let i = 0; i < artifacts.length; i++) {
Expand Down Expand Up @@ -501,7 +516,5 @@ export async function handlePublish(

await Promise.all(promises);

return new Response(undefined, {
status: 200, // Ok
});
return new Response();
}
43 changes: 38 additions & 5 deletions test/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ describe("/v1/publish", () => {
],
[
"zls-linux-x86_64-0.11.0.tar.gz",
new Blob([gzipMagicNumber, "binary1"]),
new Blob([gzipMagicNumber, "binary2"]),
],
],
});
Expand All @@ -1074,15 +1074,15 @@ describe("/v1/publish", () => {
artifacts: [
[
"zls-linux-x86_64-0.11.0.tar.xz",
new Blob([xzMagicNumber, "binary2"]),
new Blob([xzMagicNumber, "binary3"]),
],
[
"zls-linux-x86_64-0.11.0.tar.gz",
new Blob([gzipMagicNumber, "binary2"]),
new Blob([gzipMagicNumber, "binary4"]),
],
[
"zls-windows-aarch64-0.11.0.zip",
new Blob([zipMagicNumber, "binary2"]),
new Blob([zipMagicNumber, "binary5"]),
],
],
});
Expand Down Expand Up @@ -1121,11 +1121,44 @@ describe("/v1/publish", () => {
extension: "tar.gz",
fileShasum: createHash("sha256")
.update(gzipMagicNumber)
.update("binary1")
.update("binary2")
.digest("hex"),
fileSize: gzipMagicNumber.byteLength + 7,
},
],
});

const objects = await env.ZIGTOOLS_BUILDS.list({});

expect(objects.objects).toMatchObject([
{
key: "zls-linux-x86_64-0.11.0.tar.gz",
size: gzipMagicNumber.length + 7,
},
{
key: "zls-linux-x86_64-0.11.0.tar.xz",
size: xzMagicNumber.length + 7,
},
]);

assert(objects.objects[0].checksums.sha256 !== undefined);
assert(objects.objects[1].checksums.sha256 !== undefined);

expect(
Buffer.from(objects.objects[0].checksums.sha256).toString("hex"),
).toBe(
createHash("sha256")
.update(gzipMagicNumber)
.update("binary2")
.digest("hex"),
);
expect(
Buffer.from(objects.objects[1].checksums.sha256).toString("hex"),
).toBe(
createHash("sha256")
.update(xzMagicNumber)
.update("binary1")
.digest("hex"),
);
});
});

0 comments on commit c56e508

Please sign in to comment.