Comment on the pull request #257
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Comment on the pull request | |
# NOTE: have access to secrets and artifacts. | |
on: | |
workflow_run: | |
workflows: ["terraform-plan"] | |
types: | |
- completed | |
jobs: | |
upload: | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
steps: | |
- uses: dawidd6/action-download-artifact@v2 | |
with: | |
workflow: terraform.yml | |
name: pr | |
- name: Comment on PR with Terraform artifacts | |
uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
// 1. Retrieve existing bot comments for the PR | |
const { data: comments } = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
}) | |
const botComment = comments.find(comment => { | |
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style') | |
}) | |
// 2. Prepare format of the comment | |
const output = `#### Terraform Initialization ⚙️\`${ fs.readFileSync('./pr/terraform/init_outcome') }\` | |
#### Terraform Validation 🤖\`${ fs.readFileSync('./pr/terraform/validate_outcome') }\` | |
<details><summary>Validation Output</summary> | |
\`\`\`\n | |
${ fs.readFileSync('./pr/terraform/validate_output') } | |
\`\`\` | |
</details> | |
#### Terraform Plan 📖\`${ fs.readFileSync('./pr/terraform/plan_outcome') }\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${ fs.readFileSync('./pr/terraform/plan_output') } | |
\`\`\` | |
</details> | |
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; | |
// 3. If we have a comment, update it, otherwise create a new one | |
if (botComment) { | |
github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: output | |
}) | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
} |