|
| 1 | +name: Check for linter warnings / exceptions |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +jobs: |
| 9 | + check-linter: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Set up Node.js |
| 14 | + uses: actions/setup-node@v4 |
| 15 | + with: |
| 16 | + node-version: '20' |
| 17 | + |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + ref: ${{ github.event.pull_request.base.sha }} |
| 23 | + |
| 24 | + - name: Fetch base and target branches |
| 25 | + run: | |
| 26 | + git fetch origin +refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }} |
| 27 | + git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge:refs/remotes/pull/${{ github.event.pull_request.number }}/merge |
| 28 | +
|
| 29 | + - name: Install dependencies |
| 30 | + run: npm ci |
| 31 | + |
| 32 | + - name: Get the diff |
| 33 | + run: git diff --name-only origin/${{ github.event.pull_request.base.ref }}...refs/remotes/pull/${{ github.event.pull_request.number }}/merge | grep '^\(modules\|src\|libraries\|creative\)/.*\.js$' > __changed_files.txt || true |
| 34 | + |
| 35 | + - name: Run linter on base branch |
| 36 | + run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __base.json || true |
| 37 | + |
| 38 | + - name: Check out PR |
| 39 | + run: git checkout ${{ github.event.pull_request.head.sha }} |
| 40 | + |
| 41 | + - name: Install dependencies |
| 42 | + run: npm ci |
| 43 | + |
| 44 | + - name: Run linter on PR |
| 45 | + run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __pr.json || true |
| 46 | + |
| 47 | + - name: Compare them and post comment if necessary |
| 48 | + uses: actions/github-script@v7 |
| 49 | + with: |
| 50 | + script: | |
| 51 | + const fs = require('fs'); |
| 52 | + const path = require('path'); |
| 53 | + const process = require('process'); |
| 54 | + |
| 55 | + function parse(fn) { |
| 56 | + return JSON.parse(fs.readFileSync(fn)).reduce((memo, data) => { |
| 57 | + const file = path.relative(process.cwd(), data.filePath); |
| 58 | + if (!memo.hasOwnProperty(file)) { memo[file] = { errors: 0, warnings: 0} } |
| 59 | + data.messages.forEach(({severity}) => { |
| 60 | + memo[file][severity > 1 ? 'errors' : 'warnings']++; |
| 61 | + }); |
| 62 | + return memo; |
| 63 | + }, {}) |
| 64 | + } |
| 65 | + |
| 66 | + function mkDiff(old, new_) { |
| 67 | + const files = Object.fromEntries( |
| 68 | + Object.entries(new_) |
| 69 | + .map(([file, {errors, warnings}]) => { |
| 70 | + const {errors: oldErrors, warnings: oldWarnings} = old[file] || {}; |
| 71 | + return [file, {errors: Math.max(0, errors - (oldErrors ?? 0)), warnings: Math.max(0, warnings - (oldWarnings ?? 0))}] |
| 72 | + }) |
| 73 | + .filter(([_, {errors, warnings}]) => errors > 0 || warnings > 0) |
| 74 | + ) |
| 75 | + return Object.values(files).reduce((memo, {warnings, errors}) => { |
| 76 | + memo.errors += errors; |
| 77 | + memo.warnings += warnings; |
| 78 | + return memo; |
| 79 | + }, {errors: 0, warnings: 0, files}) |
| 80 | + } |
| 81 | + |
| 82 | + function mkComment({errors, warnings, files}) { |
| 83 | + function pl(noun, number) { |
| 84 | + return noun + (number === 1 ? '' : 's') |
| 85 | + } |
| 86 | + if (errors === 0 && warnings === 0) return; |
| 87 | + const summary = []; |
| 88 | + if (errors) summary.push(`**${errors}** linter ${pl('error', errors)}`) |
| 89 | + if (warnings) summary.push(`**${warnings}** linter ${pl('warning', warnings)}`) |
| 90 | + let cm = `Tread carefully! This PR adds ${summary.join(' and ')} (possibly disabled through directives):\n\n`; |
| 91 | + Object.entries(files).forEach(([file, {errors, warnings}]) => { |
| 92 | + const summary = []; |
| 93 | + if (errors) summary.push(`+${errors} ${pl('error', errors)}`); |
| 94 | + if (warnings) summary.push(`+${warnings} ${pl('warning', warnings)}`) |
| 95 | + cm += ` * \`${file}\` (${summary.join(', ')})\n` |
| 96 | + }) |
| 97 | + return cm; |
| 98 | + } |
| 99 | + |
| 100 | + const [base, pr] = ['__base.json', '__pr.json'].map(parse); |
| 101 | + const comment = mkComment(mkDiff(base, pr)); |
| 102 | + |
| 103 | + if (comment) { |
| 104 | + github.rest.issues.createComment({ |
| 105 | + owner: context.repo.owner, |
| 106 | + repo: context.repo.repo, |
| 107 | + issue_number: context.issue.number, |
| 108 | + body: comment |
| 109 | + }); |
| 110 | + } |
0 commit comments