Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function renderReport(runs, verdict, { failOn = [], parameters, notes = [
else facts.push(verdict.success ? '**Build passes**' : '**Build fails**');
facts.push(`fail-on: ${failOn.length ? failOn.join(', ') : 'none'}`);
for (const [k, v] of Object.entries(parameters ?? {})) facts.push(`${k}: ${code(v)}`);
if (env.GITHUB_SHA) facts.push(`commit ${code(env.GITHUB_SHA.slice(0, 7))}`);
const sha = commitSha(env);
if (sha) facts.push(`commit ${code(sha.slice(0, 7))}`);
const runUrl = workflowRunUrl(env);
if (runUrl) facts.push(`[workflow run](${runUrl})`);
lines.push(facts.join(' · '), '');
Expand Down Expand Up @@ -114,9 +115,18 @@ export async function upsertPrComment(markdown, env, { token, key, fetchImpl = f
}

function pullRequestNumber(env) {
return pullRequest(env)?.number;
}

// On pull requests GITHUB_SHA is a temporary merge commit; show the PR's head instead.
function commitSha(env) {
return pullRequest(env)?.head?.sha ?? env.GITHUB_SHA;
}

function pullRequest(env) {
if (!['pull_request', 'pull_request_target'].includes(env.GITHUB_EVENT_NAME) || !env.GITHUB_EVENT_PATH) return undefined;
try {
return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8')).pull_request?.number;
return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8')).pull_request;
} catch {
return undefined;
}
Expand Down
8 changes: 8 additions & 0 deletions test/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ test('updates the existing comment for the same key, across pages', async () =>
assert.deepEqual(gh.calls.at(-1), { method: 'PATCH', path: '/repos/acme/web/issues/comments/777', body: { body: '<!-- applitools-autonomous-report:qa -->\nnew' } });
});

test('shows the PR head commit, not the merge commit', () => {
const e = prEnv();
writeFileSync(e.GITHUB_EVENT_PATH, JSON.stringify({ pull_request: { number: 9, head: { sha: '2b523ef0000' } } }));
const md = renderReport([], { status: 'skipped' }, { env: { ...e, GITHUB_SHA: 'b366fb20000' } });
assert.ok(md.includes('commit `2b523ef`'), md);
assert.ok(renderReport([], { status: 'skipped' }, { env: { GITHUB_SHA: 'b366fb20000' } }).includes('commit `b366fb2`'));
});

test('does nothing outside pull requests', async () => {
const gh = fakeGitHub();
assert.equal(await upsertPrComment('x', { ...prEnv(), GITHUB_EVENT_NAME: 'push' }, { token: 't', key: 'k', fetchImpl: gh.fetchImpl }), undefined);
Expand Down
Loading