Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/comment-preview-documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Comment Preview Documentation

on:
workflow_run:
workflows: ["Deploy Preview Documentation"]
types:
- completed

permissions:
pull-requests: write
issues: write
actions: read

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
});
75 changes: 7 additions & 68 deletions .github/workflows/deploy-preview-documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:

permissions:
contents: write
pull-requests: write

jobs:
preview:
Expand Down Expand Up @@ -41,81 +40,21 @@ 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 }}
publish_dir: ./site
destination_dir: preview/pr/${{ github.event.pull_request.number }}

- name: Comment on PR with preview link
uses: actions/github-script@v9
with:
script: |
const { owner, repo } = context.repo;
const issue = context.issue;
const prNumber = issue.number;
const comment = `## 📖 Pré-visualização da Documentação

✅ 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.

---
- name: Save PR metadata
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt

*Ao fazer merge do PR, a pré-visualização será automaticamente removida.*
`;

github.rest.issues.createComment({
issue_number: prNumber,
owner: owner,
repo: repo,
body: comment
});

- name: Find and update existing comment
uses: actions/github-script@v9
- name: Upload PR metadata artifact
uses: actions/upload-artifact@v7
with:
script: |
const { owner, repo } = context.repo;
const issue = context.issue;
const prNumber = issue.number;

const previewUrl = `https://docs.cumbuca.dev/preview/pr/${prNumber}/`;

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

✅ 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.*
`;

await github.rest.issues.updateComment({
owner: owner,
repo: repo,
comment_id: comment.id,
body: newBody
});
return;
}
}
name: pr-metadata
path: pr_number.txt

- name: Upload documentation artifact
uses: actions/upload-artifact@v7
Expand Down