Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion .github/scripts/extension-checks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
//
// node .github/scripts/extension-checks.mjs package <unzipped-package-dir>
// node .github/scripts/extension-checks.mjs lint <web-ext-lint.json> <allowlist.json>
// node .github/scripts/extension-checks.mjs amo-result <exit-code> <web-ext-sign.log> <artifacts-dir>
//
// package: the zip has what both browsers need and nothing development-only.
// lint: `web-ext lint --output json` has no errors, and every warning is
// on the allowlist (each entry says why it is accepted). Allowlist
// entries that no longer match are reported, so the list shrinks
// when a warning is fixed.
// amo-result: what `web-ext sign --channel listed` achieved (see amoSignOutcome),
// printed as `status=` and `xpi=` lines for $GITHUB_OUTPUT.
//
// Problems are printed as GitHub annotations (::error / ::warning).
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
Expand Down Expand Up @@ -134,6 +137,27 @@ export function checkLint(report, allowlist) {
return { errors: report.errors, unexpected, allowed, unused: allowlist.filter((e) => !used.has(e)) };
}

// web-ext (pinned in extension-lint.sh and the release workflow) rejects with
// this when the version was submitted but AMO hasn't approved and signed it
// within --approval-timeout. Listed versions can wait for review, so that is
// not a failure: the signed file appears on AMO later.
const APPROVAL_TIMEOUT = 'Approval: timeout exceeded.';

/**
* The outcome of `web-ext sign --channel listed`.
* @param {{exitCode: number, log: string, files: string[]}} run
* exit code, combined output, and the files web-ext left in --artifacts-dir
* @returns {{status: 'signed', xpi: string} | {status: 'pending'} | {status: 'failed', reason: string}}
*/
export function amoSignOutcome({ exitCode, log, files }) {
const xpi = files.filter((f) => f.endsWith('.xpi')).sort().at(-1);
if (exitCode === 0 && xpi) return { status: 'signed', xpi };
if (log.includes(APPROVAL_TIMEOUT)) return { status: 'pending' };
if (exitCode === 0) return { status: 'pending' }; // submitted, nothing downloaded
const reason = log.match(/(?:WebExtError|Error): .*/)?.[0] ?? `web-ext sign exited with ${exitCode}`;
return { status: 'failed', reason };
}

const describe = (m) => `${m.code}: ${m.message}${m.file ? ` (${m.file})` : ''}`;
// Annotation text must stay on one line.
const oneLine = (s) => String(s).replace(/\r?\n/g, ' ');
Expand Down Expand Up @@ -163,7 +187,22 @@ function main([command, ...args]) {
console.log(`web-ext lint OK: 0 errors, ${allowed.length} allowed warnings.`);
return 0;
}
console.error('usage: extension-checks.mjs package <dir> | lint <report.json> <allowlist.json>');
if (command === 'amo-result' && args.length === 3) {
const [exitCode, logFile, artifactsDir] = args;
const outcome = amoSignOutcome({
exitCode: Number(exitCode),
log: existsSync(logFile) ? readFileSync(logFile, 'utf8') : '',
files: existsSync(artifactsDir) ? listFiles(artifactsDir) : [],
});
console.log(`status=${outcome.status}`);
if (outcome.status === 'signed') console.log(`xpi=${join(artifactsDir, outcome.xpi)}`);
if (outcome.status === 'failed') {
console.error(`::error::addons.mozilla.org submission failed: ${oneLine(outcome.reason)}`);
return 1;
}
return 0;
}
console.error('usage: extension-checks.mjs package <dir> | lint <report.json> <allowlist.json> | amo-result <exit-code> <log> <artifacts-dir>');
return 2;
}

Expand Down
64 changes: 63 additions & 1 deletion .github/scripts/extension-checks.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'nod
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { after, test } from 'node:test';
import { checkLint, checkPackage, listFiles, matches } from './extension-checks.mjs';
import { amoSignOutcome, checkLint, checkPackage, listFiles, main, matches } from './extension-checks.mjs';

const dirs = [];
after(() => dirs.forEach((d) => rmSync(d, { recursive: true, force: true })));
Expand Down Expand Up @@ -153,3 +153,65 @@ test('the checked-in allowlist is well formed', () => {
assert.ok(entry.reason?.length > 20, `${entry.code} needs a real reason`);
}
});

