Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
ci(desktop): sign Windows releases with Buildkite (#4576)
Ian G. Maia39d96ad0f6ed
diff --git a/.buildkite/commands/build-desktop-windows.ps1 b/.buildkite/commands/build-desktop-windows.ps1new file mode 100644index 000000000..282da39ff--- /dev/null+++ b/.buildkite/commands/build-desktop-windows.ps1@@ -0,0 +1,73 @@+$ErrorActionPreference = 'Stop'++function Invoke-Checked([scriptblock] $Command) {+ & $Command+ if ($LASTEXITCODE -ne 0) { throw "Command failed (exit $LASTEXITCODE): $Command" }+}++$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '../..')).Path+Set-Location $repoRoot+$config = Get-Content 'harper-desktop/src-tauri/tauri.conf.json' -Raw | ConvertFrom-Json+if ($env:BUILDKITE_TAG -and $env:BUILDKITE_TAG -ne "v$($config.version)") {+ throw "Release tag must match v$($config.version)"+}++Write-Host '--- :rust: Prepare Rust and build tools'+$cargoBin = if ($env:CARGO_HOME) { Join-Path $env:CARGO_HOME 'bin' } else { Join-Path $env:USERPROFILE '.cargo/bin' }+$env:PATH = "$cargoBin;$env:PATH"+if (-not (Get-Command rustup -ErrorAction SilentlyContinue)) {+ # The agent image has a Chocolatey Rust on PATH; rustup can coexist with it.+ $env:RUSTUP_INIT_SKIP_PATH_CHECK = 'yes'+ # rustup selects installer mode from its executable name.+ $rustupInstaller = Join-Path $env:TEMP 'rustup-init.exe'+ Invoke-WebRequest 'https://win.rustup.rs/x86_64' -OutFile $rustupInstaller+ Invoke-Checked { & $rustupInstaller -y --no-modify-path --default-toolchain stable --profile minimal }+}+Invoke-Checked { rustup update stable --no-self-update }+Invoke-Checked { rustup target add wasm32-unknown-unknown x86_64-pc-windows-msvc }++if (-not (Get-Command cargo-binstall -ErrorAction SilentlyContinue)) {+ $binstallInstaller = Join-Path $env:TEMP 'harper-install-binstall.ps1'+ Invoke-WebRequest 'https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1' -OutFile $binstallInstaller -UseBasicParsing+ Invoke-Checked { & $binstallInstaller }+}+Invoke-Checked { cargo binstall --no-confirm --force just wasm-pack }+Invoke-Checked { npm install --global [email protected] }+$pnpmPrefix = Invoke-Checked { npm prefix --global }+$env:PATH = "$pnpmPrefix;$env:PATH"++Write-Host '--- :lock: Set up Azure Trusted Signing'+$setupScript = (Get-Command setup_azure_trusted_signing.ps1 -ErrorAction Stop).Source+Invoke-Checked { & $setupScript }++# Absolute paths and separate arguments handle NSIS's working directory and spaces.+$signingConfig = Join-Path $env:TEMP "harper-windows-signing-$([guid]::NewGuid()).json"+$overlay = @{+ bundle = @{+ createUpdaterArtifacts = $false+ windows = @{+ signCommand = @{+ cmd = (Get-Command node -ErrorAction Stop).Source+ args = @((Join-Path $PSScriptRoot 'sign-windows.cjs'), '%1')+ }+ }+ }+} | ConvertTo-Json -Depth 5+[System.IO.File]::WriteAllText($signingConfig, $overlay, [System.Text.UTF8Encoding]::new($false))++try {+ Write-Host '--- :tauri: Build and sign Harper for Windows'+ Invoke-Checked { just build-desktop-windows-signed $signingConfig }++ $bundleDir = 'harper-desktop/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis'+ $installers = @(Get-ChildItem $bundleDir -Filter '*-setup.exe' -File)+ $expectedName = "Harper_$($config.version)_x64-setup.exe"+ if ($installers.Count -ne 1 -or $installers[0].Name -ne $expectedName) {+ throw "Expected exactly one Windows installer named $expectedName."+ }++ Write-Host '--- :mag: Verify the release installer'+ Invoke-Checked { & $env:SIGNTOOL_PATH verify /pa /all /v /tw $installers[0].FullName }+} finally {+ Remove-Item $signingConfig -ErrorAction SilentlyContinue+}diff --git a/.buildkite/commands/release-desktop-windows.sh b/.buildkite/commands/release-desktop-windows.shnew file mode 100644index 000000000..120e130d4--- /dev/null+++ b/.buildkite/commands/release-desktop-windows.sh@@ -0,0 +1,15 @@+#!/usr/bin/env bash+set -euo pipefail++cd "$(dirname "$0")/../.."+VERSION=$(node -p "require('./harper-desktop/src-tauri/tauri.conf.json').version")+INSTALLER="harper-desktop/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/Harper_${VERSION}_x64-setup.exe"++echo "--- :package: Download verified Windows installer"+# The Windows agent stores the artifact path with backslashes. Download patterns+# match the whole path, and the agent converts the separators when downloading.+buildkite-agent artifact download "*Harper_${VERSION}_x64-setup.exe" . --step build-desktop-windows+[[ -f "$INSTALLER" ]] || { echo "No Windows installer downloaded" >&2; exit 1; }++echo "--- :rocket: Upload Windows installer to the draft release"+node .buildkite/commands/upload-release.cjs "$BUILDKITE_TAG" "$INSTALLER"diff --git a/.buildkite/commands/sign-windows.cjs b/.buildkite/commands/sign-windows.cjsnew file mode 100644index 000000000..e77f5a45e--- /dev/null+++ b/.buildkite/commands/sign-windows.cjs@@ -0,0 +1,48 @@+const { execFileSync } = require('node:child_process');++// Tauri calls this for the app, NSIS uninstaller, installer, and bundled DLLs.+// setup_azure_trusted_signing.ps1 supplies these paths and Azure credentials.+function signWindows(file, env = process.env, run = execFileSync) {+ if (!file || !file.trim()) {+ throw new Error('Expected a file to sign.');+ }++ const required = ['SIGNTOOL_PATH', 'AZURE_CODE_SIGNING_DLIB', 'AZURE_METADATA_JSON'];+ const missing = required.filter((name) => !env[name]?.trim());+ if (missing.length) {+ throw new Error(`Azure signing setup is incomplete: ${missing.join(', ')}`);+ }++ run(+ env.SIGNTOOL_PATH,+ [+ 'sign',+ '/v',+ '/d',+ 'Harper',+ '/fd',+ 'SHA256',+ '/tr',+ env.AZURE_TIMESTAMP_SERVER || 'http://timestamp.acs.microsoft.com',+ '/td',+ 'SHA256',+ '/dlib',+ env.AZURE_CODE_SIGNING_DLIB,+ '/dmdf',+ env.AZURE_METADATA_JSON,+ file,+ ],+ { stdio: 'inherit' },+ );+ // Verify before packaging: Tauri later restores the original unsigned app binary.+ run(env.SIGNTOOL_PATH, ['verify', '/pa', '/all', '/v', '/tw', file], { stdio: 'inherit' });+}++if (require.main === module) {+ if (process.argv.length !== 3) {+ throw new Error('Usage: node sign-windows.cjs <file>');+ }+ signWindows(process.argv[2]);+}++module.exports = { signWindows };diff --git a/.buildkite/commands/sign-windows.test.cjs b/.buildkite/commands/sign-windows.test.cjsnew file mode 100644index 000000000..6dc791e22--- /dev/null+++ b/.buildkite/commands/sign-windows.test.cjs@@ -0,0 +1,92 @@+const assert = require('node:assert/strict');+const { test } = require('node:test');+const { signWindows } = require('./sign-windows.cjs');++const env = {+ SIGNTOOL_PATH: 'C:\\Program Files\\Windows Kits\\signtool.exe',+ AZURE_CODE_SIGNING_DLIB: 'C:\\Signing Tools\\Azure.CodeSigning.Dlib.dll',+ AZURE_METADATA_JSON: 'C:\\Signing Tools\\metadata.json',+};++test('signs and verifies the same file using separate arguments, including spaces', () => {+ const file = 'C:\\Build Output\\Harper_2.12.0_x64-setup.exe';+ const calls = [];+ signWindows(file, env, (...args) => calls.push(args));+ assert.deepEqual(calls, [+ [+ env.SIGNTOOL_PATH,+ [+ 'sign',+ '/v',+ '/d',+ 'Harper',+ '/fd',+ 'SHA256',+ '/tr',+ 'http://timestamp.acs.microsoft.com',+ '/td',+ 'SHA256',+ '/dlib',+ env.AZURE_CODE_SIGNING_DLIB,+ '/dmdf',+ env.AZURE_METADATA_JSON,+ file,+ ],+ { stdio: 'inherit' },+ ],+ [env.SIGNTOOL_PATH, ['verify', '/pa', '/all', '/v', '/tw', file], { stdio: 'inherit' }],+ ]);+});++for (const name of Object.keys(env)) {+ for (const value of [undefined, '', ' ']) {+ test(`rejects missing or blank ${name}: ${JSON.stringify(value)}`, () => {+ assert.throws(+ () =>+ signWindows('Harper.exe', { ...env, [name]: value }, () => assert.fail('must not sign')),+ new RegExp(name),+ );+ });+ }+}++test('rejects an absent file before calling SignTool', () => {+ for (const file of [undefined, '', ' ']) {+ assert.throws(() => signWindows(file, env, () => assert.fail('must not sign')), /file to sign/);+ }+});++test('does not verify or report success after signing fails', () => {+ const failure = new Error('Azure signing failed');+ const commands = [];+ assert.throws(+ () =>+ signWindows('Harper.exe', env, (_, args) => {+ commands.push(args[0]);+ throw failure;+ }),+ (error) => error === failure,+ );+ assert.deepEqual(commands, ['sign']);+});++test('fails the hook if signature verification fails', () => {+ const failure = new Error('Signature verification failed');+ assert.throws(+ () =>+ signWindows('Harper.exe', env, (_, args) => {+ if (args[0] === 'verify') throw failure;+ }),+ (error) => error === failure,+ );+});++test('uses the toolkit timestamp server when supplied', () => {+ const calls = [];+ signWindows(+ 'uninstall.exe',+ { ...env, AZURE_TIMESTAMP_SERVER: 'http://timestamp.example.test' },+ (_, args) => calls.push(args),+ );+ assert.equal(calls[0][calls[0].indexOf('/tr') + 1], 'http://timestamp.example.test');+});diff --git a/.buildkite/commands/upload-release.cjs b/.buildkite/commands/upload-release.cjsnew file mode 100644index 000000000..fa1014883--- /dev/null+++ b/.buildkite/commands/upload-release.cjs@@ -0,0 +1,142 @@+const { readFile, stat } = require('node:fs/promises');+const { basename } = require('node:path');+const { setTimeout: sleep } = require('node:timers/promises');++// Attach Windows artifacts to the tag's draft release. This script never creates+// a release: the macOS Buildkite step and the GitHub Actions workflows do that.+const REPOSITORY = 'Automattic/harper';++// The macOS step creates its draft earlier in the same build and is the only job+// that uploads a .dmg, so that draft is the one to use when GitHub Actions jobs+// have created others for the same tag.+function selectDraft(releases, tag) {+ const matches = releases.filter((release) => release.tag_name === tag);+ if (matches.some((release) => release.draft !== true)) {+ throw new Error(`Refusing to upload assets to published release ${tag}.`);+ }+ if (matches.length === 0) {+ throw new Error(+ `No draft release for ${tag} yet. Retry once the macOS or GitHub Actions jobs have created one.`,+ );+ }+ const candidates =+ matches.length === 1+ ? matches+ : matches.filter((release) => release.assets.some((asset) => asset.name.endsWith('.dmg')));+ if (candidates.length !== 1) {+ const ids = matches.map((release) => release.id).join(', ');+ throw new Error(+ `Expected one draft for ${tag} holding the macOS .dmg; found ${matches.length} drafts (ids ${ids}). Reconcile them before retrying.`,+ );+ }+ return candidates[0];+}++async function uploadRelease({ repository, tag, assets, token, request = fetch, wait = sleep }) {+ if (!token?.trim()) throw new Error('GITHUB_TOKEN is required.');+ if (!repository || !tag) throw new Error('A repository and release tag are required.');+ if (!assets.length) throw new Error('At least one release asset is required.');+ const names = assets.map((path) => basename(path));+ if (new Set(names).size !== names.length) {+ throw new Error('Release asset filenames must be unique.');+ }+ for (const path of assets) {+ if (!(await stat(path)).isFile()) throw new Error(`Not a release asset file: ${path}`);+ }++ const api = `https://api.github.com/repos/${repository}`;+ function transientError(method, url, error) {+ const wrapped = new Error(+ `GitHub ${method} ${url} failed: ${error.message}${error.cause ? `: ${error.cause.message}` : ''}`,+ { cause: error },+ );+ wrapped.transient = true;+ return wrapped;+ }+ async function json(url, { method = 'GET', body } = {}) {+ const response = await request(url, {+ method,+ headers: {+ Authorization: `Bearer ${token}`,+ Accept: 'application/vnd.github+json',+ 'X-GitHub-Api-Version': '2022-11-28',+ ...(body ? { 'Content-Type': 'application/octet-stream' } : {}),+ },+ body,+ signal: AbortSignal.timeout(10 * 60 * 1000),+ }).catch((error) => {+ throw transientError(method, url, error);+ });+ if (!response.ok) {+ const details = await response.json().catch(() => ({}));+ const error = new Error(+ `GitHub ${method} ${url} failed: HTTP ${response.status}: ${details.message || response.statusText}` ++ (details.errors ? ` ${JSON.stringify(details.errors)}` : ''),+ );+ error.status = response.status;+ error.transient = response.status >= 500 && response.status < 600;+ throw error;+ }+ if (response.status === 204) return null;+ // The connection can still drop while the body is read.+ return response.json().catch((error) => {+ throw transientError(method, url, error);+ });+ }+ async function list(path) {+ const items = [];+ for (let page = 1; ; page++) {+ const batch = await json(`${api}/${path}?per_page=100&page=${page}`);+ items.push(...batch);+ if (batch.length < 100) return items;+ }+ }++ // Network failures and 5xx responses are retried; refusals and 4xx are not.+ async function retry(label, action) {+ for (let attempt = 1; ; attempt++) {+ try {+ return await action();+ } catch (error) {+ if (!error.transient || attempt === 3) throw error;+ console.warn(`${error.message}. Retrying ${label} (${attempt + 1}/3).`);+ await wait(attempt * 1000);+ }+ }+ }++ const release = selectDraft(await retry('release lookup', () => list('releases')), tag);+ for (const path of assets) {+ const name = basename(path);+ const body = await readFile(path);+ await retry(name, async () => {+ const current = await json(`${api}/releases/${release.id}`);+ if (current.draft !== true) {+ throw new Error(`Refusing to upload assets to published release ${tag}.`);+ }+ // A failed upload can leave a partial asset. Re-list on each attempt.+ const existing = await list(`releases/${release.id}/assets`);+ for (const asset of existing.filter((asset) => asset.name === name)) {+ await json(`${api}/releases/assets/${asset.id}`, { method: 'DELETE' });+ }+ await json(+ `https://uploads.github.com/repos/${repository}/releases/${release.id}/assets?name=${encodeURIComponent(name)}`,+ { method: 'POST', body },+ );+ });+ }+ return release.html_url;+}++if (require.main === module) {+ const [tag, ...assets] = process.argv.slice(2);+ uploadRelease({ repository: REPOSITORY, tag, assets, token: process.env.GITHUB_TOKEN }).then(+ (url) => console.log(`Uploaded release assets to ${url}`),+ (error) => {+ console.error(error.message);+ process.exitCode = 1;+ },+ );+}++module.exports = { uploadRelease };diff --git a/.buildkite/commands/upload-release.test.cjs b/.buildkite/commands/upload-release.test.cjsnew file mode 100644index 000000000..3eae7fb80--- /dev/null+++ b/.buildkite/commands/upload-release.test.cjs@@ -0,0 +1,374 @@+const assert = require('node:assert/strict');+const { once } = require('node:events');+const { mkdtemp, rm, writeFile } = require('node:fs/promises');+const { createServer } = require('node:http');+const { tmpdir } = require('node:os');+const { basename, join } = require('node:path');+const { test } = require('node:test');+const { uploadRelease } = require('./upload-release.cjs');++async function fixture(t) {+ const directory = await mkdtemp(join(tmpdir(), 'Harper release '));+ t.after(() => rm(directory, { recursive: true, force: true }));+ const file = join(directory, 'Harper_9.0.0_x64-setup.exe');+ await writeFile(file, Buffer.from([0, 255, 1, 2]));+ const draft = {+ id: 123,+ tag_name: 'v9.0.0',+ draft: true,+ name: 'Release title',+ body: 'Maintainer release notes',+ html_url: 'https://github.com/example/harper/releases/123',+ assets: [],+ };+ const state = {+ releases: [draft],+ assets: [{ id: 1, name: 'Harper.dmg' }],+ calls: [],+ failure: undefined,+ };+ let nextId = 2;+ const request = async (url, options) => {+ const { method, body } = options;+ const { pathname, searchParams } = new URL(url);+ state.calls.push({ url, method });+ if (state.failure === method)+ return Response.json({ message: 'test failure' }, { status: 403 });+ if (method === 'GET') {+ if (pathname === '/repos/example/harper/releases/123') return Response.json(draft);+ const entries = pathname.endsWith('/assets') ? state.assets : state.releases;+ const offset = (Number(searchParams.get('page')) - 1) * 100;+ return Response.json(entries.slice(offset, offset + 100));+ }+ if (method === 'DELETE') {+ const id = Number(pathname.split('/').pop());+ state.assets = state.assets.filter((asset) => asset.id !== id);+ return new Response(null, { status: 204 });+ }+ assert.equal(method, 'POST');+ assert.equal(pathname, '/repos/example/harper/releases/123/assets');+ const asset = { id: nextId++, name: searchParams.get('name'), bytes: [...body] };+ state.assets.push(asset);+ return Response.json(asset, { status: 201 });+ };+ const options = {+ repository: 'example/harper',+ tag: 'v9.0.0',+ assets: [file],+ token: 'test-token',+ request,+ wait: t.mock.fn(async () => {}),+ };+ return { directory, file, draft, state, options };+}++test('uploads binary data and replaces only matching assets on a rerun over HTTP', async (t) => {+ const { file, draft, state, options } = await fixture(t);+ const metadata = structuredClone(draft);+ const server = createServer(async (request, response) => {+ try {+ assert.equal(request.headers.authorization, 'Bearer test-token');+ assert.equal(request.headers['x-github-api-version'], '2022-11-28');+ const chunks = [];+ for await (const chunk of request) chunks.push(chunk);+ if (request.method === 'POST') {+ assert.equal(request.headers['content-type'], 'application/octet-stream');+ }+ const result = await options.request(`https://api.github.com${request.url}`, {+ method: request.method,+ body: Buffer.concat(chunks),+ });+ response.writeHead(result.status, { 'Content-Type': 'application/json' });+ response.end(await result.text());+ } catch (error) {+ response.writeHead(500);+ response.end(error.message);+ }+ });+ server.listen(0, '127.0.0.1');+ await once(server, 'listening');+ t.after(() => new Promise((resolve) => server.close(resolve)));+ const request = (url, init) => {+ const parsed = new URL(url);+ return fetch(+ `http://127.0.0.1:${server.address().port}${parsed.pathname}${parsed.search}`,+ init,+ );+ };+ assert.equal(await uploadRelease({ ...options, request }), draft.html_url);+ await writeFile(file, Buffer.from([255, 0, 42]));+ await uploadRelease({ ...options, request });+ assert.deepEqual(state.releases, [metadata]);+ assert.deepEqual(state.assets, [+ { id: 1, name: 'Harper.dmg' },+ { id: 3, name: basename(file), bytes: [255, 0, 42] },+ ]);+ assert.equal(state.calls.filter((call) => call.method === 'DELETE').length, 1);+});++test('attaches to the draft holding the macOS dmg when Actions created another draft', async (t) => {+ const { draft, state, options } = await fixture(t);+ state.releases = [+ { ...draft, id: 124, assets: [{ id: 8, name: 'harper-chrome-plugin.zip' }] },+ { ...draft, assets: [{ id: 9, name: 'Harper_9.0.0_universal.dmg' }] },+ ];+ assert.equal(await uploadRelease(options), draft.html_url);+ const uploads = state.calls.filter((call) => call.method === 'POST');+ assert.equal(uploads.length, 1);+ assert.match(uploads[0].url, /\/releases\/123\/assets\?/);+});++for (const scenario of [+ 'missing',+ 'other-tag',+ 'duplicate',+ 'duplicate-dmg',+ 'published',+ 'published-and-draft',+]) {+ test(`rejects ${scenario} releases before any writes`, async (t) => {+ const { draft, state, options } = await fixture(t);+ const dmg = { id: 9, name: 'Harper_9.0.0_universal.dmg' };+ state.releases = {+ missing: [],+ 'other-tag': [{ ...draft, tag_name: 'v8.0.0' }],+ duplicate: [draft, { ...draft, id: 124 }],+ 'duplicate-dmg': [+ { ...draft, assets: [dmg] },+ { ...draft, id: 124, assets: [dmg] },+ ],+ published: [{ ...draft, draft: false }],+ 'published-and-draft': [draft, { ...draft, id: 124, draft: false }],+ }[scenario];+ await assert.rejects(+ uploadRelease(options),+ /No draft release|Expected one draft|published release/,+ );+ assert.ok(state.calls.every((call) => call.method === 'GET'));+ });+}++test('paginates releases and assets before selecting and replacing', async (t) => {+ const { file, draft, state, options } = await fixture(t);+ state.releases = [+ ...Array.from({ length: 100 }, (_, id) => ({ id, tag_name: `old-${id}` })),+ draft,+ ];+ state.assets = [+ ...Array.from({ length: 100 }, (_, id) => ({ id, name: `other-${id}` })),+ { id: 200, name: basename(file) },+ ];+ await uploadRelease(options);+ assert.equal(state.assets.length, 101);+ assert.equal(state.assets.filter((asset) => asset.name === basename(file)).length, 1);+ assert.ok(+ state.calls.some(+ (call) => call.url.endsWith('/releases/assets/200') && call.method === 'DELETE',+ ),+ );+});++test('does not overlook a conflicting release on a later page', async (t) => {+ const { draft, state, options } = await fixture(t);+ state.releases = [+ draft,+ ...Array.from({ length: 99 }, (_, id) => ({ id, tag_name: `old-${id}` })),+ { ...draft, id: 124 },+ ];+ await assert.rejects(uploadRelease(options), /found 2/);+ assert.ok(state.calls.every((call) => call.method === 'GET'));+});++for (const scenario of [+ 'no-token',+ 'no-tag',+ 'no-assets',+ 'missing-file',+ 'directory',+ 'duplicate-name',+]) {+ test(`rejects ${scenario} before contacting GitHub`, async (t) => {+ const { directory, file, state, options } = await fixture(t);+ const overrides = {+ 'no-token': { token: ' ' },+ 'no-tag': { tag: '' },+ 'no-assets': { assets: [] },+ 'missing-file': { assets: [file, join(directory, 'missing.exe')] },+ directory: { assets: [directory] },+ 'duplicate-name': { assets: [file, file] },+ };+ await assert.rejects(uploadRelease({ ...options, ...overrides[scenario] }));+ assert.deepEqual(state.calls, []);+ });+}++for (const method of ['GET', 'DELETE', 'POST']) {+ test(`propagates ${method} failures without continuing uploads`, async (t) => {+ const { file, state, options } = await fixture(t);+ state.assets.push({ id: 2, name: basename(file) });+ state.failure = method;+ await assert.rejects(uploadRelease(options), /HTTP 403: test failure/);+ assert.equal(state.calls.at(-1).method, method);+ assert.equal(options.wait.mock.callCount(), 0);+ });+}++for (const failure of ['upload', 'delete', 'list']) {+ test(`recovers from a transient ${failure} failure by re-listing assets`, async (t) => {+ const { file, state, options } = await fixture(t);+ state.assets.push({ id: 200, name: basename(file) });+ let failed = false;+ const request = async (url, init) => {+ const result = await options.request(url, init);+ const matches = {+ upload: init.method === 'POST',+ delete: init.method === 'DELETE',+ list: init.method === 'GET' && url.includes('/assets?'),+ };+ if (!failed && matches[failure]) {+ failed = true;+ if (failure === 'upload') {+ state.assets.at(-1).state = 'starter';+ state.assets.at(-1).bytes = [];+ }+ return Response.json({ message: 'Temporary upstream failure' }, { status: 502 });+ }+ return result;+ };+ await uploadRelease({ ...options, request });+ assert.equal(failed, true);+ assert.equal(state.assets.length, 2);+ assert.deepEqual(state.assets[0], { id: 1, name: 'Harper.dmg' });+ assert.deepEqual(state.assets[1].bytes, [0, 255, 1, 2]);+ assert.equal(state.assets[1].state, undefined);+ assert.deepEqual(+ options.wait.mock.calls.map((call) => call.arguments),+ [[1000]],+ );+ assert.equal(+ state.calls.filter((call) => call.method === 'GET' && call.url.includes('/assets?')).length,+ 2,+ );+ });+}++test('retries an upload after a network failure', async (t) => {+ const { state, options } = await fixture(t);+ let failed = false;+ const request = async (url, init) => {+ if (init.method === 'POST' && !failed) {+ failed = true;+ throw new Error('fetch failed', { cause: new Error('read ECONNRESET') });+ }+ return options.request(url, init);+ };+ await uploadRelease({ ...options, request });+ assert.equal(state.calls.filter((call) => call.method === 'POST').length, 1);+ assert.deepEqual(+ options.wait.mock.calls.map((call) => call.arguments),+ [[1000]],+ );+});++test('retries the release lookup after a transient failure', async (t) => {+ const { draft, options } = await fixture(t);+ let failed = false;+ const request = async (url, init) => {+ if (init.method === 'GET' && url.includes('/releases?') && !failed) {+ failed = true;+ return Response.json({ message: 'Unavailable' }, { status: 503 });+ }+ return options.request(url, init);+ };+ assert.equal(await uploadRelease({ ...options, request }), draft.html_url);+ assert.deepEqual(+ options.wait.mock.calls.map((call) => call.arguments),+ [[1000]],+ );+});++test('retries an upload whose response body cannot be read', async (t) => {+ const { file, state, options } = await fixture(t);+ let failed = false;+ const request = async (url, init) => {+ const result = await options.request(url, init);+ if (init.method === 'POST' && !failed) {+ failed = true;+ const broken = new ReadableStream({+ start(controller) {+ controller.error(new Error('read ECONNRESET'));+ },+ });+ return new Response(broken, { status: 201, headers: { 'Content-Type': 'application/json' } });+ }+ return result;+ };+ await uploadRelease({ ...options, request });+ assert.equal(state.calls.filter((call) => call.method === 'POST').length, 2);+ assert.equal(state.assets.filter((asset) => asset.name === basename(file)).length, 1);+ assert.deepEqual(+ options.wait.mock.calls.map((call) => call.arguments),+ [[1000]],+ );+});++test('stops after three attempts when the upload keeps returning 5xx', async (t) => {+ const { state, options } = await fixture(t);+ const request = async (url, init) => {+ const result = await options.request(url, init);+ return init.method === 'POST'+ ? Response.json({ message: 'Unavailable' }, { status: 503 })+ : result;+ };+ await assert.rejects(uploadRelease({ ...options, request }), /HTTP 503: Unavailable/);+ assert.equal(state.calls.filter((call) => call.method === 'POST').length, 3);+ assert.deepEqual(+ options.wait.mock.calls.map((call) => call.arguments),+ [[1000], [2000]],+ );+});++test('refuses to retry an upload if the draft has been published', async (t) => {+ const { draft, state, options } = await fixture(t);+ const request = async (url, init) => {+ const result = await options.request(url, init);+ if (init.method === 'POST') {+ draft.draft = false;+ return Response.json({ message: 'Unavailable' }, { status: 503 });+ }+ return result;+ };+ await assert.rejects(uploadRelease({ ...options, request }), /published release/);+ assert.equal(state.calls.filter((call) => call.method === 'POST').length, 1);+ assert.equal(state.calls.filter((call) => call.method === 'DELETE').length, 0);+});++test('includes GitHub validation details and does not retry 422 responses', async (t) => {+ const { options } = await fixture(t);+ const request = t.mock.fn(async () =>+ Response.json(+ {+ message: 'Validation Failed',+ errors: [{ resource: 'ReleaseAsset', field: 'name', code: 'already_exists' }],+ },+ { status: 422 },+ ),+ );+ await assert.rejects(+ uploadRelease({ ...options, request }),+ /HTTP 422: Validation Failed.*already_exists/,+ );+ assert.equal(request.mock.callCount(), 1);+ assert.equal(options.wait.mock.callCount(), 0);+});++test('reports the request and underlying network error', async (t) => {+ const { options } = await fixture(t);+ const request = async () => {+ throw new Error('fetch failed', { cause: new Error('connect ETIMEDOUT') });+ };+ await assert.rejects(+ uploadRelease({ ...options, request }),+ /GitHub GET .*fetch failed: connect ETIMEDOUT/,+ );+});diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.ymlindex ac241ca3c..4e43e1756 100644--- a/.buildkite/pipeline.yml+++ b/.buildkite/pipeline.yml@@ -8,7 +8,7 @@ env:IMAGE_ID: $IMAGE_IDsteps:- - label: ":tauri: Build, sign, notarize harper-desktop"+ - label: ":mac: Build, sign, notarize harper-desktop for macOS"key: build-desktopcommand: .buildkite/commands/build-desktop.shplugins: [$CI_TOOLKIT_PLUGIN, $NVM_PLUGIN]@@ -20,4 +20,30 @@ steps:- harper-desktop/src-tauri/target/*-apple-darwin/release/bundle/macos/*.app.tar.gz.signotify:- github_commit_status:- context: "Build, sign, notarize harper-desktop"+ context: "Build, sign, notarize harper-desktop for macOS"++ - label: ":windows: Build and sign harper-desktop for Windows"+ key: build-desktop-windows+ agents:+ queue: windows+ command: powershell -NoProfile -File .buildkite/commands/build-desktop-windows.ps1+ plugins: [$CI_TOOLKIT_PLUGIN, $NVM_PLUGIN]+ artifact_paths:+ - harper-desktop/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe+ notify:+ - github_commit_status:+ context: "Build and sign harper-desktop for Windows"++ - label: ":github: Upload harper-desktop release for Windows"+ key: release-desktop-windows+ if: build.tag =~ /^v[0-9]+/+ depends_on:+ - build-desktop-windows+ # Let the existing macOS release creator finish before looking for a draft.+ - step: build-desktop+ allow_failure: true+ command: bash .buildkite/commands/release-desktop-windows.sh+ plugins: [$NVM_PLUGIN]+ notify:+ - github_commit_status:+ context: "Upload harper-desktop release for Windows"diff --git a/.github/workflows/desktop_windows.yml b/.github/workflows/desktop_windows.ymlindex 9339d4791..4b42f0369 100644--- a/.github/workflows/desktop_windows.yml+++ b/.github/workflows/desktop_windows.yml@@ -3,7 +3,6 @@ name: Harper Desktop for Windowson:push:branches: ["master"]- tags: ["v*"]pull_request:branches: ["master"]merge_group:@@ -12,11 +11,24 @@ env:CARGO_TERM_COLOR: alwaysjobs:+ desktop-release-tests:+ name: just test-desktop-release+ runs-on: ubuntu-latest+ permissions:+ contents: read+ steps:+ - uses: actions/[email protected]+ - uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0+ - uses: actions/setup-node@v7+ with:+ node-version-file: ".node-version"+ - run: just test-desktop-release+desktop-windows:name: just build-desktop-windowsruns-on: ubuntu-latestpermissions:- contents: write+ contents: readsteps:- uses: actions/[email protected]- uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0@@ -54,8 +66,6 @@ jobs:run: cargo binstall wasm-pack --force --no-confirm- name: Install `cargo-xwin`run: cargo binstall cargo-xwin --force --no-confirm- - name: Install `tauri-cli`- run: cargo binstall tauri-cli --force --no-confirm- name: Build Harper Desktop for Windowsrun: just build-desktop-windows- name: Upload artifacts@@ -64,10 +74,4 @@ jobs:name: harper-desktop-windows-x64path: harper-desktop/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*if-no-files-found: error- - name: Release artifacts- if: startsWith(github.ref, 'refs/tags/v')- uses: ncipollo/release-action@339a81892b84b4eeb0f6e744e4574d79d0d9b8dd # v1.21.0- with:- artifacts: harper-desktop/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*- allowUpdates: true- draft: true+ # Signed Windows release assets are built and uploaded by Buildkite.diff --git a/biome.json b/biome.jsonindex a8904b80d..e8a3e274b 100644--- a/biome.json+++ b/biome.json@@ -8,6 +8,7 @@"files": {"ignoreUnknown": true,"includes": [+ ".buildkite/**/*.cjs","**/packages/**/*","**/harper-desktop/**/*","**/*.json",diff --git a/fastlane/Fastfile b/fastlane/Fastfileindex 875118649..80efd347d 100644--- a/fastlane/Fastfile+++ b/fastlane/Fastfile@@ -27,21 +27,18 @@ ASC_API_KEY_ENV_VARS = %w[require 'tempfile'require 'fastlane/plugin/wpmreleasetoolkit'-# Convenience alias — wpmreleasetoolkit 14.4+ namespaces this under-# Fastlane::Wpmreleasetoolkit but exposes no top-level shortcut.EnvManager = Fastlane::Wpmreleasetoolkit::EnvManagerbefore_all do- # Ensures match works correctly in CI by setting up a temporary keychain.- # No-op locally, so safe to run unconditionally.+ # Use a temporary signing keychain in CI.setup_ci- # No `.env` file in this repo today — env vars come from the Buildkite agent.- # `set_up` still needed so EnvManager has a configured instance.+ # Initialize EnvManager with values supplied by the Buildkite agent.EnvManager.set_up(env_file_name: 'harper-desktop.env')end-desc 'Fetch the Developer ID Application certificate into the keychain'+# Fetch the Developer ID Application certificate into the keychain.+# @param readonly [Boolean] Use existing certificates without creating new ones.lane :set_up_signing do |readonly: true|CODE_SIGNING_ENV_VARS.each { |k| EnvManager.get_required_env!(k) }ASC_API_KEY_ENV_VARS.each { |k| EnvManager.get_required_env!(k) } unless readonly@@ -58,7 +55,8 @@ lane :set_up_signing do |readonly: true|)end-desc 'Submit a signed .app or .dmg to Apple for notarization and staple the ticket'+# Notarize a signed macOS package and staple its ticket.+# @param package [String] Path to the signed .app or .dmg.lane :notarize_macos do |package:|ASC_API_KEY_ENV_VARS.each { |k| EnvManager.get_required_env!(k) }@@ -69,7 +67,11 @@ lane :notarize_macos do |package:|)end-desc 'Create a draft GitHub release for harper-desktop with the signed/notarized macOS artifacts attached'+# Create a draft release with the signed and notarized macOS artifacts.+# @param tag [String] Release tag.+# @param dmg [String] Path to the signed disk image.+# @param app_tarball [String] Path to the app updater archive.+# @param app_signature [String] Path to the updater archive signature.lane :create_desktop_github_release do |tag:, dmg:, app_tarball:, app_signature:|EnvManager.get_required_env!('GITHUB_TOKEN')diff --git a/justfile b/justfileindex cad622706..12f022f8e 100644--- a/justfile+++ b/justfile@@ -222,6 +222,19 @@ build-desktop-windows: build-harperjs build-lint-framework build-components builpnpm installpnpm tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc -b nsis --config '{"bundle":{"createUpdaterArtifacts":false}}'+# Build native Windows bundles using a CI-generated Azure signing config.+build-desktop-windows-signed signing_config: build-harperjs build-lint-framework build-components build-harper-editor+ #!/usr/bin/env bash+ set -eo pipefail++ cd "{{justfile_directory()}}/harper-desktop"+ pnpm install+ pnpm tauri build --target x86_64-pc-windows-msvc -b nsis --config {{quote(signing_config)}}++# Test desktop signing and release scripts without CI credentials.+test-desktop-release:+ node --test .buildkite/commands/*.test.cjs+# Build Harper Desktop for Apple Silicon only — faster than the universal recipe below.build-desktop-macos-arm64: build-harperjs build-lint-framework build-components build-harper-editor#!/usr/bin/env bashdiff --git a/packages/web/src/routes/docs/contributors/desktop/+page.md b/packages/web/src/routes/docs/contributors/desktop/+page.mdindex c1bf71a3f..315a00ab4 100644--- a/packages/web/src/routes/docs/contributors/desktop/+page.md+++ b/packages/web/src/routes/docs/contributors/desktop/+page.md@@ -2,10 +2,8 @@title: Harper Desktop----Harper Desktop aims to (eventually) serve the needs of users on a variety of platforms.-One day, we hope for it to include support for macOS, Windows, and Linux.-Right now, it just supports macOS.-Because we hope to add new platforms, there are structures in the code that would not make sense for an app specifically designed with only one operating system in mind.+Harper Desktop supports macOS and Windows, and we hope to add Linux support in the future.+The code separates platform-specific behavior from the parts shared across operating systems to make adding new platforms easier.This document will serve to give a tour of how the code for the Desktop app is structured and, more importantly, why it is structured that way.@@ -54,7 +52,7 @@ When the user changes a setting via a Tauri window, that setting is propagated fThe latter step happens in the `communication` module.The communication with the operating system happens inside of a `Broker`, which is an interface that exposes any and all platform-specific functions Harper needs.-Right now, there is only one implementation of `Broker`: `MacBroker`.+The platform implementations are `MacBroker` and `WindowsBroker`.Finally, the communication with the user happens via a `winit` window, to which we render [`egui`](https://github.com/emilk/egui) elements.The exact structure of the relevant modules for this is likely subject to change and may vary by platform, so I will forgo including additional information here to avoid misleading you.@@ -66,3 +64,34 @@ I want to emphasize this point: the highlighter process does not store any canonHarper Desktop resides in the Harper monorepo, but is not included in the `cargo` workspace. Why? It is because the Desktop app includes (through Tauri) far too many dependencies.Small changes in unrelated crates would often cause large rebuilds and rechecks.To solve it, we simply removed it from the dependency graph.++## Windows Builds and Signing++Buildkite builds and signs the Windows x64 installer with Automattic's certificate.+The build fails if signing or signature verification fails.+GitHub Actions also builds an unsigned installer to check changes, including fork pull requests.++Run `just test-desktop-release` to check the signing and upload scripts.++### Releases++Release tags must use `v<version>`, where `<version>` matches the version in `harper-desktop/src-tauri/tauri.conf.json`.+Buildkite attaches the signed installer to an existing draft release, which a maintainer publishes manually.+If there are multiple drafts for the tag, exactly one must contain the macOS `.dmg` for the upload to proceed.++If an upload fails, resolve the error and retry only the Windows upload step.+A retry replaces the Windows installer without changing release notes or other assets.+Avoid rebuilding the entire tag, which can create another draft.++Before publishing, check that the draft includes the signed Windows installer, the macOS build, and all other expected assets.+Copy any missing assets and release notes from other drafts before deleting those drafts.+Confirm that the Windows installer's SHA256 matches the Buildkite artifact.++### Testing on Windows++Before publishing a release, or after changing the signing setup, download the installer from Buildkite and test it on Windows:++1. Check the installer's signature with `Get-AuthenticodeSignature`. Expect `Status: Valid` and an Automattic signer.+2. Install and launch Harper, open its settings, and check that the highlighter starts.+3. Check the signatures of `harper-desktop.exe` and `uninstall.exe` in the installation directory the same way.+4. Quit Harper from the tray menu, uninstall it, and confirm that the installation directory is gone.