Skip to content

Commit dd11119

Browse files
fix(cli): improve SHA retrieval logic
1 parent a911ee3 commit dd11119

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

packages/app/e2e.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ 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 () => {
2229
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), E2E_TEMP_DIR_PREFIX));
2330

@@ -122,6 +129,7 @@ describe.sequential.each([
122129
GITHUB_SHA: payload.workflow_run.head_sha,
123130
GITHUB_ACTION: payload.workflow_run.id,
124131
GITHUB_JOB: payload.workflow_run.name,
132+
GITHUB_EVENT_NAME: payload.workflow_run.event,
125133
GITHUB_REF_NAME: pr
126134
? `${pr.payload.number}/merge`
127135
: payload.workflow_run.head_branch,
@@ -150,10 +158,8 @@ describe.sequential.each([
150158

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

159165
// Test download with SHA
@@ -193,10 +199,8 @@ describe.sequential.each([
193199

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

201205
// Test download
202206
const response = await worker.fetch(

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)