|
| 1 | +name: Harper (grammar + spelling) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, ready_for_review] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + harper: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Set up Rust |
| 22 | + uses: dtolnay/rust-toolchain@stable |
| 23 | + |
| 24 | + - name: Cache Cargo |
| 25 | + uses: actions/cache@v4 |
| 26 | + with: |
| 27 | + path: | |
| 28 | + ~/.cargo/registry |
| 29 | + ~/.cargo/git |
| 30 | + target |
| 31 | + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 32 | + restore-keys: | |
| 33 | + ${{ runner.os }}-cargo- |
| 34 | +
|
| 35 | + - name: Install harper-cli |
| 36 | + run: | |
| 37 | + cargo install --locked --git https://github.com/Automattic/harper.git harper-cli |
| 38 | +
|
| 39 | + - name: Write Harper dictionary |
| 40 | + env: |
| 41 | + XDG_CONFIG_HOME: ${{ github.workspace }}/.xdg |
| 42 | + run: | |
| 43 | + mkdir -p "$XDG_CONFIG_HOME/harper-ls" |
| 44 | + cat > "$XDG_CONFIG_HOME/harper-ls/dictionary.txt" <<'EOF' |
| 45 | + AerynOS |
| 46 | + astrojs |
| 47 | + sha256sum |
| 48 | + SHA256 |
| 49 | + certutil |
| 50 | + hashfile |
| 51 | + lastUpdated |
| 52 | + EOF |
| 53 | +
|
| 54 | + - name: Run Harper on PR-changed files |
| 55 | + id: harper |
| 56 | + env: |
| 57 | + XDG_CONFIG_HOME: ${{ github.workspace }}/.xdg |
| 58 | + run: | |
| 59 | + set -euo pipefail |
| 60 | +
|
| 61 | + BASE="${{ github.event.pull_request.base.sha }}" |
| 62 | + HEAD="${{ github.event.pull_request.head.sha }}" |
| 63 | +
|
| 64 | + mapfile -t FILES < <(git diff --name-only "$BASE" "$HEAD" -- \ |
| 65 | + '*.md' '*.mdx' '*.txt' || true) |
| 66 | +
|
| 67 | + : > harper-report.txt |
| 68 | +
|
| 69 | + if [ "${#FILES[@]}" -eq 0 ]; then |
| 70 | + echo "No matching files changed (.md/.mdx/.txt)." > harper-report.txt |
| 71 | + echo "fail=0" >> "$GITHUB_OUTPUT" |
| 72 | + exit 0 |
| 73 | + fi |
| 74 | +
|
| 75 | + fail=0 |
| 76 | + for f in "${FILES[@]}"; do |
| 77 | + if [ ! -f "$f" ]; then |
| 78 | + continue |
| 79 | + fi |
| 80 | +
|
| 81 | + echo "===== $f =====" >> harper-report.txt |
| 82 | + echo >> harper-report.txt |
| 83 | +
|
| 84 | + out="$(harper-cli lint "$f" || true)" |
| 85 | + echo "$out" >> harper-report.txt |
| 86 | + echo >> harper-report.txt |
| 87 | +
|
| 88 | + after="$(printf '%s\n' "$out" | sed -n 's/.*after overlap removal, \([0-9]\+\) after.*/\1/p' | tail -n 1)" |
| 89 | + after="${after:-0}" |
| 90 | +
|
| 91 | + if [ "$after" -ne 0 ]; then |
| 92 | + fail=1 |
| 93 | + fi |
| 94 | + done |
| 95 | +
|
| 96 | + echo "fail=$fail" >> "$GITHUB_OUTPUT" |
| 97 | +
|
| 98 | + - name: Comment on PR with Harper output |
| 99 | + if: github.event.pull_request |
| 100 | + uses: actions/github-script@v7 |
| 101 | + with: |
| 102 | + script: | |
| 103 | + const fs = require('fs'); |
| 104 | +
|
| 105 | + const raw = fs.readFileSync('harper-report.txt', 'utf8'); |
| 106 | +
|
| 107 | + const limit = 65000; |
| 108 | + const clipped = |
| 109 | + raw.length > limit |
| 110 | + ? raw.slice(0, limit) + "\n\n[truncated]\n" |
| 111 | + : raw; |
| 112 | +
|
| 113 | + const marker = '<!-- harper-report -->'; |
| 114 | + const body = |
| 115 | + `${marker}\n` + |
| 116 | + `<details>\n` + |
| 117 | + `<summary>Harper output</summary>\n\n` + |
| 118 | + "```text\n" + |
| 119 | + clipped + |
| 120 | + "\n```\n" + |
| 121 | + `</details>\n`; |
| 122 | +
|
| 123 | + const { owner, repo } = context.repo; |
| 124 | + const issue_number = context.issue.number; |
| 125 | +
|
| 126 | + const comments = await github.rest.issues.listComments({ |
| 127 | + owner, |
| 128 | + repo, |
| 129 | + issue_number, |
| 130 | + }); |
| 131 | +
|
| 132 | + const existing = comments.data.find(c => |
| 133 | + c.body && c.body.includes(marker) |
| 134 | + ); |
| 135 | +
|
| 136 | + if (existing) { |
| 137 | + await github.rest.issues.updateComment({ |
| 138 | + owner, |
| 139 | + repo, |
| 140 | + comment_id: existing.id, |
| 141 | + body, |
| 142 | + }); |
| 143 | + } else { |
| 144 | + await github.rest.issues.createComment({ |
| 145 | + owner, |
| 146 | + repo, |
| 147 | + issue_number, |
| 148 | + body, |
| 149 | + }); |
| 150 | + } |
| 151 | +
|
| 152 | + - name: Fail if Harper found issues |
| 153 | + if: steps.harper.outputs.fail == '1' |
| 154 | + run: exit 1 |
0 commit comments