Skip to content

Commit dc4ecf5

Browse files
authored
Remove old preview site comments on new comment (microsoft#3104)
1 parent 3415bd5 commit dc4ecf5

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

.github/workflows/close-preview.yml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ on:
99
pull_request_target:
1010
types: [closed]
1111

12+
concurrency:
13+
group: preview-site
14+
1215
jobs:
1316
close:
1417
runs-on: ubuntu-latest

.github/workflows/deploy-preview.yml

+27-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ permissions:
1717
pull-requests: write
1818
actions: read
1919

20+
concurrency:
21+
group: preview-site
22+
2023
jobs:
2124
deploy:
2225
if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
@@ -128,26 +131,45 @@ jobs:
128131
uses: Azure/static-web-apps-deploy@v1
129132
with:
130133
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PREVIEW }}
131-
repo_token: ${{ secrets.GITHUB_TOKEN }}
134+
# Unsetting the repo token so we can control commenting ourselves.
135+
# repo_token: ${{ secrets.GITHUB_TOKEN }}
132136
action: "upload"
133137
app_location: "site"
134138
skip_app_build: true
135139
production_branch: v2
136140
deployment_environment: ${{ fromJson(steps.get-info.outputs.result).pr }}
137141

138142
- name: Comment on PR
139-
# azure/static-web-apps-deploy seems to comment itself when the event is pull_request_target
140-
if: ${{ steps.get-info.outputs.result != 'null' && github.event_name != 'pull_request_target' }}
143+
if: ${{ steps.get-info.outputs.result != 'null' }}
141144
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
142145
env:
143146
PR_NUMBER: ${{ fromJson(steps.get-info.outputs.result).pr }}
144147
SITE_URL: ${{ steps.deploy.outputs.static_web_app_url }}
145148
with:
146149
github-token: ${{ secrets.GITHUB_TOKEN }}
147150
script: |
151+
const prefix = "Azure Static Web Apps: Your stage site is ready!";
152+
const comments = await github.paginate(github.rest.issues.listComments, {
153+
owner: context.repo.owner,
154+
repo: context.repo.repo,
155+
issue_number: +process.env.PR_NUMBER,
156+
per_page: 100,
157+
});
158+
159+
for (const comment of comments) {
160+
if (comment.user?.login === "github-actions[bot]" && comment.body?.startsWith(prefix)) {
161+
console.log(`Deleting comment ${comment.id}`);
162+
await github.rest.issues.deleteComment({
163+
owner: context.repo.owner,
164+
repo: context.repo.repo,
165+
comment_id: comment.id,
166+
});
167+
}
168+
}
169+
148170
await github.rest.issues.createComment({
149171
owner: context.repo.owner,
150172
repo: context.repo.repo,
151-
issue_number: process.env.PR_NUMBER,
152-
body: `Azure Static Web Apps: Your stage site is ready! Visit it here: ${process.env.SITE_URL}`
173+
issue_number: +process.env.PR_NUMBER,
174+
body: `${prefix} Visit it here: ${process.env.SITE_URL}`
153175
});

0 commit comments

Comments
 (0)