Skip to content

Update check-plugin-links.yml #6

Update check-plugin-links.yml

Update check-plugin-links.yml #6

name: Validate Plugin Download Links
on:
push:
branches:
- main
workflow_dispatch:
jobs:
validate-links:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Fetch `badges.json` from gh-pages
run: |
git fetch origin gh-pages:gh-pages || echo "gh-pages branch does not exist."
git checkout gh-pages || echo "Failed to checkout gh-pages."
if [ -f badges.json ]; then
echo "badges.json successfully fetched from gh-pages."
else
echo "No badges.json file found in gh-pages branch."
exit 1
fi
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "18"
- name: Install Dependencies
run: npm install axios
- name: Validate Download Links
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
node <<'EOF'
const axios = require('axios');
const fs = require('fs');
// Read and parse the badges.json file
const badges = JSON.parse(fs.readFileSync('./badges.json', 'utf-8'));
const plugins = badges.plugins;
async function validateLinks() {
let hasErrors = false;
for (const plugin of plugins) {
const repoUrl = plugin.url;
const releaseUrl = plugin.releaseUrl;
try {
// Generate API URL for the repository's latest release
const repoName = repoUrl.replace('https://github.com/', '');
const apiUrl = `https://api.github.com/repos/${repoName}/releases/latest`;
// Fetch the latest release information
const response = await axios.get(apiUrl, {
headers: {
'User-Agent': 'GitHub Actions',
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: 'application/vnd.github.v3+json',
},
});
const latestDownloadUrl = response.data.assets[0]?.browser_download_url || null;
if (!latestDownloadUrl) {
// No downloadable asset in the latest release
console.warn(`⚠️ ${plugin.name}: No downloadable asset found in the latest release.`);
console.log(`::warning file=badges.json,line=1::${plugin.name} has no downloadable asset in its latest release.`);
} else if (releaseUrl === latestDownloadUrl) {
// Download link matches the latest release
console.log(`✅ ${plugin.name}: Download link is up-to-date.`);
} else {
// Download link is outdated
console.error(`❌ ${plugin.name}: Download link is outdated.`);
console.error(` Expected: ${latestDownloadUrl}`);
console.error(` Found: ${releaseUrl}`);
console.log(`::error file=badges.json,line=1::${plugin.name} has an outdated download link.`);
hasErrors = true;
}
} catch (error) {
if (error.response?.status === 404) {
console.warn(`⚠️ ${plugin.name}: No releases found (404).`);
console.log(`::warning file=badges.json,line=1::${plugin.name} has no releases.`);
} else if (error.response?.status === 403) {
console.error(`⚠️ ${plugin.name}: Rate limited or access denied (403).`);
console.log(`::error file=badges.json,line=1::${plugin.name} encountered an access issue.`);
hasErrors = true;
} else {
console.error(`⚠️ Error checking ${plugin.name}: ${error.message}`);
console.log(`::error file=badges.json,line=1::${plugin.name} encountered an error: ${error.message}`);
hasErrors = true;
}
}
}
if (hasErrors) {
process.exit(1);
}
}
validateLinks().catch(err => {
console.error("Unhandled error:", err.message);
process.exit(1);