diff --git a/scripts/publish-check.sh b/scripts/publish-check.sh index 3a33b95..4fa4143 100644 --- a/scripts/publish-check.sh +++ b/scripts/publish-check.sh @@ -56,4 +56,10 @@ output: { # Annotations and the PR summary describe the tree the review actually reached, which a CI autofix can move past this head. summary: (if $here then ($review | summary_line($commit.conclusion)) else commit_summary($commit.conclusion) end), annotations: (if $here then [$review.annotations[:50][] | {path, start_line: .line, end_line: .line, annotation_level: .level, message}] else [] end) -}}' | bash "$GITHUB_ACTION_PATH/scripts/retry.sh" gh api repos/$GITHUB_REPOSITORY/check-runs/$CHECK_ID -X PATCH --input - +}}' | bash "$GITHUB_ACTION_PATH/scripts/retry.sh" gh api repos/$GITHUB_REPOSITORY/check-runs/$CHECK_ID -X PATCH --input - || { + # retry.sh never replays a write whose reply was lost, and it must not here: GitHub appends a + # patch's annotations to the ones already on the check run, so a resend would post every finding twice. + # The verdict is only unknown, not necessarily unapplied, so ask the check run itself. + landed=$(bash "$GITHUB_ACTION_PATH/scripts/retry.sh" gh api "repos/$GITHUB_REPOSITORY/check-runs/$CHECK_ID" --jq '.status == "completed"' { + for (const landed of [true, false]) { + const setup = fixture(); + executable(setup.dir, "git", "#!/bin/sh\nprintf 'head\\n'\n"); + executable(setup.dir, "sleep", "#!/bin/sh\nexit 0\n"); + executable(setup.dir, "gh", `#!/bin/sh +case "$*" in + *'/pulls/7 --jq .head.sha'*) printf 'head\\n' ;; + *'/check-runs/10 -X PATCH'*) jq -c . >> "$RUNNER_TEMP/patches.jsonl"; printf 'Patch "https://api.github.com/repos/owner/repo/check-runs/10": EOF\\n' >&2; exit 1 ;; + *'/check-runs/10 --jq'*) printf '%s\\n' '{"status":"${landed ? "completed" : "in_progress"}"}' | jq -r "$4" ;; + *) exit 1 ;; +esac +`); + writeFileSync(join(setup.dir, "review.json"), JSON.stringify({ summary: "Reviewed", commits: [{ sha: "head", conclusion: "success" }], annotations: [{ path: "a.ts", line: 1, level: "warning", message: "Check this" }] })); + const result = run("Publish check run", setup, { GH_TOKEN: "" }); + // A lost reply never resends: GitHub appends the annotations a second patch carries. + expect(result.exitCode).toBe(landed ? 0 : 1); + const patches = readFileSync(join(setup.dir, "patches.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line)); + expect(patches).toHaveLength(1); + expect(patches[0].output.annotations).toEqual([{ path: "a.ts", start_line: 1, end_line: 1, annotation_level: "warning", message: "Check this" }]); + } +}); + test("a hosted review closes its own check without publishing a verdict", () => { const setup = fixture(); executable(setup.dir, "git", "#!/bin/sh\nprintf 'head\\n'\n");