Skip to content

Commit

Permalink
feat: Allow skipping comment when planfile is empty (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
borchero authored Dec 3, 2024
1 parent 568abd7 commit bcd8b87
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 103 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
description: The header to use for the pull request comment.
required: true
default: 📝 Terraform Plan
skip-empty:
description: Whether to skip posting a pull request comment when no changes need to be performed.
required: true
default: "false"
runs:
using: node20
main: dist/index.js
188 changes: 98 additions & 90 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GitHub } from '@actions/github/lib/utils'
import * as github from '@actions/github'
import type { PullRequestEvent } from '@octokit/webhooks-types'
import type { RenderedPlan } from './render'
import { planIsEmpty, type RenderedPlan } from './render'

function renderResources(resources: Record<string, string>): string {
let result = ''
Expand All @@ -13,12 +13,7 @@ function renderResources(resources: Record<string, string>): string {
}

function renderBody(plan: RenderedPlan): string {
if (
!plan.createdResources &&
!plan.recreatedResources &&
!plan.updatedResources &&
!plan.deletedResources
) {
if (planIsEmpty(plan)) {
return '**→ No Resource Changes!**'
}

Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { createOrUpdateComment, renderComment } from './comment'
import { renderPlan } from './render'
import { planIsEmpty, renderPlan } from './render'

async function run() {
// 1) Setup
Expand All @@ -10,7 +10,8 @@ async function run() {
planfile: core.getInput('planfile', { required: true }),
terraformCmd: core.getInput('terraform-cmd', { required: true }),
workingDirectory: core.getInput('working-directory', { required: true }),
header: core.getInput('header', { required: true })
header: core.getInput('header', { required: true }),
skipEmpty: core.getBooleanInput('skip-empty', { required: true })
}
const octokit = github.getOctokit(inputs.token)

Expand All @@ -24,10 +25,12 @@ async function run() {
)

// 3) Post comment
await core.group('Render comment', () => {
const comment = renderComment({ plan, header: inputs.header })
return createOrUpdateComment({ octokit, content: comment })
})
if (!inputs.skipEmpty || !planIsEmpty(plan)) {
await core.group('Render comment', () => {
const comment = renderComment({ plan, header: inputs.header })
return createOrUpdateComment({ octokit, content: comment })
})
}
}

async function main() {
Expand Down
9 changes: 9 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export type RenderedPlan = {
deletedResources?: Record<string, string>
}

export function planIsEmpty(plan: RenderedPlan): boolean {
return (
!plan.createdResources &&
!plan.recreatedResources &&
!plan.updatedResources &&
!plan.deletedResources
)
}

type ResourceContent = {
reason?: string
lines: string[]
Expand Down

0 comments on commit bcd8b87

Please sign in to comment.