1- name : LanguageTool
1+ name : LanguageTool (PR)
22
33on :
44 pull_request :
55 types : [opened, synchronize, reopened, ready_for_review]
6- push :
7- branches : ["**"]
86
97permissions :
108 contents : read
@@ -13,7 +11,6 @@ permissions:
1311jobs :
1412 languagetool :
1513 runs-on : ubuntu-latest
16-
1714 steps :
1815 - name : Checkout
1916 uses : actions/checkout@v4
@@ -34,35 +31,20 @@ jobs:
3431 unzip -q LT.zip
3532 echo "LT_DIR=LanguageTool-${LT_VERSION}" >> "$GITHUB_ENV"
3633
37- - name : Run LanguageTool ( changed files) and comment on PR
34+ - name : Run LanguageTool on changed PR files + comment summary
3835 env :
3936 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
4037 REPO : ${{ github.repository }}
41- EVENT_NAME : ${{ github.event_name }}
4238 PR_NUMBER : ${{ github.event.pull_request.number }}
4339 BASE_SHA : ${{ github.event.pull_request.base.sha }}
4440 HEAD_SHA : ${{ github.event.pull_request.head.sha }}
4541 run : |
4642 set -euo pipefail
4743
48- # Only comment when this run is for a PR event
49- IS_PR=0
50- if [ "$EVENT_NAME" = "pull_request" ]; then
51- IS_PR=1
52- fi
53-
54- if [ "$IS_PR" -eq 1 ]; then
55- echo "Base: $BASE_SHA"
56- echo "Head: $HEAD_SHA"
57- DIFF_BASE="$BASE_SHA"
58- DIFF_HEAD="$HEAD_SHA"
59- else
60- echo "Push run: comparing against previous commit"
61- DIFF_BASE="${GITHUB_SHA}^"
62- DIFF_HEAD="${GITHUB_SHA}"
63- fi
44+ echo "Base: $BASE_SHA"
45+ echo "Head: $HEAD_SHA"
6446
65- mapfile -t FILES < <(git diff --name-only "$DIFF_BASE " "$DIFF_HEAD " \
47+ mapfile -t FILES < <(git diff --name-only "$BASE_SHA " "$HEAD_SHA " \
6648 | grep -E '\.(md|mdx|rst|txt)$' || true)
6749
6850 if [ "${#FILES[@]}" -eq 0 ]; then
@@ -76,19 +58,11 @@ jobs:
7658 JAR="$(ls -1 "$LT_DIR"/languagetool-commandline.jar)"
7759 LANG="en-US"
7860
79- issues=0
80- REPORT_FILE="$(mktemp)"
81- : > "$REPORT_FILE"
82-
83- for f in "${FILES[@]}"; do
84- echo "-----"
85- echo "Checking: $f"
86-
87- tmp="$(mktemp)"
88-
89- # Preprocess with Python. If preprocessing fails, fall back to the original file.
90- if python3 - "$f" > "$tmp" 2>/dev/null <<'PY'
61+ # Write a preprocessor script to a file (avoids YAML/heredoc indentation issues)
62+ PREPROCESS="$(mktemp)"
63+ cat > "$PREPROCESS" <<'PY'
9164import re, sys
65+
9266path = sys.argv[1]
9367text = open(path, "r", encoding="utf-8", errors="replace").read()
9468
@@ -117,7 +91,19 @@ text = re.sub(r"\b[A-Za-z][A-Za-z0-9_-]{14,}\b", " IDENT ", text)
11791text = re.sub(r"[ \t]+", " ", text)
11892sys.stdout.write(text)
11993PY
120- then
94+
95+ issues=0
96+ REPORT_FILE="$(mktemp)"
97+ : > "$REPORT_FILE"
98+
99+ for f in "${FILES[@]}"; do
100+ echo "-----"
101+ echo "Checking : $f"
102+
103+ tmp="$(mktemp)"
104+
105+ # Preprocess; if it fails, fall back to original file (but keep the workflow running)
106+ if python3 "$PREPROCESS" "$f" > "$tmp" 2>/dev/null; then
121107 :
122108 else
123109 cp "$f" "$tmp"
139125 fi
140126 done
141127
142- # If PR: upsert a single comment with a marker
143- if [ "$IS_PR" -eq 1 ]; then
144- MARKER="<!-- languagetool-report -->"
145- BODY_FILE="$(mktemp)"
128+ rm -f "$PREPROCESS" || true
146129
147- if [ "$issues" -ne 0 ]; then
148- {
149- echo "$MARKER"
150- echo "### LanguageTool findings"
151- echo
152- echo "_Checked files changed in this PR (frontmatter + fenced code removed; inline code stripped)._"
153- echo
154- echo '```'
155- cat "$REPORT_FILE"
156- echo '```'
157- } > "$BODY_FILE"
158- else
159- {
160- echo "$MARKER"
161- echo "### LanguageTool findings"
162- echo
163- echo "✅ No issues found in changed files."
164- } > "$BODY_FILE"
165- fi
130+ # Upsert a single PR comment (marker-based)
131+ MARKER="<!-- languagetool-report -->"
132+ BODY_FILE="$(mktemp)"
133+
134+ if [ "$issues" -ne 0 ]; then
135+ {
136+ echo "$MARKER"
137+ echo "### LanguageTool findings"
138+ echo
139+ echo "_Checked files changed in this PR (frontmatter + fenced code removed; inline code stripped)._"
140+ echo
141+ echo '```'
142+ cat "$REPORT_FILE"
143+ echo '```'
144+ } > "$BODY_FILE"
145+ else
146+ {
147+ echo "$MARKER"
148+ echo "### LanguageTool findings"
149+ echo
150+ echo "✅ No issues found in changed files."
151+ } > "$BODY_FILE"
152+ fi
166153
167- COMMENTS_JSON="$(mktemp)"
168- gh api "repos/$REPO/issues/$PR_NUMBER/comments?per_page=100" > "$COMMENTS_JSON"
154+ COMMENTS_JSON="$(mktemp)"
155+ gh api "repos/$REPO/issues/$PR_NUMBER/comments?per_page=100 exporting=false " > "$COMMENTS_JSON"
169156
170- COMMENT_ID="$(python3 - "$COMMENTS_JSON" <<'PY'
157+ COMMENT_ID="$(python3 - "$COMMENTS_JSON" <<'PY'
171158import json, sys
172159data = json.load(open(sys.argv[1], "r", encoding="utf-8"))
173160for c in data :
174161 if "<!-- languagetool-report -->" in (c.get("body") or "") :
175162 print(c["id"])
176163 break
177164PY
178- )"
165+ )"
179166
180- if [ -n "${COMMENT_ID:-}" ]; then
181- gh api -X PATCH "repos/$REPO/issues/comments/$COMMENT_ID" -f body="$(cat "$BODY_FILE")" >/dev/null
182- echo "Updated existing LanguageTool comment."
183- else
184- gh api -X POST "repos/$REPO/issues/$PR_NUMBER/comments" -f body="$(cat "$BODY_FILE")" >/dev/null
185- echo "Posted new LanguageTool comment."
186- fi
167+ if [ -n "${COMMENT_ID:-}" ]; then
168+ gh api -X PATCH "repos/$REPO/issues/comments/$COMMENT_ID" -f body="$(cat "$BODY_FILE")" >/dev/null
169+ echo "Updated existing LanguageTool comment."
170+ else
171+ gh api -X POST "repos/$REPO/issues/$PR_NUMBER/comments" -f body="$(cat "$BODY_FILE")" >/dev/null
172+ echo "Posted new LanguageTool comment."
187173 fi
188174
175+ rm -f "$COMMENTS_JSON" "$BODY_FILE" "$REPORT_FILE" || true
176+
177+ # Fail the check if there were findings (remove this block if you want advisory-only)
189178 if [ "$issues" -ne 0 ]; then
190179 exit 1
191180 fi
0 commit comments