|
1 | | -name: Harper (grammar + spelling) |
| 1 | +# .github/workflows/grammar.yml |
| 2 | +name: Grammar Check (PR Changes Only) |
2 | 3 |
|
3 | 4 | on: |
4 | 5 | pull_request: |
5 | | - types: [opened, synchronize, reopened, ready_for_review] |
6 | | - |
7 | | -permissions: |
8 | | - contents: read |
9 | | - pull-requests: write |
10 | 6 |
|
11 | 7 | jobs: |
12 | | - harper: |
| 8 | + grammar: |
13 | 9 | runs-on: ubuntu-latest |
14 | | - |
15 | 10 | steps: |
16 | | - - name: Checkout |
17 | | - uses: actions/checkout@v4 |
| 11 | + - uses: actions/checkout@v3 |
| 12 | + |
| 13 | + - name: Get changed markdown files |
| 14 | + id: changed |
| 15 | + uses: tj-actions/changed-files@v39 |
18 | 16 | with: |
19 | | - fetch-depth: 0 |
| 17 | + files: | |
| 18 | + **/*.md |
| 19 | + **/*.mdx |
20 | 20 |
|
21 | | - - name: Set up Rust |
22 | | - uses: dtolnay/rust-toolchain@stable |
| 21 | + - name: Skip if no docs changed |
| 22 | + if: steps.changed.outputs.any_changed != 'true' |
| 23 | + run: echo "No .md or .mdx files changed." |
23 | 24 |
|
24 | | - - name: Cache Cargo |
25 | | - uses: actions/cache@v4 |
| 25 | + - name: Set up Node |
| 26 | + if: steps.changed.outputs.any_changed == 'true' |
| 27 | + uses: actions/setup-node@v3 |
26 | 28 | 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- |
| 29 | + node-version: 18 |
34 | 30 |
|
35 | | - - name: Install harper-cli |
| 31 | + - name: Install MDX cleaner |
| 32 | + if: steps.changed.outputs.any_changed == 'true' |
36 | 33 | run: | |
37 | | - cargo install --locked --git https://github.com/Automattic/harper.git harper-cli |
| 34 | + npm install remark remark-mdx strip-markdown |
| 35 | + mkdir -p dist |
| 36 | + for file in ${{ steps.changed.outputs.all_changed_files }}; do |
| 37 | + node scripts/clean-mdx.js "$file" > "dist/${file//\//_}.txt" |
| 38 | + done |
38 | 39 |
|
39 | | - - name: Write Harper dictionary |
40 | | - env: |
41 | | - XDG_CONFIG_HOME: ${{ github.workspace }}/.xdg |
| 40 | + - name: Download LanguageTool CLI |
| 41 | + if: steps.changed.outputs.any_changed == 'true' |
42 | 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 |
| 43 | + curl -L -o lt.zip https://languagetool.org/download/LanguageTool-stable.zip |
| 44 | + unzip lt.zip |
| 45 | + echo "LT_PATH=$(pwd)/LanguageTool-*/languagetool-commandline.jar" >> $GITHUB_ENV |
53 | 46 |
|
54 | | - - name: Run Harper on PR-changed files |
55 | | - id: harper |
| 47 | + - name: Run LanguageTool with reviewdog |
| 48 | + if: steps.changed.outputs.any_changed == 'true' |
56 | 49 | env: |
57 | | - XDG_CONFIG_HOME: ${{ github.workspace }}/.xdg |
| 50 | + REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
58 | 51 | 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 |
| 52 | + for f in dist/*.txt; do |
| 53 | + java -jar $LT_PATH -l en-US "$f" | |
| 54 | + reviewdog -efm="%f: Line %l: %m" \ |
| 55 | + -name="LanguageTool" \ |
| 56 | + -reporter=github-pr-review \ |
| 57 | + -filter-mode=added \ |
| 58 | + -fail-on-error=false |
94 | 59 | 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