Skip to content

Comentário de Cobertura #778

Comentário de Cobertura

Comentário de Cobertura #778

name: Comentário de Cobertura
on:
workflow_run:
workflows: ["Testes Unitários"]
types:
- completed
permissions:
pull-requests: write
checks: write
jobs:
comentario:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
steps:
- name: Baixar artefato de cobertura
uses: actions/download-artifact@v4
with:
name: coverage-report
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Obter número do PR
id: pr
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const response = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.payload.workflow_run.head_sha
});
return response.data.length > 0 ? response.data[0].number : '';
- name: Criar comentário de cobertura
if: steps.pr.outputs.result != ''
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const coverage = JSON.parse(fs.readFileSync('coverage-summary.json', 'utf8'));
const total = coverage.total;
const lines = total.lines.pct;
const statements = total.statements.pct;
const functions = total.functions.pct;
const branches = total.branches.pct;
const comment = `## 📊 Cobertura de Código
| Métrica | Cobertura |
|-------------|----------------|
| Linhas | ${lines}% |
| Declarações | ${statements}% |
| Funções | ${functions}% |
| Branches | ${branches}% |
`;
const prNumber = '${{ steps.pr.outputs.result }}';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('📊 Cobertura de Código')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: comment
});
}