Skip to content

Commit 9bee24d

Browse files
fix: cli sha logic (#476)
1 parent bf4d5b3 commit 9bee24d

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

packages/app/e2e.test.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ const E2E_TEMP_DIR_PREFIX = "pkg-pr-new-e2e-";
1414

1515
let server: Awaited<ReturnType<ReturnType<typeof simulation>["listen"]>>;
1616
let workerUrl: string;
17-
let githubOutputDir: string;
17+
let tempDir: string;
1818
let githubOutputPath: string;
1919

2020
let worker: UnstableDevWorker;
21+
22+
const { stdout: gitRevParseOutput } = await ezSpawn.async(
23+
"git rev-parse HEAD",
24+
{ stdio: "overlapped" },
25+
);
26+
const gitRevParse = gitRevParseOutput.trim();
27+
2128
beforeAll(async () => {
29+
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), E2E_TEMP_DIR_PREFIX));
30+
2231
const app = simulation({
2332
initialState: {
2433
users: [],
@@ -51,15 +60,12 @@ beforeAll(async () => {
5160
`${import.meta.dirname}/dist/_worker.js/index.js`,
5261
{
5362
config: `${import.meta.dirname}/wrangler.toml`,
63+
persistTo: path.join(tempDir, "worker"),
5464
},
5565
);
5666
const url = `${worker.proxyData.userWorkerUrl.protocol}//${worker.proxyData.userWorkerUrl.hostname}:${worker.proxyData.userWorkerUrl.port}`;
5767
workerUrl = url;
58-
59-
githubOutputDir = await fs.mkdtemp(
60-
path.join(os.tmpdir(), E2E_TEMP_DIR_PREFIX),
61-
);
62-
githubOutputPath = path.join(githubOutputDir, "output");
68+
githubOutputPath = path.join(tempDir, "output");
6369
await fs.writeFile(githubOutputPath, "");
6470

6571
await ezSpawn.async(
@@ -74,8 +80,8 @@ beforeAll(async () => {
7480

7581
afterAll(async () => {
7682
await server.ensureClose();
77-
if (githubOutputDir?.includes(E2E_TEMP_DIR_PREFIX)) {
78-
await fs.rm(githubOutputDir, { recursive: true, force: true });
83+
if (tempDir?.includes(E2E_TEMP_DIR_PREFIX)) {
84+
await fs.rm(tempDir, { recursive: true, force: true });
7985
}
8086
});
8187

@@ -123,6 +129,7 @@ describe.sequential.each([
123129
GITHUB_SHA: payload.workflow_run.head_sha,
124130
GITHUB_ACTION: payload.workflow_run.id,
125131
GITHUB_JOB: payload.workflow_run.name,
132+
GITHUB_EVENT_NAME: payload.workflow_run.event,
126133
GITHUB_REF_NAME: pr
127134
? `${pr.payload.number}/merge`
128135
: payload.workflow_run.head_branch,
@@ -147,14 +154,12 @@ describe.sequential.each([
147154
expect(process.stderr).toContain("pkg-pr-new:");
148155
expect(process.stderr).toContain("playground-a:");
149156
expect(process.stderr).toContain("playground-b:");
150-
}, 10_000);
157+
}, 20_000);
151158

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

160165
// Test download with SHA
@@ -190,14 +195,12 @@ describe.sequential.each([
190195
expect(installProcess.stdout).toContain(
191196
"playground-a installed successfully!",
192197
);
193-
}, 10_000);
198+
}, 20_000);
194199

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

202205
// Test download
203206
const response = await worker.fetch(
@@ -225,7 +228,7 @@ describe.sequential.each([
225228
expect(installProcess.stdout).toContain(
226229
"playground-b installed successfully!",
227230
);
228-
}, 10_000);
231+
}, 20_000);
229232
});
230233

231234
describe("URL redirects", () => {

packages/cli/index.ts

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

236-
const { stdout: gitShaOutput } = await ezSpawn.async(
237-
"git rev-parse HEAD",
238-
{
239-
stdio: "overlapped",
240-
},
241-
);
242-
const sha = gitShaOutput.trim();
236+
let { sha } = await checkResponse.json();
237+
238+
// on pull_request events, GitHub creates a virtual merge commit that doesn't
239+
// actually belong to the PR, so we use the workflow's head_sha instead
240+
if (process.env.GITHUB_EVENT_NAME !== "pull_request") {
241+
try {
242+
const { stdout: gitRevParseOutput } = await ezSpawn.async(
243+
"git rev-parse HEAD",
244+
{ stdio: "overlapped" },
245+
);
246+
247+
sha = gitRevParseOutput.trim();
248+
} catch {
249+
// git rev-parse fails when the CLI runs outside a git repository
250+
// (e.g. the checkout was done in a separate job and only artifacts were restored here)
251+
// falling back to the workflow's head_sha from checkResponse
252+
}
253+
}
243254

244255
const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
245256
const realDeps: Map<string, string> | null = isPeerDepsEnabled

0 commit comments

Comments
 (0)