diff --git a/src/publish.ts b/src/publish.ts index 1aba24d..6b6be7e 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -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", ) @@ -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!`, @@ -472,6 +485,8 @@ export async function handlePublish( ), ]); + if (skipArtifactUpload) return new Response(); + const promises: Promise[] = []; for (let i = 0; i < artifacts.length; i++) { @@ -501,7 +516,5 @@ export async function handlePublish( await Promise.all(promises); - return new Response(undefined, { - status: 200, // Ok - }); + return new Response(); } diff --git a/test/publish.test.ts b/test/publish.test.ts index 1c5b2e0..5c51f76 100644 --- a/test/publish.test.ts +++ b/test/publish.test.ts @@ -1059,7 +1059,7 @@ describe("/v1/publish", () => { ], [ "zls-linux-x86_64-0.11.0.tar.gz", - new Blob([gzipMagicNumber, "binary1"]), + new Blob([gzipMagicNumber, "binary2"]), ], ], }); @@ -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"]), ], ], }); @@ -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"), + ); }); });