Skip to content

Commit 3fd239e

Browse files
fix: use the SHA from git rev-parse HEAD when publishing (#470)
1 parent 02df49c commit 3fd239e

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

packages/app/e2e.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ describe.sequential.each([
151151

152152
it(`serves and installs playground-a for ${mode}`, async () => {
153153
const [owner, repo] = payload.repository.full_name.split("/");
154-
const sha = payload.workflow_run.head_sha.substring(0, 7);
154+
const { stdout: gitHeadSha } = await ezSpawn.async("git rev-parse HEAD", {
155+
stdio: "overlapped",
156+
});
157+
const sha = gitHeadSha.trim().substring(0, 7);
155158
const ref = pr?.payload.number ?? payload.workflow_run.head_branch;
156159

157160
// Test download with SHA
@@ -191,7 +194,10 @@ describe.sequential.each([
191194

192195
it(`serves and installs playground-b for ${mode}`, async () => {
193196
const [owner, repo] = payload.repository.full_name.split("/");
194-
const sha = payload.workflow_run.head_sha.substring(0, 7);
197+
const { stdout: gitHeadSha } = await ezSpawn.async("git rev-parse HEAD", {
198+
stdio: "overlapped",
199+
});
200+
const sha = gitHeadSha.trim().substring(0, 7);
195201

196202
// Test download
197203
const response = await worker.fetch(

packages/app/server/routes/[owner]/[repo]/[packageAndRefOrSha].get.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { WorkflowData } from "../../../types";
2-
import { abbreviateCommitHash } from "@pkg-pr-new/utils";
2+
import { abbreviateCommitHash, isValidGitHash } from "@pkg-pr-new/utils";
33
import { normalizeKey } from "unstorage";
44

55
type Params = Omit<WorkflowData, "sha" | "ref"> & {
@@ -86,10 +86,3 @@ export default eventHandler(async (event) => {
8686
status: 404,
8787
});
8888
});
89-
90-
const sha1Regex = /^[\da-f]{40}$/i;
91-
const sha256Regex = /^[\da-f]{64}$/i;
92-
93-
function isValidGitHash(hash: string): boolean {
94-
return sha1Regex.test(hash) || sha256Regex.test(hash);
95-
}

packages/app/server/routes/publish.post.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import type { H3Event } from "h3";
22
import type { components as OctokitComponents } from "@octokit/openapi-types";
33
import type { Comment, PackageManager } from "@pkg-pr-new/utils";
44
import type { WorkflowData } from "../types";
5-
import { isPullRequest, isWhitelisted } from "@pkg-pr-new/utils";
5+
import {
6+
isPullRequest,
7+
isValidGitHash,
8+
isWhitelisted,
9+
} from "@pkg-pr-new/utils";
610
import { randomUUID } from "uncrypto";
711
import { setItemStream, useTemplatesBucket } from "../utils/bucket";
812
import { useOctokitInstallation } from "../utils/octokit";
@@ -23,6 +27,7 @@ export default eventHandler(async (event) => {
2327
"sb-only-templates": onlyTemplatesHeader,
2428
"sb-comment-with-sha": commentWithShaHeader,
2529
"sb-comment-with-dev": commentWithDevHeader,
30+
"sb-sha": shaOverride,
2631
} = getHeaders(event);
2732
const compact = compactHeader === "true";
2833
const onlyTemplates = onlyTemplatesHeader === "true";
@@ -54,6 +59,16 @@ export default eventHandler(async (event) => {
5459
});
5560
}
5661

62+
if (shaOverride) {
63+
if (!isValidGitHash(shaOverride)) {
64+
throw createError({
65+
statusCode: 400,
66+
message: "Invalid sb-sha: must be a 40-character hex SHA",
67+
});
68+
}
69+
workflowData.sha = shaOverride;
70+
}
71+
5772
const whitelisted = await isWhitelisted(
5873
workflowData.owner,
5974
workflowData.repo,

packages/cli/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ const main = defineCommand({
233233
process.exit(1);
234234
}
235235

236-
const { sha } = await checkResponse.json();
236+
const { stdout: gitShaOutput } = await ezSpawn.async(
237+
"git rev-parse HEAD",
238+
{
239+
stdio: "overlapped",
240+
},
241+
);
242+
const sha = gitShaOutput.trim();
237243

238244
const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
239245
const realDeps: Map<string, string> | null = isPeerDepsEnabled
@@ -575,6 +581,7 @@ const main = defineCommand({
575581
const res = await fetch(publishUrl, {
576582
method: "POST",
577583
headers: {
584+
"sb-sha": sha,
578585
"sb-comment": comment,
579586
"sb-compact": `${isCompact}`,
580587
"sb-key": key,

packages/utils/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export function abbreviateCommitHash(fullHash: string) {
3030
return fullHash.substring(0, commitLength);
3131
}
3232

33+
const sha1Regex = /^[\da-f]{40}$/i;
34+
const sha256Regex = /^[\da-f]{64}$/i;
35+
36+
export function isValidGitHash(hash: string): boolean {
37+
return sha1Regex.test(hash) || sha256Regex.test(hash);
38+
}
39+
3340
export function isPullRequest(ref: string) {
3441
return !Number.isNaN(Number(ref));
3542
}

0 commit comments

Comments
 (0)