Skip to content

Commit bcd8b87

Browse files
authored
feat: Allow skipping comment when planfile is empty (#31)
1 parent 568abd7 commit bcd8b87

File tree

5 files changed

+122
-103
lines changed

5 files changed

+122
-103
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ inputs:
2323
description: The header to use for the pull request comment.
2424
required: true
2525
default: 📝 Terraform Plan
26+
skip-empty:
27+
description: Whether to skip posting a pull request comment when no changes need to be performed.
28+
required: true
29+
default: "false"
2630
runs:
2731
using: node20
2832
main: dist/index.js

dist/index.js

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

src/comment.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { GitHub } from '@actions/github/lib/utils'
22
import * as github from '@actions/github'
33
import type { PullRequestEvent } from '@octokit/webhooks-types'
4-
import type { RenderedPlan } from './render'
4+
import { planIsEmpty, type RenderedPlan } from './render'
55

66
function renderResources(resources: Record<string, string>): string {
77
let result = ''
@@ -13,12 +13,7 @@ function renderResources(resources: Record<string, string>): string {
1313
}
1414

1515
function renderBody(plan: RenderedPlan): string {
16-
if (
17-
!plan.createdResources &&
18-
!plan.recreatedResources &&
19-
!plan.updatedResources &&
20-
!plan.deletedResources
21-
) {
16+
if (planIsEmpty(plan)) {
2217
return '**→ No Resource Changes!**'
2318
}
2419

src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
33
import { createOrUpdateComment, renderComment } from './comment'
4-
import { renderPlan } from './render'
4+
import { planIsEmpty, renderPlan } from './render'
55

66
async function run() {
77
// 1) Setup
@@ -10,7 +10,8 @@ async function run() {
1010
planfile: core.getInput('planfile', { required: true }),
1111
terraformCmd: core.getInput('terraform-cmd', { required: true }),
1212
workingDirectory: core.getInput('working-directory', { required: true }),
13-
header: core.getInput('header', { required: true })
13+
header: core.getInput('header', { required: true }),
14+
skipEmpty: core.getBooleanInput('skip-empty', { required: true })
1415
}
1516
const octokit = github.getOctokit(inputs.token)
1617

@@ -24,10 +25,12 @@ async function run() {
2425
)
2526

2627
// 3) Post comment
27-
await core.group('Render comment', () => {
28-
const comment = renderComment({ plan, header: inputs.header })
29-
return createOrUpdateComment({ octokit, content: comment })
30-
})
28+
if (!inputs.skipEmpty || !planIsEmpty(plan)) {
29+
await core.group('Render comment', () => {
30+
const comment = renderComment({ plan, header: inputs.header })
31+
return createOrUpdateComment({ octokit, content: comment })
32+
})
33+
}
3134
}
3235

3336
async function main() {

src/render.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ export type RenderedPlan = {
99
deletedResources?: Record<string, string>
1010
}
1111

12+
export function planIsEmpty(plan: RenderedPlan): boolean {
13+
return (
14+
!plan.createdResources &&
15+
!plan.recreatedResources &&
16+
!plan.updatedResources &&
17+
!plan.deletedResources
18+
)
19+
}
20+
1221
type ResourceContent = {
1322
reason?: string
1423
lines: string[]

0 commit comments

Comments
 (0)