diff --git a/.github/workflows/__update-build-deps.yml b/.github/workflows/__update-build-deps.yml index 20d99ee4..45340f08 100644 --- a/.github/workflows/__update-build-deps.yml +++ b/.github/workflows/__update-build-deps.yml @@ -1,5 +1,5 @@ --- -# Update a repository's build-deps submodule to the latest tag and refresh related prebuilt dependencies. +# Update build-deps submodules and refresh pinned prebuilt release assets. name: Update build-deps permissions: {} @@ -13,6 +13,8 @@ on: type: choice options: - Sunshine + - Koko + - One jobs: update-build-deps: @@ -29,10 +31,15 @@ jobs: filter: blob:none path: repo - - name: Update build-deps + - name: Update build-deps submodule id: update-build-deps working-directory: repo run: | + if ! git ls-files --stage third-party/build-deps | grep -q '^160000 '; then + echo "Expected a build-deps submodule at third-party/build-deps." >&2 + exit 1 + fi + git submodule update --init third-party/build-deps git -C third-party/build-deps fetch --force --tags origin @@ -56,98 +63,83 @@ jobs: git -C third-party/build-deps checkout --detach "${latest_tag}" echo "updated=true" >> "${GITHUB_OUTPUT}" - - name: Get build-deps release + - name: Update build-deps release assets id: get-release - if: steps.update-build-deps.outputs.updated == 'true' uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: BUILD_DEPS_VERSION: ${{ steps.update-build-deps.outputs.version }} with: github-token: ${{ secrets.GH_BOT_TOKEN }} script: | - const release = await github.rest.repos.getReleaseByTag({ - owner: context.repo.owner, - repo: 'build-deps', - tag: process.env.BUILD_DEPS_VERSION - }); - - core.setOutput('body', release.data.body || ''); - - for (const arch of ['x86_64', 'aarch64']) { - const name = `Linux-${arch}-ffmpeg.tar.gz`; - const assets = release.data.assets.filter(asset => asset.name === name); - - if (assets.length !== 1) { - throw new Error(`Expected one release asset named ${name}, found ${assets.length}.`); + const fs = require('fs'); + const options = {owner: context.repo.owner, repo: 'build-deps'}; + const tag = process.env.BUILD_DEPS_VERSION; + if (!tag) { + throw new Error('No build-deps submodule release tag was selected.'); + } + const response = await github.rest.repos.getReleaseByTag({...options, tag}); + const release = response.data; + if (release.tag_name !== tag) { + throw new Error(`Expected release ${tag}, received ${release.tag_name}.`); + } + // One currently has no Flatpak packaging; its submodule is the only dependency pin. + const paths = ['repo/packaging/linux/flatpak/modules/ffmpeg.json'] + .filter(path => fs.existsSync(path)); + + const updates = paths.map(path => { + const manifest = JSON.parse(fs.readFileSync(path, 'utf8')); + if (!Array.isArray(manifest.sources) || manifest.sources.length === 0) { + throw new Error(`Expected prebuilt sources in ${path}.`); } - - const asset = assets[0]; - const digest = asset.digest || ''; - const [algorithm, hash] = digest.split(':', 2); - - if (algorithm !== 'sha256' || !/^[0-9a-f]{64}$/i.test(hash)) { - throw new Error(`Release asset ${name} does not have a valid SHA-256 digest.`); + if (path.includes('/flatpak/')) { + for (const arch of ['x86_64', 'aarch64']) { + const count = manifest.sources.filter(source => + JSON.stringify(source['only-arches']) === JSON.stringify([arch])).length; + if (count !== 1) { + throw new Error(`Expected one Flatpak FFmpeg source for ${arch}, found ${count}.`); + } + } } - - core.setOutput(`${arch}_url`, asset.browser_download_url); - core.setOutput(`${arch}_sha`, hash.toLowerCase()); + const sources = manifest.sources.map(source => { + const name = new URL(source.url).pathname.split('/').pop(); + const assets = release.assets.filter(asset => asset.name === name); + if (assets.length !== 1) { + throw new Error(`Expected one release asset named ${name}, found ${assets.length}.`); + } + const asset = assets[0]; + const digest = /^sha256:([0-9a-f]{64})$/i.exec(asset.digest || ''); + if (!digest || !asset.browser_download_url) { + throw new Error(`Release asset ${name} does not have a valid URL and SHA-256 digest.`); + } + return {...source, url: asset.browser_download_url, sha256: digest[1].toLowerCase()}; + }); + const changed = sources.some((source, index) => + source.url !== manifest.sources[index].url || source.sha256 !== manifest.sources[index].sha256); + return {path, manifest: {...manifest, sources}, changed}; + }).filter(update => update.changed); + + // Validate every source before writing any manifest. + for (const {path, manifest} of updates) { + fs.writeFileSync(path, JSON.stringify(manifest, null, 2) + '\n'); } - - - name: Update Flatpak FFmpeg prebuilts - if: steps.update-build-deps.outputs.updated == 'true' - env: - AARCH64_SHA: ${{ steps.get-release.outputs.aarch64_sha }} - AARCH64_URL: ${{ steps.get-release.outputs.aarch64_url }} - X86_64_SHA: ${{ steps.get-release.outputs.x86_64_sha }} - X86_64_URL: ${{ steps.get-release.outputs.x86_64_url }} - working-directory: repo - run: | - module=packaging/linux/flatpak/modules/ffmpeg.json - - if [ ! -f "${module}" ]; then - echo "Flatpak FFmpeg module not found at ${module}." - exit 1 - fi - - for arch in x86_64 aarch64; do - matches=$(jq --arg arch "${arch}" \ - '[.sources[] | select(.["only-arches"] == [$arch])] | length' "${module}") - if [ "${matches}" -ne 1 ]; then - echo "Expected one Flatpak FFmpeg source for ${arch}, found ${matches}." - exit 1 - fi - done - - jq \ - --arg x86_64_url "${X86_64_URL}" \ - --arg x86_64_sha "${X86_64_SHA}" \ - --arg aarch64_url "${AARCH64_URL}" \ - --arg aarch64_sha "${AARCH64_SHA}" \ - ' - .sources |= map( - if .["only-arches"] == ["x86_64"] then - .url = $x86_64_url | .sha256 = $x86_64_sha - elif .["only-arches"] == ["aarch64"] then - .url = $aarch64_url | .sha256 = $aarch64_sha - else - . - end - ) - ' "${module}" > "${RUNNER_TEMP}/ffmpeg.json" - mv "${RUNNER_TEMP}/ffmpeg.json" "${module}" + core.setOutput('updated', String(updates.length > 0)); + core.setOutput('version', release.tag_name); + core.setOutput('body', release.body || ''); - name: Create/Update Pull Request id: create-pr - if: steps.update-build-deps.outputs.updated == 'true' + if: >- + steps.update-build-deps.outputs.updated == 'true' || + steps.get-release.outputs.updated == 'true' uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 with: author: "${{ vars.GH_BOT_NAME }} <${{ secrets.GH_BOT_EMAIL }}>" committer: "${{ vars.GH_BOT_NAME }} <${{ secrets.GH_BOT_EMAIL }}>" path: "repo" token: ${{ secrets.GH_BOT_TOKEN }} - commit-message: "chore(deps): Update build-deps to ${{ steps.update-build-deps.outputs.version }}" - branch: bot/bump-build-deps-${{ steps.update-build-deps.outputs.version }} + commit-message: "chore(deps): Update build-deps to ${{ steps.get-release.outputs.version }}" + branch: bot/bump-build-deps-${{ steps.get-release.outputs.version }} delete-branch: true - title: "chore(deps): Update build-deps to ${{ steps.update-build-deps.outputs.version }}" + title: "chore(deps): Update build-deps to ${{ steps.get-release.outputs.version }}" body: ${{ steps.get-release.outputs.body }} labels: dependencies diff --git a/tests/update-build-deps.test.mjs b/tests/update-build-deps.test.mjs new file mode 100644 index 00000000..310a881f --- /dev/null +++ b/tests/update-build-deps.test.mjs @@ -0,0 +1,158 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import test from 'node:test'; + +const workflow = fs.readFileSync('.github/workflows/__update-build-deps.yml', 'utf8').replaceAll('\r\n', '\n'); +const assetStep = workflow.split(' - name: Update build-deps release assets\n')[1] + .split(' - name: Create/Update Pull Request\n')[0]; +const script = assetStep.split(' script: |\n')[1] + .trimEnd().split('\n').map(line => line.slice(12)).join('\n'); +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; +const executeScript = new AsyncFunction('github', 'context', 'core', 'require', 'process', script); +const tag = 'v2026.1004.34232'; +const flatpakPath = 'repo/packaging/linux/flatpak/modules/ffmpeg.json'; + +function source(name, arch) { + const entry = { + type: 'file', + url: `https://github.com/LizardByte/build-deps/releases/download/v2026.910.121303/${name}`, + sha256: '0'.repeat(64), + 'dest-filename': 'ffmpeg.tar.gz', + 'x-checker-data': {'type': 'json', 'version-query': '.tag_name'}, + }; + if (arch) entry['only-arches'] = [arch]; + return entry; +} + +function releaseFor(sources) { + return { + tag_name: tag, + body: 'Release notes\n\nWith multiple lines.', + assets: sources.map(entry => { + const name = new URL(entry.url).pathname.split('/').pop(); + return { + name, + browser_download_url: `https://github.com/LizardByte/build-deps/releases/download/${tag}/${name}`, + digest: `sha256:${'A'.repeat(64)}`, + }; + }), + }; +} + +function harness(manifests, release, requestedTag = tag) { + const files = new Map(Object.entries(manifests).map(([path, manifest]) => [path, JSON.stringify(manifest)])); + const writes = []; + const requests = []; + const outputs = {}; + const github = {rest: {repos: { + async getReleaseByTag(options) { + requests.push({method: 'tag', ...options}); + return {data: release}; + }, + async getLatestRelease(options) { + requests.push({method: 'latest', ...options}); + return {data: release}; + }, + }}}; + const fakeFs = { + existsSync: path => files.has(path), + readFileSync: path => files.get(path), + writeFileSync(path, content) { + writes.push(path); + files.set(path, content); + }, + }; + return { + files, writes, requests, outputs, + run: () => executeScript(github, {repo: {owner: 'LizardByte'}}, { + setOutput: (name, value) => { outputs[name] = value; }, + }, name => { + assert.equal(name, 'fs'); + return fakeFs; + }, {env: {BUILD_DEPS_VERSION: requestedTag}}), + }; +} + +for (const consumer of ['Sunshine', 'Koko']) { + test(`${consumer} refreshes its Flatpak sources for the selected submodule tag and preserves metadata`, async () => { + const sources = [source('Linux-x86_64-ffmpeg.tar.gz', 'x86_64'), source('Linux-aarch64-ffmpeg.tar.gz', 'aarch64')]; + const manifest = {name: 'ffmpeg-prebuilt', 'build-commands': ['tar -xzf ffmpeg.tar.gz'], sources}; + const release = releaseFor(sources); + const run = harness({[flatpakPath]: manifest}, release, tag); + await run.run(); + const updated = JSON.parse(run.files.get(flatpakPath)); + assert.deepEqual(run.requests, [{method: 'tag', owner: 'LizardByte', repo: 'build-deps', tag}]); + assert.equal(run.outputs.version, tag); + assert.equal(run.outputs.updated, 'true'); + assert.equal(run.outputs.body, release.body); + assert.deepEqual(updated['build-commands'], manifest['build-commands']); + updated.sources.forEach((entry, index) => { + assert.equal(entry.url, release.assets[index].browser_download_url); + assert.equal(entry.sha256, 'a'.repeat(64)); + assert.deepEqual(entry['only-arches'], sources[index]['only-arches']); + assert.deepEqual(entry['x-checker-data'], sources[index]['x-checker-data']); + assert.equal(entry['dest-filename'], 'ffmpeg.tar.gz'); + }); + }); +} + +test('One updates its submodule without requiring a Flatpak or native release inventory', async () => { + const release = releaseFor([]); + const run = harness({}, release); + await run.run(); + assert.deepEqual(run.requests, [{method: 'tag', owner: 'LizardByte', repo: 'build-deps', tag}]); + assert.equal(run.outputs.version, tag); + assert.equal(run.outputs.body, release.body); + assert.equal(run.outputs.updated, 'false'); + assert.deepEqual(run.writes, []); +}); + +test('a missing submodule tag cannot fall back to the latest release', async () => { + const run = harness({}, releaseFor([]), ''); + await assert.rejects(run.run(), /No build-deps submodule release tag/); + assert.deepEqual(run.requests, []); + assert.deepEqual(run.writes, []); +}); + +test('release metadata must match the selected submodule tag', async () => { + const release = {...releaseFor([]), tag_name: 'v2026.1005.10000'}; + const run = harness({}, release); + await assert.rejects(run.run(), /Expected release/); + assert.deepEqual(run.writes, []); +}); + +for (const problem of ['missing asset', 'duplicate asset', 'invalid digest', 'empty assets']) { + test(`${problem} responses leave every manifest unchanged`, async () => { + const flatpakSources = [ + source('Linux-x86_64-ffmpeg.tar.gz', 'x86_64'), source('Linux-aarch64-ffmpeg.tar.gz', 'aarch64'), + ]; + const release = releaseFor(flatpakSources); + if (problem === 'missing asset') release.assets.pop(); + if (problem === 'duplicate asset') release.assets.push({...release.assets.at(-1)}); + if (problem === 'invalid digest') release.assets.at(-1).digest = `sha256:${'a'.repeat(64)}:extra`; + if (problem === 'empty assets') release.assets = []; + const run = harness({[flatpakPath]: {sources: flatpakSources}}, release); + const original = new Map(run.files); + await assert.rejects(run.run()); + assert.deepEqual(run.writes, []); + assert.deepEqual(run.files, original); + }); +} + +test('a missing Flatpak architecture fails before rewriting its pins', async () => { + const sources = [source('Linux-x86_64-ffmpeg.tar.gz', 'x86_64')]; + const run = harness({[flatpakPath]: {sources}}, releaseFor(sources), tag); + await assert.rejects(run.run(), /Expected one Flatpak FFmpeg source for aarch64/); + assert.deepEqual(run.writes, []); +}); + +test('an already current manifest is not changed by another updater run', async () => { + const sources = [source('Linux-x86_64-ffmpeg.tar.gz', 'x86_64'), source('Linux-aarch64-ffmpeg.tar.gz', 'aarch64')]; + const run = harness({[flatpakPath]: {sources}}, releaseFor(sources), tag); + await run.run(); + const first = run.files.get(flatpakPath); + await run.run(); + assert.equal(run.files.get(flatpakPath), first); + assert.equal(run.outputs.updated, 'false'); + assert.deepEqual(run.writes, [flatpakPath]); +});