From 0fab1bb4353cf8cabb7af72c5c20ac0f5c8f7a3c Mon Sep 17 00:00:00 2001 From: Tobias Svane Mikkelsen Date: Mon, 2 Nov 2026 15:10:06 +0100 Subject: [PATCH] fix(terraform): count imports in the plan summary and list destroys above the fold The summary regex never matched 'Plan: N to import, ...', so any plan with imports fell back to 'Changes planned' and hid its destroy count. Also lists destroyed/replaced addresses outside the collapsed plan and drops refresh noise from the commented plan so less of it gets truncated. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/terraform-stack.yml | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/.github/workflows/terraform-stack.yml b/.github/workflows/terraform-stack.yml index 062eab8f..81e8b36d 100644 --- a/.github/workflows/terraform-stack.yml +++ b/.github/workflows/terraform-stack.yml @@ -207,37 +207,45 @@ jobs: ? `> [!WARNING]\n> This stack has an **unresolved failed apply** (#${stuck.number}). The plan below may include leftover changes from it โ€” check before merging.\n\n` : '' - // Mirrors "Plan: N to add, N to change, N to destroy." with a - // colored, at-a-glance version โ€” easier to scan across a PR - // with several stacks than the plain sentence alone. + // Mirrors "Plan: [N to import, ]N to add, N to change, N to destroy." + // with a colored, at-a-glance version, read from the full plan so it + // survives the comment truncation below. let summary + let destroyed = [] if (process.env.PLAN_FAILED === 'true') { summary = 'โŒ Plan failed' } else if (process.env.HAS_CHANGES !== 'true') { summary = '๐Ÿ”ต No changes' } else { - const counts = plan.match(/^Plan: (\d+) to add, (\d+) to change, (\d+) to destroy\.$/m) + const counts = plan.match(/^Plan: (?:(\d+) to import, )?(\d+) to add, (\d+) to change, (\d+) to destroy\.$/m) if (counts) { - const [toAdd, toChange, toDestroy] = counts.slice(1).map(Number) + const [toImport, toAdd, toChange, toDestroy] = counts.slice(1).map(n => Number(n ?? 0)) summary = [ + toImport > 0 && `๐Ÿ”ต **${toImport} to import**`, toAdd > 0 && `๐ŸŸข **${toAdd} to add**`, toChange > 0 && `๐ŸŸก **${toChange} to change**`, toDestroy > 0 && `๐Ÿ”ด **${toDestroy} to destroy**`, - ].filter(Boolean).join(' ยท ') + ].filter(Boolean).join(' ยท ') || 'Changes planned' } else { summary = 'Changes planned' } + destroyed = [...plan.matchAll(/^\s+# (.+?) (will be destroyed|must be replaced)$/gm)] + .map(([, addr, what]) => `- \`${addr}\`${what === 'must be replaced' ? ' (replaced)' : ''}`) } + const shown = plan.split('\n') + .filter(l => !/: (Refreshing state|Reading|Preparing import)\.\.\.|: Read complete after /.test(l)) + .join('\n') const LIMIT = 60000 const body = [ ``, banner + `### Terraform plan โ€” \`${stack}\``, '', `**${summary}**`, '', + ...(destroyed.length ? ['> [!CAUTION]', '> **Destroyed or replaced:**', ...destroyed.map(d => `> ${d}`), ''] : []), '
Show plan', '', '```terraform', - plan.length > LIMIT - ? plan.slice(0, LIMIT) + `\n\n... truncated (${plan.length} chars); see the run log.` - : plan, + shown.length > LIMIT + ? shown.slice(0, LIMIT) + `\n\n... truncated (${shown.length} chars); see the run log.` + : shown, '```', '
', ].join('\n')