Comentar informe SHACL en PR #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Comentar informe SHACL en PR | |
| on: | |
| workflow_run: | |
| workflows: ["Validación formas SHACL"] | |
| types: [completed] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| issues: write | |
| actions: read | |
| jobs: | |
| comment: | |
| if: github.event.workflow_run.event == 'pull_request' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Descargar artefacto de informes | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: dcat-ap-es-validation-reports | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| path: reports | |
| - name: Publicar o actualizar comentario en la PR | |
| uses: actions/github-script@v9 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const marker = '<!-- dcat-ap-es-shacl-report -->'; | |
| const summaryPath = 'reports/SUMMARY.md'; | |
| const prNumberPath = 'reports/PR_NUMBER.txt'; | |
| if (!fs.existsSync(summaryPath)) { | |
| core.warning(`No se encontró ${summaryPath}; se omite el comentario.`); | |
| return; | |
| } | |
| let prNumber; | |
| if (fs.existsSync(prNumberPath)) { | |
| prNumber = Number(fs.readFileSync(prNumberPath, 'utf8').trim()); | |
| } else { | |
| prNumber = context.payload.workflow_run.pull_requests?.[0]?.number; | |
| } | |
| if (!prNumber) { | |
| core.warning('No se pudo determinar el número de PR.'); | |
| return; | |
| } | |
| const summary = fs.readFileSync(summaryPath, 'utf8').trim(); | |
| const shortSha = (context.payload.workflow_run.head_sha || '').slice(0, 7); | |
| const updatedAt = context.payload.workflow_run.updated_at || new Date().toISOString(); | |
| const body = [ | |
| marker, | |
| '## resumen de validación SHACL', | |
| '', | |
| `Estado del workflow: \`${context.payload.workflow_run.conclusion || 'unknown'}\``, | |
| shortSha ? `Commit: \`${shortSha}\`` : null, | |
| `Actualizado: \`${updatedAt}\``, | |
| '', | |
| summary | |
| ].filter(Boolean).join('\n'); | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| per_page: 100 | |
| }); | |
| const existingComment = comments.find(comment => | |
| comment.user?.type === 'Bot' && | |
| typeof comment.body === 'string' && | |
| comment.body.includes(marker) | |
| ); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body | |
| }); | |
| core.info(`Updated comment ${existingComment.id} on PR #${prNumber}.`); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body | |
| }); | |
| core.info(`Created comment on PR #${prNumber}.`); | |
| } |