From 9fd51512e3f4277e44df520c21374d668be720f7 Mon Sep 17 00:00:00 2001 From: SethBurkart123 Date: Tue, 3 Nov 2026 17:28:55 +1100 Subject: [PATCH] Gate store releases on Seth's approval and include Firefox sources --- .github/actions/build-extension/action.yml | 9 +- .github/workflows/release.yml | 120 +++++++++++++ lib/publish.js | 187 ++++----------------- scripts/package-extension-zips.mjs | 55 ++++-- 4 files changed, 198 insertions(+), 173 deletions(-) create mode 100644 .github/workflows/release.yml diff --git a/.github/actions/build-extension/action.yml b/.github/actions/build-extension/action.yml index 57918b56..6293376a 100644 --- a/.github/actions/build-extension/action.yml +++ b/.github/actions/build-extension/action.yml @@ -2,6 +2,10 @@ name: Build Extension description: Install dependencies, build Chrome and Firefox extensions, and package zips. inputs: + include_sources: + description: Include the committed source and Firefox build instructions. + required: false + default: "false" gh_release_update_check: description: Enable GitHub release update checker in the built extension. required: false @@ -29,6 +33,9 @@ outputs: firefox_zip: description: Path to the Firefox extension zip value: ${{ steps.zip.outputs.firefox_zip }} + sources_zip: + description: Path to the Firefox source zip when include_sources is true + value: ${{ steps.zip.outputs.sources_zip }} runs: using: composite @@ -56,4 +63,4 @@ runs: env: UPDATE_CHANNEL: ${{ inputs.update_channel }} BUILD_LABEL: ${{ inputs.build_label }} - run: node scripts/package-extension-zips.mjs "${{ steps.version.outputs.version }}" + run: node scripts/package-extension-zips.mjs "${{ steps.version.outputs.version }}" "${{ inputs.include_sources == 'true' && '--sources' || '' }}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..c93f6ae5 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,120 @@ +name: Publish Stores + +on: + release: + types: [published] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: store-release-${{ github.ref }} + cancel-in-progress: false + +jobs: + build: + if: github.event_name == 'workflow_dispatch' || !github.event.release.prerelease + runs-on: windows-latest + permissions: + contents: write + defaults: + run: + shell: bash + outputs: + publisher_version: ${{ steps.publisher.outputs.version }} + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + + - name: Check release version + if: github.event_name == 'release' + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + run: test "${RELEASE_TAG#v}" = "$(node -p "require('./package.json').version")" + + - name: Build store packages + id: build + uses: ./.github/actions/build-extension + with: + include_sources: "true" + + - name: Check build + run: npm run lint && npm run test:unit && npm run test:smoke + + - name: Verify Firefox source rebuild + env: + SOURCES_ZIP: ${{ steps.build.outputs.sources_zip }} + run: | + mkdir "$RUNNER_TEMP/source-rebuild" + tar -xf "$SOURCES_ZIP" -C "$RUNNER_TEMP/source-rebuild" + cd "$RUNNER_TEMP/source-rebuild" + npm ci + npm run build:firefox + diff -qr "$GITHUB_WORKSPACE/dist/firefox" dist/firefox + + - name: Record publisher version and package checksums + id: publisher + run: | + echo "version=$(node -p "require('./node_modules/publish-browser-extension/package.json').version")" >> "$GITHUB_OUTPUT" + echo "Commit: $GITHUB_SHA" >> "$GITHUB_STEP_SUMMARY" + sha256sum dist/*.zip | tee -a "$GITHUB_STEP_SUMMARY" + + - name: Upload review artifacts + uses: actions/upload-artifact@v7 + with: + name: store-packages + path: dist/*.zip + if-no-files-found: error + + - name: Attach packages to release + if: github.event_name == 'release' + env: + GH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ github.event.release.tag_name }} + run: gh release upload "$RELEASE_TAG" dist/*.zip --clobber + + publish: + needs: build + runs-on: ubuntu-latest + environment: store-publishing + strategy: + fail-fast: false + matrix: + store: [chrome, firefox] + env: + DRY_RUN: ${{ github.event_name == 'workflow_dispatch' }} + steps: + - uses: actions/setup-node@v7 + with: + node-version: 26.x + + - uses: actions/download-artifact@v8 + with: + name: store-packages + path: dist + + - name: Install publisher without lifecycle scripts + env: + PUBLISHER_VERSION: ${{ needs.build.outputs.publisher_version }} + run: npm install --prefix "$RUNNER_TEMP/publisher" --ignore-scripts --no-audit --no-fund "publish-browser-extension@$PUBLISHER_VERSION" + + - name: Submit Chrome + if: matrix.store == 'chrome' + env: + CHROME_API_VERSION: v2 + CHROME_EXTENSION_ID: ${{ secrets.CHROME_EXTENSION_ID }} + CHROME_PUBLISHER_ID: ${{ secrets.CHROME_PUBLISHER_ID }} + CHROME_SERVICE_ACCOUNT_CLIENT_EMAIL: ${{ secrets.CHROME_SERVICE_ACCOUNT_CLIENT_EMAIL }} + CHROME_SERVICE_ACCOUNT_PRIVATE_KEY: ${{ secrets.CHROME_SERVICE_ACCOUNT_PRIVATE_KEY }} + run: '"$RUNNER_TEMP/publisher/node_modules/.bin/publish-extension" --chrome-zip dist/*-chrome.zip --dry-run "$DRY_RUN"' + + - name: Submit Firefox with sources + if: matrix.store == 'firefox' + env: + FIREFOX_EXTENSION_ID: ${{ secrets.FIREFOX_EXTENSION_ID }} + FIREFOX_JWT_ISSUER: ${{ secrets.FIREFOX_JWT_ISSUER }} + FIREFOX_JWT_SECRET: ${{ secrets.FIREFOX_JWT_SECRET }} + FIREFOX_CHANNEL: listed + run: '"$RUNNER_TEMP/publisher/node_modules/.bin/publish-extension" --firefox-zip dist/*-firefox.zip --firefox-sources-zip dist/*-sources.zip --dry-run "$DRY_RUN"' diff --git a/lib/publish.js b/lib/publish.js index f96a04d6..c43b7ba2 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -1,158 +1,35 @@ -/** - * @fileoverview - * This script is a command-line utility for publishing the BetterSEQTA+ extension. - * It automates the process of finding the latest built extension ZIP files for specified - * browsers, zipping the project source code (for Firefox), and then invoking the - * `publish-extension` tool with the appropriate arguments. - * - * To use this script, invoke it with Node.js followed by browser arguments: - * e.g., `node lib/publish.js --b chrome firefox` - * or `node lib/publish.js --b chrome` - * or `node lib/publish.js --b firefox` - */ +import { execFileSync } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; -import { globSync } from "node:fs"; -import semver from "semver"; -import { execSync } from "node:child_process"; - -function getLatestVersion(files) { - console.log("Files passed to getLatestVersion:", files); - - const versions = files - .map((file) => { - const match = file.match(/@([\d.]+)-/); - console.log("Matching file:", file, "Version found:", match ? match[1] : "None"); - - if (!match) return null; - - const fullVersion = match[1]; // Original version (e.g., 3.4.5.1) - // Trim to 3 parts for semver comparison, as semver typically handles X.Y.Z - const semverVersion = fullVersion.split(".").slice(0, 3).join("."); - - return { fullVersion, semverVersion }; - }) - .filter(Boolean); // Remove null entries if any file didn't match - - console.log( - "Extracted versions:", - versions.map((v) => v.semverVersion), - ); - - if (versions.length === 0) { - console.log("No versions extracted."); - return null; - } - - // Find latest version using the trimmed semver format - const latestSemver = semver.maxSatisfying( - versions.map((v) => v.semverVersion), - "*", // Satisfy any version, effectively finding the max - ); - console.log("Latest SemVer-compatible version:", latestSemver); - - if (!latestSemver) { - console.log("Could not determine latest semver version."); - return null; - } - - // Get the original full version string that matches the identified latest SemVer version - const latestVersionData = versions.find((v) => v.semverVersion === latestSemver); - const latestFullVersion = latestVersionData ? latestVersionData.fullVersion : null; - - console.log("Final selected latest version:", latestFullVersion); - return latestFullVersion; -} - -function getLatestFiles(browser) { - const pattern = `dist/betterseqtaplus@*-*${browser}.zip`; - console.log("Glob pattern:", pattern); - - const files = globSync(pattern); - console.log("Files found for browser", browser, ":", files); - - if (files.length === 0) { - console.log("No files found for browser", browser); - return undefined; - } - - const latestVersion = getLatestVersion(files); - if (!latestVersion) { - console.log("Could not determine latest version for browser", browser); - return undefined; - } - - // Find the exact file by matching the original full version string - const latestFile = files.find((file) => file.includes(`@${latestVersion}-`)); - - console.log("Latest file for browser", browser, ":", latestFile); - return latestFile; -} - -function zipSources() { - const zipFileName = `dist/betterseqtaplus@latest-sources.zip`; - - const excludePatterns = [ - "node_modules", - "dist", - ".env*", - ".git", - ".github", - ".vscode", - "LICENSE", - "package.json", - ] - .map((pattern) => `-x!${pattern}`) // Format for 7z exclude syntax - .join(" "); - - // Command to zip the current directory's contents into zipFileName, applying exclude patterns - const zipCommand = `7zz a ${zipFileName} . ${excludePatterns}`; - - console.log("Zipping project sources with command:", zipCommand); - execSync(zipCommand, { stdio: "inherit" }); // Execute synchronously and show output - - return zipFileName; -} - -function runPublishCommand(browsers) { - const chromeZip = browsers.includes("chrome") ? getLatestFiles("chrome") : null; - const firefoxZip = browsers.includes("firefox") ? getLatestFiles("firefox") : null; - // Sources are typically only needed for Firefox submissions - const firefoxSourcesZip = browsers.includes("firefox") ? zipSources() : null; - - console.log("Chrome zip:", chromeZip); - console.log("Firefox zip:", firefoxZip); - console.log("Firefox sources zip:", firefoxSourcesZip); - - if (browsers.length === 0) { - console.log("No browsers specified. Exiting."); - process.exit(0); // Exit gracefully if no action is needed - } - - // Check if required files are missing for the specified browsers - if ( - (browsers.includes("chrome") && !chromeZip) || - (browsers.includes("firefox") && (!firefoxZip || !firefoxSourcesZip)) - ) { - console.error("Could not find required zip files for specified browsers."); - process.exit(1); // Exit with error status - } - - let command = "publish-extension"; - if (chromeZip) { - command += ` --chrome-zip ${chromeZip}`; - } - if (firefoxZip && firefoxSourcesZip) { - command += ` --firefox-zip ${firefoxZip} --firefox-sources-zip ${firefoxSourcesZip}`; - } - - console.log("Running command:", command); - execSync(command, { stdio: "inherit" }); // Execute and show output -} - -// Parse command-line arguments to determine which browsers to publish for +process.chdir(fileURLToPath(new URL("..", import.meta.url))); const args = process.argv.slice(2); -const browserIndex = args.indexOf("--b"); // Find the --b flag -// If --b is found, take all subsequent arguments as browser names -const browsers = browserIndex !== -1 ? args.slice(browserIndex + 1) : []; +const browsers = args.filter((arg) => arg !== "--b" && arg !== "--dry-run"); +if (!browsers.length || browsers.some((browser) => !["chrome", "firefox"].includes(browser))) { + throw new Error("Usage: npm run publish -- chrome firefox [--dry-run]"); +} -runPublishCommand(browsers); +const { name, version } = JSON.parse(readFileSync("package.json", "utf8")); +if (browsers.includes("firefox")) { + execFileSync( + process.execPath, + ["scripts/package-extension-zips.mjs", version, "--sources-only"], + { + stdio: "inherit", + }, + ); +} + +const flags = browsers.flatMap((browser) => [ + `--${browser}-zip`, + `dist/${name}@${version}-${browser}.zip`, +]); +if (browsers.includes("firefox")) { + flags.push("--firefox-sources-zip", `dist/${name}@${version}-sources.zip`); +} +if (args.includes("--dry-run")) flags.push("--dry-run"); +execFileSync( + process.execPath, + [fileURLToPath(import.meta.resolve("publish-browser-extension/cli")), ...flags], + { stdio: "inherit" }, +); diff --git a/scripts/package-extension-zips.mjs b/scripts/package-extension-zips.mjs index 1a2f70ab..11e35fcc 100644 --- a/scripts/package-extension-zips.mjs +++ b/scripts/package-extension-zips.mjs @@ -12,24 +12,14 @@ function zipDirectory(sourceRel, outRel) { const sourceDir = join(root, sourceRel); const outZip = join(root, outRel); if (!existsSync(sourceDir)) throw new Error(`Missing build output: ${sourceRel}`); + const manifest = JSON.parse(readFileSync(join(sourceDir, "manifest.json"), "utf8")); + if (manifest.version !== version) throw new Error(`Rebuild ${sourceRel} for version ${version}`); mkdirSync(dirname(outZip), { recursive: true }); if (existsSync(outZip)) unlinkSync(outZip); if (process.platform === "win32") { - const esc = (s) => s.replace(/'/g, "''"); - execFileSync( - "powershell", - [ - "-NoProfile", - "-Command", - [ - "Add-Type -AssemblyName System.IO.Compression.FileSystem", - `[IO.Compression.ZipFile]::CreateFromDirectory('${esc(sourceDir)}', '${esc(outZip)}')`, - ].join("; "), - ], - { stdio: "inherit" }, - ); + execFileSync("tar", ["-a", "-cf", outZip, "-C", sourceDir, "."], { stdio: "inherit" }); } else { execFileSync("zip", ["-r", "-q", outZip, "."], { cwd: sourceDir, stdio: "inherit" }); } @@ -47,10 +37,41 @@ const base = const chromeZip = `dist/${base}-chrome.zip`; const firefoxZip = `dist/${base}-firefox.zip`; -zipDirectory("dist/chrome", chromeZip); -zipDirectory("dist/firefox", firefoxZip); -console.log(`Packaged ${chromeZip}\nPackaged ${firefoxZip}`); +if (!process.argv.includes("--sources-only")) { + zipDirectory("dist/chrome", chromeZip); + zipDirectory("dist/firefox", firefoxZip); + console.log(`Packaged ${chromeZip}\nPackaged ${firefoxZip}`); +} + +const sourcesZip = `dist/${base}-sources.zip`; +if (process.argv.includes("--sources") || process.argv.includes("--sources-only")) { + // Archive the committed source only, never local credentials or dependency folders. + execFileSync("git", ["diff", "--quiet", "HEAD", "--"], { cwd: root, stdio: "inherit" }); + mkdirSync(join(root, "dist"), { recursive: true }); + const instructions = + `Build environment: ${process.platform} ${process.arch}, Node.js ${process.version}.\n` + + "On Windows, run these commands in Git Bash:\n\n" + + "npm ci\n" + + "GH_RELEASE_UPDATE_CHECK=false UPDATE_CHANNEL=stable npm run build\n\n" + + "Firefox output: dist/firefox. No .env file is required.\n"; + execFileSync( + "git", + [ + "archive", + "--format=zip", + "--output", + join(root, sourcesZip), + `--add-virtual-file=BUILD.txt:${instructions}`, + "HEAD", + ], + { cwd: root, stdio: "inherit" }, + ); + console.log(`Packaged ${sourcesZip}`); +} if (process.env.GITHUB_OUTPUT) { - appendFileSync(process.env.GITHUB_OUTPUT, `chrome_zip=${chromeZip}\nfirefox_zip=${firefoxZip}\n`); + appendFileSync( + process.env.GITHUB_OUTPUT, + `chrome_zip=${chromeZip}\nfirefox_zip=${firefoxZip}\nsources_zip=${sourcesZip}\n`, + ); }