Skip to content

Commit c155d5f

Browse files
magnhasMagnus Hård af Segerstadborchero
authored
feat: Add variable to expand comment details automatically (#62)
Co-authored-by: Magnus Hård af Segerstad <magnus.hard-afsegerstad@regionstockholm> Co-authored-by: Oliver Borchert <[email protected]>
1 parent 15d000d commit c155d5f

File tree

6 files changed

+61
-27
lines changed

6 files changed

+61
-27
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ GitHub Action to post the output of `terraform plan` to a pull request comment.
7171
# Pull request number to post the comment to (Optional)
7272
# Useful for workflow_dispatch triggers where PR context is not automatically available
7373
pr-number: ""
74+
75+
# Expand the PR comment details
76+
expand-comment: false
7477
```
7578
7679
### `token` (required)
@@ -108,7 +111,8 @@ multiple Terraform runs: each sticky pull request comment is identified by its h
108111

109112
Whether to skip posting a pull request comment when no changes need to be performed. Defaults to `false`.
110113

111-
When enabled and the plan is empty, any existing comment from a previous commit will be automatically deleted to avoid showing outdated information.
114+
When enabled and the plan is empty, any existing comment from a previous commit will be automatically deleted to avoid
115+
showing outdated information.
112116

113117
### `skip-comment`
114118

@@ -123,6 +127,11 @@ the PR number from the event context (available for `pull_request` and `pull_req
123127
This parameter is particularly useful for `workflow_dispatch` triggers or other non-PR events where you want to post a
124128
comment to a specific pull request. You can use a previous step to find the PR number associated with your branch.
125129

130+
### `expand-comment`
131+
132+
Whether to expand the comment details. When enabled, the detail points in the pull request comment are expanded by
133+
default.
134+
126135
## Outputs
127136

128137
This action provides the following output:

action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ inputs:
3232
required: true
3333
default: "false"
3434
pr-number:
35-
description: The pull request number to post the comment to. When not provided, the action will attempt to automatically determine the PR number from the event context. This is useful for workflow_dispatch triggers.
35+
description:
36+
The pull request number to post the comment to. When not provided, the action will attempt to automatically
37+
determine the PR number from the event context. This is useful for workflow_dispatch triggers.
3638
required: false
3739
default: ""
40+
expand-comment:
41+
description: Whether to expand the comment details by default.
42+
required: true
43+
default: "false"
3844
outputs:
3945
markdown:
4046
description: The raw markdown output of the terraform plan

dist/index.js

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import * as github from '@actions/github'
33
import type { PullRequestEvent } from '@octokit/webhooks-types'
44
import { planIsEmpty, type RenderedPlan } from './render'
55

6-
function renderResources(resources: Record<string, string>): string {
6+
function renderResources(
7+
resources: Record<string, string>,
8+
options: { expandDetails: boolean }
9+
): string {
710
let result = ''
811
for (const key of Object.keys(resources).sort()) {
912
const content = resources[key]
10-
result += `\n\n<details><summary><code>${key}</code></summary>\n\n${content}\n\n</details>`
13+
const openAttr = options.expandDetails ? ' open' : ''
14+
result += `\n\n<details${openAttr}><summary><code>${key}</code></summary>\n\n${content}\n\n</details>`
1115
}
1216
return result
1317
}
1418

15-
function renderBody(plan: RenderedPlan): string {
19+
function renderBody(plan: RenderedPlan, options: { expandDetails: boolean }): string {
1620
if (planIsEmpty(plan)) {
1721
return '**→ No Resource Changes!**'
1822
}
@@ -27,23 +31,23 @@ function renderBody(plan: RenderedPlan): string {
2731

2832
if (plan.createdResources) {
2933
body += '\n\n### ✨ Create'
30-
body += renderResources(plan.createdResources)
34+
body += renderResources(plan.createdResources, options)
3135
}
3236
if (plan.updatedResources) {
3337
body += '\n\n### ♻️ Update'
34-
body += renderResources(plan.updatedResources)
38+
body += renderResources(plan.updatedResources, options)
3539
}
3640
if (plan.recreatedResources) {
3741
body += '\n\n### ⚙️ Re-Create'
38-
body += renderResources(plan.recreatedResources)
42+
body += renderResources(plan.recreatedResources, options)
3943
}
4044
if (plan.deletedResources) {
4145
body += '\n\n### 🗑️ Delete'
42-
body += renderResources(plan.deletedResources)
46+
body += renderResources(plan.deletedResources, options)
4347
}
4448
if (plan.ephemeralResources) {
4549
body += '\n\n### 👻 Ephemeral'
46-
body += renderResources(plan.ephemeralResources)
50+
body += renderResources(plan.ephemeralResources, options)
4751
}
4852

4953
return body
@@ -52,14 +56,16 @@ function renderBody(plan: RenderedPlan): string {
5256
export function renderMarkdown({
5357
plan,
5458
header,
55-
includeFooter
59+
includeFooter,
60+
expandDetails
5661
}: {
5762
plan: RenderedPlan
5863
header: string
5964
includeFooter?: boolean
65+
expandDetails: boolean
6066
}): string {
6167
// Build body
62-
const body = renderBody(plan)
68+
const body = renderBody(plan, { expandDetails })
6369

6470
// Build footer
6571
let footer = ''

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ async function run() {
1414
header: core.getInput('header', { required: true }),
1515
skipEmpty: core.getBooleanInput('skip-empty', { required: true }),
1616
skipComment: core.getBooleanInput('skip-comment', { required: true }),
17-
prNumber: prNumberInput && prNumberInput !== '' ? parseInt(prNumberInput, 10) : undefined
17+
prNumber: prNumberInput && prNumberInput !== '' ? parseInt(prNumberInput, 10) : undefined,
18+
expandComment: core.getBooleanInput('expand-comment', { required: true })
1819
}
1920
const octokit = github.getOctokit(inputs.token)
2021

@@ -29,7 +30,11 @@ async function run() {
2930

3031
// 3) Render the plan diff markdown and set it as output
3132
const planMarkdown = await core.group('Render plan diff markdown', async () => {
32-
const markdown = renderMarkdown({ plan, header: inputs.header })
33+
const markdown = renderMarkdown({
34+
plan,
35+
header: inputs.header,
36+
expandDetails: inputs.expandComment
37+
})
3338
core.setOutput('markdown', markdown)
3439
core.setOutput('empty', planIsEmpty(plan))
3540
return markdown

tests/e2e.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ test.each([
1818
const renderedMarkdown = renderMarkdown({
1919
plan: renderedPlan,
2020
header: '📝 Terraform Plan',
21-
includeFooter: false
21+
includeFooter: false,
22+
expandDetails: false
2223
})
2324

2425
if (process.env.GENERATE_FIXTURE === '1') {

0 commit comments

Comments
 (0)