Codespell Comment #18
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: Codespell Comment | |
| # Posts the codespell report produced by the "Codespell" workflow back onto the | |
| # pull request. Because it is triggered by workflow_run it runs in the base | |
| # repository context with a writable token, so it can comment on fork PRs | |
| # without ever checking out or executing untrusted PR code. | |
| # | |
| # A comment is only created when spelling issues are found; an existing report | |
| # comment is updated in place (and flipped to "resolved" once issues are fixed). | |
| # | |
| # NOTE: workflow_run only triggers from workflow files on the default branch, so | |
| # this begins working once merged to master. | |
| on: | |
| workflow_run: | |
| workflows: [Codespell] | |
| types: [completed] | |
| permissions: | |
| actions: read # download the report artifact from the triggering run | |
| contents: read | |
| pull-requests: write # comment on the pull request (issues API, PR target) | |
| jobs: | |
| comment: | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.event == 'pull_request' | |
| steps: | |
| - name: Download report | |
| uses: actions/download-artifact@v4 | |
| continue-on-error: true | |
| with: | |
| name: codespell-report | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Post or update PR comment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const marker = '<!-- codespell-report -->'; | |
| const { owner, repo } = context.repo; | |
| if (!fs.existsSync('pr_number.txt')) { | |
| core.info('No report artifact found; nothing to comment on.'); | |
| return; | |
| } | |
| const issue_number = parseInt(fs.readFileSync('pr_number.txt', 'utf8').trim(), 10); | |
| if (!Number.isInteger(issue_number)) { | |
| core.info('Invalid PR number; skipping.'); | |
| return; | |
| } | |
| const findings = fs.existsSync('codespell.txt') | |
| ? fs.readFileSync('codespell.txt', 'utf8').trim() | |
| : ''; | |
| const comments = await github.paginate(github.rest.issues.listComments, { | |
| owner, repo, issue_number, per_page: 100, | |
| }); | |
| const existing = comments.find(c => c.body && c.body.includes(marker)); | |
| let body; | |
| if (!findings) { | |
| if (!existing) return; // nothing to report and nothing to update | |
| body = `${marker}\n:white_check_mark: **codespell** found no spelling issues.`; | |
| } else { | |
| body = [ | |
| marker, | |
| '### :abc: codespell found possible spelling issues', | |
| '', | |
| '```', | |
| findings, | |
| '```', | |
| '', | |
| 'If any are false positives (for example a variable name or acronym), ' | |
| + 'add them to `ignore-words-list` in `.codespellrc`.', | |
| ].join('\n'); | |
| } | |
| if (existing) { | |
| await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); | |
| } else { | |
| await github.rest.issues.createComment({ owner, repo, issue_number, body }); | |
| } |