Update broken-links.yml #7
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 README for Broken Links | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| check-links: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '18' | |
| - name: Install markdown-link-check | |
| run: npm install -g markdown-link-check | |
| - name: Check README links | |
| id: check-links | |
| run: | | |
| markdown-link-check README.md > link-check-results.txt || exit 0 | |
| - name: Process Results | |
| id: process-results | |
| run: | | |
| if grep -q "ERROR" link-check-results.txt; then | |
| echo "broken_links=true" >> $GITHUB_ENV | |
| echo "::set-output name=report::$(cat link-check-results.txt | sed 's/%/%25/g; s/\n/%0A/g; s/\r/%0D/g')" | |
| else | |
| echo "broken_links=false" >> $GITHUB_ENV | |
| echo "::set-output name=report::All links are valid!" | |
| fi | |
| - name: Notify GarlicRot | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const report = process.env.REPORT; | |
| const brokenLinks = process.env.broken_links === 'true'; | |
| const body = brokenLinks | |
| ? `### ⚠️ Broken Links Found\nHi @GarlicRot, the following broken links were detected:\n\`\`\`\n${report}\n\`\`\`\nPlease address them!` | |
| : `### ✅ All Links Valid\nHi @GarlicRot, all links in the README are valid! Great job!`; | |
| if (github.event_name === 'push') { | |
| // Comment on the commit | |
| await github.rest.repos.createCommitComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| commit_sha: context.sha, | |
| body: body, | |
| }); | |
| } else if (github.event_name === 'pull_request') { | |
| // Comment on the PR | |
| const issue_number = context.payload.pull_request.number; | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issue_number, | |
| body: body, | |
| }); | |
| } |