Skip to content

Commit

Permalink
Merge pull request #822 from naher94/development
Browse files Browse the repository at this point in the history
Update dead-link-check.yml
  • Loading branch information
naher94 authored Mar 1, 2025
2 parents 4c55648 + 1b5f5b6 commit 8bd7238
Showing 1 changed file with 117 additions and 48 deletions.
165 changes: 117 additions & 48 deletions .github/workflows/dead-link-check.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,132 @@
name: Check for Dead Links
name: Link Checker

on:
schedule:
- cron: '0 3 * * 6'
- cron: '0 10 * * *' # Runs every day at 10 AM UTC
push:
branches:
- gh-pages
pull_request:
branches:
- gh-pages
workflow_dispatch:
- development
workflow_dispatch: # Allows manual trigger

jobs:
check-links:
link-check:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Run Lychee link checker
id: lychee
run: |
lychee --verbose --output lychee/out.json
- name: Create Issue for Broken Links
if: failure()
uses: actions/github-script@v6
with:
script: |
- name: Checkout repository
uses: actions/checkout@v4

- name: Run Lychee link checker
id: lychee
uses: lycheeverse/lychee-action@v1
with:
args: >-
--verbose
--accept 200,206
--no-progress
--require-https
--max-concurrency 5
--exclude "./_sass,./_site,./_vendor,./css,./img,./_config.yml,./README.md"
--format json
.
output: ./lychee-report.json

- name: Check if report exists
id: check_report
run: |
if [ -f "./lychee-report.json" ]; then
echo "Report found."
else
echo "Lychee report not found."
exit 1
fi
- name: Upload Lychee report
uses: actions/upload-artifact@v4
with:
name: lychee-report
path: ./lychee-report.json
retention-days: 7

- name: Parse Dead Links & Create Issue
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
node - <<EOF
const fs = require('fs');
const content = fs.readFileSync('lychee/out.json', 'utf8');
const results = JSON.parse(content);
const brokenLinks = results.issues.map(issue => issue.message).join('\n');
const issueTitle = 'Broken Links Detected';
const issueBody = `The following links were found to be broken:\n\n${brokenLinks}`;
const { data: existingIssues } = await github.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'broken-link'
});
let issueExists = false;
for (const issue of existingIssues) {
if (issue.title === issueTitle) {
issueExists = true;
break;
}
const path = './lychee-report.json';
if (!fs.existsSync(path)) {
console.log('No report found');
process.exit(0);
}
if (!issueExists) {
await github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
body: issueBody,
labels: ['broken-link']
let fileContent = fs.readFileSync(path, 'utf8').trim();
try {
// Extract valid JSON portion
let firstCurlyBrace = fileContent.indexOf('{');
let lastCurlyBrace = fileContent.lastIndexOf('}');
if (firstCurlyBrace === -1 || lastCurlyBrace === -1) {
throw new Error("Invalid JSON structure detected");
}
fileContent = fileContent.substring(firstCurlyBrace, lastCurlyBrace + 1);
const report = JSON.parse(fileContent);
if (!report.summary || report.summary.errors === 0) {
console.log('No dead links found');
process.exit(0);
}
console.log(`Found ${report.summary.errors} broken links`);
let issueBody = "🚨 **Lychee found " + report.summary.errors + " broken links!**\n\n";
issueBody += "| Status | Count |\n|---------------|-------|\n";
issueBody += "| πŸ” Total | " + report.summary.total + " |\n";
issueBody += "| βœ… Successful | " + report.summary.successful + " |\n";
issueBody += "| ⏳ Timeouts | " + report.summary.timeouts + " |\n";
issueBody += "| 🚫 Errors | " + report.summary.errors + " |\n\n";
issueBody += "**Broken Links:**\n";
report.links.forEach(link => {
if (link.status !== 'OK') {
issueBody += "- **" + link.url + "** (in **" + link.file + "**)\n";
}
});
}
issueBody += `\n[Download Full Report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})`;
const issueTitle = "🚨 Dead Links Found!";
const { Octokit } = require("@octokit/core");
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
async function createIssue() {
const { data: issues } = await octokit.request('GET /repos/${{ github.repository }}/issues', {
owner: '${{ github.repository_owner }}',
repo: '${{ github.event.repository.name }}',
state: 'open',
labels: 'dead-link-alert'
});
if (!issues.some(issue => issue.title === issueTitle)) {
console.log("Creating GitHub issue...");
await octokit.request('POST /repos/${{ github.repository }}/issues', {
owner: '${{ github.repository_owner }}',
repo: '${{ github.event.repository.name }}',
title: issueTitle,
body: issueBody,
labels: ["dead-link-alert"]
});
} else {
console.log("Issue already exists.");
}
}
createIssue().catch(error => console.error(error));
} catch (error) {
console.log("Error parsing lychee-report.json. It might not be in JSON format.");
console.log("File content:\n", fileContent);
}
EOF

0 comments on commit 8bd7238

Please sign in to comment.