|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// The `PKGBUILD` file of `mingw-w64-git-credential-manager` uses a `case` |
| 4 | +// statement to select architecture-specific artifacts and their checksums. |
| 5 | + |
| 6 | +// This script fetches the checksums from the GitHub release page and updates |
| 7 | +// the PKGBUILD accordingly. |
| 8 | + |
| 9 | +(async () => { |
| 10 | + const version = process.argv[2] |
| 11 | + |
| 12 | + const githubApiRequest = require('../../github-api-request') |
| 13 | + const { assets } = await githubApiRequest( |
| 14 | + console, |
| 15 | + null, |
| 16 | + 'GET', |
| 17 | + `/repos/git-ecosystem/git-credential-manager/releases/tags/v${version}` |
| 18 | + ) |
| 19 | + |
| 20 | + // Map architecture names from GitHub assets to MSYS2 architecture names |
| 21 | + const archMap = { |
| 22 | + 'x86': 'i686', |
| 23 | + 'x64': 'x86_64', |
| 24 | + 'arm64': 'aarch64' |
| 25 | + } |
| 26 | + |
| 27 | + const sha256sums = {} |
| 28 | + let sourceArchiveSha = null |
| 29 | + |
| 30 | + // Find checksums from the release assets (GitHub provides sha256 digests) |
| 31 | + for (const asset of assets) { |
| 32 | + const match = asset.name.match(/^gcm-win-(x86|x64|arm64)-\S+\.zip$/) |
| 33 | + if (match) { |
| 34 | + const arch = match[1] |
| 35 | + const msys2Arch = archMap[arch] |
| 36 | + // Use the digest from GitHub's API |
| 37 | + if (asset.digest && asset.digest.sha256) { |
| 38 | + sha256sums[msys2Arch] = asset.digest.sha256 |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Get the source archive checksum |
| 44 | + const sourceAsset = assets.find(a => a.name === `git-credential-manager-${version}.tar.gz` || a.name === 'Source code (tar.gz)') |
| 45 | + if (sourceAsset && sourceAsset.digest && sourceAsset.digest.sha256) { |
| 46 | + sourceArchiveSha = sourceAsset.digest.sha256 |
| 47 | + } |
| 48 | + |
| 49 | + // Alternatively, download and compute the checksums ourselves |
| 50 | + if (!sha256sums.i686 || !sha256sums.x86_64 || !sha256sums.aarch64 || !sourceArchiveSha) { |
| 51 | + const crypto = require('crypto') |
| 52 | + const https = require('https') |
| 53 | + const agent = new https.Agent({ keepAlive: false }) |
| 54 | + |
| 55 | + const computeSha256 = async (url) => { |
| 56 | + console.error(`Downloading ${url.length > 120 ? url.substring(0, 117) + '...' : url}`) |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + https.get(url, { agent }, (response) => { |
| 59 | + // Follow redirects |
| 60 | + if (response.statusCode === 301 || response.statusCode === 302) { |
| 61 | + return computeSha256(response.headers.location).then(resolve).catch(reject) |
| 62 | + } |
| 63 | + if (response.statusCode !== 200) { |
| 64 | + reject(new Error(`HTTP ${response.statusCode}`)) |
| 65 | + return |
| 66 | + } |
| 67 | + const hash = crypto.createHash('sha256') |
| 68 | + response.on('data', (chunk) => hash.update(chunk)) |
| 69 | + response.on('end', () => resolve(hash.digest('hex'))) |
| 70 | + }).on('error', reject) |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + console.log('Computing SHA256 checksums from artifacts...') |
| 75 | + const baseUrl = `https://github.com/git-ecosystem/git-credential-manager/releases/download/v${version}` |
| 76 | + |
| 77 | + sha256sums.i686 = await computeSha256(`${baseUrl}/gcm-win-x86-${version}.zip`) |
| 78 | + sha256sums.x86_64 = await computeSha256(`${baseUrl}/gcm-win-x64-${version}.zip`) |
| 79 | + sha256sums.aarch64 = await computeSha256(`${baseUrl}/gcm-win-arm64-${version}.zip`) |
| 80 | + sourceArchiveSha = await computeSha256(`https://github.com/git-ecosystem/git-credential-manager/archive/v${version}.zip`) |
| 81 | + } |
| 82 | + |
| 83 | + if (!sha256sums.i686 || !sha256sums.x86_64 || !sha256sums.aarch64 || !sourceArchiveSha) { |
| 84 | + throw new Error('Could not obtain all required checksums') |
| 85 | + } |
| 86 | + |
| 87 | + console.log(`Checksums for v${version}:`) |
| 88 | + console.log(` i686: ${sha256sums.i686}`) |
| 89 | + console.log(` x86_64: ${sha256sums.x86_64}`) |
| 90 | + console.log(` aarch64: ${sha256sums.aarch64}`) |
| 91 | + console.log(` source: ${sourceArchiveSha}`) |
| 92 | + |
| 93 | + // Update the PKGBUILD file |
| 94 | + const fs = require('fs') |
| 95 | + const lines = fs.readFileSync('PKGBUILD').toString('utf-8').split(/\r?\n/) |
| 96 | + |
| 97 | + let currentArch = null |
| 98 | + lines.forEach((line, i) => { |
| 99 | + let match |
| 100 | + |
| 101 | + // Detect which architecture case we're in |
| 102 | + if ((match = line.match(/^\s*mingw-w64-(i686|x86_64|clang-aarch64)\)$/))) { |
| 103 | + const msys2Arch = match[1] === 'clang-aarch64' ? 'aarch64' : match[1] |
| 104 | + currentArch = msys2Arch |
| 105 | + } else if (line.match(/^\s*\*\)$/)) { |
| 106 | + // Default case, use i686 |
| 107 | + currentArch = 'i686' |
| 108 | + } else if ((match = line.match(/^(\s*_sha=)[0-9a-f]{64}$/))) { |
| 109 | + // Update the _sha variable with the correct checksum |
| 110 | + if (currentArch && sha256sums[currentArch]) { |
| 111 | + lines[i] = `${match[1]}${sha256sums[currentArch]}` |
| 112 | + } |
| 113 | + } else if ((match = line.match(/^(\s*)'[0-9a-f]{64}'(\s*)\)?\s*$/))) { |
| 114 | + // Update the source archive checksum in sha256sums array |
| 115 | + lines[i] = `${match[1]}'${sourceArchiveSha}'${match[2]})` |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + fs.writeFileSync('PKGBUILD', lines.join('\n')) |
| 120 | + console.log('PKGBUILD updated successfully') |
| 121 | +})().catch((err) => {console.log(err); process.exit(1)}) |
0 commit comments