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

Show plan in PR comment #2

Merged
merged 10 commits into from
Jan 5, 2024
Merged
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
72 changes: 71 additions & 1 deletion .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Terraform

on:
push:
branches: [main]
pull_request:

jobs:
run:
Expand All @@ -12,6 +14,7 @@ jobs:
pull-requests: write
env:
TF_HTTP_PASSWORD: ${{ github.token }}
TF_IN_AUTOMATION: "true"
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -21,4 +24,71 @@ jobs:
terraform_version: "1.6.6"

- run: terraform init
- run: terraform plan

- run: terraform plan -out=tfplan
- run: terraform apply tfplan
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}

- run: terraform show -no-color tfplan > summary.txt
if: ${{ github.event_name == 'pull_request' }}
- name: Show plan on PR
uses: actions/github-script@v7
if: ${{ github.event_name == 'pull_request' }}
with:
github-token: ${{ github.token }}
script: |
const { repository: { pullRequest: { comments } } } = await github.graphql(`
query($owner:String!, $name:String!, $pr:Int!) {
repository(owner:$owner, name:$name) {
pullRequest(number:$pr) {
comments(last: 10) {
nodes {
id,
minimizedReason
author {
...on Bot {
login
}
}
}
}
}
}
}
`, {
owner: context.repo.owner,
name: context.repo.repo,
pr: context.issue.number,
})

const commentsToHide = comments.nodes.filter((comment) => {
return !comment.minimizedReason && comment.author.login == "github-actions"
})
console.log({ commentsToHide })
await github.graphql(`
mutation {
${commentsToHide.map((c,i) =>
`c${i}: minimizeComment(input: { subjectId: "${c.id}", classifier: OUTDATED }) {
clientMutationId
}
`
).join("")}
}
`)

const fs = require('fs').promises
const plan = await fs.readFile('summary.txt', 'utf-8')

const codefence = "```"
const body = `
🏗️ Terraform Plan
${codefence}
${plan.trim("\n")}
${codefence}`

github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
})