Ниан диздок #67
Workflow file for this run
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_target: | |
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. Подтягиваем ветку PR для получения актуального HEAD_SHA | |
- name: Fetch PR head branch | |
id: fetch_pr | |
run: | | |
git remote add pr "${{ github.event.pull_request.head.repo.clone_url }}" | |
git fetch pr "${{ github.event.pull_request.head.ref }}" | |
echo "::set-output name=head_sha::$(git rev-parse pr/${{ github.event.pull_request.head.ref }})" | |
# 7. Выполняем проверку грамматики для изменённых файлов | |
- name: Run grammar check (RU) with Pandoc | |
id: run_languagetool | |
run: | | |
BASE_SHA=${{ github.event.pull_request.base.sha }} | |
HEAD_SHA=${{ steps.fetch_pr.outputs.head_sha }} | |
echo "Using BASE_SHA=$BASE_SHA" | |
echo "Using HEAD_SHA=$HEAD_SHA" | |
# Вычисляем общий предок между HEAD и базовой веткой | |
MERGE_BASE=$(git merge-base $HEAD_SHA $BASE_SHA) | |
echo "Using MERGE_BASE=$MERGE_BASE" | |
# Получаем список изменённых файлов (новых и изменённых) относительно merge base | |
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT $MERGE_BASE $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 | |
# Извлекаем содержимое файла из PR-ветки (это работает для новых и изменённых файлов) | |
if git show $HEAD_SHA:"$FILE" > /tmp/pr_file.md; then | |
pandoc /tmp/pr_file.md -t plain -o /tmp/conversion.txt | |
java -jar lt/LanguageTool-6.5/languagetool-commandline.jar \ | |
--language ru \ | |
--mothertongue ru \ | |
/tmp/conversion.txt \ | |
>> grammar_output.txt \ | |
|| true | |
echo "" >> grammar_output.txt | |
else | |
echo "Файл $FILE не найден в коммите HEAD." >> grammar_output.txt | |
fi | |
done | |
# 8. Публикуем результаты в 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 | |
}); | |
} |