Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Replace id input with more general header parameter #11

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ exclude: ^dist/
repos:
- repo: local
hooks:
- id: build
name: build
entry: pnpm build
language: system
pass_filenames: false
always_run: true
- id: test:fixtures
name: test:fixtures
entry: pnpm test:fixtures
Expand All @@ -20,6 +14,12 @@ repos:
language: system
pass_filenames: false
types: [file, ts]
- id: build
name: build
entry: pnpm build
language: system
pass_filenames: false
always_run: true
- id: prettier
name: prettier
entry: pnpm prettier
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The directory where the Terraform binary ought to be called. Defaults to `$GITHU
> [!IMPORTANT]
> `planfile` must be specified relative to the working directory.

### `id`
### `header`

A custom identifier for the Terraform execution. This allows to distinguish multiple Terraform runs: each sticky pull
request comment is tied to an ID.
The header that is used for the pull request comment posted by this action. Changing the default allows to distinguish
multiple Terraform runs: each sticky pull request comment is identified by its header.
7 changes: 4 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ inputs:
description: The directory where the Terraform binary should be called.
required: true
default: "."
id:
description: A unique ID for the Terraform deployment.
required: false
header:
description: The header to use for the pull request comment.
required: true
default: 📝 Terraform Plan
runs:
using: node20
main: dist/index.js
12 changes: 4 additions & 8 deletions dist/index.js

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

12 changes: 3 additions & 9 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ function renderBody(plan: RenderedPlan): string {

export function renderComment({
plan,
id,
header,
includeFooter
}: {
plan: RenderedPlan
id?: string
header: string
includeFooter?: boolean
}): string {
// Build header
let header = '## 📝 Terraform Deployment'
if ((id?.length ?? 0) > 0) {
header += ` - \`${id}\``
}

// Build body
const body = renderBody(plan)

Expand All @@ -75,7 +69,7 @@ export function renderComment({
` Commit: \`${(github.context.payload as PullRequestEvent).pull_request.head.sha}\`_`
}

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

export async function createOrUpdateComment({
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function run() {
planfile: core.getInput('planfile', { required: true }),
terraformCmd: core.getInput('terraform-cmd', { required: true }),
workingDirectory: core.getInput('working-directory', { required: true }),
id: core.getInput('id')
header: core.getInput('header', { required: true })
}
const octokit = github.getOctokit(inputs.token)

Expand All @@ -25,7 +25,7 @@ async function run() {

// 3) Post comment
await core.group('Render comment', () => {
const comment = renderComment({ plan, id: inputs.id })
const comment = renderComment({ plan, header: inputs.header })
return createOrUpdateComment({ octokit, content: comment })
})
}
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ test.each(['basic/0-create', 'basic/1-modify', 'basic/2-delete'])('parse-success
const planTxt = fs.readFileSync(`tests/fixtures/${arg}/plan.txt`, 'utf-8')
const planfile = parsePlanfileJSON(planJson)
const renderedPlan = internalRenderPlan(planfile, planTxt)
const renderedComment = renderComment({ plan: renderedPlan, includeFooter: false })
const renderedComment = renderComment({
plan: renderedPlan,
header: '📝 Terraform Plan',
includeFooter: false
})

if (process.env.GENERATE_FIXTURE === '1') {
fs.writeFileSync(`tests/fixtures/${arg}/rendered.md`, renderedComment)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/basic/0-create/rendered.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 📝 Terraform Deployment
## 📝 Terraform Plan

**→ Resource Changes: 1 to create, 0 to update, 0 to re-create, 0 to delete.**

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/basic/1-modify/rendered.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 📝 Terraform Deployment
## 📝 Terraform Plan

**→ Resource Changes: 0 to create, 0 to update, 1 to re-create, 0 to delete.**

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/basic/2-delete/rendered.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 📝 Terraform Deployment
## 📝 Terraform Plan

**→ Resource Changes: 0 to create, 0 to update, 0 to re-create, 1 to delete.**

Expand Down