From b403614ab20b84b0511ae4934dd9680df51b49a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Let=C3=ADcia=20Fernandes?= Date: Sat, 11 Apr 2026 14:28:50 -0300 Subject: [PATCH 1/3] test: validate preview workflow for fork PRs --- .../deploy-preview-documentation.yml | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/.github/workflows/deploy-preview-documentation.yml b/.github/workflows/deploy-preview-documentation.yml index 2f538f4..da3d51b 100644 --- a/.github/workflows/deploy-preview-documentation.yml +++ b/.github/workflows/deploy-preview-documentation.yml @@ -41,6 +41,7 @@ jobs: run: mkdocs build - name: Deploy to GitHub Pages (PR Preview) + if: github.event.pull_request.head.repo.full_name == github.repository uses: peaceiris/actions-gh-pages@v4 with: github_token: ${{ secrets.GITHUB_TOKEN }} @@ -54,46 +55,31 @@ jobs: const { owner, repo } = context.repo; const issue = context.issue; const prNumber = issue.number; - const comment = `## 📖 Pré-visualização da Documentação + const isFork = + context.payload.pull_request.head.repo.full_name !== `${owner}/${repo}`; + const runId = context.runId; - ✅ A documentação foi compilada com sucesso! - - ### 🔗 Acessar a pré-visualização: - **[Visualizar documentação →](https://docs.cumbuca.dev/preview/pr/${prNumber}/)** - - Esta pré-visualização estará disponível enquanto o PR estiver aberto. + const previewUrl = `https://docs.cumbuca.dev/preview/pr/${prNumber}/`; - --- + const body = isFork + ? `## 📖 Pré-visualização da Documentação - *Ao fazer merge do PR, a pré-visualização será automaticamente removida.* - `; + ✅ A documentação foi compilada com sucesso! - github.rest.issues.createComment({ - issue_number: prNumber, - owner: owner, - repo: repo, - body: comment - }); + ### 📦 Baixar pré-visualização: + Como este PR vem de um fork, o deploy automático não é possível por restrições de segurança do GitHub Actions. - - name: Find and update existing comment - uses: actions/github-script@v8 - with: - script: | - const { owner, repo } = context.repo; - const issue = context.issue; - const prNumber = issue.number; + **[⬇️ Acessar artifact da documentação](https://github.com/${owner}/${repo}/actions/runs/${runId})** - const previewUrl = `https://docs.cumbuca.dev/preview/pr/${prNumber}/`; + 1. Acesse o link acima + 2. Clique em **documentation-preview-pr-${prNumber}** na seção *Artifacts* + 3. Extraia o arquivo baixado e abra \`index.html\` no navegador - const comments = await github.rest.issues.listComments({ - owner: owner, - repo: repo, - issue_number: prNumber - }); + --- - for (const comment of comments.data) { - if (comment.user.type === 'Bot' && comment.body.includes('Pré-visualização da Documentação')) { - const newBody = `## 📖 Pré-visualização da Documentação + *O artifact ficará disponível por 7 dias.*` + : + `## 📖 Pré-visualização da Documentação ✅ A documentação foi compilada com sucesso! @@ -104,19 +90,33 @@ jobs: --- - *Ao fazer merge do PR, a pré-visualização será automaticamente removida.* - `; + *Ao fazer merge do PR, a pré-visualização será automaticamente removida.*`; + + const comments = await github.rest.issues.listComments({ + owner: owner, + repo: repo, + issue_number: prNumber + }); + for (const comment of comments.data) { + if (comment.user.type === 'Bot' && comment.body.includes('Pré-visualização da Documentação')) { await github.rest.issues.updateComment({ owner: owner, repo: repo, comment_id: comment.id, - body: newBody + body: body }); return; } } + await github.rest.issues.createComment({ + issue_number: prNumber, + owner: owner, + repo: repo, + body: body + }); + - name: Upload documentation artifact uses: actions/upload-artifact@v7 with: From 687fc838ba6913ea3a2d97bc52d5f4ad4fe3db14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Let=C3=ADcia=20Fernandes?= Date: Sun, 12 Apr 2026 16:41:36 -0300 Subject: [PATCH 2/3] ci: separate comment section from preview workflow --- .../comment-preview-documentation.yml | 88 +++++++++++++++++++ .../deploy-preview-documentation.yml | 75 ++-------------- 2 files changed, 95 insertions(+), 68 deletions(-) create mode 100644 .github/workflows/comment-preview-documentation.yml diff --git a/.github/workflows/comment-preview-documentation.yml b/.github/workflows/comment-preview-documentation.yml new file mode 100644 index 0000000..1f91599 --- /dev/null +++ b/.github/workflows/comment-preview-documentation.yml @@ -0,0 +1,88 @@ +name: Comment Preview Documentation + +on: + workflow_run: + workflows: ["Deploy Preview Documentation"] + types: + - completed + +permissions: + pull-requests: write + +jobs: + comment: + runs-on: ubuntu-latest + steps: + - name: Download PR metadata artifact + uses: actions/download-artifact@v4 + with: + name: pr-metadata + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Comment on PR with preview link + uses: actions/github-script@v8 + with: + script: | + const { owner, repo } = context.repo; + const prNumber = parseInt(require('fs').readFileSync('pr_number.txt', 'utf8').trim()); + const isFork = + context.payload.workflow_run.head_repository.full_name !== `${owner}/${repo}`; + const runId = context.payload.workflow_run.id; + + const previewUrl = `https://docs.cumbuca.dev/preview/pr/${prNumber}/`; + + const body = isFork + ? `## 📖 Pré-visualização da Documentação + + ✅ A documentação foi compilada com sucesso! + + ### 📦 Baixar pré-visualização: + Como este PR vem de um fork, o deploy automático não é possível por restrições de segurança do GitHub Actions. + + **[⬇️ Acessar artifact da documentação](https://github.com/${owner}/${repo}/actions/runs/${runId})** + + 1. Acesse o link acima + 2. Clique em **documentation-preview-pr-${prNumber}** na seção *Artifacts* + 3. Extraia o arquivo baixado e abra \`index.html\` no navegador + + --- + + *O artifact ficará disponível por 7 dias.*` + : `## 📖 Pré-visualização da Documentação + + ✅ A documentação foi compilada com sucesso! + + ### 🔗 Acessar a pré-visualização: + **[Visualizar documentação →](${previewUrl})** + + Esta pré-visualização estará disponível enquanto o PR estiver aberto. + + --- + + *Ao fazer merge do PR, a pré-visualização será automaticamente removida.*`; + + const comments = await github.rest.issues.listComments({ + owner: owner, + repo: repo, + issue_number: prNumber + }); + + for (const comment of comments.data) { + if (comment.user.type === 'Bot' && comment.body.includes('Pré-visualização da Documentação')) { + await github.rest.issues.updateComment({ + owner: owner, + repo: repo, + comment_id: comment.id, + body: body + }); + return; + } + } + + await github.rest.issues.createComment({ + issue_number: prNumber, + owner: owner, + repo: repo, + body: body + }); diff --git a/.github/workflows/deploy-preview-documentation.yml b/.github/workflows/deploy-preview-documentation.yml index da3d51b..b2c1c0a 100644 --- a/.github/workflows/deploy-preview-documentation.yml +++ b/.github/workflows/deploy-preview-documentation.yml @@ -9,7 +9,6 @@ on: permissions: contents: write - pull-requests: write jobs: preview: @@ -48,74 +47,14 @@ jobs: publish_dir: ./site destination_dir: preview/pr/${{ github.event.pull_request.number }} - - name: Comment on PR with preview link - uses: actions/github-script@v8 - with: - script: | - const { owner, repo } = context.repo; - const issue = context.issue; - const prNumber = issue.number; - const isFork = - context.payload.pull_request.head.repo.full_name !== `${owner}/${repo}`; - const runId = context.runId; - - const previewUrl = `https://docs.cumbuca.dev/preview/pr/${prNumber}/`; - - const body = isFork - ? `## 📖 Pré-visualização da Documentação - - ✅ A documentação foi compilada com sucesso! - - ### 📦 Baixar pré-visualização: - Como este PR vem de um fork, o deploy automático não é possível por restrições de segurança do GitHub Actions. - - **[⬇️ Acessar artifact da documentação](https://github.com/${owner}/${repo}/actions/runs/${runId})** - - 1. Acesse o link acima - 2. Clique em **documentation-preview-pr-${prNumber}** na seção *Artifacts* - 3. Extraia o arquivo baixado e abra \`index.html\` no navegador - - --- - - *O artifact ficará disponível por 7 dias.*` - : - `## 📖 Pré-visualização da Documentação + - name: Save PR metadata + run: echo "${{ github.event.pull_request.number }}" > pr_number.txt - ✅ A documentação foi compilada com sucesso! - - ### 🔗 Acessar a pré-visualização: - **[Visualizar documentação →](${previewUrl})** - - Esta pré-visualização estará disponível enquanto o PR estiver aberto. - - --- - - *Ao fazer merge do PR, a pré-visualização será automaticamente removida.*`; - - const comments = await github.rest.issues.listComments({ - owner: owner, - repo: repo, - issue_number: prNumber - }); - - for (const comment of comments.data) { - if (comment.user.type === 'Bot' && comment.body.includes('Pré-visualização da Documentação')) { - await github.rest.issues.updateComment({ - owner: owner, - repo: repo, - comment_id: comment.id, - body: body - }); - return; - } - } - - await github.rest.issues.createComment({ - issue_number: prNumber, - owner: owner, - repo: repo, - body: body - }); + - name: Upload PR metadata artifact + uses: actions/upload-artifact@v7 + with: + name: pr-metadata + path: pr_number.txt - name: Upload documentation artifact uses: actions/upload-artifact@v7 From 168b8c79bc2f5f8694e847d48874ff538733b3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Let=C3=ADcia=20Fernandes?= Date: Sun, 12 Apr 2026 17:34:29 -0300 Subject: [PATCH 3/3] ci: update permissions for comment preview workflow --- .github/workflows/comment-preview-documentation.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/comment-preview-documentation.yml b/.github/workflows/comment-preview-documentation.yml index 1f91599..385f507 100644 --- a/.github/workflows/comment-preview-documentation.yml +++ b/.github/workflows/comment-preview-documentation.yml @@ -8,6 +8,8 @@ on: permissions: pull-requests: write + issues: write + actions: read jobs: comment: