Skip to content

Update check-plugin-links.yml #5

Update check-plugin-links.yml

Update check-plugin-links.yml #5

name: Validate Plugin Download Links
on:
push:
branches:
- main # Runs on every push to the main branch
workflow_dispatch: # Allows manual triggering of the workflow
jobs:
validate-links:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Fetch `badges.json` from the gh-pages branch
- 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
# Step 3: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "18"
# Step 4: Install Axios for HTTP requests
- name: Install Dependencies
run: npm install axios
# Step 5: Validate plugin download links
- name: Validate Download Links
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', Accept: 'application/vnd.github.v3+json' }
});
// Extract the download URL of the latest release's asset
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) {
// Handle errors during API call
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);
});
EOF