|
1 | | -name: reviewdog |
2 | | -on: [pull_request] |
| 1 | +name: Harper (grammar suggestions) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + pull-requests: write |
| 9 | + |
3 | 10 | jobs: |
4 | | - linter_name: |
5 | | - name: LanguageTool grammar check |
| 11 | + harper: |
6 | 12 | runs-on: ubuntu-latest |
| 13 | + |
7 | 14 | steps: |
8 | | - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
9 | | - - uses: reviewdog/action-languagetool@ea19c757470ce0dbfcbc34aec090317cef1ff0b5 # v1.22.0 |
| 15 | + - uses: actions/checkout@v4 |
10 | 16 | with: |
11 | | - github_token: ${{ secrets.github_token }} |
12 | | - # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review]. |
13 | | - reporter: github-pr-review |
14 | | - # Change reporter level if you need. |
15 | | - level: info |
16 | | - patterns: "**/*.md **/*.mdx **/*.txt" |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Install Harper |
| 20 | + run: | |
| 21 | + curl -fsSL https://raw.githubusercontent.com/automattic/harper/main/install.sh | sh |
| 22 | + echo "$HOME/.harper/bin" >> $GITHUB_PATH |
| 23 | +
|
| 24 | + - name: Run Harper on changed md/mdx |
| 25 | + id: harper |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | +
|
| 29 | + mapfile -t FILES < <( |
| 30 | + git diff --name-only origin/${{ github.base_ref }}...${{ github.sha }} \ |
| 31 | + -- '*.md' '*.mdx' || true |
| 32 | + ) |
| 33 | +
|
| 34 | + if [ ${#FILES[@]} -eq 0 ]; then |
| 35 | + echo "No markdown files changed." > harper.txt |
| 36 | + exit 0 |
| 37 | + fi |
| 38 | +
|
| 39 | + { |
| 40 | + echo "## Harper grammar suggestions" |
| 41 | + echo |
| 42 | + for f in "${FILES[@]}"; do |
| 43 | + [ -f "$f" ] || continue |
| 44 | + echo "### $f" |
| 45 | + harper "$f" || true |
| 46 | + echo |
| 47 | + done |
| 48 | + } > harper.txt |
| 49 | +
|
| 50 | + - name: Post PR comment |
| 51 | + uses: actions/github-script@v7 |
| 52 | + with: |
| 53 | + script: | |
| 54 | + const fs = require("fs"); |
| 55 | + const body = fs.readFileSync("harper.txt", "utf8").trim(); |
| 56 | +
|
| 57 | + const marker = "<!-- harper-report -->"; |
| 58 | + const commentBody = `${marker}\n${body || "No issues found."}`; |
| 59 | +
|
| 60 | + const { data: comments } = await github.rest.issues.listComments({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + issue_number: context.issue.number, |
| 64 | + }); |
| 65 | +
|
| 66 | + const existing = comments.find(c => |
| 67 | + c.body && c.body.includes(marker) |
| 68 | + ); |
| 69 | +
|
| 70 | + if (existing) { |
| 71 | + await github.rest.issues.updateComment({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + comment_id: existing.id, |
| 75 | + body: commentBody, |
| 76 | + }); |
| 77 | + } else { |
| 78 | + await github.rest.issues.createComment({ |
| 79 | + owner: context.repo.owner, |
| 80 | + repo: context.repo.repo, |
| 81 | + issue_number: context.issue.number, |
| 82 | + body: commentBody, |
| 83 | + }); |
| 84 | + } |
0 commit comments