@@ -10,12 +10,15 @@ jobs:
1010 if : ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
1111 runs-on : ubuntu-latest
1212 permissions :
13- issues : write
13+ actions : read
14+ contents : read
1415 pull-requests : write
1516
1617 steps :
1718 - name : Check out repository
1819 uses : actions/checkout@v6
20+ with :
21+ persist-credentials : false
1922
2023 - name : Download artifact
2124 uses : dawidd6/action-download-artifact@v20
@@ -25,48 +28,67 @@ jobs:
2528 path : docs-preview
2629 name : docs-preview
2730
28- - name : Set PR number
29- run : echo "PR_NUMBER=$(cat docs-preview/.pr_number)" >> $GITHUB_ENV
31+ - name : Validate and set PR number
32+ run : |
33+ PR_NUMBER=$(cat docs-preview/.pr_number)
34+ if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
35+ echo "Invalid PR number: $PR_NUMBER"
36+ exit 1
37+ fi
38+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
39+
40+ - name : Generate GitHub App Token
41+ id : generate-token
42+ uses : actions/create-github-app-token@v3
43+ with :
44+ client-id : ${{ secrets.CLIENT_ID }}
45+ private-key : ${{ secrets.CLIENT_PRIVATE_KEY }}
46+ owner : litestar-org
47+ repositories : advanced-alchemy-docs-preview
48+ permission-contents : write
3049
3150 - name : Deploy docs preview
3251 uses : JamesIves/github-pages-deploy-action@v4
3352 with :
3453 folder : docs-preview/docs/_build/html
35- token : ${{ secrets.DOCS_PREVIEW_DEPLOY_TOKEN }}
54+ token : ${{ steps.generate-token.outputs.token }}
3655 repository-name : litestar-org/advanced-alchemy-docs-preview
3756 clean : false
3857 target-folder : ${{ env.PR_NUMBER }}
3958 branch : gh-pages
4059
41- - uses : actions/github-script@v8
60+ - uses : actions/github-script@v9
4261 env :
4362 PR_NUMBER : ${{ env.PR_NUMBER }}
4463 with :
4564 script : |
46- const issue_number = process.env.PR_NUMBER
47- const body = "Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/" + issue_number
65+ const issue_number = parseInt(process.env.PR_NUMBER, 10)
66+ const previewUrl = "https://litestar-org.github.io/advanced-alchemy-docs-preview/" + issue_number
67+ const marker = "<!-- docs-preview -->"
68+ const section = marker + `\n\n<hr>\n📚 Documentation preview: <a href="${previewUrl}">${previewUrl}</a> \n`
4869
49- const opts = github.rest.issues.listComments.endpoint.merge ({
70+ const { data: pr } = await github.rest.pulls.get ({
5071 owner: context.repo.owner,
5172 repo: context.repo.repo,
52- issue_number : issue_number,
53- });
73+ pull_number : issue_number,
74+ })
5475
55- const comments = await github.paginate(opts)
76+ let body = pr.body || ""
5677
57- for (const comment of comments) {
58- if (comment.user.id === 41898282 && comment.body === body) {
59- await github.rest.issues.deleteComment({
60- owner: context.repo.owner,
61- repo: context.repo.repo,
62- comment_id: comment.id
63- })
64- }
78+ if (body.includes(marker)) {
79+ // Update existing section
80+ body = body.replace(
81+ new RegExp(marker + "[\\s\\S]*$"),
82+ section,
83+ )
84+ } else {
85+ // Append section
86+ body = body + "\n\n" + section
6587 }
6688
67- await github.rest.issues.createComment ({
68- owner: context.repo.owner,
69- repo: context.repo.repo,
70- issue_number : issue_number,
71- body: body,
89+ await github.rest.pulls.update ({
90+ owner: context.repo.owner,
91+ repo: context.repo.repo,
92+ pull_number : issue_number,
93+ body: body,
7294 })
0 commit comments