Skip to content

Commit

Permalink
feat: Always output step summary and respect skip logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludgy4 committed Dec 4, 2024
1 parent b265876 commit 8dff9d6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 51 deletions.
63 changes: 39 additions & 24 deletions dist/index.js

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

23 changes: 16 additions & 7 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import type { GitHub } from '@actions/github/lib/utils'
import * as github from '@actions/github'
import type { PullRequestEvent } from '@octokit/webhooks-types'
Expand All @@ -12,7 +13,7 @@ function renderResources(resources: Record<string, string>): string {
return result
}

export function renderBody(plan: RenderedPlan): string {
function renderBody(plan: RenderedPlan): string {
if (planIsEmpty(plan)) {
return '**→ No Resource Changes!**'
}
Expand Down Expand Up @@ -44,22 +45,30 @@ export function renderBody(plan: RenderedPlan): string {
return body
}

export function renderComment({
body,
export function renderMarkdown({
plan,
header,
includeFooter
}: {
body: string
plan: RenderedPlan
header: string
includeFooter?: boolean
}): string {
// Build body
const body = renderBody(plan)

// Build footer
let footer = ''
if (includeFooter === undefined || includeFooter === true) {
footer =
`\n\n---\n\n_Triggered by @${github.context.actor},` +
` Commit: \`${(github.context.payload as PullRequestEvent).pull_request.head.sha}\`_`
let commitInfo = ''
if ('pull_request' in github.context.payload) {
commitInfo = `, Commit: \`${(github.context.payload as PullRequestEvent).pull_request.head.sha}\``
}
footer = `\n\n---\n\n_Triggered by @${github.context.actor}${commitInfo}_`
}

core.debug(`footer: ${footer}`)

return `## ${header}\n\n${body}${footer}`
}

Expand Down
33 changes: 19 additions & 14 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, renderBody, renderComment } from './comment'
import { renderPlan } from './render'
import { createOrUpdateComment, renderMarkdown } from './comment'
import { planIsEmpty, renderPlan } from './render'

async function run() {
// 1) Setup
Expand All @@ -11,6 +11,7 @@ async function run() {
terraformCmd: core.getInput('terraform-cmd', { required: true }),
workingDirectory: core.getInput('working-directory', { required: true }),
header: core.getInput('header', { required: true }),
skipEmpty: core.getBooleanInput('skip-empty', { required: true }),
shouldComment: core.getInput('should-comment', { required: false })
}
const octokit = github.getOctokit(inputs.token)
Expand All @@ -24,19 +25,23 @@ async function run() {
})
)

// 3) Render the plan diff markdown and set it as output
const planMarkdown = await core.group('Rendering plan diff markdown', () => {
const markdown = renderBody(plan)
core.debug(`Outputting plan as markdown: ${markdown}`)
core.setOutput('plan-markdown', markdown)
return Promise.resolve(markdown)
})
if (!inputs.skipEmpty || !planIsEmpty(plan)) {
// 3) Render the plan diff markdown and set it as output
const planMarkdown = await core.group('Render plan diff markdown', () => {
const markdown = renderMarkdown({ plan, header: inputs.header })
core.setOutput('plan-markdown', markdown)
return Promise.resolve(markdown)
})
// 4) Post comment with markdown (if applicable)
if (inputs.shouldComment === 'true') {
await core.group('Render comment', () => {
return createOrUpdateComment({ octokit, content: planMarkdown })
})
}

// 4) Post comment with markdown (if applicable)
if (inputs.shouldComment === 'true') {
await core.group('Render comment', () => {
const comment = renderComment({ body: planMarkdown, header: inputs.header })
return createOrUpdateComment({ octokit, content: comment })
// 5) Add plan to GitHub step summary
await core.group('Adding plan to step summary', async () => {
await core.summary.addRaw(planMarkdown).write()
})
}
}
Expand Down
11 changes: 5 additions & 6 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs'
import { internalRenderPlan } from '../src/render'
import { parsePlanfileJSON } from '../src/planfile'
import { renderBody, renderComment } from '../src/comment'
import { renderMarkdown } from '../src/comment'

test.each(['basic/0-create', 'basic/1-modify', 'basic/2-delete', 'basic/3-empty'])(
'parse-successful',
Expand All @@ -10,18 +10,17 @@ test.each(['basic/0-create', 'basic/1-modify', 'basic/2-delete', 'basic/3-empty'
const planTxt = fs.readFileSync(`tests/fixtures/${arg}/plan.txt`, 'utf-8')
const planfile = parsePlanfileJSON(planJson)
const renderedPlan = internalRenderPlan(planfile, planTxt)
const renderedMarkdown = renderBody(renderedPlan)
const renderedComment = renderComment({
body: renderedMarkdown,
const renderedMarkdown = renderMarkdown({
plan: renderedPlan,
header: '📝 Terraform Plan',
includeFooter: false
})

if (process.env.GENERATE_FIXTURE === '1') {
fs.writeFileSync(`tests/fixtures/${arg}/rendered.md`, renderedComment)
fs.writeFileSync(`tests/fixtures/${arg}/rendered.md`, renderedMarkdown)
} else {
const expected = fs.readFileSync(`tests/fixtures/${arg}/rendered.md`, 'utf-8')
expect(renderedComment).toBe(expected)
expect(renderedMarkdown).toBe(expected)
}
}
)

0 comments on commit 8dff9d6

Please sign in to comment.