Skip to content

Commit de55ef0

Browse files
committed
Split preview comment into workflow_run to fix Dependabot write token restriction
GitHub hard-limits the GITHUB_TOKEN to read-only for Dependabot-triggered pull_request events — the permissions block cannot override this. The fix is to move the PR comment step into a separate workflow triggered by workflow_run, which always runs in the base branch context with a full write token regardless of who opened the PR. - preview.yml: remove comment step; upload PR number as a short-lived artifact - preview-comment.yml: new workflow triggered after Preview completes; downloads the artifact and posts/updates the Surge preview URL comment https://claude.ai/code/session_018BsvAZMR1V4p6qRAvtk82P
1 parent 91119b9 commit de55ef0

2 files changed

Lines changed: 59 additions & 32 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Preview Comment
2+
3+
on:
4+
workflow_run:
5+
workflows: [Preview]
6+
types: [completed]
7+
8+
jobs:
9+
post-comment:
10+
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'
11+
runs-on: ubuntu-latest
12+
permissions:
13+
issues: write
14+
pull-requests: write
15+
actions: read
16+
steps:
17+
- uses: actions/download-artifact@v7
18+
with:
19+
name: pr-number
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
21+
run-id: ${{ github.event.workflow_run.id }}
22+
- name: Post preview URL comment
23+
uses: actions/github-script@v7
24+
with:
25+
script: |
26+
const fs = require('fs');
27+
const prNumber = parseInt(fs.readFileSync('pr-number.txt', 'utf8').trim());
28+
const url = `https://petrsvihlik-pr-${prNumber}.surge.sh`;
29+
const marker = '<!-- surge-preview -->';
30+
const body = `${marker}\n🚀 **Preview:** [${url}](${url})`;
31+
32+
const comments = await github.rest.issues.listComments({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
issue_number: prNumber,
36+
});
37+
38+
const existing = comments.data.find(c => c.body.includes(marker));
39+
if (existing) {
40+
await github.rest.issues.updateComment({
41+
owner: context.repo.owner,
42+
repo: context.repo.repo,
43+
comment_id: existing.id,
44+
body,
45+
});
46+
} else {
47+
await github.rest.issues.createComment({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
issue_number: prNumber,
51+
body,
52+
});
53+
}

.github/workflows/preview.yml

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ jobs:
99
deploy-preview:
1010
if: github.event.action != 'closed'
1111
runs-on: ubuntu-latest
12-
permissions:
13-
issues: write
14-
pull-requests: write
1512
steps:
1613
- uses: actions/checkout@v6
1714
- uses: actions/setup-dotnet@v5
@@ -32,36 +29,13 @@ jobs:
3229
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }}
3330
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
3431
run: surge ./output petrsvihlik-pr-${{ github.event.pull_request.number }}.surge.sh
35-
- name: Post preview URL comment
36-
uses: actions/github-script@v7
32+
- name: Save PR number
33+
run: echo "${{ github.event.pull_request.number }}" > pr-number.txt
34+
- uses: actions/upload-artifact@v7
3735
with:
38-
script: |
39-
const url = 'https://petrsvihlik-pr-${{ github.event.pull_request.number }}.surge.sh';
40-
const marker = '<!-- surge-preview -->';
41-
const body = `${marker}\n🚀 **Preview:** [${url}](${url})`;
42-
43-
const comments = await github.rest.issues.listComments({
44-
owner: context.repo.owner,
45-
repo: context.repo.repo,
46-
issue_number: context.issue.number,
47-
});
48-
49-
const existing = comments.data.find(c => c.body.includes(marker));
50-
if (existing) {
51-
await github.rest.issues.updateComment({
52-
owner: context.repo.owner,
53-
repo: context.repo.repo,
54-
comment_id: existing.id,
55-
body,
56-
});
57-
} else {
58-
await github.rest.issues.createComment({
59-
owner: context.repo.owner,
60-
repo: context.repo.repo,
61-
issue_number: context.issue.number,
62-
body,
63-
});
64-
}
36+
name: pr-number
37+
path: pr-number.txt
38+
retention-days: 1
6539

6640
teardown-preview:
6741
if: github.event.action == 'closed'

0 commit comments

Comments
 (0)