diff --git a/packages/app/e2e.test.ts b/packages/app/e2e.test.ts index 14901678..b3819313 100644 --- a/packages/app/e2e.test.ts +++ b/packages/app/e2e.test.ts @@ -14,11 +14,20 @@ const E2E_TEMP_DIR_PREFIX = "pkg-pr-new-e2e-"; let server: Awaited["listen"]>>; let workerUrl: string; -let githubOutputDir: string; +let tempDir: string; let githubOutputPath: string; let worker: UnstableDevWorker; + +const { stdout: gitRevParseOutput } = await ezSpawn.async( + "git rev-parse HEAD", + { stdio: "overlapped" }, +); +const gitRevParse = gitRevParseOutput.trim(); + beforeAll(async () => { + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), E2E_TEMP_DIR_PREFIX)); + const app = simulation({ initialState: { users: [], @@ -51,15 +60,12 @@ beforeAll(async () => { `${import.meta.dirname}/dist/_worker.js/index.js`, { config: `${import.meta.dirname}/wrangler.toml`, + persistTo: path.join(tempDir, "worker"), }, ); const url = `${worker.proxyData.userWorkerUrl.protocol}//${worker.proxyData.userWorkerUrl.hostname}:${worker.proxyData.userWorkerUrl.port}`; workerUrl = url; - - githubOutputDir = await fs.mkdtemp( - path.join(os.tmpdir(), E2E_TEMP_DIR_PREFIX), - ); - githubOutputPath = path.join(githubOutputDir, "output"); + githubOutputPath = path.join(tempDir, "output"); await fs.writeFile(githubOutputPath, ""); await ezSpawn.async( @@ -74,8 +80,8 @@ beforeAll(async () => { afterAll(async () => { await server.ensureClose(); - if (githubOutputDir?.includes(E2E_TEMP_DIR_PREFIX)) { - await fs.rm(githubOutputDir, { recursive: true, force: true }); + if (tempDir?.includes(E2E_TEMP_DIR_PREFIX)) { + await fs.rm(tempDir, { recursive: true, force: true }); } }); @@ -123,6 +129,7 @@ describe.sequential.each([ GITHUB_SHA: payload.workflow_run.head_sha, GITHUB_ACTION: payload.workflow_run.id, GITHUB_JOB: payload.workflow_run.name, + GITHUB_EVENT_NAME: payload.workflow_run.event, GITHUB_REF_NAME: pr ? `${pr.payload.number}/merge` : payload.workflow_run.head_branch, @@ -147,14 +154,12 @@ describe.sequential.each([ expect(process.stderr).toContain("pkg-pr-new:"); expect(process.stderr).toContain("playground-a:"); expect(process.stderr).toContain("playground-b:"); - }, 10_000); + }, 20_000); it(`serves and installs playground-a for ${mode}`, async () => { const [owner, repo] = payload.repository.full_name.split("/"); - const { stdout: gitHeadSha } = await ezSpawn.async("git rev-parse HEAD", { - stdio: "overlapped", - }); - const sha = gitHeadSha.trim().substring(0, 7); + const fullSha = pr ? payload.workflow_run.head_sha : gitRevParse; + const sha = fullSha.substring(0, 7); const ref = pr?.payload.number ?? payload.workflow_run.head_branch; // Test download with SHA @@ -190,14 +195,12 @@ describe.sequential.each([ expect(installProcess.stdout).toContain( "playground-a installed successfully!", ); - }, 10_000); + }, 20_000); it(`serves and installs playground-b for ${mode}`, async () => { const [owner, repo] = payload.repository.full_name.split("/"); - const { stdout: gitHeadSha } = await ezSpawn.async("git rev-parse HEAD", { - stdio: "overlapped", - }); - const sha = gitHeadSha.trim().substring(0, 7); + const fullSha = pr ? payload.workflow_run.head_sha : gitRevParse; + const sha = fullSha.substring(0, 7); // Test download const response = await worker.fetch( @@ -225,7 +228,7 @@ describe.sequential.each([ expect(installProcess.stdout).toContain( "playground-b installed successfully!", ); - }, 10_000); + }, 20_000); }); describe("URL redirects", () => { diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 885f19c7..205e632c 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -233,13 +233,24 @@ const main = defineCommand({ process.exit(1); } - const { stdout: gitShaOutput } = await ezSpawn.async( - "git rev-parse HEAD", - { - stdio: "overlapped", - }, - ); - const sha = gitShaOutput.trim(); + let { sha } = await checkResponse.json(); + + // on pull_request events, GitHub creates a virtual merge commit that doesn't + // actually belong to the PR, so we use the workflow's head_sha instead + if (process.env.GITHUB_EVENT_NAME !== "pull_request") { + try { + const { stdout: gitRevParseOutput } = await ezSpawn.async( + "git rev-parse HEAD", + { stdio: "overlapped" }, + ); + + sha = gitRevParseOutput.trim(); + } catch { + // git rev-parse fails when the CLI runs outside a git repository + // (e.g. the checkout was done in a separate job and only artifacts were restored here) + // falling back to the workflow's head_sha from checkResponse + } + } const deps: Map = new Map(); // pkg.pr.new versions of the package const realDeps: Map | null = isPeerDepsEnabled