From d8a2f6c71453f7b3ed398c316ce2dd34b5a0d8c2 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sat, 5 Dec 2026 06:10:47 -0500 Subject: [PATCH 1/2] feat: take down the plan comment when every unit is unchanged A pull request whose plan changes nothing got a comment saying so on every push, which is noise a reviewer has to scroll past, and skipping the post alone would leave an earlier push's stale "changes" comment standing. Signed-off-by: Yordis Prieto --- actions/sticky-comment/README.md | 11 ++- actions/sticky-comment/action.yml | 5 +- actions/sticky-comment/lib/index.mjs | 38 ++++++--- actions/terragrunt/report/README.md | 6 ++ actions/terragrunt/report/action.yml | 5 ++ actions/terragrunt/report/lib/index.mjs | 22 +++++- actions/terragrunt/report/lib/report.mjs | 6 +- tests/node/sticky-comment/index.test.mjs | 42 ++++++++++ tests/node/terragrunt/report/index.test.mjs | 81 ++++++++++++++++++++ tests/node/terragrunt/report/report.test.mjs | 11 +++ 10 files changed, 209 insertions(+), 18 deletions(-) diff --git a/actions/sticky-comment/README.md b/actions/sticky-comment/README.md index c77824a..1389a1e 100644 --- a/actions/sticky-comment/README.md +++ b/actions/sticky-comment/README.md @@ -26,7 +26,7 @@ jobs: - if: ${{ !cancelled() && github.event_name == 'pull_request' }} uses: TrogonStack/github-actions/actions/sticky-comment@ # vX.Y.Z with: - path: ${{ steps.plan.outputs.report-path }} + path: ${{ steps.plan.outputs.comment-path }} marker: terraform-plan ``` @@ -34,6 +34,11 @@ jobs: empty path or a missing file skips the step instead of failing it, because the step that writes it may have failed before it got that far. +An empty file deletes the comment an earlier run left under the marker, and +posts nothing. `terragrunt/report` writes one to `comment-path` when every unit +is unchanged, so a pull request whose plan used to change something, and no +longer does, keeps no comment saying it still does. + ### Several comments on one pull request The marker is the identity of the comment. Give each workflow or job that @@ -44,7 +49,7 @@ second replaces the first. | Input | Default | Description | | --- | --- | --- | -| `path` | required | File holding the comment body, posted as it stands. | +| `path` | required | File holding the comment body, posted as it stands. An empty file deletes the earlier comment instead. | | `marker` | required | Identity of the comment, unique within the pull request. Letters and digits, joined by single dots, dashes or underscores. | | `token` | `github.token` | Reads and writes the comments. Needs `pull-requests: write`. | @@ -52,7 +57,7 @@ second replaces the first. | Output | Description | | --- | --- | -| `comment-id` | ID of the comment created or updated. Empty when the step skipped. | +| `comment-id` | ID of the comment created or updated. Empty when the step skipped or deleted it. | ## Fixed behaviour diff --git a/actions/sticky-comment/action.yml b/actions/sticky-comment/action.yml index b20fa95..39735fd 100644 --- a/actions/sticky-comment/action.yml +++ b/actions/sticky-comment/action.yml @@ -9,7 +9,8 @@ inputs: description: >- File holding the comment body, posted as it stands. An empty path or a missing file skips the step rather than failing it, so it can run after a - step that failed before writing one. + step that failed before writing one. An empty file deletes the comment + an earlier run posted under the marker, because there is nothing to say. required: true marker: description: >- @@ -24,7 +25,7 @@ inputs: outputs: comment-id: - description: ID of the comment created or updated. Empty when the step skipped. + description: ID of the comment created or updated. Empty when the step skipped or deleted it. runs: using: node24 diff --git a/actions/sticky-comment/lib/index.mjs b/actions/sticky-comment/lib/index.mjs index 5f5cab0..b1358b6 100644 --- a/actions/sticky-comment/lib/index.mjs +++ b/actions/sticky-comment/lib/index.mjs @@ -141,6 +141,21 @@ export async function post({ client, owner, repo, number, marker, body }) { return { action: "created", id: created.id }; } +// An empty file says there is nothing to report, so the comment an earlier run +// left is wrong now and goes. Not hidden: a hidden comment still claims what it +// said, one click away. +export async function remove({ client, owner, repo, number, marker }) { + const { viewer, comments } = await listComments({ client, owner, repo, number }); + const existing = findExisting({ comments, viewer, marker }); + + if (!existing) { + return { action: "skipped", reason: "Nothing to post, and no earlier comment to take down." }; + } + + await client.request("DELETE", `/repos/${owner}/${repo}/issues/comments/${existing.databaseId}`); + return { action: "deleted", id: existing.databaseId }; +} + export async function run({ inputs, event, env, fetch }) { const marker = markerOf(inputs.marker); @@ -160,14 +175,6 @@ export async function run({ inputs, event, env, fetch }) { } const [owner, repo] = env.GITHUB_REPOSITORY.split("/"); - const serverUrl = env.GITHUB_SERVER_URL || "https://github.com"; - const body = renderBody({ - marker, - content: fs.readFileSync(inputs.path, "utf8"), - runUrl: `${serverUrl}/${owner}/${repo}/actions/runs/${env.GITHUB_RUN_ID}`, - sha: event?.pull_request?.head?.sha ?? env.GITHUB_SHA, - }); - const client = createClient({ token: inputs.token, apiUrl: env.GITHUB_API_URL || "https://api.github.com", @@ -175,6 +182,19 @@ export async function run({ inputs, event, env, fetch }) { fetch, }); + const content = fs.readFileSync(inputs.path, "utf8"); + if (content.trim() === "") { + return remove({ client, owner, repo, number, marker }); + } + + const serverUrl = env.GITHUB_SERVER_URL || "https://github.com"; + const body = renderBody({ + marker, + content, + runUrl: `${serverUrl}/${owner}/${repo}/actions/runs/${env.GITHUB_RUN_ID}`, + sha: event?.pull_request?.head?.sha ?? env.GITHUB_SHA, + }); + return post({ client, owner, repo, number, marker, body }); } @@ -200,7 +220,7 @@ export async function main() { } info(`Comment ${result.id} ${result.action}.`); - setOutput("comment-id", String(result.id)); + if (result.action !== "deleted") setOutput("comment-id", String(result.id)); } catch (thrown) { setFailed(thrown.message); } diff --git a/actions/terragrunt/report/README.md b/actions/terragrunt/report/README.md index 94531f6..6d57e25 100644 --- a/actions/terragrunt/report/README.md +++ b/actions/terragrunt/report/README.md @@ -89,6 +89,7 @@ overwrites the first. | Output | Description | | --- | --- | | `report-path` | The file the report was written to. Empty when rendering failed. | +| `comment-path` | The report as a pull request comment, next to `report-path`: the same markdown, or an empty file when every unit is unchanged. Empty when rendering failed. | | `exit-code` | The command's exit status. | ## Fixed behaviour @@ -112,6 +113,11 @@ These are not inputs, on purpose. - A unit that left its provider as it found it is a row in a collapsed `Unchanged` table, not a diff. It is still listed, because a unit that converged and a unit missing from the run are different things. +- A run in which every unit is unchanged has nothing to review, so its + `comment-path` file is empty. `sticky-comment` reads an empty file as "take + down the earlier comment". `report-path` and the job summary still hold the + whole report. A failed command, a changed unit or a run with no unit is + never quiet. - A failed unit shows tofu's own diagnostics, falling back to Terragrunt's error records when tofu wrote nothing. A unit that failed only because a dependency did has a row and nothing else, and the line above the table says diff --git a/actions/terragrunt/report/action.yml b/actions/terragrunt/report/action.yml index 9c610ff..b7a2734 100644 --- a/actions/terragrunt/report/action.yml +++ b/actions/terragrunt/report/action.yml @@ -45,6 +45,11 @@ inputs: outputs: report-path: description: The file the report was written to, ready to post as a comment. + comment-path: + description: >- + The report as a pull request comment: the same markdown, or an empty file + when every unit is unchanged, so `sticky-comment` takes down the comment + an earlier push left instead of posting one with nothing to review. exit-code: description: The command's exit status. The step fails with it when it is not zero. diff --git a/actions/terragrunt/report/lib/index.mjs b/actions/terragrunt/report/lib/index.mjs index 548a6e5..e24f1db 100644 --- a/actions/terragrunt/report/lib/index.mjs +++ b/actions/terragrunt/report/lib/index.mjs @@ -10,6 +10,12 @@ import { render, streamLine } from "./report.mjs"; export const DEFAULT_REPORT_NAME = "terragrunt-report.md"; +// Next to the report, so two runs in one job with a report path each also get +// a comment path each. +export function commentPathOf(reportPath) { + return `${reportPath.replace(/\.md$/, "")}.comment.md`; +} + // Thrown for anything a caller can fix by editing their workflow, so main can // report it as a GitHub error annotation rather than a stack trace. export class InputError extends Error {} @@ -86,11 +92,13 @@ export async function run({ }) { const lines = []; let streamError; + const commentPath = commentPathOf(reportPath); // Gone before anything runs, so the path never holds a report from an earlier // run in the job. A render that fails returns no path, but a later step // reading the default path directly would otherwise post the old report. fs.rmSync(reportPath, { force: true }); + fs.rmSync(commentPath, { force: true }); // `exec 2>&1` inside the shell rather than two pipes merged here, because two // pipes are read in whatever order they become ready, and a diagnostic would @@ -154,7 +162,7 @@ export async function run({ report = render({ lines, root, title, preamble }); } catch (error) { warning(`The run finished with status ${exitCode} but rendering its report did not: ${error.message}`); - return { exitCode: exitCode === 0 ? 1 : exitCode, reportPath: "" }; + return { exitCode: exitCode === 0 ? 1 : exitCode, reportPath: "", commentPath: "" }; } if (report.records === 0) { @@ -165,6 +173,13 @@ export async function run({ fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, report.markdown, "utf8"); + // Empty rather than absent when there is nothing to review, so a comment + // step can tell "take down the comment an earlier push left" from "the step + // that writes this never got that far". The report itself stays whole, + // because the job summary and anything parsing the file still want every + // unit. A failed command is never quiet, whatever the log says. + const quiet = exitCode === 0 && report.unchanged; + fs.writeFileSync(commentPath, quiet ? "" : report.markdown, "utf8"); if (summary) { if (process.env.GITHUB_STEP_SUMMARY) { @@ -174,13 +189,14 @@ export async function run({ } } - return { exitCode, reportPath }; + return { exitCode, reportPath, commentPath }; } export async function main() { try { - const { exitCode, reportPath } = await run(readInputs()); + const { exitCode, reportPath, commentPath } = await run(readInputs()); setOutput("report-path", reportPath); + setOutput("comment-path", commentPath); setOutput("exit-code", String(exitCode)); if (exitCode !== 0) { // Not `setFailed`: the command has already said what went wrong, in the diff --git a/actions/terragrunt/report/lib/report.mjs b/actions/terragrunt/report/lib/report.mjs index b17dc83..f7eb0b4 100644 --- a/actions/terragrunt/report/lib/report.mjs +++ b/actions/terragrunt/report/lib/report.mjs @@ -340,5 +340,9 @@ export function render({ lines, root, title, preamble = "" }) { out += "\n"; } - return { markdown: out, records: records.length }; + // Nothing to review: every unit ran and left its provider as it found it. + // A failure, a change or a run with no unit at all is never unchanged. + const unchanged = !failed && rows === "" && quiet !== ""; + + return { markdown: out, records: records.length, unchanged }; } diff --git a/tests/node/sticky-comment/index.test.mjs b/tests/node/sticky-comment/index.test.mjs index 5478a21..03966ba 100644 --- a/tests/node/sticky-comment/index.test.mjs +++ b/tests/node/sticky-comment/index.test.mjs @@ -253,6 +253,48 @@ test("an empty path skips without calling GitHub", async () => { assert.equal(github.calls.length, 0); }); +test("an empty file deletes the comment an earlier run left", async () => { + const github = fakeGitHub({ pages: [[comment({ databaseId: 5 })]] }); + const result = await run({ + inputs: { path: reportFile(""), marker: "plan", token: "t" }, + event: EVENT, + env: ENV, + fetch: github.fetch, + }); + + assert.deepEqual(result, { action: "deleted", id: 5 }); + const [write, ...rest] = writes(github.calls); + assert.equal(rest.length, 0); + assert.equal(write.method, "DELETE"); + assert.equal(write.url, "https://api.example.test/repos/acme/widgets/issues/comments/5"); +}); + +test("an empty file with no earlier comment posts nothing", async () => { + const github = fakeGitHub({ pages: [[comment({ body: "\nold" })]] }); + const result = await run({ + inputs: { path: reportFile("\n"), marker: "plan", token: "t" }, + event: EVENT, + env: ENV, + fetch: github.fetch, + }); + + assert.equal(result.action, "skipped"); + assert.equal(writes(github.calls).length, 0); +}); + +test("an empty file never deletes a comment another account wrote", async () => { + const github = fakeGitHub({ pages: [[comment({ author: { login: "someone" } })]] }); + const result = await run({ + inputs: { path: reportFile(""), marker: "plan", token: "t" }, + event: EVENT, + env: ENV, + fetch: github.fetch, + }); + + assert.equal(result.action, "skipped"); + assert.equal(writes(github.calls).length, 0); +}); + test("a refused write names the status", async () => { const fetch = async () => new Response("Resource not accessible by integration", { status: 403 }); await assert.rejects( diff --git a/tests/node/terragrunt/report/index.test.mjs b/tests/node/terragrunt/report/index.test.mjs index f2e0b89..b1d3dbe 100644 --- a/tests/node/terragrunt/report/index.test.mjs +++ b/tests/node/terragrunt/report/index.test.mjs @@ -8,6 +8,7 @@ import { DEFAULT_REPORT_NAME, InputError, childEnv, + commentPathOf, readInputs, run, } from "../../../../actions/terragrunt/report/lib/index.mjs"; @@ -244,3 +245,83 @@ test("run forwards a cancellation to the command's process group", async () => { assert.ok(fs.existsSync(marker), "the grandchild never saw the interrupt"); assert.notEqual(result.exitCode, 0); }); + +test("commentPathOf sits next to the report", () => { + assert.equal(commentPathOf("/tmp/plan.md"), "/tmp/plan.comment.md"); + assert.equal(commentPathOf("/tmp/plan"), "/tmp/plan.comment.md"); +}); + +test("run leaves the comment empty and the report whole when every unit is unchanged", async () => { + const dir = scratch(); + const reportPath = path.join(dir, "plan.md"); + + const result = await capture({ + command: replay(dir, "no-changes"), + title: "Plan", + workingDirectory: dir, + root: ROOT, + reportPath, + summary: false, + env: { ...process.env, LOG: path.join(dir, "no-changes.jsonl") }, + }); + + assert.equal(result.exitCode, 0); + assert.equal(result.commentPath, path.join(dir, "plan.comment.md")); + assert.equal(fs.readFileSync(result.commentPath, "utf8"), ""); + assert.match(fs.readFileSync(reportPath, "utf8"), /Every unit matches its configuration/); +}); + +test("run comments the whole report when a unit changes", async () => { + const dir = scratch(); + const reportPath = path.join(dir, "plan.md"); + + const result = await capture({ + command: replay(dir, "changes"), + title: "Plan", + workingDirectory: dir, + root: ROOT, + reportPath, + summary: false, + env: { ...process.env, LOG: path.join(dir, "changes.jsonl") }, + }); + + assert.equal(fs.readFileSync(result.commentPath, "utf8"), fs.readFileSync(reportPath, "utf8")); +}); + +test("run comments the whole report when the command fails, even if every unit is unchanged", async () => { + const dir = scratch(); + const reportPath = path.join(dir, "plan.md"); + + const result = await capture({ + command: `${replay(dir, "no-changes")}; exit 1`, + title: "Plan", + workingDirectory: dir, + root: ROOT, + reportPath, + summary: false, + env: { ...process.env, LOG: path.join(dir, "no-changes.jsonl") }, + }); + + assert.equal(result.exitCode, 1); + assert.equal(fs.readFileSync(result.commentPath, "utf8"), fs.readFileSync(reportPath, "utf8")); +}); + +test("run removes a comment an earlier run left before the command starts", async () => { + const dir = scratch(); + const reportPath = path.join(dir, "plan.md"); + const commentPath = commentPathOf(reportPath); + fs.writeFileSync(commentPath, "stale"); + + const result = await capture({ + command: 'test ! -e "$COMMENT"', + title: "Plan", + workingDirectory: dir, + root: ROOT, + reportPath, + summary: false, + env: { ...process.env, COMMENT: commentPath }, + }); + + assert.equal(result.exitCode, 0); + assert.doesNotMatch(fs.readFileSync(commentPath, "utf8"), /stale/); +}); diff --git a/tests/node/terragrunt/report/report.test.mjs b/tests/node/terragrunt/report/report.test.mjs index fb8b428..88932e9 100644 --- a/tests/node/terragrunt/report/report.test.mjs +++ b/tests/node/terragrunt/report/report.test.mjs @@ -254,3 +254,14 @@ test("a malformed record does not take the report down", () => { test("the preamble sits under the title", () => { assert.match(report("no-changes", { preamble: "Dev account." }), /^### Plan\n\nDev account\.\n\nEvery unit/); }); + +test("a run where every unit is unchanged is unchanged", () => { + assert.equal(render({ lines: FIXTURES["no-changes"], root: ROOT, title: "Plan" }).unchanged, true); + assert.equal(render({ lines: FIXTURES.apply.slice(4), root: ROOT, title: "Apply" }).unchanged, true); +}); + +test("a change, a failure or a run with no unit is not unchanged", () => { + for (const name of ["changes", "outputs-only", "failed", "error-only", "root-failure", "not-terragrunt"]) { + assert.equal(render({ lines: FIXTURES[name], root: ROOT, title: "Plan" }).unchanged, false, name); + } +}); From 4866fb3c2a86bd6fdeb3cb3556d45b76e211f09f Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sat, 5 Dec 2026 06:43:52 -0500 Subject: [PATCH 2/2] fix: keep the plan comment unless the run proves nothing changed An output-only apply, a unit with no recognisable outcome, or a duplicate comment could each leave a reviewer with a deleted or stale comment that no longer matched the run. Signed-off-by: Yordis Prieto --- actions/sticky-comment/README.md | 7 ++++--- actions/sticky-comment/action.yml | 5 +++-- actions/sticky-comment/lib/index.mjs | 20 ++++++++++++++------ actions/terragrunt/report/lib/index.mjs | 6 +++--- actions/terragrunt/report/lib/report.mjs | 9 ++++++++- tests/node/sticky-comment/index.test.mjs | 18 +++++++++++++++++- tests/node/terragrunt/report/index.test.mjs | 6 +++--- tests/node/terragrunt/report/report.test.mjs | 15 +++++++++++++++ 8 files changed, 67 insertions(+), 19 deletions(-) diff --git a/actions/sticky-comment/README.md b/actions/sticky-comment/README.md index 1389a1e..e13de25 100644 --- a/actions/sticky-comment/README.md +++ b/actions/sticky-comment/README.md @@ -34,8 +34,9 @@ jobs: empty path or a missing file skips the step instead of failing it, because the step that writes it may have failed before it got that far. -An empty file deletes the comment an earlier run left under the marker, and -posts nothing. `terragrunt/report` writes one to `comment-path` when every unit +An empty or whitespace-only file deletes every comment this token left under +the marker, and posts nothing. A hidden comment, or one another account wrote, +is left alone. `terragrunt/report` writes one to `comment-path` when every unit is unchanged, so a pull request whose plan used to change something, and no longer does, keeps no comment saying it still does. @@ -49,7 +50,7 @@ second replaces the first. | Input | Default | Description | | --- | --- | --- | -| `path` | required | File holding the comment body, posted as it stands. An empty file deletes the earlier comment instead. | +| `path` | required | File holding the comment body, posted as it stands. An empty or whitespace-only file deletes the earlier comment instead. | | `marker` | required | Identity of the comment, unique within the pull request. Letters and digits, joined by single dots, dashes or underscores. | | `token` | `github.token` | Reads and writes the comments. Needs `pull-requests: write`. | diff --git a/actions/sticky-comment/action.yml b/actions/sticky-comment/action.yml index 39735fd..0254098 100644 --- a/actions/sticky-comment/action.yml +++ b/actions/sticky-comment/action.yml @@ -9,8 +9,9 @@ inputs: description: >- File holding the comment body, posted as it stands. An empty path or a missing file skips the step rather than failing it, so it can run after a - step that failed before writing one. An empty file deletes the comment - an earlier run posted under the marker, because there is nothing to say. + step that failed before writing one. An empty or whitespace-only file + deletes every comment this token posted earlier under the marker, + because there is nothing to say. required: true marker: description: >- diff --git a/actions/sticky-comment/lib/index.mjs b/actions/sticky-comment/lib/index.mjs index b1358b6..924d54d 100644 --- a/actions/sticky-comment/lib/index.mjs +++ b/actions/sticky-comment/lib/index.mjs @@ -143,17 +143,20 @@ export async function post({ client, owner, repo, number, marker, body }) { // An empty file says there is nothing to report, so the comment an earlier run // left is wrong now and goes. Not hidden: a hidden comment still claims what it -// said, one click away. +// said, one click away. Every match goes, not only the first, so a duplicate +// two concurrent runs posted does not survive to say something stale. export async function remove({ client, owner, repo, number, marker }) { const { viewer, comments } = await listComments({ client, owner, repo, number }); - const existing = findExisting({ comments, viewer, marker }); + const existing = comments.filter((comment) => findExisting({ comments: [comment], viewer, marker })); - if (!existing) { + if (existing.length === 0) { return { action: "skipped", reason: "Nothing to post, and no earlier comment to take down." }; } - await client.request("DELETE", `/repos/${owner}/${repo}/issues/comments/${existing.databaseId}`); - return { action: "deleted", id: existing.databaseId }; + for (const comment of existing) { + await client.request("DELETE", `/repos/${owner}/${repo}/issues/comments/${comment.databaseId}`); + } + return { action: "deleted", ids: existing.map((comment) => comment.databaseId) }; } export async function run({ inputs, event, env, fetch }) { @@ -219,8 +222,13 @@ export async function main() { return; } + if (result.action === "deleted") { + info(`Nothing to post; deleted comment ${result.ids.join(", ")}.`); + return; + } + info(`Comment ${result.id} ${result.action}.`); - if (result.action !== "deleted") setOutput("comment-id", String(result.id)); + setOutput("comment-id", String(result.id)); } catch (thrown) { setFailed(thrown.message); } diff --git a/actions/terragrunt/report/lib/index.mjs b/actions/terragrunt/report/lib/index.mjs index e24f1db..47574e9 100644 --- a/actions/terragrunt/report/lib/index.mjs +++ b/actions/terragrunt/report/lib/index.mjs @@ -10,10 +10,10 @@ import { render, streamLine } from "./report.mjs"; export const DEFAULT_REPORT_NAME = "terragrunt-report.md"; -// Next to the report, so two runs in one job with a report path each also get -// a comment path each. +// Next to the report and named after all of it, so two runs in one job with +// a report path each also get a comment path each, even `plan` and `plan.md`. export function commentPathOf(reportPath) { - return `${reportPath.replace(/\.md$/, "")}.comment.md`; + return `${reportPath}.comment.md`; } // Thrown for anything a caller can fix by editing their workflow, so main can diff --git a/actions/terragrunt/report/lib/report.mjs b/actions/terragrunt/report/lib/report.mjs index f7eb0b4..cd1e598 100644 --- a/actions/terragrunt/report/lib/report.mjs +++ b/actions/terragrunt/report/lib/report.mjs @@ -164,6 +164,9 @@ export function verdict(body) { if (line.startsWith("Apply complete! Resources: ")) { line = line.slice("Apply complete! Resources: ".length); } + // An apply that touched no resource but changed an output still changed + // something, and calling it a no-op would hide it. + if (isNoop(line) && body.includes("Changes to Outputs")) return "outputs only"; return line; } if (body.includes("Changes to Outputs")) return "outputs only"; @@ -241,6 +244,9 @@ export function render({ lines, root, title, preamble = "" }) { let spent = 0; let budgetHit = false; let failed = false; + // A unit whose output named no outcome at all may have converged or may + // not; it still sits in the quiet table, but it never makes the run quiet. + let unknown = false; for (const unit of unitsOf(records, root)) { // The two streams are trimmed apart and joined afterwards rather than @@ -271,6 +277,7 @@ export function render({ lines, root, title, preamble = "" }) { // unit missing from the report and a unit that converged are not the same // thing, and a reviewer cannot tell them apart. if (isNoop(unitVerdict)) { + if (unitVerdict === "no output") unknown = true; quiet += `| \`${unit}\` | ${unitVerdict} |\n`; continue; } @@ -342,7 +349,7 @@ export function render({ lines, root, title, preamble = "" }) { // Nothing to review: every unit ran and left its provider as it found it. // A failure, a change or a run with no unit at all is never unchanged. - const unchanged = !failed && rows === "" && quiet !== ""; + const unchanged = !failed && !unknown && rows === "" && quiet !== ""; return { markdown: out, records: records.length, unchanged }; } diff --git a/tests/node/sticky-comment/index.test.mjs b/tests/node/sticky-comment/index.test.mjs index 03966ba..3bef77e 100644 --- a/tests/node/sticky-comment/index.test.mjs +++ b/tests/node/sticky-comment/index.test.mjs @@ -262,13 +262,29 @@ test("an empty file deletes the comment an earlier run left", async () => { fetch: github.fetch, }); - assert.deepEqual(result, { action: "deleted", id: 5 }); + assert.deepEqual(result, { action: "deleted", ids: [5] }); const [write, ...rest] = writes(github.calls); assert.equal(rest.length, 0); assert.equal(write.method, "DELETE"); assert.equal(write.url, "https://api.example.test/repos/acme/widgets/issues/comments/5"); }); +test("an empty file deletes every duplicate under the marker", async () => { + const github = fakeGitHub({ pages: [[comment({ databaseId: 5 }), comment({ databaseId: 6 })]] }); + const result = await run({ + inputs: { path: reportFile(""), marker: "plan", token: "t" }, + event: EVENT, + env: ENV, + fetch: github.fetch, + }); + + assert.deepEqual(result, { action: "deleted", ids: [5, 6] }); + assert.deepEqual( + writes(github.calls).map((call) => [call.method, call.url.split("/").at(-1)]), + [["DELETE", "5"], ["DELETE", "6"]], + ); +}); + test("an empty file with no earlier comment posts nothing", async () => { const github = fakeGitHub({ pages: [[comment({ body: "\nold" })]] }); const result = await run({ diff --git a/tests/node/terragrunt/report/index.test.mjs b/tests/node/terragrunt/report/index.test.mjs index b1d3dbe..32a37fb 100644 --- a/tests/node/terragrunt/report/index.test.mjs +++ b/tests/node/terragrunt/report/index.test.mjs @@ -247,8 +247,8 @@ test("run forwards a cancellation to the command's process group", async () => { }); test("commentPathOf sits next to the report", () => { - assert.equal(commentPathOf("/tmp/plan.md"), "/tmp/plan.comment.md"); - assert.equal(commentPathOf("/tmp/plan"), "/tmp/plan.comment.md"); + assert.equal(commentPathOf("/tmp/plan.md"), "/tmp/plan.md.comment.md"); + assert.notEqual(commentPathOf("/tmp/plan"), commentPathOf("/tmp/plan.md")); }); test("run leaves the comment empty and the report whole when every unit is unchanged", async () => { @@ -266,7 +266,7 @@ test("run leaves the comment empty and the report whole when every unit is uncha }); assert.equal(result.exitCode, 0); - assert.equal(result.commentPath, path.join(dir, "plan.comment.md")); + assert.equal(result.commentPath, path.join(dir, "plan.md.comment.md")); assert.equal(fs.readFileSync(result.commentPath, "utf8"), ""); assert.match(fs.readFileSync(reportPath, "utf8"), /Every unit matches its configuration/); }); diff --git a/tests/node/terragrunt/report/report.test.mjs b/tests/node/terragrunt/report/report.test.mjs index 88932e9..5ca9b24 100644 --- a/tests/node/terragrunt/report/report.test.mjs +++ b/tests/node/terragrunt/report/report.test.mjs @@ -265,3 +265,18 @@ test("a change, a failure or a run with no unit is not unchanged", () => { assert.equal(render({ lines: FIXTURES[name], root: ROOT, title: "Plan" }).unchanged, false, name); } }); + +test("an apply that changed only outputs is not a no-op", () => { + const body = 'Changes to Outputs:\n + endpoint = "x"\n\nApply complete! Resources: 0 added, 0 changed, 0 destroyed.\n'; + assert.equal(verdict(body), "outputs only"); +}); + +test("a unit with no recognisable outcome never makes the run unchanged", () => { + const lines = [ + ...FIXTURES["no-changes"], + JSON.stringify({ level: "stdout", "working-dir": `${ROOT}/mystery`, msg: "something tofu said\n" }), + ]; + const report = render({ lines, root: ROOT, title: "Plan" }); + assert.match(report.markdown, /\| `mystery` \| no output \|/); + assert.equal(report.unchanged, false); +});