name: Comment BDF info on: workflow_run: workflows: [BDF info] types: [completed] permissions: actions: read pull-requests: write jobs: comment: if: >- ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest steps: - name: Check for BDF info id: artifact uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9 with: script: | const artifacts = await github.paginate( github.rest.actions.listWorkflowRunArtifacts, { owner: context.repo.owner, repo: context.repo.repo, run_id: context.payload.workflow_run.id, per_page: 100, }, ); core.setOutput( 'exists', artifacts.some((artifact) => artifact.name === 'bdf-info'), ); - name: Download BDF info if: ${{ steps.artifact.outputs.exists == 'true' }} uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 with: name: bdf-info path: bdf-info run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} - name: Update pull request comment if: ${{ steps.artifact.outputs.exists == 'true' }} uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9 with: script: | const fs = require('fs'); const marker = ''; const workflowRun = context.payload.workflow_run; let pullRequest = workflowRun.pull_requests[0]; if (!pullRequest) { const headRepository = workflowRun.head_repository; if (!headRepository) { core.info('The completed run has no head repository.'); return; } const pullRequests = await github.paginate( github.rest.pulls.list, { owner: context.repo.owner, repo: context.repo.repo, state: 'open', head: `${headRepository.owner.login}:${workflowRun.head_branch}`, per_page: 100, }, ); pullRequest = pullRequests.find( (candidate) => candidate.head.sha === workflowRun.head_sha, ); } if (!pullRequest) { core.info('No open pull request matches the completed run.'); return; } const maxOutputLength = 60000; let output = fs.readFileSync('bdf-info/bdf-info.md', 'utf8'); let truncated = false; if (output.length > maxOutputLength) { output = output.slice(0, maxOutputLength); truncated = true; } // Prevent decoder output from ending the Markdown code block. output = output.replaceAll('```', '``\\`'); const truncationNotice = truncated ? '\n\n[Output truncated to fit in a pull request comment.]' : ''; const body = `${marker} ## BDF information \`\`\`text ${output}${truncationNotice} \`\`\``; const comments = await github.paginate( github.rest.issues.listComments, { owner: context.repo.owner, repo: context.repo.repo, issue_number: pullRequest.number, per_page: 100, }, ); const previousComment = comments.find( (comment) => comment.user?.login === 'github-actions[bot]' && comment.body?.startsWith(marker), ); const params = { owner: context.repo.owner, repo: context.repo.repo, body, }; if (previousComment) { await github.rest.issues.updateComment({ ...params, comment_id: previousComment.id, }); } else { await github.rest.issues.createComment({ ...params, issue_number: pullRequest.number, }); }