|
| 1 | +/** |
| 2 | + * Usage: |
| 3 | + * |
| 4 | + * 1. Ensure that Node.js and npm are installed on your system. |
| 5 | + * 2. Run this script using the command `node choco-auto.js <projectVersion>`. |
| 6 | + * |
| 7 | + * Parameters: |
| 8 | + * - projectVersion: The version of the project you want to fetch information for (e.g., 0.19.1). |
| 9 | + * |
| 10 | + * Ensure that you replace `<projectVersion>` with the actual project version, do not include the 'v'. |
| 11 | + */ |
| 12 | + |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | +const { exec } = require('child_process'); |
| 16 | + |
| 17 | +const args = process.argv.slice(2); |
| 18 | + |
| 19 | +if (!(args.length === 1)) { |
| 20 | + console.error('Usage: node choco-auto.js <projectVersion>'); |
| 21 | + process.exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +const projectVersion = args[0]; |
| 25 | + |
| 26 | +if (projectVersion.startsWith('v')) { |
| 27 | + console.error('Please provide the project version without the "v" prefix.'); |
| 28 | + console.error('Example: node choco-auto.js 0.19.1'); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +// Async function to fetch release information from GitHub API for a given tag |
| 33 | +// For this script we are only interested in the checksums for the windows x64 asset |
| 34 | +async function fetchGithubReleaseInfo() { |
| 35 | + const tag = projectVersion; |
| 36 | + const res = await fetch( |
| 37 | + `https://api.github.com/repos/headlamp-k8s/headlamp/releases/tags/v${tag}` |
| 38 | + ); |
| 39 | + |
| 40 | + if (!res.ok) { |
| 41 | + console.error( |
| 42 | + `Error fetching release information for tag ${tag}: ${res.status} ${res.statusText}` |
| 43 | + ); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + |
| 47 | + const resJSON = await res.json(); |
| 48 | + |
| 49 | + const checksum = await updateReleaseInfo(resJSON, tag); |
| 50 | + return checksum; |
| 51 | +} |
| 52 | + |
| 53 | +async function getChecksums(checksumsUrl, fileName) { |
| 54 | + const res = await fetch(checksumsUrl); |
| 55 | + const checksums = await res.text(); |
| 56 | + |
| 57 | + for (const line of checksums.split('\n')) { |
| 58 | + const [checksum, filename] = line.split(' '); |
| 59 | + |
| 60 | + if (!filename) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + if (filename === fileName) { |
| 65 | + return checksum; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return ''; |
| 70 | +} |
| 71 | + |
| 72 | +async function updateReleaseInfo(response, tag) { |
| 73 | + const assets = response.assets; |
| 74 | + const checksums = assets.find(v => v.name === 'checksums.txt'); |
| 75 | + const checksumsUrl = checksums.browser_download_url; |
| 76 | + // asset for windows x64, can add more assets for other archs |
| 77 | + const winx64Asset = assets.find(asset => asset.name === `Headlamp-${tag}-win-x64.exe`); |
| 78 | + const browserDownloadName = winx64Asset.name; |
| 79 | + |
| 80 | + const checksum = await getChecksums(checksumsUrl, browserDownloadName); |
| 81 | + console.log('Checksum:', checksum); |
| 82 | + |
| 83 | + return checksum; |
| 84 | +} |
| 85 | + |
| 86 | +async function runChocoUpdate() { |
| 87 | + const checksum = await fetchGithubReleaseInfo(); |
| 88 | + |
| 89 | + console.log('using checksum', checksum); |
| 90 | + |
| 91 | + exec(`./choco-bump.sh ${projectVersion} ${checksum}`, (error, stdout, stderr) => { |
| 92 | + if (error) { |
| 93 | + console.error(`exec error - source choco-auto.js : ${error}`); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + console.log(`stdout - source choco-auto.js : ${stdout}`); |
| 98 | + console.error(`stderr - source choco-auto.js : ${stderr}`); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +runChocoUpdate(); |
0 commit comments