Skip to content

Commit bb9db1c

Browse files
authored
feat(cleanup): add PR comment update feature (#4)
Add ability to update the deploy PR comment when cleaning up, showing that the deployment was removed. This completes the deployment lifecycle in the PR conversation. New inputs: - update-pr-comment: Enable PR comment updating (default: false) - comment-header: Header to find the original comment New output: - pr-comment-updated: Whether the comment was updated The comment changes from: "## 🚀 Preview Deployment" → "## 🧹 Preview Deployment (Cleaned Up)" And shows the URL as strikethrough to indicate it's no longer available.
1 parent 1bae5ff commit bb9db1c

3 files changed

Lines changed: 93 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Automatically post/update a comment on PRs with the deployment URL
1313
- New inputs: `comment-on-pr`, `github-token`, `comment-header`
1414
- Upsert behavior: updates existing comment instead of creating duplicates
15+
- **cleanup** action: PR comment update feature
16+
- Update the deploy PR comment to show cleanup status when PR is closed
17+
- New inputs: `update-pr-comment`, `comment-header`
18+
- New output: `pr-comment-updated`
19+
- Changes comment header from "🚀 Preview Deployment" to "🧹 Preview Deployment (Cleaned Up)"
1520
- CI/CD pipeline with ShellCheck, actionlint, and yamllint
1621
- Branch protection and governance files (CODEOWNERS, issue templates, PR template)
1722
- CONTRIBUTING.md with development guidelines

cleanup/README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Removes a ZAD deployment and optionally cleans up associated GitHub resources (e
1818
| `github-token` | No | `''` | GitHub token for deployment and container deletion (`deployments: write`, `packages: delete`) |
1919
| `github-admin-token` | No | `''` | GitHub token for environment deletion (needs repo admin permission) |
2020
| `api-base-url` | No | `https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api` | ZAD Operations Manager API base URL |
21+
| `update-pr-comment` | No | `false` | Update the deploy PR comment to show cleanup status |
22+
| `comment-header` | No | `## 🚀 Preview Deployment` | Header of the deploy comment to find and update |
2123

2224
## Outputs
2325

@@ -27,6 +29,7 @@ Removes a ZAD deployment and optionally cleans up associated GitHub resources (e
2729
| `github-env-deleted` | Whether the GitHub environment was deleted (`true`/`false`) |
2830
| `github-deployments-deleted` | Whether GitHub deployments were deleted (`true`/`false`) |
2931
| `container-deleted` | Whether the container image was deleted (`true`/`false`) |
32+
| `pr-comment-updated` | Whether the PR comment was updated (`true`/`false`) |
3033

3134
## Example Usage
3235

@@ -69,6 +72,7 @@ cleanup-preview:
6972
permissions:
7073
deployments: write
7174
packages: write
75+
pull-requests: write # For update-pr-comment
7276
steps:
7377
- name: Cleanup PR preview
7478
uses: RijksICTGilde/zad-actions/cleanup@v1
@@ -84,16 +88,40 @@ cleanup-preview:
8488
container-tag: pr-${{ github.event.number }}
8589
github-token: ${{ secrets.GITHUB_TOKEN }}
8690
github-admin-token: ${{ secrets.GITHUB_ADMIN_TOKEN }}
91+
update-pr-comment: true
8792
```
8893
94+
### PR Comment Update
95+
96+
When used with the deploy action's `comment-on-pr` feature, the cleanup action can update the PR comment to show that the deployment was cleaned up:
97+
98+
**Before cleanup:**
99+
> ## 🚀 Preview Deployment
100+
>
101+
> Your changes have been deployed to a preview environment:
102+
>
103+
> **URL:** https://web-pr123-my-project.rig...
104+
>
105+
> This deployment will be automatically cleaned up when the PR is closed.
106+
107+
**After cleanup:**
108+
> ## 🧹 Preview Deployment (Cleaned Up)
109+
>
110+
> ~~https://editor-pr123-my-project.rig...~~
111+
>
112+
> This deployment was automatically cleaned up when the PR was closed.
113+
114+
To enable this, add `update-pr-comment: true` to your cleanup step. The action will find the existing deploy comment by its header and update it.
115+
89116
## Permissions
90117

91118
### Required Workflow Permissions
92119

93120
```yaml
94121
permissions:
95-
deployments: write # For delete-github-deployments
96-
packages: write # For delete-container
122+
deployments: write # For delete-github-deployments
123+
packages: write # For delete-container
124+
pull-requests: write # For update-pr-comment
97125
```
98126

99127
### Token Requirements
@@ -104,6 +132,7 @@ permissions:
104132
| Delete GitHub deployments | `github-token` | `deployments: write` |
105133
| Delete GitHub environment | `github-admin-token` | Repository admin access |
106134
| Delete container image | `github-token` | `packages: delete` |
135+
| Update PR comment | `github-token` | `pull-requests: write` |
107136

108137
**Note:** The default `GITHUB_TOKEN` cannot delete GitHub environments. You need a Personal Access Token (PAT) or GitHub App token with admin permissions for the repository.
109138

@@ -186,6 +215,7 @@ Check cleanup results and take action:
186215
2. **Delete GitHub Deployments** (optional): Marks all deployments for the environment as inactive, then deletes them
187216
3. **Delete GitHub Environment** (optional): Deletes the GitHub environment
188217
4. **Delete Container Image** (optional): Finds and deletes the container version with the specified tag
218+
5. **Update PR Comment** (optional): Updates the deploy comment to show cleanup status
189219

190220
Each step runs independently and won't fail the action if it fails (cleanup is best-effort). Check the outputs to see what was actually deleted.
191221

cleanup/action.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ inputs:
5353
description: 'ZAD Operations Manager API base URL'
5454
required: false
5555
default: 'https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api'
56+
update-pr-comment:
57+
description: 'Update the deploy PR comment to show cleanup status (requires github-token with pull-requests:write)'
58+
required: false
59+
default: 'false'
60+
comment-header:
61+
description: 'Header of the deploy comment to find and update (default: "## 🚀 Preview Deployment")'
62+
required: false
63+
default: '## 🚀 Preview Deployment'
5664

5765
outputs:
5866
zad-deleted:
@@ -67,6 +75,9 @@ outputs:
6775
container-deleted:
6876
description: 'Whether the container image was deleted'
6977
value: ${{ steps.delete-container.outputs.deleted }}
78+
pr-comment-updated:
79+
description: 'Whether the PR comment was updated'
80+
value: ${{ steps.update-pr-comment.outputs.updated }}
7081

7182
runs:
7283
using: 'composite'
@@ -261,3 +272,48 @@ runs:
261272
done
262273
263274
echo "deleted=$DELETED" >> "$GITHUB_OUTPUT"
275+
276+
- name: Update PR Comment
277+
id: update-pr-comment
278+
if: inputs.update-pr-comment == 'true' && inputs.github-token != '' && github.event_name == 'pull_request'
279+
shell: bash
280+
env:
281+
GH_TOKEN: ${{ inputs.github-token }}
282+
COMMENT_HEADER: ${{ inputs.comment-header }}
283+
PR_NUMBER: ${{ github.event.pull_request.number }}
284+
GITHUB_REPOSITORY: ${{ github.repository }}
285+
DEPLOYMENT_NAME: ${{ inputs.deployment-name }}
286+
PROJECT_ID: ${{ inputs.project-id }}
287+
run: |
288+
# Build the updated comment body
289+
COMMENT_BODY="## 🧹 Preview Deployment (Cleaned Up)
290+
291+
~~https://editor-${DEPLOYMENT_NAME}-${PROJECT_ID}.rig.prd1.gn2.quattro.rijksapps.nl~~
292+
293+
This deployment was automatically cleaned up when the PR was closed."
294+
295+
# Find existing comment by header
296+
EXISTING_COMMENT=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
297+
--jq ".[] | select(.body | startswith(\"${COMMENT_HEADER}\")) | {id: .id, body: .body}" \
298+
2>/dev/null | head -n1 || echo "")
299+
300+
if [ -z "$EXISTING_COMMENT" ]; then
301+
echo "No existing deploy comment found with header: ${COMMENT_HEADER}"
302+
echo "updated=false" >> "$GITHUB_OUTPUT"
303+
exit 0
304+
fi
305+
306+
COMMENT_ID=$(echo "$EXISTING_COMMENT" | jq -r '.id')
307+
308+
if [ -n "$COMMENT_ID" ] && [ "$COMMENT_ID" != "null" ]; then
309+
echo "Updating PR comment (ID: $COMMENT_ID)"
310+
gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" \
311+
-X PATCH \
312+
-f body="$COMMENT_BODY" \
313+
--silent
314+
echo "PR comment updated to show cleanup status"
315+
echo "updated=true" >> "$GITHUB_OUTPUT"
316+
else
317+
echo "Could not find comment ID"
318+
echo "updated=false" >> "$GITHUB_OUTPUT"
319+
fi

0 commit comments

Comments
 (0)