Check size comment #13
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: Check binary size comment | |
| on: | |
| workflow_run: | |
| workflows: [Check binary size] | |
| types: | |
| - completed | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| actions: read | |
| jobs: | |
| comment: | |
| name: Post Size Check Results | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.conclusion == 'failure' | |
| steps: | |
| - name: Download artifacts | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| mkdir -p artifacts && cd artifacts | |
| artifacts_url="${{ github.event.workflow_run.artifacts_url }}" | |
| gh api --paginate "$artifacts_url" -q '.artifacts[] | [.name, .archive_download_url] | @tsv' | while read artifact | |
| do | |
| IFS=$'\t' read name url <<< "$artifact" | |
| if [ "$name" != "size-check-results" ]; then | |
| continue | |
| fi | |
| gh api $url > "$name.zip" | |
| unzip -d "$name" "$name.zip" | |
| done | |
| - name: Check for artifacts | |
| id: artifact_check | |
| run: | | |
| if [ -e artifacts/size-check-results/event.json ] && [ -e artifacts/size-check-results/comment.md ]; then | |
| echo "artifacts_found=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "artifacts_found=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Post comment to PR | |
| if: steps.artifact_check.outputs.artifacts_found == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const event = JSON.parse(fs.readFileSync('artifacts/size-check-results/event.json', 'utf8')); | |
| const commentBody = fs.readFileSync('artifacts/size-check-results/comment.md', 'utf8'); | |
| if (!event.pull_request) { | |
| console.log('Not a pull request event, skipping comment'); | |
| return; | |
| } | |
| const { owner, repo } = context.repo; | |
| const prNumber = event.pull_request.number; | |
| console.log(`Posting comment to PR #${prNumber}`); | |
| const comments = await github.rest.issues.listComments({ | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| }); | |
| const existingComment = comments.data.find(comment => | |
| comment.body.includes('## Binary Size Check Failed') && | |
| comment.user.type === 'Bot' | |
| ); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner, | |
| repo, | |
| comment_id: existingComment.id, | |
| body: commentBody | |
| }); | |
| console.log(`Updated existing comment ${existingComment.id}`); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| body: commentBody | |
| }); | |
| console.log('Created new comment'); | |
| } |