From 8a8118fa5f9d4613195ea91def552a8f13a7da70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Fri, 4 Dec 2026 07:55:49 -0700 Subject: [PATCH] feat: publish multi-version releases list and verify GitHub asset digest --- scripts/build-index.mjs | 107 +++++++++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 11 deletions(-) diff --git a/scripts/build-index.mjs b/scripts/build-index.mjs index d993f6c..608b69a 100644 --- a/scripts/build-index.mjs +++ b/scripts/build-index.mjs @@ -219,6 +219,23 @@ function summarizeReleaseBody(body) { return { 'zh-CN': clipped, 'en-US': clipped } } +function parseDigestSha256(digest) { + if (typeof digest !== 'string') return '' + const s = digest.trim().toLowerCase() + if (!s.startsWith('sha256:')) return '' + const hex = s.slice('sha256:'.length) + return RE_SHA256.test(hex) ? hex : '' +} + +function inferVersionFromTag(tagName, tagPrefix) { + let s = String(tagName || '').trim() + if (tagPrefix && s.startsWith(tagPrefix)) { + s = s.slice(tagPrefix.length) + } + s = s.replace(/^v/i, '').trim() + return s +} + async function resolveLatestReleaseCandidates(entry, channel) { if (!entry.repo) { throw new Error('auto-tracked plugin entry must specify repo (owner/name)') @@ -226,6 +243,7 @@ async function resolveLatestReleaseCandidates(entry, channel) { const releases = await fetchGitHubReleases(entry.repo) const tagPrefix = typeof entry.tag_prefix === 'string' ? entry.tag_prefix : '' const candidates = [] + const allReleases = [] let totalDownloads = 0 for (const rel of releases) { if (!rel || rel.draft) continue @@ -233,18 +251,32 @@ async function resolveLatestReleaseCandidates(entry, channel) { const asset = selectReleaseAsset(rel, entry) if (!asset || !asset.browser_download_url) continue if (Number.isFinite(asset.download_count)) totalDownloads += asset.download_count - if (channel === 'stable' && rel.prerelease) continue - candidates.push({ + const relChannel = rel.prerelease ? 'alpha' : 'stable' + const digestSha256 = parseDigestSha256(asset.digest) + const publishedAt = rel.published_at || rel.created_at || new Date().toISOString() + const cand = { tag: rel.tag_name, + version: inferVersionFromTag(rel.tag_name, tagPrefix), + channel: relChannel, downloadURL: asset.browser_download_url, - publishedAt: rel.published_at || rel.created_at || new Date().toISOString(), + digestSha256, + sizeBytes: Number.isFinite(asset.size) ? asset.size : 0, + publishedAt, body: rel.body || '', - }) + } + allReleases.push(cand) + if (channel === 'stable' && rel.prerelease) continue + candidates.push(cand) } if (candidates.length === 0) { - throw new Error(`no matching ${channel} GitHub Release with a .zip plugin asset found in ${entry.repo}`) + // Fall back to all matching releases (e.g. if repo only has prereleases). + if (allReleases.length > 0) { + candidates.push(...allReleases) + } else { + throw new Error(`no matching ${channel} GitHub Release with a .zip plugin asset found in ${entry.repo}`) + } } - return { candidates, totalDownloads } + return { candidates, allReleases, totalDownloads } } async function downloadBuffer(url) { @@ -310,7 +342,7 @@ function validateWithdrawn(withdrawnDoc) { return { ids, list: out } } -async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, pinnedVersion, pinnedSHA256, stats, opts, tmpRoot }) { +async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, allReleases, pinnedVersion, pinnedSHA256, stats, opts, tmpRoot }) { const parsedURL = new URL(candidate.downloadURL) if (parsedURL.protocol !== 'https:') { throw new Error(`download_url must use https:// (got ${candidate.downloadURL})`) @@ -321,6 +353,9 @@ async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, if (pinnedSHA256 && gotSum !== pinnedSHA256) { throw new Error(`SHA-256 mismatch: registry=${pinnedSHA256}, actual=${gotSum}`) } + if (candidate.digestSha256 && gotSum !== candidate.digestSha256) { + throw new Error(`SHA-256 mismatch: github_digest=${candidate.digestSha256}, actual=${gotSum}`) + } const manifestRaw = readFileFromZipBuffer(buf, 'plugin.json') const manifest = JSON.parse(manifestRaw.toString('utf8')) @@ -352,6 +387,54 @@ async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, const tier = scanReport?.tier || (manifest.entry?.sidecar ? 'L3' : manifest.entry?.panelDir ? 'L4' : 'L1') const effectiveTrust = trust === 'official' || trust === 'verified' ? trust : 'community' const icon = extractPluginIcon(buf, manifest) + const primaryChannel = candidate.channel || channel + const primaryChangelog = normalizeLocalized(entry.changelog) || summarizeReleaseBody(candidate.body) + const primaryPublished = entry.updated_at || candidate.publishedAt || new Date().toISOString() + + const releaseList = [] + const seenVersions = new Set() + const addRelease = (r) => { + if (!r.version || !RE_SHA256.test(r.sha256) || seenVersions.has(r.version)) return + seenVersions.add(r.version) + const clean = { + version: r.version, + channel: r.channel || 'stable', + download_url: r.download_url, + sha256: r.sha256, + size_bytes: r.size_bytes > 0 ? r.size_bytes : undefined, + published_at: r.published_at || undefined, + changelog: r.changelog || undefined, + } + for (const k of Object.keys(clean)) { + if (clean[k] === undefined) delete clean[k] + } + releaseList.push(clean) + } + addRelease({ + version, + channel: primaryChannel, + download_url: candidate.downloadURL, + sha256: gotSum, + size_bytes: buf.length, + published_at: primaryPublished, + changelog: primaryChangelog, + }) + if (Array.isArray(allReleases)) { + for (const rel of allReleases) { + if (releaseList.length >= 10) break + if (!rel.version || !rel.digestSha256) continue + addRelease({ + version: rel.version, + channel: rel.channel, + download_url: rel.downloadURL, + sha256: rel.digestSha256, + size_bytes: rel.sizeBytes, + published_at: rel.publishedAt, + changelog: summarizeReleaseBody(rel.body), + }) + } + } + const marketItem = { id: manifest.id, version, @@ -359,7 +442,7 @@ async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, description: normalizeLocalized(manifest.description) || { 'en-US': '' }, publisher: manifest.publisher || (entry.repo ? entry.repo.split('/')[0] : 'community'), license: manifest.license || '', - channel, + channel: primaryChannel, trust: effectiveTrust, kind, tier, @@ -372,11 +455,12 @@ async function verifyAndBuildCandidate({ entry, channel, kind, trust, candidate, download_url: candidate.downloadURL, sha256: gotSum, size_bytes: buf.length, - changelog: normalizeLocalized(entry.changelog) || summarizeReleaseBody(candidate.body), + changelog: primaryChangelog, + releases: releaseList.length > 0 ? releaseList : undefined, tags: Array.isArray(entry.tags) ? entry.tags : undefined, stars: stats?.stars > 0 ? stats.stars : undefined, downloads: stats?.downloads > 0 ? stats.downloads : undefined, - updated_at: entry.updated_at || candidate.publishedAt || new Date().toISOString(), + updated_at: primaryPublished, } for (const k of Object.keys(marketItem)) { if (marketItem[k] === undefined) delete marketItem[k] @@ -469,7 +553,7 @@ async function main() { marketPlugins.push(built.marketItem) reportRows.push(built.reportRow) } else { - const { candidates, totalDownloads } = await resolveLatestReleaseCandidates(entry, channel) + const { candidates, allReleases, totalDownloads } = await resolveLatestReleaseCandidates(entry, channel) let built = null let lastErr = null // In PR --check mode, only accept the latest release; in scheduled publish mode, @@ -483,6 +567,7 @@ async function main() { kind, trust, candidate: candidates[i], + allReleases, pinnedVersion: '', pinnedSHA256: '', stats: { stars: repoStats.stars, downloads: totalDownloads },