test('amoSignOutcome: a signed .xpi downloaded within the timeout', () => {
const outcome = amoSignOutcome({ exitCode: 0, log: 'Downloaded:\n./tts_study_assistant-0.3.5.xpi', files: ['tts_study_assistant-0.3.5.xpi'] });
assert.deepEqual(outcome, { status: 'signed', xpi: 'tts_study_assistant-0.3.5.xpi' });
});

test('amoSignOutcome: submitted but awaiting review is pending, not failed', () => {
const log = 'Waiting for approval...\nWebExtError: Approval: timeout exceeded. When approved the signed XPI file can be downloaded from https://addons.mozilla.org/developers/addon/x/versions/1';
assert.deepEqual(amoSignOutcome({ exitCode: 1, log, files: [] }), { status: 'pending' });
// --approval-timeout 0, or a success with nothing downloaded.
assert.deepEqual(amoSignOutcome({ exitCode: 0, log: 'Waiting for approval and download of signed XPI skipped.', files: [] }), { status: 'pending' });
});

test('amoSignOutcome: real errors fail with their message', () => {
// A validation timeout is a different context from approval.
for (const log of ['WebExtError: Validation: timeout exceeded.', 'Error: Upload failed: Unauthorized.\n{"detail": "Incorrect authentication credentials."}']) {
const outcome = amoSignOutcome({ exitCode: 1, log, files: [] });
assert.equal(outcome.status, 'failed', log);
assert.match(outcome.reason, /timeout exceeded|Unauthorized/);
}
assert.deepEqual(amoSignOutcome({ exitCode: 3, log: '', files: [] }), { status: 'failed', reason: 'web-ext sign exited with 3' });
});

test('amo-result command prints GitHub outputs and fails only on errors', () => {
const dir = mkdtempSync(join(tmpdir(), 'amo-'));
dirs.push(dir);
const artifacts = join(dir, 'artifacts');
mkdirSync(artifacts);
const log = join(dir, 'sign.log');
const run = (exitCode, text) => {
writeFileSync(log, text);
const lines = [];
const errors = [];
const [out, err] = [console.log, console.error];
console.log = (l) => lines.push(l);
console.error = (l) => errors.push(l);
try {
return { code: main(['amo-result', String(exitCode), log, artifacts]), lines, errors };
} finally {
[console.log, console.error] = [out, err];
}
};

assert.deepEqual(run(1, 'WebExtError: Approval: timeout exceeded.'), { code: 0, lines: ['status=pending'], errors: [] });
const failed = run(1, 'Error: Upload failed: Forbidden.');
assert.equal(failed.code, 1);
assert.deepEqual(failed.lines, ['status=failed']);
assert.match(failed.errors[0], /^::error::addons\.mozilla\.org submission failed: Error: Upload failed: Forbidden\.$/);

writeFileSync(join(artifacts, 'tts_study_assistant-0.3.5.xpi'), 'signed');
assert.deepEqual(run(0, 'Downloaded'), { code: 0, lines: ['status=signed', `xpi=${join(artifacts, 'tts_study_assistant-0.3.5.xpi')}`], errors: [] });
});

test('the release workflow signs with the web-ext version the lint script pins', () => {
// amoSignOutcome matches web-ext's approval-timeout message, so both must
// move together (and the allowlist is tuned to that version's linter).
const pinned = (path, re) => readFileSync(new URL(path, import.meta.url), 'utf8').match(re)?.[1];
const lint = pinned('./extension-lint.sh', /WEB_EXT_VERSION="\$\{WEB_EXT_VERSION:-([\d.]+)\}"/);
const release = pinned('../workflows/extension-release.yml', /WEB_EXT_VERSION: ([\d.]+)/);
assert.ok(lint, 'version not found in extension-lint.sh');
assert.equal(release, lint);
});
27 changes: 27 additions & 0 deletions .github/scripts/extension-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Lints an unzipped extension package with Mozilla's web-ext (the linter
# addons.mozilla.org runs on upload) and compares the result with the
# allowlist. Used by Extension CI and the release workflow, so both lint the
# same way with the same pinned version.
#
# .github/scripts/extension-lint.sh <unzipped-package-dir> <report-dir>
#
# Writes <report-dir>/lint.json (the raw report) and <report-dir>/lint.txt
# (the check's output) and exits non-zero on errors or unexpected warnings.
set -euo pipefail

# Pinned for reproducible results. To update: bump this, run Extension CI, and
# adjust extension-lint-allowlist.json for new or removed warnings
# (https://github.com/mozilla/web-ext/releases).
WEB_EXT_VERSION="${WEB_EXT_VERSION:-10.8.0}"

package="$1"
reports="$2"
scripts="$(dirname "$0")"
mkdir -p "$reports"

