Skip to content

Commit 2db4543

Browse files
committed
feat: add removing older bot pr comments
1 parent 086147b commit 2db4543

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

.github/workflows/terraform.yaml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ jobs:
5151
with:
5252
github-token: ${{ secrets.GITHUB_TOKEN }}
5353
script: |
54-
const output = `#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
54+
// 1. Retrieve existing bot comments for the PR
55+
const { data: comments } = await github.rest.issues.listComments({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
issue_number: context.issue.number,
59+
});
60+
const botComment = comments.find(comment => {
61+
return comment.user.type === 'Bot' && comment.body.includes('<!-- GitHub Actions Terraform PR comment bot -->')
62+
});
63+
64+
// 2. Put together bot new comment contents for the PR
65+
const output = `<!-- GitHub Actions Terraform PR comment bot -->
66+
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
67+
5568
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
5669
5770
<details><summary>Show Plan</summary>
@@ -63,12 +76,22 @@ jobs:
6376
</details>
6477
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
6578
79+
// 3. Delete previous comment for the PR
80+
if (botComment) {
81+
github.rest.issues.deleteComment({
82+
owner: context.repo.owner,
83+
repo: context.repo.repo,
84+
comment_id: botComment.id,
85+
});
86+
}
87+
88+
// 4. Create a new comment
6689
github.rest.issues.createComment({
67-
issue_number: context.issue.number,
6890
owner: context.repo.owner,
6991
repo: context.repo.repo,
92+
issue_number: context.issue.number,
7093
body: output
71-
})
94+
});
7295
7396
- name: Terraform Plan Status
7497
if: steps.plan.outcome == 'failure'

0 commit comments

Comments
 (0)