Antique Laser Gun #42
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: "Link Checker" | ||
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' | ||
jobs: | ||
link-check: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- name: Fetch PR head branch | ||
run: | | ||
git remote add pr "${{ github.event.pull_request.head.repo.clone_url }}" | ||
git fetch pr "${{ github.event.pull_request.head.ref }}" | ||
- name: Extract added lines from changed Markdown files | ||
run: | | ||
BASE_SHA="${{ github.event.pull_request.base.sha }}" | ||
HEAD_REF="${{ github.event.pull_request.head.ref }}" | ||
# Сравнение базовой ветки с PR-веткой только для Markdown файлов | ||
git diff "$BASE_SHA" "pr/$HEAD_REF" -- "*.md" > diff.txt | ||
# Извлекаем только строки, добавленные в PR (начинаются с '+' но не с '+++') | ||
grep '^+' diff.txt | grep -v '^+++' > added-links.md | ||
echo "Содержимое добавленных строк:" | ||
cat added-links.md | ||
- name: Run lychee on added links | ||
id: run-lychee | ||
run: | | ||
if [ ! -s added-links.md ]; then | ||
echo "No added lines found." | ||
# Создаем пустой JSON, чтобы дальнейший шаг мог корректно отработать | ||
echo '{"links": []}' > lychee-output.json | ||
else | ||
# Передаем содержимое файла через STDIN в lychee | ||
cat added-links.md | lychee --output json --dump ./lychee-output.json || echo "Обнаружены проблемы со ссылками." | ||
fi | ||
- name: Post PR comment with link check results | ||
if: always() | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const outputFile = './lychee-output.json'; | ||
if (!fs.existsSync(outputFile)) { | ||
await github.rest.issues.createComment({ | ||
issue_number: context.payload.pull_request.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: 'Не удалось создать отчет о проверке ссылок. Убедитесь, что Markdown файлы доступны для проверки.', | ||
}); | ||
return; | ||
} | ||
const output = JSON.parse(fs.readFileSync(outputFile, 'utf8')); | ||
if (!output.links || output.links.length === 0) { | ||
await github.rest.issues.createComment({ | ||
issue_number: context.payload.pull_request.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: '🔍 В добавленном тексте не обнаружено ссылок или все ссылки корректны.', | ||
}); | ||
return; | ||
} | ||
const failedLinks = output.links.filter(link => link.status !== 'Ok'); | ||
if (failedLinks.length === 0) { | ||
await github.rest.issues.createComment({ | ||
issue_number: context.payload.pull_request.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: '✅ Все ссылки в добавленном тексте корректны!', | ||
}); | ||
return; | ||
} | ||
const errorDetails = failedLinks | ||
.map(link => `- [${link.uri}](${link.uri}) (Файл: ${link.source}) → Статус: ${link.status}`) | ||
.join('\n'); | ||
const issueComment = ` | ||
### Отчет о проверке ссылок | ||
Обнаружены следующие проблемы со ссылками в добавленном тексте: | ||
${errorDetails} | ||
Пожалуйста, исправьте их перед слиянием. | ||
`; | ||
await github.rest.issues.createComment({ | ||
issue_number: context.payload.pull_request.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: issueComment, | ||
}); |