Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Broken link validation feature #2670

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/link-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
schedule:
- cron: '0 0 * * SUN'

permissions:
contents: write

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -14,4 +17,20 @@ jobs:
with:
node-version: '14.x'
- run: npm ci
- run: npm run weekly-link-check
- run: npm test

- name: Set up Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'

- name: Commit changes
run: |
git add .
git commit -m "Automated commit from GitHub Actions for weekly deadlink check" || echo "No changes to commit"

- name: Push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push origin main || echo "No changes to push"
19 changes: 19 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ name: Node.js CI
on:
pull_request:

permissions:
contents: write

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -16,3 +19,19 @@ jobs:
node-version: '14.x'
- run: npm ci
- run: npm test

- name: Set up Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'

- name: Commit changes
run: |
git add .
git commit -m "Automated commit from GitHub Actions for weekly deadlink check" || echo "No changes to commit"

- name: Push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push origin main || echo "No changes to push"
27 changes: 27 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"https://hub.docker.com/editions/community/docker-ce-desktop-windowshttps://hub.docker.com/editions/community/docker-ce-desktop-windows": {
"initial_date": "2024-06-01",
"latest_date": "2024-06-11",
"broken": true
},
"https://www.ic.gc.ca/eic/site/061.nsf/vwapj/SFGSME-EFCPME_2017_eng_public.pdf/$file/SFGSME-EFCPME_2017_eng_public.pdf": {
"initial_date": "2024-06-03",
"latest_date": "2024-06-11",
"broken": true
},
"http://www.oag-bvg.gc.ca/internet/English/parl_oag_201004_01_e_33714.html": {
"initial_date": "2024-06-11",
"latest_date": "2024-06-11",
"broken": false
},
"https://www.digitalthinktankictc.com/ictc-admin/resources/admin/ict001genderreportdesignfnl-2.pdf": {
"initial_date": "2024-06-11",
"latest_date": "2024-06-11",
"broken": false
},
"https://joinup.ec.europa.eu/collection/eu-fossa-2": {
"initial_date": "2024-06-11",
"latest_date": "2024-06-11",
"broken": false
}
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"open-licences": "licensee --errors-only",
"licence-compatibility": "license-compatibility-checker",
"site-validator": "site-validator https://sara-sabr.github.io/ITStrategy/ --verbose",
"test": "npm run lint && npm run lint-editorconfig && npm run spellcheck && npm run link-check-pages && npm run link-check-posts && npm run open-licences",
"weekly-link-check": "npm run link-check-pages && npm run link-check-posts",
"validate-broken-link": "node ./validate-broken-link.js",
"test": "npm run lint && npm run lint-editorconfig && npm run spellcheck && npm run link-check-pages && npm run link-check-posts && npm run validate-broken-link && npm run open-licences",
"weekly-link-check": "npm run link-check-pages && npm run link-check-posts && npm run validate-broken-link",
"other-tests": "npm run site-validator && npm rum licence-compatibility && npm audit"
},
"repository": {
Expand All @@ -37,7 +38,7 @@
"editorconfig-checker": "^4.0.2",
"license-compatibility-checker": "^0.3.0",
"licensee": "^10.0.0",
"markdown-link-check": "git+https://[email protected]/sara-sabr/markdown-link-check.git#af79d715c1929316806cdb6130a0a5be6a341683",
"markdown-link-check": "git+https://[email protected]/sara-sabr/markdown-link-check.git#e0a6be0dd6625ed83b543de837089b48a123b5b0",
"markdownlint-cli": "^0.32.2",
"site-validator-cli": "^1.3.5",
"yaml": "^2.2.2"
Expand Down
50 changes: 50 additions & 0 deletions validate-broken-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const path = require('path');

async function readJsonFile(filename) {
const rootDir = process.cwd();
const filePath = path.join(rootDir, filename);
try {
const data = fs.readFileSync(filePath);
return JSON.parse(data);
} catch (err) {
console.error(`Error reading file from ${filePath}: ${err}`);
return null;
}
}

async function writeJsonFile(filename, data) {
const rootDir = process.cwd();
const filePath = path.join(rootDir, filename);
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 4));
} catch (err) {
console.error(`Error writing file to ${filePath}: ${err}`);
return null;
}
}

function validateData(data) {
const copy_data = { ...data };
for (const link in copy_data) {
const print = new Date().toISOString().split('T')[0];
if (data[link].latest_date !== new Date().toISOString().split('T')[0]) {
delete data[link];
} else {
const diff = new Date(data[link].latest_date) - new Date(data[link].initial_date);
const diff_days = diff / (1000 * 60 * 60 * 24);
if (diff_days >= 7) {
data[link].broken = true;
console.log(`Link ${link} has been broken for 7 or more days.`);
}
}
}
}

async function main() {
const data = await readJsonFile('data.json');
validateData(data);
await writeJsonFile('data.json', data);
}

main();