PR comment #387
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
| # This workflow comments on a PR with the results of the `cargo bloat check` performed in the performance workflow. | |
| # This is a workaround for the limitations imposed by GitHub Actions on workflows triggered by pull requests from forked repositories. | |
| # The restrictions apply to the pull_request event triggered by a fork opening a pull request in the upstream repository. | |
| # - Events from forks cannot access secrets, except for the default GITHUB_TOKEN. | |
| # - The GITHUB_TOKEN has read-only access when an event is triggered by a forked repository. | |
| # | |
| # These restrictions mean that during a pull_request event triggered by a forked repository, | |
| # actions have no write access to GitHub resources and will fail on any attempt. | |
| name: PR comment | |
| on: | |
| workflow_run: # zizmor: ignore[dangerous-triggers] | |
| workflows: [Performance] | |
| types: [completed] | |
| workflow_dispatch: | |
| inputs: | |
| workflow_run_id: | |
| description: The CI workflow that triggers the workflow run | |
| required: true | |
| permissions: {} | |
| jobs: | |
| bloat-comment: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0 | |
| with: | |
| name: bloat-check-results | |
| path: /tmp/bloat-check/ | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id || github.event.inputs.workflow_run_id }} | |
| - name: Comment bloat check on PR | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| with: | |
| script: | | |
| const fs = require('node:fs'); | |
| const marker = '<!-- prek-bloat-check -->'; | |
| const comparison = await fs.promises.readFile('/tmp/bloat-check/bloat-comparison.txt', 'utf8'); | |
| const body = `${marker}\n${comparison}`; | |
| const { repo, owner } = context.repo; | |
| const prNumber = await fs.promises.readFile('/tmp/bloat-check/pr-number.txt', 'utf8').then(Number); | |
| const comments = await github.paginate(github.rest.issues.listComments, { | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| per_page: 100, | |
| }); | |
| const existing = comments.find((comment) => comment.body?.includes(marker)); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| owner, | |
| repo, | |
| comment_id: existing.id, | |
| body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| body, | |
| }); | |
| } |