Пиво и Ликёр #30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Grammar & Punctuation Check (RU)" | |
on: | |
pull_request: | |
paths: | |
- '00_Правила_оформления/**/*.md' | |
- '01_Вселенная/**/*.md' | |
- '02_Объекты/**/*.md' | |
- '03_Государства/**/*.md' | |
- '04_Корпорации/**/*.md' | |
- '05_Организации/**/*.md' | |
- '06_Расы/**/*.md' | |
- '07_Существа/**/*.md' | |
- '08_Технологии/**/*.md' | |
- '09_Вооружение/**/*.md' | |
- '10_Явления/**/*.md' | |
- '11_Товары/**/*.md' | |
- '12_Другое/**/*.md' | |
- '13_Истории/**/*.md' | |
- '14_Повседневность/**/*.md' | |
- 'README.md' | |
- 'LICENSE.md' | |
jobs: | |
grammar-check: | |
runs-on: ubuntu-latest | |
steps: | |
# 1. Клонируем репозиторий, но с полным fetch | |
- name: Check out the code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
# 2. Устанавливаем Java | |
- name: Set up Java | |
uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17' | |
# 3. Устанавливаем Pandoc | |
- name: Install Pandoc | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y pandoc | |
# 4. Скачиваем LanguageTool (v6.5) | |
- name: Download LanguageTool | |
run: | | |
wget https://languagetool.org/download/LanguageTool-6.5.zip -O lt.zip | |
unzip lt.zip -d lt | |
# 5. Отключаем экранирование путей в Git (важно для кириллических файлов!) | |
- name: Disable Git path quoting | |
run: git config core.quotepath off | |
# 6. Собственно проверка .md-файлов | |
- name: Run grammar check (RU) with Pandoc | |
id: run_languagetool | |
run: | | |
# SHAs для diff | |
BASE_SHA=${{ github.event.pull_request.base.sha }} | |
HEAD_SHA=${{ github.event.pull_request.head.sha }} | |
# Получаем список изменённых .md-файлов | |
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT $BASE_SHA $HEAD_SHA | grep '\.md$' || true) | |
if [ -z "$CHANGED_FILES" ]; then | |
echo "No changed .md files to check." | |
echo "" > grammar_output.txt | |
exit 0 | |
fi | |
echo "Changed markdown files:" | |
echo "$CHANGED_FILES" | |
# Очищаем/создаём общий файл вывода | |
rm -f grammar_output.txt | |
touch grammar_output.txt | |
for FILE in $CHANGED_FILES; do | |
echo "--------------------------------" >> grammar_output.txt | |
echo "Проверка файла: $FILE" >> grammar_output.txt | |
echo "" >> grammar_output.txt | |
# 1) MD -> plain text (Pandoc) | |
pandoc "$FILE" -t plain -o /tmp/conversion.txt | |
# 2) Запуск LanguageTool CLI | |
java -jar lt/LanguageTool-6.5/languagetool-commandline.jar \ | |
--language ru \ | |
--mothertongue ru \ | |
/tmp/conversion.txt \ | |
>> grammar_output.txt \ | |
|| true | |
echo "" >> grammar_output.txt | |
done | |
# 7. Публикуем результаты в PR | |
- name: Create PR comment with suggestions | |
if: always() | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const fs = require('fs'); | |
const path = 'grammar_output.txt'; | |
if (!fs.existsSync(path)) { | |
core.setOutput('No grammar output file found.'); | |
return; | |
} | |
const suggestions = fs.readFileSync(path, 'utf8'); | |
if (!suggestions.trim()) { | |
core.setOutput('No grammar issues found.'); | |
return; | |
} | |
const files = suggestions.split('--------------------------------\n'); | |
const comments = []; | |
for (const fileData of files) { | |
if (!fileData.trim()) continue; | |
const lines = fileData.trim().split('\n'); | |
const fileNameMatch = lines[0].match(/Проверка файла: (.*)/); | |
if (!fileNameMatch) continue; | |
const fileName = fileNameMatch[1]; | |
const content = lines.slice(2).join('\n'); | |
if (content.trim()) { | |
const commentBody = `<details><summary>Грамматические замечания для <code>${fileName}</code></summary>\n\n\`\`\`\n${content.trim()}\n\`\`\`\n</details>`; | |
comments.push(commentBody); | |
} | |
} | |
// Ограничение на количество комментариев (50) | |
const maxComments = 50; | |
let currentComment = ""; | |
let commentCounter = 0; | |
for (const comment of comments) { | |
if (commentCounter < maxComments){ | |
currentComment += comment + "\n\n"; | |
} | |
else { | |
const body = `## LanguageTool (RU) — Проверка грамматики\n\n${currentComment}` | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); | |
currentComment = comment + "\n\n" | |
commentCounter = 0; | |
} | |
commentCounter++; | |
} | |
if (currentComment) { | |
const body = `## LanguageTool (RU) — Проверка грамматики\n\n${currentComment}` | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); | |
} |