|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from 'fs'; |
| 18 | +import path from 'path'; |
| 19 | +import { fileURLToPath } from 'url'; |
| 20 | +import semver from 'semver'; |
| 21 | +import { listWorkspaces } from './list-workspaces.js'; |
| 22 | + |
| 23 | +const __filename = fileURLToPath(import.meta.url); |
| 24 | +const __dirname = path.dirname(__filename); |
| 25 | + |
| 26 | +// get latest stable Backstage release |
| 27 | +async function getLatestBackstageVersion() { |
| 28 | + const response = await fetch( |
| 29 | + 'https://versions.backstage.io/v1/tags/main/manifest.json', |
| 30 | + ); |
| 31 | + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); |
| 32 | + |
| 33 | + const manifest = await response.json(); |
| 34 | + return manifest.releaseVersion; |
| 35 | +} |
| 36 | + |
| 37 | +// get all workspace versions |
| 38 | +async function getWorkspaceVersions() { |
| 39 | + const workspaceNames = await listWorkspaces(); |
| 40 | + const workspacesDir = path.join(__dirname, '..', 'workspaces'); |
| 41 | + const workspaces = []; |
| 42 | + |
| 43 | + for (const workspaceName of workspaceNames) { |
| 44 | + const backstageJsonPath = path.join( |
| 45 | + workspacesDir, |
| 46 | + workspaceName, |
| 47 | + 'backstage.json', |
| 48 | + ); |
| 49 | + |
| 50 | + if (fs.existsSync(backstageJsonPath)) { |
| 51 | + try { |
| 52 | + const backstageJson = JSON.parse( |
| 53 | + fs.readFileSync(backstageJsonPath, 'utf8'), |
| 54 | + ); |
| 55 | + if (backstageJson.version) { |
| 56 | + workspaces.push({ |
| 57 | + name: workspaceName, |
| 58 | + version: backstageJson.version, |
| 59 | + }); |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + console.warn( |
| 63 | + `Warning: Could not read version from ${workspaceName}/backstage.json:`, |
| 64 | + error.message, |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return workspaces; |
| 71 | +} |
| 72 | + |
| 73 | +// calculate version difference |
| 74 | +function getVersionDifference(currentVersion, latestVersion) { |
| 75 | + const current = semver.clean(currentVersion); |
| 76 | + const latest = semver.clean(latestVersion); |
| 77 | + |
| 78 | + if (!current || !latest) return 0; |
| 79 | + |
| 80 | + // if major versions differ, treat as significantly outdated |
| 81 | + if (semver.major(current) !== semver.major(latest)) { |
| 82 | + return semver.major(latest) > semver.major(current) ? 10 : 0; |
| 83 | + } |
| 84 | + |
| 85 | + return semver.minor(latest) - semver.minor(current); |
| 86 | +} |
| 87 | + |
| 88 | +// categorize workspaces by how outdated they are |
| 89 | +function categorizeWorkspaces(workspaces, latestVersion) { |
| 90 | + const tiers = { tier1: [], tier2: [], tier3: [] }; |
| 91 | + |
| 92 | + workspaces.forEach(workspace => { |
| 93 | + const versionDiff = getVersionDifference(workspace.version, latestVersion); |
| 94 | + if (versionDiff >= 3) tiers.tier1.push(workspace); |
| 95 | + else if (versionDiff === 2) tiers.tier2.push(workspace); |
| 96 | + else if (versionDiff === 1) tiers.tier3.push(workspace); |
| 97 | + }); |
| 98 | + |
| 99 | + // sort each tier by workspace name |
| 100 | + Object.values(tiers).forEach(tier => |
| 101 | + tier.sort((a, b) => a.name.localeCompare(b.name)), |
| 102 | + ); |
| 103 | + |
| 104 | + return tiers; |
| 105 | +} |
| 106 | + |
| 107 | +// generate markdown table for a tier |
| 108 | +function generateTierTable(workspaces, emoji, title, description) { |
| 109 | + if (workspaces.length === 0) return ''; |
| 110 | + |
| 111 | + let output = `## ${emoji} ${title}${ |
| 112 | + description ? ` – ${description}` : '' |
| 113 | + }\n\n`; |
| 114 | + output += '| Workspace | Current Version |\n'; |
| 115 | + output += '|-----------|----------------|\n'; |
| 116 | + workspaces.forEach(workspace => { |
| 117 | + output += `| ${workspace.name} | ${workspace.version} |\n`; |
| 118 | + }); |
| 119 | + return `${output}\n`; |
| 120 | +} |
| 121 | + |
| 122 | +// generate the dashboard output |
| 123 | +function generateDashboard(tiers, latestVersion) { |
| 124 | + let output = |
| 125 | + 'Tracking workspaces not on the latest Backstage minor version\n\n'; |
| 126 | + output += `**Latest Version:** ${latestVersion}\n\n`; |
| 127 | + |
| 128 | + output += generateTierTable( |
| 129 | + tiers.tier1, |
| 130 | + '🔴', |
| 131 | + '≥ 3 minor versions behind', |
| 132 | + '', |
| 133 | + ); |
| 134 | + output += generateTierTable(tiers.tier2, '🟠', '2 minor versions behind', ''); |
| 135 | + output += generateTierTable(tiers.tier3, '🟡', '1 minor version behind', ''); |
| 136 | + |
| 137 | + const totalOutdated = |
| 138 | + tiers.tier1.length + tiers.tier2.length + tiers.tier3.length; |
| 139 | + if (totalOutdated === 0) { |
| 140 | + output += '## 🎉 All workspaces are up to date!\n\n'; |
| 141 | + } |
| 142 | + |
| 143 | + output += '---\n\n### Summary\n\n'; |
| 144 | + output += `- **Total outdated workspaces:** ${totalOutdated}\n`; |
| 145 | + output += `- **≥ 3 minor versions behind:** ${tiers.tier1.length}\n`; |
| 146 | + output += `- **2 minor versions behind:** ${tiers.tier2.length}\n`; |
| 147 | + output += `- **1 minor version behind:** ${tiers.tier3.length}\n\n`; |
| 148 | + |
| 149 | + output += `*Dashboard generated on ${ |
| 150 | + new Date().toISOString().split('T')[0] |
| 151 | + }*\n`; |
| 152 | + |
| 153 | + return output.trim(); |
| 154 | +} |
| 155 | + |
| 156 | +// main function |
| 157 | +async function main() { |
| 158 | + const latestVersion = await getLatestBackstageVersion(); |
| 159 | + const workspaces = await getWorkspaceVersions(); |
| 160 | + const tiers = categorizeWorkspaces(workspaces, latestVersion); |
| 161 | + const dashboard = generateDashboard(tiers, latestVersion); |
| 162 | + |
| 163 | + console.log(dashboard); |
| 164 | +} |
| 165 | + |
| 166 | +// run the script |
| 167 | +main().catch(error => { |
| 168 | + console.error('Error generating dashboard:', error); |
| 169 | + process.exit(1); |
| 170 | +}); |
| 171 | + |
| 172 | +export { |
| 173 | + getLatestBackstageVersion, |
| 174 | + getWorkspaceVersions, |
| 175 | + categorizeWorkspaces, |
| 176 | + generateDashboard, |
| 177 | +}; |
0 commit comments