echo "web-ext ${WEB_EXT_VERSION}" > "$reports/lint.txt"
# web-ext exits non-zero on lint errors; the check below reports them.
npx --yes "web-ext@${WEB_EXT_VERSION}" lint --source-dir "$package" --output json > "$reports/lint.json" || true
# pipefail (set above): the check's exit code, not tee's.
node "$scripts/extension-checks.mjs" lint "$reports/lint.json" "$scripts/extension-lint-allowlist.json" | tee -a "$reports/lint.txt"
5 changes: 4 additions & 1 deletion .github/scripts/extension-release-notes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ previous="$(git tag -l 'extension-v*' --sort=v:refname | awk -v t="${tag}" '$0 =
range="${previous:+${previous}..}${tag}"

{
echo "Chrome extension **v${version}**: install by unzipping the attached file and loading it at \`chrome://extensions\` (Developer mode → Load unpacked), or upload the zip to the Chrome Web Store."
echo "Extension **v${version}** for Chrome and Firefox. The attached \`tts-study-assistant-extension-v${version}.zip\` is the package for both stores: upload it to the Chrome Web Store and to addons.mozilla.org."
echo
echo "- **Chrome:** unzip and load the folder at \`chrome://extensions\` (Developer mode → Load unpacked)."
echo "- **Firefox:** \`about:debugging#/runtime/this-firefox\` → Load Temporary Add-on → pick the zip. A \`tts-study-assistant-firefox-v${version}.xpi\`, when attached, is the version signed by addons.mozilla.org."
echo
echo "### Changes in \`extension/\`"
echo
Expand Down
10 changes: 6 additions & 4 deletions .github/scripts/extension-version-check.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env bash
# Fails unless the extension's version is higher than every version it must
# beat: main's manifest.json and the latest extension-v* release tag. The
# Chrome Web Store only accepts uploads with a higher version than the
# published one, so every shipped change needs a bump.
# beat: main's manifest.json and the latest extension-v* release tag. Both
# stores need it: the Chrome Web Store only accepts a higher version than the
# published one, and addons.mozilla.org rejects any version number it has seen
# before (even a rejected or deleted one). So every shipped change needs a bump.
#
# .github/scripts/extension-version-check.sh <new-version> <base-version> [latest-release-version]
set -euo pipefail
Expand All @@ -11,7 +12,8 @@ new="$1"
base="$2"
released="${3:-}"

# Chrome: 1-4 dot-separated integers, each 0-65535, no leading zeros.
# Chrome: 1-4 dot-separated integers, each 0-65535, no leading zeros. Every
# such version is also a valid Firefox version, so one rule covers both stores.
valid() {
[[ "$1" =~ ^(0|[1-9][0-9]{0,4})(\.(0|[1-9][0-9]{0,4})){0,3}$ ]] || return 1
local IFS=. part
Expand Down
16 changes: 3 additions & 13 deletions .github/workflows/extension-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ jobs:
runs-on: ubuntu-latest
needs: changes
if: needs.changes.outputs.extension == 'true'
env:
# Pinned for reproducible lint results. To update: bump this, run the
# job, and adjust extension-lint-allowlist.json for new or removed
# warnings (https://github.com/mozilla/web-ext/releases).
WEB_EXT_VERSION: 10.8.0
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
Expand All @@ -103,18 +98,13 @@ jobs:
- name: Check the package
run: node .github/scripts/extension-checks.mjs package "$RUNNER_TEMP/package"
- name: web-ext lint
run: |
set -o pipefail # the check's exit code, not tee's
# web-ext exits non-zero on lint errors; the check below reports them.
npx --yes "web-ext@${WEB_EXT_VERSION}" lint \
--source-dir "$RUNNER_TEMP/package" --output json > "$RUNNER_TEMP/lint.json" || true
node .github/scripts/extension-checks.mjs lint "$RUNNER_TEMP/lint.json" .github/scripts/extension-lint-allowlist.json \
| tee "$RUNNER_TEMP/lint.txt"
# The same script as the release workflow; web-ext is pinned in it.
run: .github/scripts/extension-lint.sh "$RUNNER_TEMP/package" "$RUNNER_TEMP"
- name: Summary
if: always()
run: |
{
echo "### Extension package and Firefox lint (web-ext ${WEB_EXT_VERSION})"
echo "### Extension package and Firefox lint"
echo ""
if [ -f "$RUNNER_TEMP/lint.txt" ]; then
echo '```'
Expand Down
Loading
Loading