From 1d4c4055bd7a61a3d00e402d9e218c281b9c1e36 Mon Sep 17 00:00:00 2001 From: Henning Muszynski Date: Wed, 11 Nov 2026 10:54:31 +0100 Subject: [PATCH] fix: migrate Comms CLI to todoist owner and package scope --- .agents/checks/architecture.md | 2 +- .agents/checks/evidence.md | 2 +- .agents/scripts/instructions.test.ts | 4 +- .agents/skills/review-changes/SKILL.md | 2 +- .agents/skills/verify/SKILL.md | 2 +- .agents/skills/write-tests/SKILL.md | 2 +- .github/workflows/issue-automation.yml | 2 +- .github/workflows/publish-release.yml | 140 +++++ .github/workflows/release.yml | 158 ++---- .github/workflows/update-comms-sdk.yml | 16 +- AGENTS.md | 6 +- CODEBASE.md | 20 +- CONTRIBUTING.md | 10 +- README.md | 9 +- docs/SPEC.md | 4 +- docs/icon.md | 4 +- docs/releasing.md | 242 +++++++++ oauth/client-metadata.json | 2 +- package.json | 16 +- release.config.js | 54 +- scripts/artifact-source.mjs | 107 ++++ scripts/artifact-source.test.ts | 127 +++++ scripts/github-pages.mjs | 25 + scripts/github-pages.test.ts | 56 ++ scripts/github-release-record.mjs | 302 +++++++++++ scripts/github-release-record.test.ts | 306 +++++++++++ scripts/pending-release.mjs | 117 +++++ scripts/pending-release.test.ts | 65 +++ scripts/prepare-release.mjs | 38 ++ scripts/record-release.mjs | 94 ++++ scripts/record-release.test.ts | 78 +++ scripts/registry-release.mjs | 209 ++++++++ scripts/registry-release.test.ts | 234 +++++++++ scripts/release-analysis.mjs | 27 + scripts/release-analysis.test.ts | 55 ++ scripts/release-policy.mjs | 81 +++ scripts/release-policy.test.ts | 131 +++++ scripts/release.mjs | 155 ++++++ scripts/release.test.ts | 91 ++++ scripts/source-release-record.mjs | 228 ++++++++ scripts/source-release-record.test.ts | 487 ++++++++++++++++++ scripts/validate-source-run.mjs | 33 ++ skills/comms-cli/SKILL.md | 2 + src/commands/account/account.test.ts | 2 +- src/commands/account/index.ts | 4 +- src/commands/auth/auth.test.ts | 10 +- src/commands/auth/helpers.ts | 2 +- src/commands/auth/index.ts | 2 +- src/commands/auth/login.ts | 2 +- src/commands/auth/logout.ts | 2 +- src/commands/auth/status.ts | 4 +- src/commands/auth/store-wrap.ts | 2 +- src/commands/changelog.test.ts | 4 +- src/commands/changelog.ts | 4 +- src/commands/channel/channel.test.ts | 2 +- src/commands/channel/create.ts | 2 +- src/commands/channel/helpers.ts | 2 +- src/commands/channel/list.ts | 4 +- src/commands/channel/members.test.ts | 2 +- src/commands/channel/members.ts | 4 +- src/commands/channel/membership-helpers.ts | 2 +- src/commands/channel/set.ts | 2 +- src/commands/channel/threads.test.ts | 2 +- src/commands/channel/threads.ts | 4 +- src/commands/channel/update.ts | 2 +- src/commands/comment/comment.test.ts | 2 +- src/commands/config/config.test.ts | 4 +- src/commands/config/view.ts | 2 +- .../conversation/conversation.test.ts | 8 +- src/commands/conversation/helpers.ts | 4 +- src/commands/conversation/list.ts | 2 +- src/commands/conversation/reply.ts | 11 +- src/commands/conversation/unread.ts | 2 +- src/commands/conversation/view.ts | 2 +- src/commands/conversation/with.ts | 2 +- src/commands/doctor.test.ts | 2 +- src/commands/groups/groups.test.ts | 2 +- src/commands/groups/list.ts | 2 +- src/commands/groups/view.ts | 2 +- src/commands/inbox.test.ts | 2 +- src/commands/inbox.ts | 4 +- src/commands/mentions.test.ts | 2 +- src/commands/migrate/migrate.test.ts | 6 +- src/commands/migrate/urls.ts | 2 +- src/commands/msg/msg.test.ts | 2 +- src/commands/react.test.ts | 2 +- src/commands/search.test.ts | 2 +- src/commands/skill/skill.test.ts | 2 +- src/commands/status/helpers.ts | 2 +- src/commands/status/set.ts | 2 +- src/commands/status/status.test.ts | 2 +- src/commands/thread/create.ts | 2 +- src/commands/thread/helpers.ts | 2 +- src/commands/thread/read.ts | 2 +- src/commands/thread/reply.ts | 2 +- src/commands/thread/thread.test.ts | 9 +- src/commands/thread/unread.ts | 2 +- src/commands/thread/view.ts | 2 +- src/commands/update/index.ts | 2 +- src/commands/update/update.test.ts | 6 +- src/commands/user.test.ts | 2 +- src/commands/user.ts | 2 +- src/commands/view.test.ts | 2 +- src/commands/workspace.ts | 2 +- src/index.ts | 2 +- src/lib/api.test.ts | 18 +- src/lib/api.ts | 2 +- src/lib/attachments.test.ts | 14 +- src/lib/attachments.ts | 9 +- src/lib/auth-pages.ts | 2 +- src/lib/auth-provider.test.ts | 4 +- src/lib/auth-provider.ts | 2 +- src/lib/auth.test.ts | 6 +- src/lib/auth.ts | 2 +- src/lib/config.test.ts | 6 +- src/lib/config.ts | 2 +- src/lib/errors.test.ts | 2 +- src/lib/errors.ts | 6 +- src/lib/global-args.ts | 4 +- src/lib/input.ts | 2 +- src/lib/markdown.ts | 2 +- src/lib/oauth-client-metadata.test.ts | 2 +- src/lib/options.ts | 2 +- src/lib/output.test.ts | 2 +- src/lib/output.ts | 2 +- src/lib/progress.test.ts | 2 +- src/lib/refs.ts | 2 +- src/lib/search-api.test.ts | 7 +- src/lib/search-api.ts | 2 +- src/lib/search-helpers.ts | 2 +- src/lib/skills/content.ts | 2 + src/lib/spinner.test.ts | 4 +- src/lib/spinner.ts | 4 +- src/lib/status.ts | 2 +- src/lib/threads.ts | 2 +- src/lib/update.ts | 4 +- src/lib/usage-tracking.test.ts | 11 +- src/lib/usage-tracking.ts | 2 +- src/lib/user-records.ts | 2 +- vitest.config.ts | 12 +- 140 files changed, 3719 insertions(+), 359 deletions(-) create mode 100644 .github/workflows/publish-release.yml create mode 100644 docs/releasing.md create mode 100644 scripts/artifact-source.mjs create mode 100644 scripts/artifact-source.test.ts create mode 100644 scripts/github-pages.mjs create mode 100644 scripts/github-pages.test.ts create mode 100644 scripts/github-release-record.mjs create mode 100644 scripts/github-release-record.test.ts create mode 100644 scripts/pending-release.mjs create mode 100644 scripts/pending-release.test.ts create mode 100644 scripts/prepare-release.mjs create mode 100644 scripts/record-release.mjs create mode 100644 scripts/record-release.test.ts create mode 100644 scripts/registry-release.mjs create mode 100644 scripts/registry-release.test.ts create mode 100644 scripts/release-analysis.mjs create mode 100644 scripts/release-analysis.test.ts create mode 100644 scripts/release-policy.mjs create mode 100644 scripts/release-policy.test.ts create mode 100644 scripts/release.mjs create mode 100644 scripts/release.test.ts create mode 100644 scripts/source-release-record.mjs create mode 100644 scripts/source-release-record.test.ts create mode 100644 scripts/validate-source-run.mjs diff --git a/.agents/checks/architecture.md b/.agents/checks/architecture.md index 1c295d3..7dc3c7e 100644 --- a/.agents/checks/architecture.md +++ b/.agents/checks/architecture.md @@ -10,7 +10,7 @@ Rules lint and types can't express. Most are written in `AGENTS.md` and `CODEBAS - Reference parsing (ids, URLs, fuzzy names) written again instead of using `src/lib/refs.ts`. - A positional `[workspace-ref]` without the `--workspace` alias, or one that accepts both without an error. - A new top-level command not wired into the lazy registry in `src/index.ts`. -- A barrel file, or a helper that duplicates one in `src/lib/` or `@doist/cli-core`. Where cli-core has a convention, the CLI follows it. +- A barrel file, or a helper that duplicates one in `src/lib/` or `@todoist/cli-core`. Where cli-core has a convention, the CLI follows it. ## Fine diff --git a/.agents/checks/evidence.md b/.agents/checks/evidence.md index 9b84975..2577501 100644 --- a/.agents/checks/evidence.md +++ b/.agents/checks/evidence.md @@ -6,7 +6,7 @@ Apply to the PR description and any report from an agent. - A behaviour claim ("fixed", "works", "safe", "no regressions") with no test or command that ran it. - A "Not verified" section missing when something could not be checked: the real Comms API, output in a real terminal, the other Node version CI tests on. -- A change to shared code (`src/lib/`, command registration or global flags in `src/index.ts`, an `@doist/cli-core` or `@doist/comms-sdk` upgrade) without the one fact that makes it safe. +- A change to shared code (`src/lib/`, command registration or global flags in `src/index.ts`, an `@todoist/cli-core` or `@todoist/comms-sdk` upgrade) without the one fact that makes it safe. - A PR title whose type doesn't match the change: behaviour changes need `feat:`, `fix:` or `perf:` to release (`!` for breaking); tooling and docs use `chore:`, `ci:`, `docs:` or `test:` so they don't. ## Fine diff --git a/.agents/scripts/instructions.test.ts b/.agents/scripts/instructions.test.ts index 2f66fe6..557458d 100644 --- a/.agents/scripts/instructions.test.ts +++ b/.agents/scripts/instructions.test.ts @@ -145,7 +145,7 @@ it('lists every skill in AGENTS.md, and only those', () => { expect(listedUnder(text, '## Skills').toSorted()).toEqual(skills.toSorted()) }) -it('names only test helpers that the fixtures, global-args or @doist/cli-core/testing export', async () => { +it('names only test helpers that the fixtures, global-args or @todoist/cli-core/testing export', async () => { const skill = readFileSync(join(SKILLS_DIR, 'write-tests/SKILL.md'), 'utf8') const section = skill.split('## Helpers')[1]?.split('\n## ')[0] ?? '' const named = [...section.matchAll(/`(\w+)`/g)].map((match) => match[1] ?? '') @@ -161,7 +161,7 @@ it('names only test helpers that the fixtures, global-args or @doist/cli-core/te ), ].map((match) => match[1]), ) - const shared = Object.keys(await import('@doist/cli-core/testing')) + const shared = Object.keys(await import('@todoist/cli-core/testing')) expect(named.length).toBeGreaterThan(5) expect(named.filter((name) => !local.includes(name) && !shared.includes(name))).toEqual([]) }) diff --git a/.agents/skills/review-changes/SKILL.md b/.agents/skills/review-changes/SKILL.md index 4053a55..2f200b5 100644 --- a/.agents/skills/review-changes/SKILL.md +++ b/.agents/skills/review-changes/SKILL.md @@ -18,7 +18,7 @@ Done when the brief is written and the tree holds only the change, with no secre ## 2. Run the reviews in parallel - **Task review**, always: a fresh subagent with the brief and the diff. It looks for missing pieces, unrequested changes, unhandled cases, tests that would pass with the feature broken, and, in a refactor, any changed test input or assertion. Doistbot can't do this: it never sees the task. -- **Blast-radius review**, when the change alters code others rely on: `src/lib/` (the client and caches in `src/lib/api.ts` above all), command registration, global flags or argv handling in `src/index.ts`, the `@doist/cli-core` or `@doist/comms-sdk` version, other `package.json` dependencies, build or CI config. It names the one fact that makes the change safe and proves it by running code. It looks where text search stops: library versions in `package-lock.json`, the shape of API responses, scripts and installed skills reading `--json` output. +- **Blast-radius review**, when the change alters code others rely on: `src/lib/` (the client and caches in `src/lib/api.ts` above all), command registration, global flags or argv handling in `src/index.ts`, the `@todoist/cli-core` or `@todoist/comms-sdk` version, other `package.json` dependencies, build or CI config. It names the one fact that makes the change safe and proves it by running code. It looks where text search stops: library versions in `package-lock.json`, the shape of API responses, scripts and installed skills reading `--json` output. - **Code review**: Doistbot if it is set up (below). Otherwise one subagent per relevant check in `.agents/checks/`, each with the brief and that check. Every brief says: **the shared working tree is read-only.** A reviewer that needs to run or break code makes its own copy with `npm run review:worktree -- `, then `npm ci` in it, and removes it after. diff --git a/.agents/skills/verify/SKILL.md b/.agents/skills/verify/SKILL.md index 31c3b0f..c2c01bd 100644 --- a/.agents/skills/verify/SKILL.md +++ b/.agents/skills/verify/SKILL.md @@ -43,7 +43,7 @@ Rate every claim on this ladder: Claims about behaviour ("fixed", "works", "safe", "no regressions") need **ran** or higher. A fact the code shows needs **cited**. Anything lower goes under **Not verified**, with why. -For a change to code others rely on (shared helpers in `src/lib/`, command registration and global flags in `src/index.ts`, an `@doist/cli-core` or `@doist/comms-sdk` upgrade), name the one fact that makes it safe, and how you proved it. +For a change to code others rely on (shared helpers in `src/lib/`, command registration and global flags in `src/index.ts`, an `@todoist/cli-core` or `@todoist/comms-sdk` upgrade), name the one fact that makes it safe, and how you proved it. Report format: diff --git a/.agents/skills/write-tests/SKILL.md b/.agents/skills/write-tests/SKILL.md index 8a6c1e5..8562b61 100644 --- a/.agents/skills/write-tests/SKILL.md +++ b/.agents/skills/write-tests/SKILL.md @@ -15,7 +15,7 @@ description: Writes or changes tests in this repository to its quality bar, with ## Helpers -From `src/lib/__fixtures__/accounts.ts`: `STORED_ALAN`, `STORED_ELLIE`, `ACCOUNT_ALAN`, `ACCOUNT_ELLIE`. From `@doist/cli-core/testing`: `createTestProgram`, `captureConsole`, `captureStream`, `describeEmptyMachineOutput` (the shared check that `--json` and `--ndjson` print nothing extra for an empty result). Reset global flag state between tests with `resetGlobalArgs` from `src/lib/global-args.ts`. +From `src/lib/__fixtures__/accounts.ts`: `STORED_ALAN`, `STORED_ELLIE`, `ACCOUNT_ALAN`, `ACCOUNT_ELLIE`. From `@todoist/cli-core/testing`: `createTestProgram`, `captureConsole`, `captureStream`, `describeEmptyMachineOutput` (the shared check that `--json` and `--ndjson` print nothing extra for an empty result). Reset global flag state between tests with `resetGlobalArgs` from `src/lib/global-args.ts`. ## The quality bar diff --git a/.github/workflows/issue-automation.yml b/.github/workflows/issue-automation.yml index 54f43b7..b638909 100644 --- a/.github/workflows/issue-automation.yml +++ b/.github/workflows/issue-automation.yml @@ -18,5 +18,5 @@ jobs: - name: Add issue to project uses: actions/add-to-project@27022a19346d0433f2b9859dd2b69d38f4277808 with: - project-url: https://github.com/orgs/Doist/projects/83 + project-url: https://github.com/orgs/todoist/projects/83 github-token: ${{ secrets.HERO_BOARD_PAT }} diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..8cb33d9 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,140 @@ +name: Publish retained release + +on: + workflow_dispatch: + inputs: + artifact_run_id: + description: Successful Release artifact run ID + required: true + artifact_run_attempt: + description: Original successful build attempt (never a later rerun) + required: true + source_sha: + description: Full reviewed build commit SHA + required: true + destination: + type: choice + options: [both, github, npmjs] + default: both + tag: + type: choice + options: [normal, migration-recovery] + default: normal + workflow_run: + workflows: [Release artifact] + types: [completed] + branches: [main, next] + +permissions: + contents: read + actions: read + packages: write + id-token: write + +# Serialize both channels and all mirror retries; do not cancel an in-flight publish. +concurrency: + group: comms-cli-publication + cancel-in-progress: false + +jobs: + publish: + if: >- + (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') || + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.event == 'push' && vars.COMMS_CLI_AUTOMATIC_RELEASES == 'true') + environment: npm-release + runs-on: ubuntu-latest + timeout-minutes: 30 + env: + ARTIFACT_RUN_ID: ${{ inputs.artifact_run_id || github.event.workflow_run.id }} + ARTIFACT_RUN_ATTEMPT: ${{ inputs.artifact_run_attempt || github.event.workflow_run.run_attempt }} + RELEASE_SHA: ${{ inputs.source_sha || github.event.workflow_run.head_sha }} + steps: + # Validation/publishing code always comes from the publisher's reviewed default branch. + - uses: actions/checkout@v6 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + - uses: actions/setup-node@v6 + with: + node-version-file: .nvmrc + - name: Validate successful source run + id: source + env: + GH_TOKEN: ${{ github.token }} + run: | + mkdir -p .tmp + node scripts/validate-source-run.mjs + - uses: actions/download-artifact@v4 + if: steps.source.outputs.available == 'true' + with: + name: ${{ steps.source.outputs.artifact }} + path: .tmp/release + run-id: ${{ env.ARTIFACT_RUN_ID }} + github-token: ${{ github.token }} + - name: Verify retained bytes and select channel + id: artifact + if: steps.source.outputs.available == 'true' + env: + GH_TOKEN: ${{ github.token }} + REQUESTED_TAG: ${{ inputs.tag || 'normal' }} + run: | + node scripts/release.mjs verify .tmp/release + node --input-type=module <<'JS' + import fs from 'node:fs' + const release = JSON.parse(fs.readFileSync('.tmp/release/release.json')) + const tag = process.env.REQUESTED_TAG === 'migration-recovery' ? 'migration-recovery' : release.branch === 'next' ? 'next' : 'latest' + fs.appendFileSync(process.env.GITHUB_ENV, `RELEASE_TAG=${tag}\n`) + fs.appendFileSync(process.env.GITHUB_OUTPUT, `version=${release.version}\n`) + JS + - name: Publish missing npm mirror using configured trust + if: steps.source.outputs.available == 'true' && inputs.destination != 'github' + env: + GH_TOKEN: ${{ github.token }} + GH_PACKAGES_TOKEN: ${{ github.token }} + run: node scripts/release.mjs publish .tmp/release npmjs + - name: Publish missing GitHub mirror + if: steps.source.outputs.available == 'true' && inputs.destination != 'npmjs' + env: + GH_TOKEN: ${{ github.token }} + GH_PACKAGES_TOKEN: ${{ github.token }} + run: node scripts/release.mjs publish .tmp/release github + - name: Require identical bytes in both registries + if: steps.source.outputs.available == 'true' + env: + GH_TOKEN: ${{ github.token }} + GH_PACKAGES_TOKEN: ${{ github.token }} + run: | + node scripts/release.mjs check-registry .tmp/release npmjs + node scripts/release.mjs check-registry .tmp/release github + - name: Generate release-record token + if: steps.source.outputs.available == 'true' && env.RELEASE_TAG != 'migration-recovery' + id: generate_token + uses: actions/create-github-app-token@v3 + with: + app-id: ${{ secrets.DOIST_RELEASE_BOT_ID }} + private-key: ${{ secrets.DOIST_RELEASE_BOT_PRIVATE_KEY }} + permission-contents: write + - name: Record release without rebuilding or recalculating + if: steps.source.outputs.available == 'true' && env.RELEASE_TAG != 'migration-recovery' + id: record + run: node scripts/record-release.mjs .tmp/release + env: + GH_TOKEN: ${{ steps.generate_token.outputs.token }} + SOURCE_GH_TOKEN: ${{ github.token }} + GH_PACKAGES_TOKEN: ${{ github.token }} + - name: Announce stable release in Comms + if: steps.record.outputs.created == 'true' && steps.source.outputs.branch == 'main' + uses: todoist/comms-actions/post-comment-action@aeeefe3270ad30d0c4cdd7dbf94b2fac1dcb8ceb + with: + comms-client-id: ${{ secrets.COMMS_DOISTBOT_CLIENT_ID }} + comms-client-secret: ${{ secrets.COMMS_DOISTBOT_CLIENT_SECRET }} + comms-username: ${{ secrets.COMMS_DOISTBOT_USER }} + comms-password: ${{ secrets.COMMS_DOISTBOT_PASSWORD }} + workspace-id: 69 + thread-id: CZECS4Yebwx7c1c8hDRmB + content: >- + Comms CLI v${{ steps.artifact.outputs.version }} published. + https://github.com/todoist/comms-cli/releases/tag/v${{ steps.artifact.outputs.version }} + audience: thread + continue-on-error: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b49c4e9..003ee52 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,145 +1,51 @@ -name: Release +name: Release artifact on: push: - branches: - - main - - next + branches: [main, next] + workflow_dispatch: +# semantic-release dry-run checks push authorization. This workflow never pushes or publishes. permissions: - # Enable `semantic-release` to publish a GitHub release and push commits contents: write - # Enable `semantic-release` to post comments on issues - issues: write - # Enable `semantic-release` to post comments on pull requests - pull-requests: write - # Enable the use of OIDC for trusted publishing and npm provenance - id-token: write + actions: read -# Release involves crucial steps that shouldn't be cancelled mid-run, -# so new workflow runs are queued until the previous one finishes. concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: release-artifact-${{ github.ref }} cancel-in-progress: false jobs: - release: - name: Release + build: + if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/next' runs-on: ubuntu-latest - timeout-minutes: 60 - + timeout-minutes: 30 steps: - - name: Generate bot token - id: generate_token - uses: actions/create-github-app-token@v3 - with: - app-id: ${{ secrets.DOIST_RELEASE_BOT_ID }} - private-key: ${{ secrets.DOIST_RELEASE_BOT_PRIVATE_KEY }} - permission-contents: write - permission-issues: write - permission-pull-requests: write - - - name: Get bot user ID - id: bot_user - run: | - user_id=$(gh api "/users/${{ steps.generate_token.outputs.app-slug }}[bot]" --jq .id) - if [ -z "$user_id" ]; then - echo "Failed to get bot user ID" >&2 - exit 1 - fi - echo "id=$user_id" >> "$GITHUB_OUTPUT" - env: - GH_TOKEN: ${{ steps.generate_token.outputs.token }} - - - name: Checkout repository - uses: actions/checkout@v6 + - uses: actions/checkout@v6 with: - token: ${{ steps.generate_token.outputs.token }} + ref: ${{ github.sha }} fetch-depth: 0 - - - name: Setup Node.js - uses: actions/setup-node@v6 + # Keep HEAD attached at the event SHA: semantic-release must not move a detached + # checkout to a newer branch tip during its history fetch. + - name: Attach reviewed event commit + env: + RELEASE_BRANCH: ${{ github.ref_name }} + RELEASE_SHA: ${{ github.sha }} + run: git checkout -B "$RELEASE_BRANCH" "$RELEASE_SHA" + - uses: actions/setup-node@v6 with: - node-version: '24' + node-version-file: .nvmrc cache: npm - scope: '@doist' - registry-url: 'https://registry.npmjs.org/' - - - name: Install dependencies - run: npm ci - - - name: Capture previous tag - if: github.ref_name == 'main' - id: previous_tag - run: echo "tag=$(git describe --tags --abbrev=0 --exclude='*-*' 2>/dev/null || true)" >> "$GITHUB_OUTPUT" - - - name: Release - run: npx semantic-release + - run: npm ci --ignore-scripts --no-audit --no-fund + - run: npm run verify + - name: Analyze and pack once + id: prepare + run: node scripts/prepare-release.mjs env: - GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} - GIT_AUTHOR_NAME: ${{ steps.generate_token.outputs.app-slug }}[bot] - GIT_AUTHOR_EMAIL: ${{ steps.bot_user.outputs.id }}+${{ steps.generate_token.outputs.app-slug }}[bot]@users.noreply.github.com - GIT_COMMITTER_NAME: ${{ steps.generate_token.outputs.app-slug }}[bot] - GIT_COMMITTER_EMAIL: ${{ steps.bot_user.outputs.id }}+${{ steps.generate_token.outputs.app-slug }}[bot]@users.noreply.github.com - # `npm ci` installs the git hooks (the `prepare` script). The pull request - # already ran every check, so the release commit and push skip them. - LEFTHOOK: '0' - - - name: Derive release announcement - if: github.ref_name == 'main' - id: announcement - env: - PREVIOUS_TAG: ${{ steps.previous_tag.outputs.tag }} - run: | - git fetch --force --tags origin - - new_tag="$(git describe --tags --abbrev=0 2>/dev/null || true)" - if [ -z "${new_tag}" ] || [ "${new_tag}" = "${PREVIOUS_TAG}" ]; then - echo "should_announce=false" >> "$GITHUB_OUTPUT" - exit 0 - fi - - package_name="$(node -p "JSON.parse(require('fs').readFileSync('package.json', 'utf8')).name")" - package_version="$(node -p "JSON.parse(require('fs').readFileSync('package.json', 'utf8')).version")" - release_version="${new_tag#v}" - - package_url="https://www.npmjs.com/package/${package_name}/v/${package_version}" - release_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/releases/tag/${new_tag}" - repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" - - if [ -n "${PREVIOUS_TAG}" ]; then - changelog="$(git log --no-merges --reverse --pretty='format:- %s (%H-%h)' "${PREVIOUS_TAG}..${new_tag}" | grep -v '^- chore(release): ' || true)" - else - changelog="$(git log --no-merges --reverse --pretty='format:- %s (%H-%h)' "${new_tag}" | grep -v '^- chore(release): ' || true)" - fi - - if [ -z "${changelog}" ]; then - changelog='- No additional commits listed.' - else - changelog="$(printf '%s\n' "${changelog}" | sed -E -e 's,\(([a-f0-9]+)-([a-f0-9]+)\),([`\2`]('"${repo_url}"'/commit/\1)),g' | sed -E -e 's,\(#([0-9]+)\),([#\1]('"${repo_url}"'/pull/\1)),g')" - fi - - { - echo "should_announce=true" - echo "message<> "$GITHUB_OUTPUT" - - - name: Announce release in Comms - if: github.ref_name == 'main' && steps.announcement.outputs.should_announce == 'true' - uses: Doist/comms-actions/post-comment-action@aeeefe3270ad30d0c4cdd7dbf94b2fac1dcb8ceb + GITHUB_TOKEN: ${{ github.token }} + - uses: actions/upload-artifact@v4 + if: steps.prepare.outputs.prepared == 'true' with: - comms-client-id: ${{ secrets.COMMS_DOISTBOT_CLIENT_ID }} - comms-client-secret: ${{ secrets.COMMS_DOISTBOT_CLIENT_SECRET }} - comms-username: ${{ secrets.COMMS_DOISTBOT_USER }} - comms-password: ${{ secrets.COMMS_DOISTBOT_PASSWORD }} - workspace-id: 69 - thread-id: CZECS4Yebwx7c1c8hDRmB - content: ${{ steps.announcement.outputs.message }} - audience: thread - continue-on-error: true + name: release-${{ github.run_id }}-${{ github.run_attempt }} + path: .tmp/release/ + if-no-files-found: error + retention-days: 90 diff --git a/.github/workflows/update-comms-sdk.yml b/.github/workflows/update-comms-sdk.yml index cbaa7f5..9e9c829 100644 --- a/.github/workflows/update-comms-sdk.yml +++ b/.github/workflows/update-comms-sdk.yml @@ -27,8 +27,8 @@ jobs: - name: Install current dependencies run: npm ci - - name: Update @doist/comms-sdk to latest - run: npm install @doist/comms-sdk@latest + - name: Update @todoist/comms-sdk to latest + run: npm install @todoist/comms-sdk@latest - name: Build run: npm run build @@ -46,10 +46,10 @@ jobs: id: changes run: | if git diff --quiet package.json package-lock.json; then - echo "No updates available - @doist/comms-sdk is already at the latest version" + echo "No updates available - @todoist/comms-sdk is already at the latest version" echo "has_changes=false" >> $GITHUB_OUTPUT else - echo "Changes detected - @doist/comms-sdk has been updated" + echo "Changes detected - @todoist/comms-sdk has been updated" echo "has_changes=true" >> $GITHUB_OUTPUT fi @@ -59,7 +59,7 @@ jobs: git config --local user.email "action@github.com" git config --local user.name "GitHub Action" git add package.json package-lock.json - git commit -m "fix(deps): update to latest \`@doist/comms-sdk\` package" + git commit -m "fix(deps): update to latest \`@todoist/comms-sdk\` package" git push - name: Trigger release workflow @@ -74,9 +74,9 @@ jobs: - name: Report success run: | if [ "${{ steps.changes.outputs.has_changes }}" == "true" ]; then - echo "โœ… Successfully updated @doist/comms-sdk package" + echo "โœ… Successfully updated @todoist/comms-sdk package" echo "๐Ÿ“ฆ Changes committed to main branch" - echo "๐Ÿš€ Release workflow triggered - semantic release will create a new patch version" + echo "๐Ÿš€ Release workflow triggered - semantic-release will analyze and retain the next release artifact" else - echo "โ„น๏ธ No updates needed - @doist/comms-sdk is already at the latest version" + echo "โ„น๏ธ No updates needed - @todoist/comms-sdk is already at the latest version" fi diff --git a/AGENTS.md b/AGENTS.md index d995af9..e97b230 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,7 +50,7 @@ The project uses two tsconfig files: `tsconfig.json` includes test files for typ Tests are colocated next to the module they cover (for example `src/commands/thread/thread.test.ts` or `src/lib/refs.test.ts`). Shared Vitest manual mocks for npm packages live in `src/__mocks__/` (e.g. `chalk.ts`). -`vitest.config.ts` lists `@doist/cli-core` in `server.deps.inline` so `vi.mock('@doist/cli-core', โ€ฆ)` and `vi.doMock('node:fs/promises', โ€ฆ)` reach cli-core's compiled imports. Without it, vitest treats the package as external and Node's native resolver bypasses the mock substitution, breaking the `auth` / `config` / spinner suites. +`vitest.config.ts` lists `@todoist/cli-core` in `server.deps.inline` so `vi.mock('@todoist/cli-core', โ€ฆ)` and `vi.doMock('node:fs/promises', โ€ฆ)` reach cli-core's compiled imports. Without it, vitest treats the package as external and Node's native resolver bypasses the mock substitution, breaking the `auth` / `config` / spinner suites. Run a single test file: @@ -76,7 +76,7 @@ This is a TypeScript CLI (`tdc`) for Comms messaging, built with Commander.js. **Lib** (`src/lib/`): -- `api.ts` - Singleton CommsApi client from `@doist/comms-sdk`, workspace/user caching +- `api.ts` - Singleton CommsApi client from `@todoist/comms-sdk`, workspace/user caching - `refs.ts` - Reference parsing: accepts IDs (`id:` or bare IDs when unambiguous), Comms URLs, or fuzzy names for workspaces/users/channels/groups - `output.ts` - JSON/NDJSON formatting with essential field filtering per entity type - `config.ts` - Persists config to `~/.config/comms-cli/config.json` @@ -95,7 +95,7 @@ This is a TypeScript CLI (`tdc`) for Comms messaging, built with Commander.js. ## Error Handling -- **Always use `CliError` from `src/lib/errors.ts`** instead of bare `throw new Error(...)`, `console.error() + process.exit(1)`, or `console.error() + process.exitCode = 1`. This ensures structured error output in `--json` mode and consistent formatting in text mode. The global error handler in `src/index.ts` catches all errors, formats them appropriately, and sets the exit code. The same handler also matches `BaseCliError` (re-exported from `src/lib/errors.ts`) so errors thrown by `@doist/cli-core` helpers route through the same formatter. +- **Always use `CliError` from `src/lib/errors.ts`** instead of bare `throw new Error(...)`, `console.error() + process.exit(1)`, or `console.error() + process.exitCode = 1`. This ensures structured error output in `--json` mode and consistent formatting in text mode. The global error handler in `src/index.ts` catches all errors, formats them appropriately, and sets the exit code. The same handler also matches `BaseCliError` (re-exported from `src/lib/errors.ts`) so errors thrown by `@todoist/cli-core` helpers route through the same formatter. ```typescript import { CliError } from '../../lib/errors.js' diff --git a/CODEBASE.md b/CODEBASE.md index 6ade10f..345f3da 100644 --- a/CODEBASE.md +++ b/CODEBASE.md @@ -6,14 +6,14 @@ ## What this project is -`@doist/comms-cli` is a **TypeScript CLI** for Comms messaging. Binary name: -`tdc`. It wraps `@doist/comms-sdk` and publishes a single executable +`@todoist/comms-cli` is a **TypeScript CLI** for Comms messaging. Binary name: +`tdc`. It wraps `@todoist/comms-sdk` and publishes a single executable (`dist/index.js`). ESM-only ยท Node `^20.19 || >=22.12` ยท Commander 14 ยท vitest ยท oxlint + oxfmt (no eslint/prettier) ยท semantic-release on merge to `main`. Shared building blocks (config I/O, output formatters, spinner, OAuth/keyring auth, command attachers) -come from `@doist/cli-core`. +come from `@todoist/cli-core`. ## Top-level layout @@ -46,7 +46,7 @@ come from `@doist/cli-core`. โ”œโ”€ README.md / CONTRIBUTING.md โ”œโ”€ tsconfig.json # Includes src + tests (type-check, IDE) โ”œโ”€ tsconfig.build.json # Excludes *.test.ts/.spec.ts, __mocks__, __fixtures__ -โ”œโ”€ vitest.config.ts # { globals, root: 'src', inlines @doist/cli-core } +โ”œโ”€ vitest.config.ts # { globals, root: 'src', inlines @todoist/cli-core } โ”œโ”€ .oxlintrc.json / .oxfmtrc.json โ”œโ”€ lefthook.yml # Git hooks: branch guard; pre-commit lint, format, types; โ”‚ # pre-push tests @@ -217,7 +217,7 @@ is tried as an id vs a name. ## Auth & token storage -`@doist/cli-core/auth` owns the keyring, multi-user `TokenStore`, OAuth flow, +`@todoist/cli-core/auth` owns the keyring, multi-user `TokenStore`, OAuth flow, and the `login` / `logout` / `status` / `token view` and `account list/use/current/remove` attachers. comms supplies (a) a `UserRecordStore` adapter (`user-records.ts`) over its config file @@ -259,8 +259,8 @@ see a per-command `--user`). - **Fixtures:** `src/lib/__fixtures__/accounts.ts` (`ACCOUNT_ALAN` / `ACCOUNT_ELLIE`) โ€” don't hand-build account objects. Manual npm-package mocks in `src/__mocks__/` (`chalk.ts`). -- **`@doist/cli-core` inlining:** `vitest.config.ts` lists it in - `server.deps.inline` so `vi.mock('@doist/cli-core', โ€ฆ)` / +- **`@todoist/cli-core` inlining:** `vitest.config.ts` lists it in + `server.deps.inline` so `vi.mock('@todoist/cli-core', โ€ฆ)` / `vi.doMock('node:fs/promises', โ€ฆ)` reach its compiled imports โ€” without it the auth/config/spinner suites break. @@ -280,9 +280,11 @@ see a per-command `--user`). formats staged files and runs the type check; `commit-msg` repeats the branch rule for empty commits; `pre-push` refuses pushes to `main`, then runs the tests. See `docs/agents.md`. -- **Release:** semantic-release on merge to `main`; Conventional Commits required +- **Release:** semantic-release analyzes main/next and retains an artifact; the protected + publisher completes both registries and stable source/skill bookkeeping. See + [the release runbook](docs/releasing.md). Conventional Commits required (enforced by `check-semantic-pull-request.yml`). `update-comms-sdk.yml` keeps - `@doist/comms-sdk` current. + `@todoist/comms-sdk` current. ## Skill content flow diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 381cf2d..5f90a90 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ The following is a set of guidelines for contributing to Comms CLI. Please read ## Open Development -All work on Comms CLI happens directly on [GitHub](https://github.com/Doist/comms-cli). Both core team members and external contributors send pull requests that go through the same review process. +All work on Comms CLI happens directly on [GitHub](https://github.com/todoist/comms-cli). Both core team members and external contributors send pull requests that go through the same review process. ## Semantic Versioning @@ -14,7 +14,7 @@ Every significant change is documented in the [CHANGELOG.md](CHANGELOG.md) file. ## Branch Organization -Submit all changes to the [main](https://github.com/Doist/comms-cli/tree/main) branch (via PR) by default. For pre-release work, target the `next` branch instead โ€” see [Release Process](#release-process-core-team-only) for details. +Submit all changes to the [main](https://github.com/todoist/comms-cli/tree/main) branch (via PR) by default. For pre-release work, target the `next` branch instead โ€” see [Release Process](#release-process-core-team-only) for details. We do our best to keep `main` in good shape, with all tests passing. @@ -67,7 +67,7 @@ PR titles are validated against the Conventional Commits format by CI. When squa ## Release Process (core team only) -The release process is fully automated with [semantic-release](https://github.com/semantic-release/semantic-release). Pushing to `main` (after CI passes) triggers a stable release automatically. +The release process uses [semantic-release](https://github.com/semantic-release/semantic-release) for version analysis. Main/next pushes build and retain one verified artifact; the protected publisher delivers identical bytes to npmjs and GitHub Packages. During Migration 001, automatic publication stays disabled until the first real package and registry access are ready. See [the release runbook](docs/releasing.md) for setup, bootstrap, channels and retries. ### Pre-releases @@ -83,7 +83,7 @@ To test features before publishing a stable release: 2. **Develop on a feature branch** based off `next` and open a PR targeting `next`. -3. **Automatic pre-release:** Each PR merge into `next` triggers CI and publishes a pre-release version (e.g., `1.0.0-next.1`) to the `next` dist-tag on npm. +3. **Automatic pre-release:** Each PR merge into `next` triggers CI and prepares a pre-release artifact (e.g., `1.0.0-next.1`) for the `next` dist-tag in both registries; the configured publisher completes it. 4. **Promote to stable:** When ready, open a PR to merge `next` into `main`. Use a regular merge commit (not squash), as this preserves the individual commit history and allows semantic-release to correctly generate a comprehensive changelog and determine the version bump automatically. @@ -92,7 +92,7 @@ To test features before publishing a stable release: ### Installing a pre-release ```sh -npm install @doist/comms-cli@next +npm install @todoist/comms-cli@next ``` ## License diff --git a/README.md b/README.md index 91da5ca..20b6fae 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A command-line interface for Comms. ## Installation > ```bash -> npm install -g @doist/comms-cli +> npm install -g @todoist/comms-cli > ``` ### Agent Skills @@ -32,6 +32,9 @@ tdc skill list tdc skill uninstall ``` +For release prerequisites, the one-time old-scope installation transition, and +registry access, see [the release runbook](docs/releasing.md). + ## Uninstallation First, remove any installed agent skills: @@ -43,13 +46,13 @@ tdc skill uninstall Then uninstall the CLI: ```bash -npm uninstall -g @doist/comms-cli +npm uninstall -g @todoist/comms-cli ``` ## Local Setup ```bash -git clone https://github.com/Doist/comms-cli.git +git clone https://github.com/todoist/comms-cli.git cd comms-cli npm install npm run build diff --git a/docs/SPEC.md b/docs/SPEC.md index c6fffc1..8da1b51 100644 --- a/docs/SPEC.md +++ b/docs/SPEC.md @@ -8,7 +8,7 @@ A command-line interface for Comms, following the architecture and patterns esta - **Language**: TypeScript 5.x (strict mode) - **CLI Framework**: Commander.js - **Terminal Styling**: chalk -- **API Client**: `@doist/comms-sdk` +- **API Client**: `@todoist/comms-sdk` - **Testing**: vitest - **Formatting**: prettier - **Git Hooks**: lefthook @@ -42,7 +42,7 @@ __tests__/ # Test suite ## Package & Binary -- **Package name**: `@doist/comms-cli` +- **Package name**: `@todoist/comms-cli` - **Binary**: `tdc` ## Authentication diff --git a/docs/icon.md b/docs/icon.md index 68efb07..fa91d8d 100644 --- a/docs/icon.md +++ b/docs/icon.md @@ -7,7 +7,7 @@ The icon shows in four places. | OAuth consent screen (`tdc auth login`) | `logo_uri` in `oauth/client-metadata.json`, or an older URL (see below) | | Sign-in result pages in the browser | The URL in `LOGO_URI` | | README (GitHub and npm) | `icons/comms-cli.png` | -| Comms web, Settings โ†’ Integrations | Its own copies in `Doist/comms-web` | +| Comms web, Settings โ†’ Integrations | Its own copies in `todoist/comms-web` | ## 1. Export the artwork @@ -26,7 +26,7 @@ The consent screen picks up a replaced file as soon as it's on `main` (allow abo ## 3. Update Comms web -`Doist/comms-web` keeps its own copies for the CLI card in Settings โ†’ Integrations: +`todoist/comms-web` keeps its own copies for the CLI card in Settings โ†’ Integrations: - `src/settings/integrations/comms-cli-icon.png` (32ร—32) - `src/settings/integrations/comms-cli-icon@2x.png` (64ร—64) diff --git a/docs/releasing.md b/docs/releasing.md new file mode 100644 index 0000000..6dd0ab8 --- /dev/null +++ b/docs/releasing.md @@ -0,0 +1,242 @@ +# Releasing Comms CLI after Migration 001 + +This draft is **WAITING_FOR_PUBLICATION**, not mergeable. It prepares public +`@todoist/comms-cli`; `tdc`, config paths, keyring service, OAuth client ID, +product domains and API version remain unchanged. No migration publication has +happened. The original lockfile is deliberately unchanged until real upstream +releases exist; do not replace its integrity or URLs by hand. + +## Dependencies and release lineage + +Current main, `v4.1.0` and npm `@doist/comms-cli@latest` agree on 4.1.0. The +first proposed new-scope version is **4.1.1**. `release.config.js` retains the +scope-independent `v${version}` tags and semantic-release Conventional Commits. +Missing history, a different first baseline, or an intervening feature/breaking +change requires target reconciliation. Following releases use normal analysis, +including `next.N` and the `next` npm channel. Existing releases/tags are untouched. + +The CLI consumes intended `@todoist/comms-sdk@4.0.1` ([SDK draft #139](https://github.com/Doist/comms-sdk-typescript/pull/139)), +which requires intended `@todoist/sdk-kmp@0.6.2` ([KMP draft #74](https://github.com/Doist/sdk-kmp/pull/74)). +Neither was available on npm at preparation. GitHub registry 404s did not establish +absence or package readability. Require both real copies and actual consumer access. + +`cli-core` is outside this repository's write scope. Its current main/npm release +is **@doist/cli-core 1.10.0**; no new-scope release or migration PR was found. +Henning routes the existing owner to prepare and publish the current-forward +`@todoist/cli-core` successor to both registries with the same artifact/version. +The temporary manifest constraint `~1.10.0` expresses the compatible current line, +**not a claimed new-scope version**. Replace it with the owner's exact real version +before merge. Preserve the root, `auth`, `commands`, `markdown`, and `testing` +exports, token store/OAuth/config contracts, updater channels, and `cli-agent` +header contract. Reconcile any changed API before finalization; do not invent a +successor version or backport an old line. + +SDK 4 requires a workspace ID when uploading attachments. Each CLI upload uses +the already resolved destination channel/thread/conversation workspace. Dry-run, +read-only guards, path validation and command syntax stay intact. The core 1.10 +usage-header addition is exercised with a fixed agent fixture. + +## Finalize this draft after upstream publication + +Use the actual owner-selected `CLI_CORE_VERSION`; the registry must contain it. +The following commands have **not** been run against new-scope packages: + +```sh +npm view @todoist/comms-sdk@4.0.1 version dependencies dist --registry=https://registry.npmjs.org/ --json +npm view @todoist/sdk-kmp@0.6.2 version dist --registry=https://registry.npmjs.org/ --json +npm view "@todoist/cli-core@$CLI_CORE_VERSION" version exports dist --registry=https://registry.npmjs.org/ --json +npm install --package-lock-only --ignore-scripts --no-audit --no-fund --save-exact \ + "@todoist/cli-core@$CLI_CORE_VERSION" @todoist/comms-sdk@4.0.1 +npm ci --ignore-scripts --no-audit --no-fund +npm run verify +``` + +Review the genuine lock identities, URLs, integrity and transitive KMP version; +repeat fresh review and update this same draft. No aliases or local paths belong +in the committed dependency tree. Installs during preparation use `--ignore-scripts`: +`prepare` resets shared Git hooks, and `postinstall` updates user-installed skills. +Only `npm run sync:skill` regenerates this repository's standalone skill; never run +global skill/update/completion commands as a check. + +## Operator setup and merge prerequisites + +Merge wave: **M04 dependent CLI producer**, after SDK/KMP and cli-core publication; +TodoistOS [#616](https://github.com/Doist/todoist-os/pull/616) activates in M05 after +this CLI is published and installed successfully from both required registries. +A merged draft or tag alone does not satisfy either consumer gate. + +Existing package/GitHub owners, routed through Henning, must complete: + +1. Rename/access/M01 activation. The existing bootstrap action is + `todoist/comms-actions/post-comment-action@aeeefe3270ad30d0c4cdd7dbf94b2fac1dcb8ceb`. + Do not gate this publisher on unpublished-dependency consumers actions#243 or + comms-actions#7, or invent their eventual bundle pins. +2. Resolve the **OAuth continuity gate**. The immutable client ID is + `https://raw.githubusercontent.com/Doist/comms-cli/main/oauth/client-metadata.json`. + The OAuth server fetches exactly that URL, requires the document's matching + `client_id`, and does not follow redirects. The existing auth owner must verify + that exact identity still works after rename or arrange an approved compatible + server transition before cutover. Changing the ID in this PR would invalidate + existing credentials. Preserve the old logo URL and every shipped icon path too. +3. [Create the protected npm-release environment](https://github.com/todoist/comms-cli/settings/environments), + with maintainer approval and default-branch publisher execution. None existed + during preparation. Preserve existing release App secret names and approved + contents-write permissions; do not bypass branch rules for source commitback. +4. [Confirm GitHub package settings](https://github.com/orgs/todoist/packages/npm/package/comms-cli): + intended visibility independently of npm's public visibility, repository link, + publisher Write, and only actual GitHub-routed consumers' Read grants. + Local read/write credentials need package scopes, repository access and required + organization SSO. A package inventory or whoami is not readability evidence. +5. First publish the real retained artifact interactively as below. Only after the + npm identity exists, near the next release, configure + [npm trusted publishing](https://www.npmjs.com/package/@todoist/comms-cli/access): + owner `todoist`, repository `comms-cli`, workflow **publish-release.yml**, + environment **npm-release**, direct npm publish. No reusable caller. + GitHub-hosted ubuntu-latest, Node 24, npm >=11.5.1, id-token:write. + Manual publication does not validate OIDC. Configure close to use (new unused + connections expire after two days); separately authorize dist-tag management. +6. Set `COMMS_CLI_AUTOMATIC_RELEASES=true` only after bootstrap, package ACLs and + publisher setup have been verified. Until then, automatic publication is off. + +After these gates and review, Henning may approve and merge the draft normally. +Do not use admin/bypass, force, auto-merge or a ready transition during preparation. +A future authorized merge command is: + +```sh +gh pr merge "$CLI_PR_URL" --repo todoist/comms-cli --squash --match-head-commit "$REVIEWED_HEAD" +``` + +## Build once, publish the retained artifact + +`release.yml` on main/next builds, verifies, analyzes and packs; it writes no tag, +release, source commit or registry. Manual builds are restricted to main/next. +Artifacts are retained 90 days under `release-RUN-ATTEMPT`. The workflow's +contents:write permission is used for semantic-release's dry-run authorization +probe. Feature branch pushes and draft PRs cannot enter this path. + +`publish-release.yml` is a separate protected main-branch workflow. Automatic +workflow_run publication requires the explicit variable above and a successful +push build. Manual builds never auto-publish. Publication is serialized across +channels and retries. The normal Comms announcement remains attached only to a +new completed stable release; its existing service identities and destination +are unchanged. Recovery and next do not announce. + +Future operator commands, **not performed during preparation**: + +```sh +gh workflow run release.yml --repo todoist/comms-cli --ref main +gh run list --repo todoist/comms-cli --workflow release.yml --limit 10 \ + --json databaseId,headSha,headBranch,event,conclusion +``` + +Select the actual successful original `ARTIFACT_RUN_ID`, `ARTIFACT_RUN_ATTEMPT`, +full reviewed `RELEASE_SHA` and a new absolute `RELEASE_DIR`. Do not substitute a +later rerun: source validation uses the original attempts endpoint. The helper +binds the unique artifact ID, authenticated API ZIP digest, metadata and tarball +entry bytes. A local checksum or self-declared JSON file is insufficient. + +Obtain `GH_PACKAGES_TOKEN` through the existing secure credential flow, never in +source or shell history. For a first unreadable GitHub mirror, use an authorized +active organization-owner classic PAT with read:org and read:packages (also +write:packages for its first upload), including required SSO. The same token must +prove owner access or read the actual registry package name **and dist-tags**. +Metadata REST/whoami/list[] alone cannot establish absence. `gh` source artifact +read authentication is separate from the package token. + +```sh +export ARTIFACT_RUN_ID ARTIFACT_RUN_ATTEMPT RELEASE_SHA GH_PACKAGES_TOKEN +gh run download "$ARTIFACT_RUN_ID" --repo todoist/comms-cli \ + --name "release-$ARTIFACT_RUN_ID-$ARTIFACT_RUN_ATTEMPT" --dir "$RELEASE_DIR" +node scripts/release.mjs verify "$RELEASE_DIR" +node scripts/release.mjs bootstrap-npm "$RELEASE_DIR" +node scripts/release.mjs publish "$RELEASE_DIR" github +# After mirror visibility/linkage/ACLs are correct, skip matching copies and record: +gh workflow run publish-release.yml --repo todoist/comms-cli --ref main \ + -f artifact_run_id="$ARTIFACT_RUN_ID" -f artifact_run_attempt="$ARTIFACT_RUN_ATTEMPT" \ + -f source_sha="$RELEASE_SHA" -f destination=github -f tag=normal +``` + +Both login and publish inherit terminal input for fresh 2FA. Each destination has +an isolated registry/scope mapping, config and cache; global npm settings are +unchanged. npm receives public access and no GitHub credentials. GitHub visibility +is administered separately. The tarball preserves `tdc`, runtime dependencies and +skill postinstall, removes release/development scripts and routing overrides, +and uses `--ignore-scripts --provenance=false` for upload. A later publisher run +must not attest that it built an earlier source artifact. + +Before **every registry write**, including npm-only bootstrap or GitHub-only +retry, preflight any existing same-version copies in **both** registries against +the retained integrity. Stop on mismatch, auth/network failure or ambiguous +absence. Repeat after interactive login. Matching copies skip; there is no +independent version bump, rebuild or overwrite for a missing mirror. + +For later trusted releases or partial retry use the same dispatch command with +`destination=both`, `npmjs` or `github` as appropriate. Every path still checks +both copies. A pending original artifact blocks another build for its version, +including when a later failed rerun changes the run's latest status. + +## Record and recover + +Only after both registry bytes and normal latest/next channels match does the +publisher record the scope-independent tag at the original build SHA and create +an attributed release. It validates published timestamp, draft/prerelease state, +source/tag identity and **all existing assets before uploading anything**. A +matching interrupted draft resumes with the same release ID and original bytes; +conflicting/incomplete assets stop for owner reconciliation. It never deletes, +recreates, retargets or force-moves an old tag/release. + +Stable source bookkeeping updates only CHANGELOG.md, root package/lock versions, +and the existing skill metadata version. It preserves intervening main changes, +records a separate source-record SHA in semantic-release notes and reuses an +already-completed record after a lost push response. The generated commit body +contains identity trailers, never copied breaking-change release notes. Full +notes stay in CHANGELOG/GitHub. `next` excludes stable source commitback; its +channel note still advances normal prereleases. A normal non-force push detects +concurrent changes; retry the original publisher after resolving the race. + +A recorded release is complete only when its assets, tag, channels and stable +source record all validate. If latest/next has advanced, use explicit +`tag=migration-recovery` (or `RELEASE_TAG=migration-recovery` locally). It fills +an older missing mirror without moving normal channels or recording/announcing. +The existing owner handles temporary-tag cleanup only after byte verification: + +```sh +npm dist-tag rm @todoist/comms-cli migration-recovery \ + --registry=https://registry.npmjs.org/ --@todoist:registry=https://registry.npmjs.org/ +npm dist-tag rm @todoist/comms-cli migration-recovery \ + --registry=https://npm.pkg.github.com/ --@todoist:registry=https://npm.pkg.github.com/ +``` + +Use isolated destination credentials/config; interactive auth unless separately +configured OIDC tag permission and a supported CLI (npm >=11.21 on v11 or >=12.2 +on v12). Expired original Actions artifacts or unsupported metadata stop for an +explicitly reviewed original-byte recovery procedure, never repacking an +immutable published version. Workflow maintenance alone does not invalidate +an original successful source run. + +## Consumer handoff + +TodoistOS #616 needs this producer PR, final version, `tdc` bin, identical +npmjs/GitHub tarballs, actual registry access and installation evidence. The +prepared candidate is 4.1.1; update consumers if normal release analysis changes +it. New CLI self-update reads `@todoist/comms-cli` with latest for stable and next +for prereleases. An already-installed old-scope CLI cannot discover the new +package by itself: the operator/setup must explicitly migrate that installation, +retaining its config and credentials. No global installation was run here. + +In an isolated consumer test prefix after real publication, repeat for each +registry with its actual routing/authentication (lifecycle-disabled for the +initial package/bin smoke check): + +```sh +npm install --prefix "$CONSUMER_PREFIX" --ignore-scripts --no-audit --no-fund \ + @todoist/comms-cli@4.1.1 --registry=https://registry.npmjs.org/ \ + --@todoist:registry=https://registry.npmjs.org/ +"$CONSUMER_PREFIX/node_modules/.bin/tdc" --version +"$CONSUMER_PREFIX/node_modules/.bin/tdc" update --help +``` + +Repeat using `https://npm.pkg.github.com/` and the intended consumer credential; +compare both downloaded tarballs to the retained SHA/integrity. Then the owner +can resume TodoistOS's existing controlled setup/update process. Keep its package +channel policy and `tdc` executable; do not edit TodoistOS from this repository. diff --git a/oauth/client-metadata.json b/oauth/client-metadata.json index 4eb679f..4f4ef56 100644 --- a/oauth/client-metadata.json +++ b/oauth/client-metadata.json @@ -1,7 +1,7 @@ { "client_id": "https://raw.githubusercontent.com/Doist/comms-cli/main/oauth/client-metadata.json", "client_name": "Comms CLI", - "client_uri": "https://github.com/doist/comms-cli", + "client_uri": "https://github.com/todoist/comms-cli", "logo_uri": "https://raw.githubusercontent.com/doist/comms-cli/main/icons/comms-cli.png", "redirect_uris": ["http://127.0.0.1/callback", "http://localhost/callback"], "grant_types": ["authorization_code", "refresh_token"], diff --git a/package.json b/package.json index f224154..2a65329 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@doist/comms-cli", + "name": "@todoist/comms-cli", "version": "4.1.0", "description": "TypeScript CLI for Comms", "type": "module", @@ -38,24 +38,24 @@ }, "repository": { "type": "git", - "url": "https://github.com/Doist/comms-cli.git" + "url": "https://github.com/todoist/comms-cli.git" }, "publishConfig": { "access": "public", - "provenance": true + "provenance": false }, - "homepage": "https://github.com/Doist/comms-cli#readme", + "homepage": "https://github.com/todoist/comms-cli#readme", "bugs": { - "url": "https://github.com/Doist/comms-cli/issues" + "url": "https://github.com/todoist/comms-cli/issues" }, "files": [ "dist", - "scripts", + "scripts/postinstall.js", "CHANGELOG.md" ], "dependencies": { - "@doist/cli-core": "1.9.0", - "@doist/comms-sdk": "3.11.0", + "@todoist/cli-core": "~1.10.0", + "@todoist/comms-sdk": "4.0.1", "@pnpm/tabtab": "0.5.4", "chalk": "5.6.2", "commander": "14.0.3", diff --git a/release.config.js b/release.config.js index 8607c30..13ac12e 100644 --- a/release.config.js +++ b/release.config.js @@ -1,42 +1,18 @@ -/** - * @type {import('semantic-release').GlobalConfig} - */ - -const prereleaseBranches = [{ name: 'next', prerelease: true }] - -const isPrerelease = prereleaseBranches.some((b) => b.name === process.env.GITHUB_REF_NAME) +// The former stable-only changelog/git prepare hooks are completed by record-release.mjs +// AFTER exact-artifact dual publication. Build analysis must never commit these files. +export const stableSourceRecordAssets = [ + 'CHANGELOG.md', + 'package.json', + 'package-lock.json', + 'skills/comms-cli/SKILL.md', +] +/** @type {import('semantic-release').GlobalConfig} */ export default { - branches: ['main', ...prereleaseBranches], - plugins: [ - ['@semantic-release/commit-analyzer', { preset: 'conventionalcommits' }], - ['@semantic-release/release-notes-generator', { preset: 'conventionalcommits' }], - // Only update CHANGELOG.md and commit back on stable releases - ...(isPrerelease - ? [] - : [ - '@semantic-release/changelog', - ['@semantic-release/exec', { prepareCmd: 'npx oxfmt CHANGELOG.md' }], - ]), - '@semantic-release/npm', - ...(isPrerelease - ? [] - : [ - ['@semantic-release/exec', { prepareCmd: 'npm run sync:skill' }], - [ - '@semantic-release/git', - { - assets: [ - 'CHANGELOG.md', - 'package.json', - 'package-lock.json', - 'skills/comms-cli/SKILL.md', - ], - message: - 'chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}', - }, - ], - ]), - '@semantic-release/github', - ], + branches: ['main', { name: 'next', prerelease: true }], + // Scope-independent history: v4.1.0 remains the baseline for the first new-scope 4.1.1. + tagFormat: 'v${version}', + // Analysis only. The separate publisher records tags/releases after BOTH registries match. + dryRun: true, + plugins: ['./scripts/release-analysis.mjs'], } diff --git a/scripts/artifact-source.mjs b/scripts/artifact-source.mjs new file mode 100644 index 0000000..29ddb06 --- /dev/null +++ b/scripts/artifact-source.mjs @@ -0,0 +1,107 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { mkdtempSync, readFileSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { resolve } from 'node:path' +import { githubPages } from './github-pages.mjs' +import { REPOSITORY, VERSION } from './release-policy.mjs' + +export function downloadRetainedArtifact(sourceRun, target, io = { execFileSync }) { + const request = (path) => + JSON.parse( + io.execFileSync('gh', ['api', `repos/${REPOSITORY}/${path}`], { + encoding: 'utf8', + maxBuffer: 16 * 1024 * 1024, + }), + ) + const name = `release-${sourceRun.id}-${sourceRun.run_attempt}` + const artifacts = [ + ...githubPages(`actions/runs/${sourceRun.id}/artifacts`, 'artifacts', request), + ].filter((item) => item.name === name) + assert.equal( + artifacts.length, + 1, + 'Expected the unique original attempt artifact; stop, never rebuild', + ) + const artifact = artifacts[0] + assert.equal( + artifact.expired, + false, + 'Original artifact expired; stop for original-byte recovery', + ) + assert.equal(artifact.workflow_run.id, sourceRun.id) + assert.equal(artifact.workflow_run.head_sha, sourceRun.head_sha) + assert.match( + artifact.digest || '', + /^sha256:[a-f0-9]{64}$/, + 'Original archive digest unavailable; stop for original-byte recovery', + ) + assert.ok(Number.isSafeInteger(artifact.id) && artifact.id > 0) + const archive = io.execFileSync( + 'gh', + ['api', `repos/${REPOSITORY}/actions/artifacts/${artifact.id}/zip`], + { maxBuffer: 64 * 1024 * 1024 }, + ) + assert.equal( + `sha256:${createHash('sha256').update(archive).digest('hex')}`, + artifact.digest, + 'Downloaded archive differs from authenticated GitHub digest', + ) + const archivePath = resolve(target, 'source.zip') + writeFileSync(archivePath, archive, { mode: 0o600 }) + const entries = io + .execFileSync('unzip', ['-Z1', archivePath], { encoding: 'utf8' }) + .trim() + .split('\n') + const manifest = io.execFileSync('unzip', ['-p', archivePath, 'release.json'], { + maxBuffer: 1024 * 1024, + }) + const metadata = JSON.parse(manifest) + assert.match(metadata.version, VERSION) + assert.equal(metadata.filename, `todoist-comms-cli-${metadata.version}.tgz`) + assert.equal(metadata.runId, String(sourceRun.id)) + assert.equal(metadata.runAttempt, String(sourceRun.run_attempt)) + assert.equal(metadata.sourceSha, sourceRun.head_sha) + assert.deepEqual( + entries.sort(), + ['release.json', metadata.filename].sort(), + 'Unsupported original artifact layout; never rebuild a published version', + ) + writeFileSync(resolve(target, 'release.json'), manifest) + writeFileSync( + resolve(target, metadata.filename), + io.execFileSync('unzip', ['-p', archivePath, metadata.filename], { + maxBuffer: 64 * 1024 * 1024, + }), + ) + rmSync(archivePath) +} + +export function compareRetainedArtifact( + directory, + sourceRun, + download = (_runId, _artifact, target) => downloadRetainedArtifact(sourceRun, target), +) { + const retained = mkdtempSync(resolve(tmpdir(), 'comms-cli-retained-')) + try { + download(sourceRun.id, `release-${sourceRun.id}-${sourceRun.run_attempt}`, retained) + // Authenticate the whole manifest against GitHub's actual run artifact, not its own hashes. + const manifest = readFileSync(resolve(retained, 'release.json')) + assert.deepEqual( + readFileSync(resolve(directory, 'release.json')), + manifest, + 'Local release metadata differs from the authenticated build artifact', + ) + const metadata = JSON.parse(manifest) + assert.match(metadata.version, VERSION) + assert.equal(metadata.filename, `todoist-comms-cli-${metadata.version}.tgz`) + assert.deepEqual( + readFileSync(resolve(directory, metadata.filename)), + readFileSync(resolve(retained, metadata.filename)), + 'Local tarball differs from the authenticated build artifact', + ) + } finally { + rmSync(retained, { recursive: true, force: true }) + } +} diff --git a/scripts/artifact-source.test.ts b/scripts/artifact-source.test.ts new file mode 100644 index 0000000..e1ee865 --- /dev/null +++ b/scripts/artifact-source.test.ts @@ -0,0 +1,127 @@ +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { compareRetainedArtifact, downloadRetainedArtifact } from './artifact-source.mjs' + +describe('authenticated retained artifact binding', () => { + it.each(['matching', 'replaced tarball', 'replaced metadata'])( + 'checks %s against the actual selected run artifact', + (scenario) => { + const directory = mkdtempSync(join(tmpdir(), 'artifact-fixture-')) + const filename = 'todoist-comms-cli-4.1.1.tgz' + const metadata = JSON.stringify({ filename, version: '4.1.1', integrity: 'original' }) + try { + writeFileSync( + join(directory, 'release.json'), + scenario === 'replaced metadata' + ? metadata.replace('original', 'forged') + : metadata, + ) + writeFileSync( + join(directory, filename), + scenario === 'replaced tarball' ? 'forged' : 'original', + ) + const download = (runId: number, artifact: string, target: string) => { + expect(runId).toBe(42) + expect(artifact).toBe('release-42-2') + writeFileSync(join(target, 'release.json'), metadata) + writeFileSync(join(target, filename), 'original') + } + const verify = () => + compareRetainedArtifact(directory, { id: 42, run_attempt: 2 }, download) + if (scenario === 'matching') expect(verify).not.toThrow() + else expect(verify).toThrow(/differs from the authenticated build artifact/) + } finally { + rmSync(directory, { recursive: true, force: true }) + } + }, + ) +}) + +describe('original GitHub archive binding', () => { + it.each([ + 'matching original', + 'archive bytes changed', + 'digest absent', + 'wrong attempt', + 'wrong source', + 'expired', + ])('checks %s before accepting bootstrap files', (scenario) => { + const directory = mkdtempSync(join(tmpdir(), 'archive-binding-')) + try { + const target = join(directory, 'download') + mkdirSync(target) + const filename = 'todoist-comms-cli-4.1.1.tgz' + const metadata = { + version: '4.1.1', + filename, + runId: '42', + runAttempt: '2', + sourceSha: 'a'.repeat(40), + } + const manifest = JSON.stringify(metadata) + writeFileSync(join(directory, 'release.json'), manifest) + writeFileSync(join(directory, filename), 'original-tarball') + execFileSync('zip', ['-q', 'fixture.zip', 'release.json', filename], { cwd: directory }) + const archive = readFileSync(join(directory, 'fixture.zip')) + const artifact = { + id: 17, + name: 'release-42-2', + expired: scenario === 'expired', + digest: + scenario === 'digest absent' + ? null + : `sha256:${createHash('sha256').update(archive).digest('hex')}`, + workflow_run: { + id: 42, + head_sha: scenario === 'wrong source' ? 'b'.repeat(40) : 'a'.repeat(40), + }, + } + let extracted = false + const io = { + execFileSync(bin: string, args: string[], options: any) { + if (bin === 'gh') { + if ( + args[1] === + 'repos/todoist/comms-cli/actions/runs/42/artifacts?per_page=100&page=1' + ) + return JSON.stringify({ artifacts: [artifact] }) + expect(args).toEqual([ + 'api', + 'repos/todoist/comms-cli/actions/artifacts/17/zip', + ]) + return scenario === 'archive bytes changed' + ? Buffer.from('different archive') + : archive + } + expect(bin).toBe('unzip') + extracted = true + return execFileSync(bin, args, options) + }, + } + const check = () => + downloadRetainedArtifact( + { + id: 42, + run_attempt: scenario === 'wrong attempt' ? 3 : 2, + head_sha: 'a'.repeat(40), + }, + target, + io, + ) + if (scenario === 'matching original') { + expect(check).not.toThrow() + expect(readFileSync(join(target, 'release.json'), 'utf8')).toBe(manifest) + expect(readFileSync(join(target, filename), 'utf8')).toBe('original-tarball') + } else { + expect(check).toThrow() + expect(extracted).toBe(false) + } + } finally { + rmSync(directory, { recursive: true, force: true }) + } + }) +}) diff --git a/scripts/github-pages.mjs b/scripts/github-pages.mjs new file mode 100644 index 0000000..81b4f60 --- /dev/null +++ b/scripts/github-pages.mjs @@ -0,0 +1,25 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { REPOSITORY } from './release-policy.mjs' + +// Bound each gh response instead of collecting the complete history in one child buffer. +// Errors always propagate: an unread page must never imply that a release is absent. +export function* githubPages( + path, + key, + request = (route) => + JSON.parse( + execFileSync('gh', ['api', `repos/${REPOSITORY}/${route}`], { + encoding: 'utf8', + maxBuffer: 16 * 1024 * 1024, + }), + ), +) { + for (let page = 1; ; page++) { + const result = request(`${path}${path.includes('?') ? '&' : '?'}per_page=100&page=${page}`) + const items = key ? result[key] : result + assert.ok(Array.isArray(items), 'Unexpected GitHub pagination response; stop') + yield* items + if (items.length < 100) return + } +} diff --git a/scripts/github-pages.test.ts b/scripts/github-pages.test.ts new file mode 100644 index 0000000..68ea763 --- /dev/null +++ b/scripts/github-pages.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from 'vitest' +import { githubPages } from './github-pages.mjs' +import { findRelease } from './github-release-record.mjs' + +describe('GitHub history reads', () => { + it('finds a draft on a later page without one unbounded subprocess response', () => { + const routes: string[] = [] + const release = { id: 42, tag_name: 'v4.1.1', draft: true } + const api = { + list: () => + githubPages('releases', undefined, (route: string) => { + routes.push(route) + return route.endsWith('page=1') + ? Array.from({ length: 100 }, (_, id) => ({ id, tag_name: `v0.0.${id}` })) + : [release] + }), + get: (id: number) => { + expect(id).toBe(42) + return release + }, + } + expect(findRelease(api, 'v4.1.1')).toEqual(release) + expect(routes).toEqual(['releases?per_page=100&page=1', 'releases?per_page=100&page=2']) + }) + it('reads workflow result envelopes with their existing filters intact', () => { + const routes: string[] = [] + const runs = [ + ...githubPages( + 'actions/workflows/release.yml/runs?branch=main&status=success', + 'workflow_runs', + (route: string) => { + routes.push(route) + return { workflow_runs: [{ id: 123 }] } + }, + ), + ] + expect(runs).toEqual([{ id: 123 }]) + expect(routes).toEqual([ + 'actions/workflows/release.yml/runs?branch=main&status=success&per_page=100&page=1', + ]) + }) + it('never treats an unread later page as release absence', () => { + const api = { + list: () => + githubPages('releases', undefined, (route: string) => { + if (route.endsWith('page=1')) + return Array.from({ length: 100 }, (_, id) => ({ + id, + tag_name: `v0.0.${id}`, + })) + throw new Error('network failure') + }), + } + expect(() => findRelease(api, 'v4.1.1')).toThrow('network failure') + }) +}) diff --git a/scripts/github-release-record.mjs b/scripts/github-release-record.mjs new file mode 100644 index 0000000..b1492ab --- /dev/null +++ b/scripts/github-release-record.mjs @@ -0,0 +1,302 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' +import { githubPages } from './github-pages.mjs' +import { REPOSITORY, SHA, VERSION, validateSourceRun } from './release-policy.mjs' +import { recordStableSource } from './source-release-record.mjs' + +export function githubReleaseApi() { + const request = (path, body, method) => + JSON.parse( + execFileSync( + 'gh', + [ + 'api', + `repos/${REPOSITORY}/${path}`, + ...(body ? ['--method', method || 'POST', '--input', '-'] : []), + ], + { encoding: 'utf8', ...(body ? { input: JSON.stringify(body) } : {}) }, + ), + ) + return { + list: () => githubPages('releases'), + get: (id) => request(`releases/${id}`), + sourceRun: (id, attempt) => request(`actions/runs/${id}/attempts/${attempt}`), + workflowId: () => request('actions/workflows/release.yml').id, + compare: (base, head) => request(`compare/${base}...${head}`), + sourceRecord: (metadata) => recordStableSource(metadata, { verifyOnly: true }), + assets: (id) => [...githubPages(`releases/${id}/assets`)], + readAsset: (id) => + execFileSync( + 'gh', + [ + 'api', + '-H', + 'Accept: application/octet-stream', + `repos/${REPOSITORY}/releases/assets/${id}`, + ], + { maxBuffer: 64 * 1024 * 1024 }, + ), + tagSha: (tag) => { + const refs = request(`git/matching-refs/tags/${tag}`) + const ref = refs.find((item) => item.ref === `refs/tags/${tag}`) + if (!ref) return undefined + assert.equal(ref.object.type, 'commit', 'Unexpected tag object; never replace it') + return ref.object.sha + }, + createTag: (tag, sha) => request('git/refs', { ref: `refs/tags/${tag}`, sha }), + createDraft: (metadata) => + request('releases', { + tag_name: `v${metadata.version}`, + target_commitish: metadata.sourceSha, + name: `v${metadata.version}`, + body: metadata.notes, + draft: true, + prerelease: metadata.branch === 'next', + }), + upload: (id, name, file) => + JSON.parse( + execFileSync( + 'gh', + [ + 'api', + '--method', + 'POST', + `https://uploads.github.com/repos/${REPOSITORY}/releases/${id}/assets?name=${encodeURIComponent(name)}`, + '-H', + 'Content-Type: application/octet-stream', + '--input', + file, + ], + { encoding: 'utf8' }, + ), + ), + publish: (id, metadata) => + request( + `releases/${id}`, + { + draft: false, + make_latest: metadata.branch === 'next' ? 'false' : 'true', + }, + 'PATCH', + ), + } +} + +export function findRelease(api, tag) { + const matches = [] + for (const item of api.list()) if (item.tag_name === tag) matches.push(item) + assert.ok( + matches.length <= 1, + `Multiple records for ${tag}; stop for maintainer reconciliation`, + ) + return matches[0] ? api.get(matches[0].id) : undefined +} + +function assertIdentity(release, metadata) { + assert.match(metadata.version, VERSION) + assert.match(metadata.sourceSha, SHA) + assert.ok(['main', 'next'].includes(metadata.branch)) + assert.equal(metadata.version.includes('-next.'), metadata.branch === 'next') + assert.ok(metadata.notes.startsWith('[Posted by Jarvis]\n\n')) + assert.equal( + release.tag_name, + `v${metadata.version}`, + 'Release tag differs from the original artifact', + ) + assert.equal( + release.target_commitish, + metadata.sourceSha, + 'Release targets another source; never retarget it', + ) + assert.equal( + release.prerelease, + metadata.branch === 'next', + 'Release prerelease state is wrong', + ) + assert.equal( + release.body, + metadata.notes, + 'Release notes differ from original metadata; inspect this same record', + ) + assert.equal(typeof release.draft, 'boolean') + if (release.draft) + assert.equal(release.published_at, null, 'Unexpected draft publication state') + else + assert.ok( + typeof release.published_at === 'string' && + Number.isFinite(Date.parse(release.published_at)), + 'Release has no valid published timestamp', + ) +} + +function inspectAssets(api, release, metadata, files, allowMissing) { + const assets = api.assets(release.id) + assert.ok( + assets.every((asset) => files.some((file) => file.name === asset.name)), + 'Unexpected release asset; inspect all original assets before any upload', + ) + const missing = [] + for (const file of files) { + const matches = assets.filter((asset) => asset.name === file.name) + assert.ok(matches.length <= 1, `Duplicate release asset ${file.name}`) + const asset = matches[0] + if (!asset) { + assert.ok( + allowMissing, + `Published release ${release.id} lacks ${file.name}. Recover original run ${metadata.runId}/${metadata.runAttempt} bytes into this same record; never rebuild, delete, recreate or retag.`, + ) + missing.push(file) + continue + } + assert.equal( + asset.state, + 'uploaded', + `Incomplete release asset ${asset.id} (${file.name}). Stop; retain original run ${metadata.runId}/${metadata.runAttempt} bytes and inspect this same draft without deleting or replacing assets.`, + ) + assert.equal( + asset.size, + file.bytes.length, + `Release asset ${asset.id} size differs from original bytes`, + ) + assert.deepEqual( + api.readAsset(asset.id), + file.bytes, + `Release asset ${asset.id} differs from original bytes; never replace it`, + ) + } + return missing +} + +export function assertPublishedRecord(api, release, metadata, files) { + assert.ok( + release, + `Release v${metadata.version} is not recorded; finish original run ${metadata.runId}/${metadata.runAttempt}`, + ) + assertIdentity(release, metadata) + assert.equal( + release.draft, + false, + `Release ${release.id} is still a draft; resume original run ${metadata.runId}/${metadata.runAttempt}`, + ) + assert.equal( + api.tagSha(`v${metadata.version}`), + metadata.sourceSha, + 'Release tag commit differs; never retag', + ) + inspectAssets(api, release, metadata, files, false) +} + +export function releaseFiles(directory, metadata) { + assert.equal(metadata.filename, `todoist-comms-cli-${metadata.version}.tgz`) + return [metadata.filename, 'release.json'].map((name) => ({ + name, + path: resolve(directory, name), + bytes: readFileSync(resolve(directory, name)), + })) +} + +// A tag can already exist after an interrupted recorder. Check the durable release record +// before treating any of its ancestor runs as completed history, even after Actions expiry. +export async function assertRecordedBaseline(api, tag) { + const release = findRelease(api, tag) + assert.ok(release, `Baseline ${tag} has no release record; recover its original run first`) + assert.equal(release.tag_name, tag) + assert.equal( + release.draft, + false, + `Baseline ${tag} is still a draft; resume its original run first`, + ) + assert.ok( + typeof release.published_at === 'string' && + Number.isFinite(Date.parse(release.published_at)), + 'Baseline has no published timestamp', + ) + if (tag === 'v4.1.0') { + // The inspected legacy release has no assets. This exception is bound to its exact commit. + assert.equal(api.tagSha(tag), '7d75593399fb3c4a51aa6dcfa331098ffbfa8b26') + assert.equal(release.prerelease, false) + return + } + const assets = api.assets(release.id) + const metadataAssets = assets.filter((asset) => asset.name === 'release.json') + assert.equal( + metadataAssets.length, + 1, + 'Baseline lacks unique original release.json; recover original bytes', + ) + assert.equal(metadataAssets[0].state, 'uploaded') + const bytes = api.readAsset(metadataAssets[0].id) + const metadata = JSON.parse(bytes.toString('utf8')) + assert.equal(tag, `v${metadata.version}`) + assert.equal(metadata.name, '@todoist/comms-cli') + assert.equal(metadata.filename, `todoist-comms-cli-${metadata.version}.tgz`) + assert.match(metadata.runId, /^\d+$/) + assert.match(metadata.runAttempt, /^\d+$/) + const tarballs = assets.filter((asset) => asset.name === metadata.filename) + assert.equal( + tarballs.length, + 1, + 'Baseline lacks unique original tarball; recover original bytes', + ) + const tarball = api.readAsset(tarballs[0].id) + assert.equal(createHash('sha256').update(tarball).digest('hex'), metadata.sha256) + assert.equal( + `sha512-${createHash('sha512').update(tarball).digest('base64')}`, + metadata.integrity, + ) + assertPublishedRecord(api, release, metadata, [ + { name: 'release.json', bytes }, + { name: metadata.filename, bytes: tarball }, + ]) + const run = api.sourceRun(metadata.runId, metadata.runAttempt) + assert.equal(String(run.id), metadata.runId) + assert.equal(String(run.run_attempt), metadata.runAttempt) + assert.equal(run.head_branch, metadata.branch) + await validateSourceRun(run, api.workflowId(), metadata.sourceSha, api.compare) + if (metadata.branch === 'main') api.sourceRecord(metadata) +} + +// Caller has already authenticated the source artifact and both registry copies. Every API +// response is re-read before success. No delete, clobber, release recreation or retag operation. +export function completeReleaseRecord(api, metadata, files) { + const tag = `v${metadata.version}` + let release = findRelease(api, tag) + const tagSha = api.tagSha(tag) + if (release) { + assertIdentity(release, metadata) + assert.equal( + tagSha, + metadata.sourceSha, + 'Existing release has a missing/different tag; never recreate or retag', + ) + if (!release.draft) { + assertPublishedRecord(api, release, metadata, files) + return false + } + } else { + assert.ok( + tagSha === undefined || tagSha === metadata.sourceSha, + 'Tag belongs to another source; never retag', + ) + if (tagSha === undefined) api.createTag(tag, metadata.sourceSha) + release = api.createDraft(metadata) + release = api.get(release.id) + assertIdentity(release, metadata) + assert.equal(release.draft, true) + } + // Check ALL completed assets before uploading any missing ones; a mismatch cannot be hidden + // by a partially successful retry. Lost upload responses resume against fresh asset listings. + const missing = inspectAssets(api, release, metadata, files, true) + for (const file of missing) api.upload(release.id, file.name, file.path) + release = api.get(release.id) + assertIdentity(release, metadata) + assert.equal(release.draft, true, 'Release changed during recovery; inspect before continuing') + inspectAssets(api, release, metadata, files, false) + assert.equal(api.tagSha(tag), metadata.sourceSha, 'Tag moved before publication; stop') + api.publish(release.id, metadata) + assertPublishedRecord(api, api.get(release.id), metadata, files) + return true +} diff --git a/scripts/github-release-record.test.ts b/scripts/github-release-record.test.ts new file mode 100644 index 0000000..a32d7c1 --- /dev/null +++ b/scripts/github-release-record.test.ts @@ -0,0 +1,306 @@ +import assert from 'node:assert/strict' +import { describe, expect, it } from 'vitest' + +function required(value: T | undefined): T { + assert.ok(value) + return value +} +import { assertRecordedBaseline, completeReleaseRecord } from './github-release-record.mjs' + +// GitHub API is the boundary. Persist its state across failures/retries as GitHub does. +function fixture() { + const metadata = { + name: '@todoist/comms-cli', + version: '4.1.1', + sourceSha: 'a'.repeat(40), + branch: 'main', + notes: '[Posted by Jarvis]\n\nOriginal notes', + filename: 'todoist-comms-cli-4.1.1.tgz', + runId: '123', + runAttempt: '2', + sha256: 'f75a2e8a5a6cb4e64169f570ada8d4916aa4f0b684b2a39894a46a9bfe2d7264', + integrity: + 'sha512-Wn368XR7iqmcszTtKatnSwBwtbCBki8nnx6HwsOnEKCNhUSlh/6zh3j61jXh1OFwlUjhASA7mrEHx4+WcgRrpg==', + } + const files = [ + { + name: metadata.filename, + path: '/original/package.tgz', + bytes: Buffer.from('original-tarball'), + }, + { + name: 'release.json', + path: '/original/release.json', + bytes: Buffer.from(JSON.stringify(metadata)), + }, + ] + const original = { + id: 42, + tag_name: 'v4.1.1', + target_commitish: metadata.sourceSha, + body: metadata.notes, + draft: true, + prerelease: false, + published_at: null as string | null, + } + const state = { + release: { ...original } as typeof original | undefined, + tag: metadata.sourceSha as string | undefined, + assets: [] as { id: number; name: string; size: number; state: string; bytes: Buffer }[], + writes: [] as unknown[], + loseResponse: '', + failedPublish: false, + sourceRun: { + id: 123, + run_attempt: 2, + repository: { full_name: 'todoist/comms-cli' }, + head_repository: { full_name: 'todoist/comms-cli' }, + workflow_id: 7, + path: '.github/workflows/release.yml', + status: 'completed', + conclusion: 'success', + event: 'push', + head_branch: 'main', + head_sha: metadata.sourceSha, + }, + } + const lose = (operation: string) => { + if (state.loseResponse === operation) { + state.loseResponse = '' + throw new Error(`lost ${operation} response`) + } + } + const add = (name: string, bytes: Buffer) => { + state.assets.push({ + id: 100 + state.assets.length, + name, + bytes, + size: bytes.length, + state: 'uploaded', + }) + } + const api = { + list: () => (state.release ? [{ id: 42, tag_name: 'v4.1.1' }] : []), + get: (id: number) => { + expect(id).toBe(42) + return { ...state.release } + }, + assets: (id: number) => { + expect(id).toBe(42) + return state.assets + }, + readAsset: (id: number) => required(state.assets.find((asset) => asset.id === id)).bytes, + tagSha: (tag: string) => { + expect(tag).toMatch(/^v4\.1\.[01]$/) + return state.tag + }, + createTag: (tag: string, sha: string) => { + state.writes.push(['tag', tag, sha]) + state.tag = sha + lose('tag') + }, + createDraft: (plan: typeof metadata) => { + state.writes.push(['draft', plan]) + state.release = { ...original } + lose('draft') + return { id: 42 } + }, + upload: (id: number, name: string, path: string) => { + state.writes.push(['upload', id, name, path]) + add(name, required(files.find((file) => file.path === path)).bytes) + lose('upload') + }, + publish: (id: number, plan: typeof metadata) => { + state.writes.push(['publish', id, plan]) + if (!state.failedPublish) { + required(state.release).draft = false + required(state.release).published_at = '2026-11-10T21:00:00Z' + } + lose('publish') + }, + sourceRun: (id: string, attempt: string) => { + expect([id, attempt]).toEqual(['123', '2']) + return state.sourceRun + }, + workflowId: () => 7, + sourceRecord: (plan: typeof metadata) => { + expect(plan).toEqual(metadata) + return { recordSha: 'c'.repeat(40) } + }, + compare: () => ({ status: 'ahead', merge_base_commit: { sha: metadata.sourceSha } }), + } + const populate = () => { + for (const file of files) add(file.name, file.bytes) + } + const published = () => { + required(state.release).draft = false + required(state.release).published_at = '2026-11-10T21:00:00Z' + populate() + } + const record = () => completeReleaseRecord(api, metadata, files) + const baseline = () => assertRecordedBaseline(api, 'v4.1.1') + return { metadata, files, state, api, populate, published, record, baseline } +} + +describe('release record recovery', () => { + it('skips only a published record with exact original assets and metadata', () => { + const f = fixture() + f.published() + expect(f.record()).toBe(false) + expect(f.state.writes).toEqual([]) + }) + it('resumes the same draft, keeps uploaded original bytes, and publishes after both assets exist', () => { + const f = fixture() + f.populate() + f.state.assets.pop() + expect(f.record()).toBe(true) + expect(f.state.writes).toEqual([ + ['upload', 42, 'release.json', '/original/release.json'], + ['publish', 42, f.metadata], + ]) + expect(f.state.release).toMatchObject({ + id: 42, + draft: false, + prerelease: false, + published_at: '2026-11-10T21:00:00Z', + }) + expect(f.state.assets.map((asset) => [asset.name, asset.bytes])).toEqual( + f.files.map((file) => [file.name, file.bytes]), + ) + }) + it.each(['tag', 'draft', 'upload', 'publish'])( + 'recovers a lost %s response without recreation, duplication or retagging', + (operation) => { + const f = fixture() + f.state.release = undefined + f.state.tag = undefined + f.state.loseResponse = operation + expect(f.record).toThrow(`lost ${operation} response`) + expect(f.record()).toBe(operation !== 'publish') + expect(f.state.writes).toEqual([ + ['tag', 'v4.1.1', f.metadata.sourceSha], + ['draft', f.metadata], + ['upload', 42, 'todoist-comms-cli-4.1.1.tgz', '/original/package.tgz'], + ['upload', 42, 'release.json', '/original/release.json'], + ['publish', 42, f.metadata], + ]) + expect(f.record()).toBe(false) + }, + ) + it.each([ + 'wrong commit', + 'missing tag', + 'wrong target', + 'wrong prerelease', + 'wrong notes', + 'bad draft timestamp', + 'incomplete asset', + 'different bytes', + 'different metadata', + 'duplicate asset', + 'unexpected asset', + ])('stops before any write on %s', (scenario) => { + const f = fixture() + f.populate() + if (scenario === 'unexpected asset') + f.state.assets.push({ ...f.state.assets[0], name: 'unreviewed.txt' }) + if (scenario === 'wrong commit') f.state.tag = 'b'.repeat(40) + if (scenario === 'missing tag') f.state.tag = undefined + if (scenario === 'wrong target') required(f.state.release).target_commitish = 'b'.repeat(40) + if (scenario === 'wrong prerelease') required(f.state.release).prerelease = true + if (scenario === 'wrong notes') required(f.state.release).body += ' edited' + if (scenario === 'bad draft timestamp') + required(f.state.release).published_at = '2026-11-10T21:00:00Z' + if (scenario === 'incomplete asset') f.state.assets[0].state = 'starter' + if (scenario === 'different bytes') f.state.assets[0].bytes = Buffer.from('different-bytes') + if (scenario === 'different metadata') + f.state.assets[1].bytes = Buffer.from( + f.state.assets[1].bytes.toString().replace('Original', 'Replaced'), + ) + if (scenario === 'duplicate asset') f.state.assets.push({ ...f.state.assets[0] }) + expect(f.record).toThrow() + expect(f.state.writes).toEqual([]) + }) + it('checks every existing asset before uploading an earlier missing asset', () => { + const f = fixture() + f.populate() + f.state.assets.shift() + f.state.assets[0].bytes = Buffer.from('conflict') + expect(f.record).toThrow(/differs/) + expect(f.state.writes).toEqual([]) + }) + it('does not report success if the publish response did not finish publication', () => { + const f = fixture() + f.populate() + f.state.failedPublish = true + expect(f.record).toThrow(/still a draft/) + }) + it('stops on an incomplete published record with exact original-run recovery guidance', () => { + const f = fixture() + f.published() + f.state.assets.pop() + expect(f.record).toThrow( + /Recover original run 123\/2 bytes into this same record; never rebuild, delete, recreate or retag/, + ) + expect(f.state.writes).toEqual([]) + }) +}) + +describe('recorded semantic-release baseline', () => { + it('does not accept a stable baseline whose source changelog/version record is still pending', async () => { + const f = fixture() + f.published() + f.api.sourceRecord = () => { + throw new Error('source record incomplete') + } + await expect(f.baseline()).rejects.toThrow('source record incomplete') + expect(f.state.writes).toEqual([]) + }) + + it('accepts durable original assets after Actions artifact expiry', async () => { + const f = fixture() + f.published() + await expect(f.baseline()).resolves.toBeUndefined() + expect(f.state.writes).toEqual([]) + }) + it.each([ + 'bare tag', + 'draft', + 'missing metadata', + 'missing tarball', + 'wrong commit', + 'wrong prerelease', + 'changed bytes', + 'unsuccessful run', + ])('rejects %s as completed history', async (scenario) => { + const f = fixture() + f.published() + if (scenario === 'bare tag') f.state.release = undefined + if (scenario === 'draft') { + required(f.state.release).draft = true + required(f.state.release).published_at = null + } + if (scenario === 'missing metadata') f.state.assets.pop() + if (scenario === 'missing tarball') f.state.assets.shift() + if (scenario === 'unexpected asset') + f.state.assets.push({ ...f.state.assets[0], name: 'unreviewed.txt' }) + if (scenario === 'wrong commit') f.state.tag = 'b'.repeat(40) + if (scenario === 'wrong prerelease') required(f.state.release).prerelease = true + if (scenario === 'changed bytes') f.state.assets[0].bytes = Buffer.from('forged') + if (scenario === 'unsuccessful run') f.state.sourceRun.conclusion = 'failure' + await expect(f.baseline()).rejects.toThrow() + expect(f.state.writes).toEqual([]) + }) + it('binds the asset-free legacy exception to the inspected v4.1.0 commit and published state', async () => { + const f = fixture() + f.state.tag = '7d75593399fb3c4a51aa6dcfa331098ffbfa8b26' + required(f.state.release).tag_name = 'v4.1.0' + required(f.state.release).draft = false + required(f.state.release).published_at = '2026-11-10T17:25:09Z' + f.api.list = () => [{ id: 42, tag_name: 'v4.1.0' }] + await expect(assertRecordedBaseline(f.api, 'v4.1.0')).resolves.toBeUndefined() + f.state.tag = 'b'.repeat(40) + await expect(assertRecordedBaseline(f.api, 'v4.1.0')).rejects.toThrow() + expect(f.state.writes).toEqual([]) + }) +}) diff --git a/scripts/pending-release.mjs b/scripts/pending-release.mjs new file mode 100644 index 0000000..179a9d6 --- /dev/null +++ b/scripts/pending-release.mjs @@ -0,0 +1,117 @@ +import assert from 'node:assert/strict' +import { execFileSync, spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync, mkdirSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { resolve } from 'node:path' +import { downloadRetainedArtifact } from './artifact-source.mjs' +import { githubPages } from './github-pages.mjs' +import { + githubReleaseApi, + assertRecordedBaseline, + assertPublishedRecord, + findRelease, + releaseFiles, +} from './github-release-record.mjs' +import { validateSourceRun } from './release-policy.mjs' +import { REPOSITORY, VERSION } from './release-policy.mjs' + +// Analysis and publication are separate jobs. Do not make two different tarballs for the same +// next version when another push arrives before the earlier artifact's publication is recorded. +export function requireRecordedPlans(plans, isRecorded) { + for (const plan of plans) { + assert.match(plan.version, VERSION) + assert.ok( + isRecorded(plan), + `Retained release ${plan.version} from run ${plan.runId} is not recorded. Finish that exact artifact first, then dispatch a new build from the current release branch.`, + ) + } +} + +export function requireUnallocatedVersion(version, tagExists) { + assert.match(version, VERSION) + assert.ok( + !tagExists(`v${version}`), + `Version ${version} is already tagged; complete its release/channel record before building again`, + ) +} + +export async function checkPendingReleases(baselineTag, proposedVersion) { + const releases = githubReleaseApi() + await assertRecordedBaseline(releases, baselineTag) + const api = (path) => + JSON.parse(execFileSync('gh', ['api', `repos/${REPOSITORY}/${path}`], { encoding: 'utf8' })) + requireUnallocatedVersion( + proposedVersion, + (tag) => + spawnSync('git', ['rev-parse', '--verify', `refs/tags/${tag}`], { stdio: 'ignore' }) + .status === 0, + ) + const branch = process.env.GITHUB_REF_NAME + assert.ok(['main', 'next'].includes(branch)) + const directory = mkdtempSync(resolve(tmpdir(), 'comms-cli-pending-')) + try { + for await (const { target } of retainedPlans(branch, directory, { + request: api, + beforeBaseline: (sha) => + spawnSync('git', ['merge-base', '--is-ancestor', sha, baselineTag], { + stdio: 'ignore', + }).status === 0, + })) { + const plan = JSON.parse(readFileSync(resolve(target, 'release.json'), 'utf8')) + requireRecordedPlans([plan], (metadata) => { + assertPublishedRecord( + releases, + findRelease(releases, `v${metadata.version}`), + metadata, + releaseFiles(target, metadata), + ) + if (metadata.branch === 'main') releases.sourceRecord(metadata) + return true + }) + } + } finally { + rmSync(directory, { recursive: true, force: true }) + } +} + +// A failed later rerun must not hide an earlier successful attempt's unrecorded artifact. +export async function* retainedPlans( + branch, + directory, + { request, beforeBaseline = () => false, download = downloadRetainedArtifact }, +) { + const workflow = request('actions/workflows/release.yml') + for (const run of githubPages( + `actions/workflows/release.yml/runs?branch=${branch}`, + 'workflow_runs', + request, + )) { + if (beforeBaseline(run.head_sha)) continue + const artifacts = [...githubPages(`actions/runs/${run.id}/artifacts`, 'artifacts', request)] + const attempts = new Set() + for (const artifact of artifacts) { + const match = new RegExp(`^release-${run.id}-([1-9]\\d*)$`).exec(artifact.name) + if (!match) continue + assert.ok( + !attempts.has(match[1]), + 'Duplicate original attempt artifact; inspect before another build', + ) + attempts.add(match[1]) + assert.equal( + artifact.expired, + false, + 'Original artifact expired; recover its bytes before another build', + ) + const original = request(`actions/runs/${run.id}/attempts/${match[1]}`) + assert.equal(original.id, run.id) + assert.equal(String(original.run_attempt), match[1]) + await validateSourceRun(original, workflow.id, run.head_sha, async (base, head) => + request(`compare/${base}...${head}`), + ) + const target = resolve(directory, `${run.id}-${match[1]}`) + mkdirSync(target) + download(original, target) + yield { target, original } + } + } +} diff --git a/scripts/pending-release.test.ts b/scripts/pending-release.test.ts new file mode 100644 index 0000000..3e1baeb --- /dev/null +++ b/scripts/pending-release.test.ts @@ -0,0 +1,65 @@ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { expect, it } from 'vitest' +import { retainedPlans } from './pending-release.mjs' + +it.each(['success', 'failure'])( + 'uses the original attempt after the latest rerun failed (%s original)', + async (conclusion) => { + const directory = mkdtempSync(join(tmpdir(), 'pending-release-')) + const sha = 'a'.repeat(40) + const original = { + id: 42, + run_attempt: 2, + repository: { full_name: 'todoist/comms-cli' }, + head_repository: { full_name: 'todoist/comms-cli' }, + workflow_id: 7, + path: '.github/workflows/release.yml', + status: 'completed', + conclusion, + event: 'push', + head_branch: 'main', + head_sha: sha, + } + const requests: string[] = [] + const downloads: unknown[] = [] + const request = (route: string) => { + requests.push(route) + if (route === 'actions/workflows/release.yml') return { id: 7 } + if (route === 'actions/workflows/release.yml/runs?branch=main&per_page=100&page=1') + return { + workflow_runs: [ + { id: 42, run_attempt: 3, conclusion: 'failure', head_sha: sha }, + ], + } + if (route === 'actions/runs/42/artifacts?per_page=100&page=1') + return { artifacts: [{ id: 11, name: 'release-42-2', expired: false }] } + if (route === 'actions/runs/42/attempts/2') return original + if (route === `compare/${sha}...main`) + return { status: 'ahead', merge_base_commit: { sha } } + throw Error(`unexpected request ${route}`) + } + const collect = async () => { + const values = [] + for await (const value of retainedPlans('main', directory, { + request, + download: (run: unknown) => downloads.push(run), + })) + values.push(value) + return values + } + try { + if (conclusion === 'success') { + expect(await collect()).toEqual([{ target: join(directory, '42-2'), original }]) + expect(downloads).toEqual([original]) + } else { + await expect(collect()).rejects.toThrow() + expect(downloads).toEqual([]) + } + expect(requests).toContain('actions/runs/42/attempts/2') + } finally { + rmSync(directory, { recursive: true, force: true }) + } + }, +) diff --git a/scripts/prepare-release.mjs b/scripts/prepare-release.mjs new file mode 100644 index 0000000..ee6c23d --- /dev/null +++ b/scripts/prepare-release.mjs @@ -0,0 +1,38 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs' +import semanticRelease from 'semantic-release' +import { checkPendingReleases } from './pending-release.mjs' +import { REPOSITORY } from './release-policy.mjs' + +assert.equal(process.env.GITHUB_REPOSITORY?.toLowerCase(), REPOSITORY) +assert.ok(['main', 'next'].includes(process.env.GITHUB_REF_NAME)) +assert.equal( + execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim(), + process.env.GITHUB_SHA, +) +// semantic-release's dry run checks push authorization but never pushes, tags or publishes. +const result = await semanticRelease() +if (result) await checkPendingReleases(result.lastRelease.gitTag, result.nextRelease.version) +assert.equal( + execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim(), + process.env.GITHUB_SHA, +) +if (result) { + mkdirSync('.tmp/release', { recursive: true }) + writeFileSync( + '.tmp/release/plan.json', + JSON.stringify({ + version: result.nextRelease.version, + notes: result.nextRelease.notes, + branch: process.env.GITHUB_REF_NAME, + sourceSha: process.env.GITHUB_SHA, + runId: process.env.GITHUB_RUN_ID, + runAttempt: process.env.GITHUB_RUN_ATTEMPT, + }), + ) + execFileSync('node', ['scripts/release.mjs', 'pack', '.tmp/release'], { stdio: 'inherit' }) + appendFileSync(process.env.GITHUB_OUTPUT, 'prepared=true\n') +} else { + appendFileSync(process.env.GITHUB_OUTPUT, 'prepared=false\n') +} diff --git a/scripts/record-release.mjs b/scripts/record-release.mjs new file mode 100644 index 0000000..bad295c --- /dev/null +++ b/scripts/record-release.mjs @@ -0,0 +1,94 @@ +import assert from 'node:assert/strict' +import { execFileSync, spawnSync } from 'node:child_process' +import { readFileSync, appendFileSync } from 'node:fs' +import { resolve } from 'node:path' +import { githubReleaseApi, completeReleaseRecord, releaseFiles } from './github-release-record.mjs' +import { REPOSITORY, VERSION, SHA } from './release-policy.mjs' +import { recordStableSource, releaseChannelNote } from './source-release-record.mjs' + +const directory = resolve(process.argv[2]) +const metadata = JSON.parse(readFileSync(resolve(directory, 'release.json'), 'utf8')) +const run = (bin, args, options = {}) => + execFileSync(bin, args, { encoding: 'utf8', ...options }).trim() +assert.equal(process.env.GITHUB_REPOSITORY?.toLowerCase(), REPOSITORY) +assert.match(metadata.version, VERSION) +assert.match(metadata.sourceSha, SHA) +assert.ok(metadata.notes.startsWith('[Posted by Jarvis]\n\n')) +assert.notEqual( + process.env.RELEASE_TAG, + 'migration-recovery', + 'Recovery does not promote or create a release record', +) +// Verification is independent of release.json assertions and repeats the GitHub source-run checks. +const readEnv = { ...process.env, GH_TOKEN: process.env.SOURCE_GH_TOKEN || process.env.GH_TOKEN } +run('node', ['scripts/release.mjs', 'verify', directory], { env: readEnv }) +for (const destination of ['npmjs', 'github']) { + const state = JSON.parse( + run('node', ['scripts/release.mjs', 'check-registry', directory, destination], { + env: readEnv, + }), + ) + const channel = metadata.branch === 'next' ? 'next' : 'latest' + assert.equal( + state.tags[channel], + metadata.version, + 'Record only after both normal channels match; recovery stays separate', + ) +} +const tag = `v${metadata.version}` +const created = completeReleaseRecord( + githubReleaseApi(), + metadata, + releaseFiles(directory, metadata), +) +// Stable-only compatibility with the former changelog/git prepare hooks. Both registry copies +// and the completed GitHub record were verified above. Never change/repack the source artifact. +const sourceRecord = recordStableSource(metadata) + +// semantic-release needs the channel note to continue next.N as well as the v tag. +// Keep the tag at the original build SHA; the separate stable source commit is recorded here. +const remote = `https://github.com/${REPOSITORY}.git` +const gitAuth = [ + '-c', + 'http.https://github.com/.extraheader=', + '-c', + 'credential.helper=', + '-c', + 'credential.helper=!gh auth git-credential', +] +const notesRef = 'refs/notes/semantic-release' +if (run('git', [...gitAuth, 'ls-remote', remote, notesRef])) { + run('git', [...gitAuth, 'fetch', remote, `${notesRef}:${notesRef}`]) +} +run('git', [...gitAuth, 'fetch', remote, `refs/tags/${tag}:refs/tags/${tag}`]) +const note = spawnSync('git', ['notes', '--ref', notesRef, 'show', tag], { encoding: 'utf8' }) +if (note.status !== 0) { + assert.equal(note.status, 1) + assert.match( + note.stderr, + /no note found for object/, + 'Cannot read existing semantic-release note; never replace unknown history', + ) +} +const existingNote = note.status === 0 ? JSON.parse(note.stdout) : { channels: [] } +const updatedNote = releaseChannelNote(existingNote, metadata, sourceRecord) +if (JSON.stringify(updatedNote) !== JSON.stringify(existingNote)) { + run('git', [ + '-c', + 'user.name=github-actions[bot]', + '-c', + 'user.email=41898282+github-actions[bot]@users.noreply.github.com', + 'notes', + '--ref', + notesRef, + 'add', + '-f', + '-m', + JSON.stringify(updatedNote), + tag, + ]) + run('git', [...gitAuth, 'push', remote, notesRef]) +} +appendFileSync(process.env.GITHUB_OUTPUT, `created=${created}\n`) +if (sourceRecord) + appendFileSync(process.env.GITHUB_OUTPUT, `source_record_sha=${sourceRecord.recordSha}\n`) diff --git a/scripts/record-release.test.ts b/scripts/record-release.test.ts new file mode 100644 index 0000000..d8bd017 --- /dev/null +++ b/scripts/record-release.test.ts @@ -0,0 +1,78 @@ +import { spawnSync } from 'node:child_process' +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { describe, expect, it } from 'vitest' + +// Run the real record CLI. Only external process boundaries are faked; no remote operations. +function runRecord(overrides: Record) { + const directory = mkdtempSync(join(tmpdir(), 'comms-record-cli-')) + try { + const bin = join(directory, 'bin') + mkdirSync(bin) + const sourceSha = 'a'.repeat(40) + const metadata = { + version: '4.1.1', + sourceSha, + branch: 'main', + notes: '[Posted by Jarvis]\n\nNotes', + filename: 'todoist-comms-cli-4.1.1.tgz', + } + const release = { + id: 42, + tag_name: 'v4.1.1', + target_commitish: sourceSha, + draft: false, + prerelease: false, + published_at: '2026-11-10T00:00:00Z', + body: metadata.notes, + assets: [], + ...overrides, + } + writeFileSync(join(directory, 'release.json'), JSON.stringify(metadata)) + writeFileSync(join(directory, metadata.filename), 'original-tarball') + const executable = (name: string, code: string) => + writeFileSync(join(bin, name), `#!${process.execPath}\n${code}\n`, { mode: 0o755 }) + executable( + 'node', + `const args=process.argv.slice(2); if(args[1]==='check-registry') console.log(JSON.stringify({tags:{latest:'4.1.1'}})); else if(args[1]!=='verify') process.exit(91)`, + ) + executable( + 'git', + `const args=process.argv.slice(2); if(args.includes('show')) console.log('{"channels":[null]}'); else if(!args.includes('ls-remote')&&!args.includes('fetch')) process.exit(92)`, + ) + executable( + 'gh', + `const args=process.argv.slice(2); const route=args.find(x=>x.startsWith('repos/')); if(args.includes('POST')||args.includes('PATCH')||args.includes('DELETE')) {console.error('unexpected write'); process.exit(93)}; if(route?.includes('git/matching-refs')) console.log(JSON.stringify([{ref:'refs/tags/v4.1.1',object:{type:'commit',sha:'${sourceSha}'}}])); else if(route?.includes('/assets')) console.log('[]'); else if(route?.startsWith('repos/todoist/comms-cli/releases?')) console.log(JSON.stringify([${JSON.stringify(release)}])); else if(route==='repos/todoist/comms-cli/releases/42') console.log(JSON.stringify(${JSON.stringify(release)})); else if(route?.includes('/assets')) console.log('[]'); else process.exit(94)`, + ) + return spawnSync(process.execPath, [resolve('scripts/record-release.mjs'), directory], { + encoding: 'utf8', + env: { + ...process.env, + PATH: bin, + GITHUB_REPOSITORY: 'todoist/comms-cli', + RELEASE_TAG: 'latest', + GITHUB_OUTPUT: join(directory, 'output'), + }, + }) + } finally { + rmSync(directory, { recursive: true, force: true }) + } +} + +describe('record CLI does not accept tag name alone', () => { + it.each([ + ['wrong prerelease', { prerelease: true }, 'Release prerelease state is wrong'], + [ + 'no published timestamp', + { published_at: null }, + 'Release has no valid published timestamp', + ], + ['missing original assets', {}, 'lacks todoist-comms-cli-4.1.1.tgz'], + ])('rejects a same-tag published record with %s', (_label, change, error) => { + const result = runRecord(change as Record) + expect(result.status, result.stderr).not.toBe(0) + expect(result.stderr).toContain(error) + expect(result.stderr).not.toContain('unexpected write') + }) +}) diff --git a/scripts/registry-release.mjs b/scripts/registry-release.mjs new file mode 100644 index 0000000..eeb61f2 --- /dev/null +++ b/scripts/registry-release.mjs @@ -0,0 +1,209 @@ +import assert from 'node:assert/strict' +import { execFileSync, spawnSync } from 'node:child_process' +import { mkdtempSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { resolve } from 'node:path' +import { registryJson, validatePublicationTag } from './release-policy.mjs' + +export function publishArtifact( + { metadata, tarball, destination, command, tag }, + io = { execFileSync, spawnSync }, + environment = process.env, +) { + assert.ok(['npmjs', 'github'].includes(destination)) + assert.ok( + environment.GH_PACKAGES_TOKEN, + 'GH_PACKAGES_TOKEN with read:packages is required to preflight BOTH registries', + ) + const isolated = mkdtempSync(resolve(tmpdir(), 'comms-cli-publish-')) + try { + const registries = { + npmjs: 'https://registry.npmjs.org/', + github: 'https://npm.pkg.github.com/', + } + const options = {} + for (const [name, registry] of Object.entries(registries)) { + // Each destination has its own config and credentials; npm never receives a GitHub token. + const env = Object.fromEntries( + Object.entries(environment).filter( + ([key]) => + !/^npm_config_/i.test(key) && + ![ + 'NODE_AUTH_TOKEN', + 'NPM_TOKEN', + 'GH_TOKEN', + 'GITHUB_TOKEN', + 'GH_PACKAGES_TOKEN', + 'SOURCE_GH_TOKEN', + ].includes(key), + ), + ) + const userconfig = resolve(isolated, `${name}.npmrc`) + const globalconfig = resolve(isolated, 'global.npmrc') + const auth = + name === 'github' ? '//npm.pkg.github.com/:_authToken=${GH_PACKAGES_TOKEN}\n' : '' + if (name === 'github') env.GH_PACKAGES_TOKEN = environment.GH_PACKAGES_TOKEN + writeFileSync( + userconfig, + `registry=${registry}\n@todoist:registry=${registry}\n${auth}`, + { mode: 0o600 }, + ) + writeFileSync(globalconfig, '') + Object.assign(env, { + NPM_CONFIG_USERCONFIG: userconfig, + NPM_CONFIG_GLOBALCONFIG: globalconfig, + NPM_CONFIG_CACHE: resolve(isolated, `${name}-cache`), + }) + options[name] = { cwd: isolated, env } + for (const key of ['registry', '@todoist:registry']) { + assert.equal( + io + .execFileSync('npm', ['config', 'get', key], { + ...options[name], + encoding: 'utf8', + }) + .trim(), + registry, + ) + } + } + const spec = `${metadata.name}@${metadata.version}` + const view = (name, target, field) => + registryJson( + io.spawnSync( + 'npm', + [ + 'view', + target, + field, + '--prefer-online', + '--json', + `--registry=${registries[name]}`, + ], + { ...options[name], encoding: 'utf8' }, + ), + ) + const requireGitHubAbsenceAccess = () => { + // Package-level readability makes a version-level 404 meaningful. An ACL-filtered + // package 404 is otherwise ambiguous, even with valid registry login or list[]. + const packageName = view('github', metadata.name, 'name') + if (packageName !== undefined) { + assert.equal(packageName, metadata.name, 'Unexpected GitHub package metadata') + const registryTags = view('github', metadata.name, 'dist-tags') + assert.ok( + registryTags && + typeof registryTags === 'object' && + !Array.isArray(registryTags) && + Object.values(registryTags).every((version) => typeof version === 'string'), + 'GitHub registry root/dist-tags must be readable with the same token', + ) + return + } + const response = io.execFileSync( + 'gh', + ['api', '--hostname', 'github.com', '--include', 'user/memberships/orgs/todoist'], + { + encoding: 'utf8', + env: { ...environment, GH_TOKEN: environment.GH_PACKAGES_TOKEN }, + }, + ) + const separator = /\r?\n\r?\n/.exec(response) + assert.ok(separator, 'Cannot verify GitHub organization owner response') + const headers = response.slice(0, separator.index) + assert.match(headers, /^HTTP\/\S+ 200\b/) + const scopes = (headers.match(/^x-oauth-scopes:[ \t]*([^\r\n]*)/im)?.[1] || '') + .split(',') + .map((scope) => scope.trim()) + assert.ok( + scopes.some((scope) => ['read:packages', 'write:packages'].includes(scope)), + 'Owner absence proof requires a classic PAT with read:packages', + ) + const membership = JSON.parse(response.slice(separator.index + separator[0].length)) + assert.equal(membership.organization?.login?.toLowerCase(), 'todoist') + assert.equal( + membership.state, + 'active', + 'GitHub package is unreadable; active org-owner proof required', + ) + assert.equal( + membership.role, + 'admin', + 'GitHub package is unreadable; organization-owner proof required', + ) + } + const preflightBoth = () => { + const copies = {} + for (const name of Object.keys(registries)) { + copies[name] = view(name, spec, 'dist.integrity') + if (copies[name] !== undefined) + assert.equal( + copies[name], + metadata.integrity, + `Different same-version bytes on ${name}; no write permitted`, + ) + else if (name === 'github') requireGitHubAbsenceAccess() + } + return copies + } + const tags = (name) => { + const value = view(name, metadata.name, 'dist-tags') ?? {} + assert.ok( + value && typeof value === 'object' && !Array.isArray(value), + 'Invalid registry tags', + ) + return value + } + let copies = preflightBoth() + if (copies[destination] !== undefined) + return { status: 'already-matches', tags: tags(destination) } + assert.notEqual(command, 'check-registry', `Missing ${destination} mirror`) + if (command === 'bootstrap-npm') { + assert.notEqual( + environment.GITHUB_ACTIONS, + 'true', + 'Bootstrap needs an interactive maintainer', + ) + io.execFileSync('npm', ['login', '--auth-type=web', `--registry=${registries.npmjs}`], { + ...options.npmjs, + stdio: 'inherit', + }) + } + const previousTags = tags(destination) + validatePublicationTag(metadata.version, tag, previousTags) + // Repeat immediately before EVERY registry write, including after interactive login. + copies = preflightBoth() + if (copies[destination] !== undefined) + return { status: 'already-matches', tags: tags(destination) } + const args = [ + 'publish', + tarball, + '--ignore-scripts', + `--tag=${tag}`, + `--registry=${registries[destination]}`, + '--provenance=false', + ] + if (destination === 'npmjs') args.push('--access=public') + // GitHub visibility is independently administered; never send --access there. + // Both login and publish can prompt for browser auth/2FA. + io.execFileSync('npm', args, { ...options[destination], stdio: 'inherit' }) + copies = preflightBoth() + assert.equal( + copies[destination], + metadata.integrity, + 'Publication may have succeeded; inspect before retrying', + ) + const currentTags = tags(destination) + assert.equal(currentTags[tag], metadata.version) + for (const channel of ['latest', 'next']) { + if (tag !== channel) + assert.equal( + currentTags[channel], + previousTags[channel], + `${channel} changed during recovery`, + ) + } + return { status: 'published', tags: currentTags } + } finally { + rmSync(isolated, { recursive: true, force: true }) + } +} diff --git a/scripts/registry-release.test.ts b/scripts/registry-release.test.ts new file mode 100644 index 0000000..3745d58 --- /dev/null +++ b/scripts/registry-release.test.ts @@ -0,0 +1,234 @@ +import { readFileSync } from 'node:fs' +import { describe, expect, it } from 'vitest' +import { publishArtifact } from './registry-release.mjs' + +const metadata = { name: '@todoist/comms-cli', version: '4.1.1', integrity: 'sha512-reviewed' } +const environment = { + GH_PACKAGES_TOKEN: 'fixture-gh', + GH_TOKEN: 'fixture-api', + SOURCE_GH_TOKEN: 'fixture-read', + NODE_AUTH_TOKEN: 'fixture-old', + NPM_CONFIG_REGISTRY: 'https://wrong.invalid', +} +function fixture( + copies: Record = {}, + access: { + tagsUnreadable?: boolean + readable: boolean + role: string + state: string + scopes: string + loseAfterLogin?: boolean + ownerError?: boolean + } = { readable: true, role: 'member', state: 'active', scopes: 'read:org, read:packages' }, +) { + const events: { args: string[]; options: any }[] = [] + let ghReads = 0 + const io = { + execFileSync(bin: string, args: string[], options: any) { + events.push({ args, options }) + if (bin === 'gh') { + if (access.ownerError) throw new Error('owner proof unavailable') + expect(args).toEqual([ + 'api', + '--hostname', + 'github.com', + '--include', + 'user/memberships/orgs/todoist', + ]) + expect(options.env.GH_TOKEN).toBe('fixture-gh') + return `HTTP/2.0 200 OK\r\nx-oauth-scopes: ${access.scopes}\r\n\r\n${JSON.stringify({ role: access.role, state: access.state, organization: { login: 'todoist' } })}` + } + if (args[0] === 'config') + return readFileSync(options.env.NPM_CONFIG_USERCONFIG, 'utf8') + .split('\n') + .find((line) => line.startsWith(`${args[2]}=`)) + ?.slice(args[2].length + 1) + if (args[0] === 'login' && access.loseAfterLogin) { + access.readable = false + access.state = 'pending' + } + if (args[0] === 'publish') + copies[args.some((x) => x.includes('npm.pkg.github.com')) ? 'github' : 'npmjs'] = + metadata.integrity + return '' + }, + spawnSync(_bin: string, args: string[], options: any) { + events.push({ args, options }) + const destination = args.some((x) => x.includes('npm.pkg.github.com')) + ? 'github' + : 'npmjs' + if (args[2] === 'name') + return access.readable + ? { status: 0, stdout: JSON.stringify('@todoist/comms-cli') } + : { status: 1, stdout: '{"error":{"code":"E404"}}' } + if (args[2] === 'dist-tags' && destination === 'github' && access.tagsUnreadable) + return { status: 1, stdout: '{"error":{"code":"E404"}}' } + if (args[2] === 'dist-tags') + return { + status: 0, + stdout: JSON.stringify({ + latest: copies[destination] === metadata.integrity ? '4.1.1' : '4.1.0', + }), + } + if (destination === 'github') ghReads++ + const value = copies[destination] + if (value === 'E401') return { status: 1, stdout: '{"error":{"code":"E401"}}' } + if (value === 'late-conflict' && ghReads > 1) + return { status: 0, stdout: '"sha512-conflict"' } + return value === undefined || value === 'late-conflict' + ? { status: 1, stdout: '{"error":{"code":"E404"}}' } + : { status: 0, stdout: JSON.stringify(value) } + }, + } + const execute = (command = 'publish', destination = 'npmjs') => + publishArtifact( + { metadata, tarball: '/fixture/reviewed.tgz', destination, command, tag: 'latest' }, + io, + environment, + ) + return { execute, events } +} + +describe('same artifact dual registry publication', () => { + it.each(['publish', 'bootstrap-npm'])( + 'rejects an ACL-hidden GitHub package before %s despite a nonempty token', + (command) => { + const f = fixture( + {}, + { + readable: false, + role: 'member', + state: 'active', + scopes: 'read:org, read:packages', + }, + ) + expect(() => f.execute(command)).toThrow(/owner|readable/i) + expect(f.events.filter((e) => ['publish', 'login'].includes(e.args[0]))).toEqual([]) + }, + ) + it('does not let an already-matching selected mirror hide an unreadable opposite package', () => { + const f = fixture( + { npmjs: metadata.integrity }, + { readable: false, role: 'member', state: 'active', scopes: 'read:org, read:packages' }, + ) + expect(() => f.execute()).toThrow(/owner|readable/i) + }) + + it('permits a version 404 only when the same credential can read the package', () => { + const f = fixture({}) + expect(f.execute().status).toBe('published') + expect(f.events.some((e) => e.args[0] === 'api')).toBe(false) + }) + it('allows initial absence only with active owner and package-read proof from the same token', () => { + const f = fixture( + {}, + { readable: false, role: 'admin', state: 'active', scopes: 'read:org, read:packages' }, + ) + expect(f.execute().status).toBe('published') + expect(f.events.filter((e) => e.args[0] === 'api')).toHaveLength(3) + }) + it.each([ + { readable: false, role: 'admin', state: 'pending', scopes: 'read:org, read:packages' }, + { readable: false, role: 'admin', state: 'active', scopes: 'read:org' }, + ])('rejects insufficient owner access %j before any write', (access) => { + const f = fixture({}, access) + expect(() => f.execute()).toThrow() + expect(f.events.filter((e) => ['publish', 'login'].includes(e.args[0]))).toEqual([]) + }) + + it('rechecks read access after interactive login before publication', () => { + const f = fixture( + {}, + { + readable: true, + role: 'admin', + state: 'active', + scopes: 'read:org, read:packages', + loseAfterLogin: true, + }, + ) + expect(() => f.execute('bootstrap-npm')).toThrow(/owner/) + expect(f.events.some((e) => e.args[0] === 'login')).toBe(true) + expect(f.events.some((e) => e.args[0] === 'publish')).toBe(false) + }) + it('stops when owner proof has an auth or network failure', () => { + const f = fixture( + {}, + { + readable: false, + role: 'admin', + state: 'active', + scopes: 'read:org, read:packages', + ownerError: true, + }, + ) + expect(() => f.execute()).toThrow('owner proof unavailable') + expect(f.events.filter((e) => ['publish', 'login'].includes(e.args[0]))).toEqual([]) + }) + + it.each(['sha512-conflict', 'E401'])( + 'stops before any write when the other mirror returns %s', + (github) => { + const f = fixture({ github }) + expect(() => f.execute()).toThrow() + expect(f.events.filter((e) => ['publish', 'login'].includes(e.args[0]))).toEqual([]) + }, + ) + it('checks the other mirror even when the selected destination already matches', () => { + const f = fixture({ npmjs: metadata.integrity, github: 'sha512-conflict' }) + expect(() => f.execute()).toThrow() + }) + it('skips identical copies and only writes the missing destination', () => { + const both = fixture({ npmjs: metadata.integrity, github: metadata.integrity }) + expect(both.execute().status).toBe('already-matches') + expect(both.events.some((e) => e.args[0] === 'publish')).toBe(false) + const missing = fixture({ github: metadata.integrity }) + expect(missing.execute().status).toBe('published') + const writes = missing.events.filter((e) => e.args[0] === 'publish') + expect(writes.map((e) => e.args)).toEqual([ + [ + 'publish', + '/fixture/reviewed.tgz', + '--ignore-scripts', + '--tag=latest', + '--registry=https://registry.npmjs.org/', + '--provenance=false', + '--access=public', + ], + ]) + }) + it('preflights again after interactive login and before publish', () => { + const f = fixture({ github: 'late-conflict' }) + expect(() => f.execute('bootstrap-npm')).toThrow() + expect(f.events.some((e) => e.args[0] === 'login')).toBe(true) + expect(f.events.some((e) => e.args[0] === 'publish')).toBe(false) + }) + it('lets login and publish prompt for 2FA without exposing GitHub credentials to npm', () => { + const f = fixture() + f.execute('bootstrap-npm') + for (const event of f.events.filter((e) => ['login', 'publish'].includes(e.args[0]))) { + expect(event.options.stdio).toBe('inherit') + expect(event.options.env.GH_PACKAGES_TOKEN).toBeUndefined() + expect(event.options.env.GH_TOKEN).toBeUndefined() + expect(event.options.env.SOURCE_GH_TOKEN).toBeUndefined() + expect(event.options.env.NODE_AUTH_TOKEN).toBeUndefined() + expect(event.options.env.NPM_CONFIG_REGISTRY).toBeUndefined() + } + }) +}) + +it('rejects a name-only response when registry dist-tags are unreadable', () => { + const f = fixture( + {}, + { + readable: true, + tagsUnreadable: true, + role: 'member', + state: 'active', + scopes: 'read:packages', + }, + ) + expect(() => f.execute()).toThrow(/registry|readable/i) + expect(f.events.filter((e) => ['publish', 'login'].includes(e.args[0]))).toEqual([]) +}) diff --git a/scripts/release-analysis.mjs b/scripts/release-analysis.mjs new file mode 100644 index 0000000..bc77acd --- /dev/null +++ b/scripts/release-analysis.mjs @@ -0,0 +1,27 @@ +import assert from 'node:assert/strict' +import { analyzeCommits as analyze } from '@semantic-release/commit-analyzer' +import { generateNotes as notes } from '@semantic-release/release-notes-generator' + +export async function analyzeCommits(config, context) { + const type = await analyze({ ...config, preset: 'conventionalcommits' }, context) + // Tags remain v across the rename. Never reinterpret old tags as unpublished 1.0.0. + assert.ok(context.lastRelease.version, 'Existing v release history is required') + if (context.lastRelease.version === '4.1.0') { + assert.equal( + context.branch.name, + 'main', + 'Publish stable 4.1.1 before starting new-scope next', + ) + assert.ok( + !type || type === 'patch', + 'New feature/breaking commits require target reconciliation', + ) + return 'patch' // explicitly approved first real @todoist/comms-cli artifact + } + return type +} + +export async function generateNotes(config, context) { + const body = await notes({ ...config, preset: 'conventionalcommits' }, context) + return `[Posted by Jarvis]\n\n${body}` +} diff --git a/scripts/release-analysis.test.ts b/scripts/release-analysis.test.ts new file mode 100644 index 0000000..526903c --- /dev/null +++ b/scripts/release-analysis.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, it } from 'vitest' +import { requireRecordedPlans, requireUnallocatedVersion } from './pending-release.mjs' +import { analyzeCommits } from './release-analysis.mjs' + +const context = (version: string, message: string, branch = 'main') => ({ + lastRelease: { version }, + branch: { name: branch }, + commits: [{ hash: 'a'.repeat(40), message }], + cwd: process.cwd(), + logger: { log: () => {} }, +}) + +describe('release lineage', () => { + it('selects the agreed first patch on the existing scope-independent baseline', async () => { + await expect(analyzeCommits({}, context('4.1.0', 'chore: migrate scope'))).resolves.toBe( + 'patch', + ) + }) + it('continues normal conventional commit analysis after the first release', async () => { + await expect(analyzeCommits({}, context('4.1.1', 'fix: repair pagination'))).resolves.toBe( + 'patch', + ) + await expect(analyzeCommits({}, context('4.1.1', 'feat: add endpoint'))).resolves.toBe( + 'minor', + ) + await expect(analyzeCommits({}, context('4.1.1', 'chore: update docs'))).resolves.toBeNull() + }) + it('stops rather than resetting history or squeezing a feature into the first patch', async () => { + await expect(analyzeCommits({}, context('', 'chore: migrate scope'))).rejects.toThrow() + await expect(analyzeCommits({}, context('4.1.0', 'feat: new endpoint'))).rejects.toThrow() + await expect(analyzeCommits({}, context('4.1.0', 'fix: repair', 'next'))).rejects.toThrow() + }) + it('rejects an already-tagged prerelease even when its channel note is still missing', () => { + // Without the next channel note, semantic-release can propose this version again. + expect(() => + requireUnallocatedVersion('4.2.0-next.1', (tag: string) => tag === 'v4.2.0-next.1'), + ).toThrow(/already tagged/) + expect(() => + requireUnallocatedVersion('4.2.0-next.2', (tag: string) => tag === 'v4.2.0-next.1'), + ).not.toThrow() + }) + it('holds later builds until a pending artifact is recorded', () => { + const plans = [{ version: '4.1.1', runId: '42' }] + expect(() => requireRecordedPlans(plans, () => false)).toThrow( + /Finish that exact artifact first/, + ) + expect(() => + requireRecordedPlans( + plans, + (plan: { version: string; runId: string }) => + plan.version === '4.1.1' && plan.runId === '42', + ), + ).not.toThrow() + }) +}) diff --git a/scripts/release-policy.mjs b/scripts/release-policy.mjs new file mode 100644 index 0000000..f14b29a --- /dev/null +++ b/scripts/release-policy.mjs @@ -0,0 +1,81 @@ +import assert from 'node:assert/strict' + +export const REPOSITORY = 'todoist/comms-cli' +export const VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-next\.([1-9]\d*))?$/ +export const SHA = /^[a-f0-9]{40}$/ + +export function readSourceRun(api, environment = process.env) { + const id = environment.ARTIFACT_RUN_ID || '' + const attempt = environment.ARTIFACT_RUN_ATTEMPT || '' + assert.match(id, /^[1-9]\d*$/) + assert.match( + attempt, + /^[1-9]\d*$/, + 'Select the original successful ARTIFACT_RUN_ATTEMPT; never substitute the latest attempt', + ) + const run = api(`actions/runs/${id}/attempts/${attempt}`) + assert.equal(String(run.id), id) + assert.equal(String(run.run_attempt), attempt) + return run +} + +export async function validateSourceRun(run, workflowId, sourceSha, compare) { + assert.match(sourceSha, SHA) + assert.equal(run.repository.full_name.toLowerCase(), REPOSITORY) + assert.equal(run.head_repository.full_name.toLowerCase(), REPOSITORY) + assert.equal(run.workflow_id, workflowId) + assert.equal(run.path, '.github/workflows/release.yml') + assert.equal(run.status, 'completed') + assert.equal(run.conclusion, 'success') + assert.ok(['push', 'workflow_dispatch'].includes(run.event)) + assert.ok(['main', 'next'].includes(run.head_branch)) + // The builder checks out github.sha, with no custom checkout input. + assert.equal(run.head_sha, sourceSha) + const comparison = await compare(sourceSha, run.head_branch) + assert.ok( + ['ahead', 'identical'].includes(comparison.status), + 'Source is outside reviewed history', + ) + assert.equal(comparison.merge_base_commit.sha, sourceSha) +} + +export function validatePublicationTag(version, tag, tags) { + assert.match(version, VERSION) + assert.ok(['latest', 'next', 'migration-recovery'].includes(tag)) + if (tag === 'migration-recovery') return + assert.equal(version.includes('-next.'), tag === 'next', 'Stable/prerelease channel mismatch') + const current = tags[tag] + if (current === undefined) return + assert.match(current, VERSION, 'Unexpected registry channel; inspect before publishing') + const parts = (value) => + value + .match(VERSION) + .slice(1) + .map((part) => (part === undefined ? null : BigInt(part))) + const targetParts = parts(version) + const currentParts = parts(current) + for (let i = 0; i < 4; i++) { + if (targetParts[i] === currentParts[i]) continue + if (targetParts[i] === null) return // stable follows prereleases + assert.ok( + currentParts[i] !== null && targetParts[i] > currentParts[i], + `Refusing to downgrade ${tag}; use migration-recovery for an older missing mirror`, + ) + return + } +} + +export function registryJson(result) { + if (result.status === 0) return JSON.parse(result.stdout) + for (const stream of [result.stdout, result.stderr]) { + if (!stream) continue + const text = stream.trim() + const start = text.startsWith('{') ? 0 : text.lastIndexOf('\n{') + 1 + try { + if (JSON.parse(text.slice(start)).error?.code === 'E404') return undefined + } catch { + /* Non-JSON errors are failures, not absence. */ + } + } + throw new Error('Registry lookup failed; check authentication/network before retrying') +} diff --git a/scripts/release-policy.test.ts b/scripts/release-policy.test.ts new file mode 100644 index 0000000..3b8bdd0 --- /dev/null +++ b/scripts/release-policy.test.ts @@ -0,0 +1,131 @@ +import { describe, expect, it } from 'vitest' +import { + registryJson, + validatePublicationTag, + validateSourceRun, + readSourceRun, +} from './release-policy.mjs' + +const sha = 'a'.repeat(40) +const run = { + id: 42, + run_attempt: 1, + repository: { full_name: 'todoist/comms-cli' }, + head_repository: { full_name: 'todoist/comms-cli' }, + workflow_id: 7, + path: '.github/workflows/release.yml', + status: 'completed', + conclusion: 'success', + event: 'workflow_dispatch', + head_branch: 'main', + head_sha: sha, +} +const compare = async () => ({ status: 'identical', merge_base_commit: { sha } }) + +describe('retained release source', () => { + it('accepts a successful main build with checkout at the run SHA', async () => { + await expect(validateSourceRun(run, 7, sha, compare)).resolves.toBeUndefined() + }) + it('accepts a reviewed source when its release branch has advanced', async () => { + await expect( + validateSourceRun(run, 7, sha, async () => ({ + status: 'ahead', + merge_base_commit: { sha }, + })), + ).resolves.toBeUndefined() + }) + it('rejects an ahead response with a different merge base', async () => { + await expect( + validateSourceRun(run, 7, sha, async () => ({ + status: 'ahead', + merge_base_commit: { sha: 'b'.repeat(40) }, + })), + ).rejects.toThrow() + }) + it.each([ + { event: 'pull_request' }, + { conclusion: 'failure' }, + { head_branch: 'feature' }, + { head_sha: 'b'.repeat(40) }, + { workflow_id: 8 }, + { path: '.github/workflows/ci.yml' }, + { head_repository: { full_name: 'attacker/comms-cli' } }, + { repository: { full_name: 'attacker/comms-cli' } }, + ])('rejects untrusted source metadata %j', async (change) => { + await expect(validateSourceRun({ ...run, ...change }, 7, sha, compare)).rejects.toThrow() + }) + it('rejects a source outside reviewed release history', async () => { + await expect( + validateSourceRun(run, 7, sha, async () => ({ + status: 'diverged', + merge_base_commit: { sha }, + })), + ).rejects.toThrow() + }) +}) + +describe('registry recovery', () => { + it('allows a forward latest release and refuses to downgrade it', () => { + expect(() => validatePublicationTag('4.1.1', 'latest', { latest: '4.1.0' })).not.toThrow() + expect(() => validatePublicationTag('4.1.1', 'latest', { latest: '4.1.2' })).toThrow() + }) + it('keeps next separate from latest and guards its ordering', () => { + expect(() => + validatePublicationTag('4.2.0-next.2', 'next', { + next: '4.2.0-next.1', + latest: '4.1.1', + }), + ).not.toThrow() + expect(() => + validatePublicationTag('4.2.0-next.2', 'next', { next: '4.2.0-next.3' }), + ).toThrow() + expect(() => validatePublicationTag('4.2.0-next.2', 'latest', {})).toThrow() + expect(() => validatePublicationTag('4.1.0', 'next', {})).toThrow() + }) + it('allows explicit recovery without reusing a normal channel', () => { + expect(() => + validatePublicationTag('4.1.1', 'migration-recovery', { latest: '4.2.0' }), + ).not.toThrow() + expect(() => validatePublicationTag('4.1.1', 'internal', {})).toThrow() + }) + it('only treats a structured 404 as absence, never auth or service failure', () => { + expect(registryJson({ status: 1, stdout: '{"error":{"code":"E404"}}' })).toBeUndefined() + expect(registryJson({ status: 0, stdout: '"sha512-value"' })).toBe('sha512-value') + expect(() => registryJson({ status: 1, stderr: '{"error":{"code":"E401"}}' })).toThrow() + expect(() => registryJson({ status: 1, stderr: 'upstream unavailable' })).toThrow() + }) +}) + +describe('original source run attempt', () => { + it('reads the selected successful attempt instead of the latest rerun', () => { + const selected = readSourceRun( + (path: string) => { + expect(path).toBe('actions/runs/42/attempts/2') + return { id: 42, run_attempt: 2 } + }, + { ARTIFACT_RUN_ID: '42', ARTIFACT_RUN_ATTEMPT: '2' }, + ) + expect(selected).toEqual({ id: 42, run_attempt: 2 }) + }) + it.each([undefined, '', '0', 'latest'])( + 'rejects an unspecified original attempt %s', + (attempt) => { + expect(() => + readSourceRun( + () => { + throw new Error('must not request') + }, + { ARTIFACT_RUN_ID: '42', ARTIFACT_RUN_ATTEMPT: attempt }, + ), + ).toThrow(/original/) + }, + ) + it('rejects a response for another attempt', () => { + expect(() => + readSourceRun(() => ({ id: 42, run_attempt: 3 }), { + ARTIFACT_RUN_ID: '42', + ARTIFACT_RUN_ATTEMPT: '2', + }), + ).toThrow() + }) +}) diff --git a/scripts/release.mjs b/scripts/release.mjs new file mode 100644 index 0000000..32b026d --- /dev/null +++ b/scripts/release.mjs @@ -0,0 +1,155 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { mkdtempSync, readFileSync, writeFileSync, rmSync, cpSync, mkdirSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { resolve, dirname } from 'node:path' +import { fileURLToPath } from 'node:url' +import { compareRetainedArtifact } from './artifact-source.mjs' +import { publishArtifact } from './registry-release.mjs' +import { validateSourceRun, readSourceRun, REPOSITORY, VERSION, SHA } from './release-policy.mjs' + +const root = resolve(dirname(fileURLToPath(import.meta.url)), '..') +const [command, directory = 'release-artifact', requestedDestination] = process.argv.slice(2) +const destination = command === 'bootstrap-npm' ? 'npmjs' : requestedDestination +const output = resolve(directory) +const json = (file) => JSON.parse(readFileSync(file, 'utf8')) +const hash = (bytes, algorithm, encoding) => createHash(algorithm).update(bytes).digest(encoding) +const run = (bin, args, options = {}) => + execFileSync(bin, args, { encoding: 'utf8', ...options }).trim() + +function inspect(tarball) { + const files = run('tar', ['-tzf', tarball]).split('\n').sort() + assert.ok(files.includes('package/dist/index.js')) + assert.ok(files.includes('package/scripts/postinstall.js')) + for (const file of files) { + assert.ok( + /^package\/(?:package.json|README.md|LICENSE|CHANGELOG.md|scripts\/postinstall.js|dist\/[^\n]+)$/.test( + file, + ), + `Unexpected packed file: ${file}`, + ) + assert.ok( + !file.split('/').includes('..') && !file.includes('.test.'), + `Unsafe packed file: ${file}`, + ) + } + const manifest = JSON.parse(run('tar', ['-xOzf', tarball, 'package/package.json'])) + assert.equal(manifest.name, '@todoist/comms-cli') + assert.match(manifest.version, VERSION) + assert.equal(manifest.private, undefined) + // Routing, visibility and provenance belong to each destination, never the shared tarball. + assert.equal(manifest.publishConfig, undefined) + assert.deepEqual(manifest.scripts, { postinstall: 'node scripts/postinstall.js' }) + assert.equal(manifest.repository.url, 'https://github.com/todoist/comms-cli.git') + assert.match(manifest.dependencies['@todoist/comms-sdk'], VERSION) + assert.match(manifest.dependencies['@todoist/cli-core'], /^\d+\.\d+\.\d+$/) + assert.ok(!Object.keys(manifest.dependencies).some((name) => name.startsWith('@doist/'))) + assert.deepEqual(manifest.bin, { tdc: 'dist/index.js' }) + const bytes = readFileSync(tarball) + return { + name: manifest.name, + version: manifest.version, + sha256: hash(bytes, 'sha256', 'hex'), + integrity: `sha512-${hash(bytes, 'sha512', 'base64')}`, + } +} + +async function verify() { + const metadata = json(resolve(output, 'release.json')) + assert.match(process.env.RELEASE_SHA || '', SHA) + assert.equal(metadata.sourceSha, process.env.RELEASE_SHA) + assert.match(process.env.ARTIFACT_RUN_ID || '', /^\d+$/) + const api = (path) => JSON.parse(run('gh', ['api', `repos/${REPOSITORY}/${path}`])) + const sourceRun = readSourceRun(api) + await validateSourceRun( + sourceRun, + api('actions/workflows/release.yml').id, + process.env.RELEASE_SHA, + async (base, head) => api(`compare/${base}...${head}`), + ) + compareRetainedArtifact(output, sourceRun) + assert.equal(metadata.runId, String(sourceRun.id)) + assert.equal(metadata.runAttempt, String(sourceRun.run_attempt)) + assert.equal(metadata.branch, sourceRun.head_branch) + assert.equal(metadata.version.includes('-next.'), metadata.branch === 'next') + assert.ok(metadata.notes.startsWith('[Posted by Jarvis]\n\n')) + assert.equal(metadata.filename, `todoist-comms-cli-${metadata.version}.tgz`) + const tarball = resolve(output, metadata.filename) + const actual = inspect(tarball) + for (const key of Object.keys(actual)) assert.equal(metadata[key], actual[key]) + return { metadata, tarball } +} + +if (command === 'pack') { + assert.equal( + run('git', ['status', '--porcelain', '--untracked-files=no'], { cwd: root }), + '', + 'Commit reviewed source before packing', + ) + const sourceSha = run('git', ['rev-parse', 'HEAD'], { cwd: root }) + if (process.env.RELEASE_SHA) { + assert.match(process.env.RELEASE_SHA, /^[a-f0-9]{40}$/) + assert.equal(sourceSha, process.env.RELEASE_SHA) + } + const plan = json(resolve(output, 'plan.json')) + assert.equal(plan.sourceSha, sourceSha) + assert.match(plan.version, VERSION) + const isolated = mkdtempSync(resolve(tmpdir(), 'comms-cli-pack-')) + try { + const manifest = json(resolve(root, 'package.json')) + const lock = json(resolve(root, 'package-lock.json')) + assert.equal(lock.name, manifest.name, 'Finalize the genuine new-scope lock first') + assert.deepEqual(lock.packages[''].dependencies, manifest.dependencies) + manifest.version = plan.version + manifest.scripts = { postinstall: 'node scripts/postinstall.js' } + delete manifest.publishConfig + delete manifest.devDependencies + writeFileSync(resolve(isolated, 'package.json'), JSON.stringify(manifest, null, 4)) + mkdirSync(resolve(isolated, 'scripts')) + for (const file of [ + 'dist', + 'scripts/postinstall.js', + 'README.md', + 'LICENSE', + 'CHANGELOG.md', + ]) + cpSync(resolve(root, file), resolve(isolated, file), { recursive: true }) + if (plan.branch === 'main') + writeFileSync( + resolve(isolated, 'CHANGELOG.md'), + `${plan.notes.trim()}\n\n${readFileSync(resolve(root, 'CHANGELOG.md'), 'utf8')}`, + ) + const [packed] = JSON.parse( + run('npm', ['pack', '--ignore-scripts', '--json', '--pack-destination', output], { + cwd: isolated, + }), + ) + const metadata = { + ...plan, + ...inspect(resolve(output, packed.filename)), + filename: packed.filename, + } + writeFileSync(resolve(output, 'release.json'), `${JSON.stringify(metadata, null, 2)}\n`) + rmSync(resolve(output, 'plan.json')) + console.log(JSON.stringify(metadata, null, 2)) + } finally { + rmSync(isolated, { recursive: true, force: true }) + } +} else if (command === 'verify') { + console.log(JSON.stringify((await verify()).metadata, null, 2)) +} else if (['publish', 'bootstrap-npm', 'check-registry'].includes(command)) { + const { metadata, tarball } = await verify() + const result = publishArtifact({ + metadata, + tarball, + destination, + command, + tag: process.env.RELEASE_TAG || (metadata.branch === 'next' ? 'next' : 'latest'), + }) + console.log(JSON.stringify(result)) +} else { + throw new Error( + 'Usage: node scripts/release.mjs pack|verify DIRECTORY, publish DIRECTORY npmjs|github, or bootstrap-npm DIRECTORY', + ) +} diff --git a/scripts/release.test.ts b/scripts/release.test.ts new file mode 100644 index 0000000..5b8a44c --- /dev/null +++ b/scripts/release.test.ts @@ -0,0 +1,91 @@ +import { execFileSync, spawnSync } from 'node:child_process' +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' +import { expect, it } from 'vitest' +import { createTempRepo, REPO_ROOT } from '../.agents/scripts/lib/test-repo.ts' + +it.each(['4.0.1', '4.0.2'])( + 'packs the CLI and a subsequent exact SDK pin %s without running lifecycle scripts', + (sdkVersion) => { + const repo = createTempRepo() + try { + repo.copyFromCheckout('scripts') + const manifest = { + name: '@todoist/comms-cli', + version: '4.1.0', + type: 'module', + bin: { tdc: 'dist/index.js' }, + repository: { url: 'https://github.com/todoist/comms-cli.git' }, + dependencies: { '@todoist/comms-sdk': sdkVersion, '@todoist/cli-core': '1.10.0' }, + files: ['dist', 'scripts/postinstall.js', 'CHANGELOG.md'], + scripts: { prepare: 'exit 97', postinstall: 'node scripts/postinstall.js' }, + publishConfig: { registry: 'https://wrong.invalid', provenance: true }, + } + repo.write('package.json', JSON.stringify(manifest)) + repo.write( + 'package-lock.json', + JSON.stringify({ + name: manifest.name, + packages: { '': { dependencies: manifest.dependencies } }, + }), + ) + repo.write( + 'dist/index.js', + "import p from '../package.json' with {type:'json'}; console.log(p.name+' '+p.version)", + ) + repo.write('dist/postinstall.js', "throw Error('must not execute postinstall')") + repo.write('README.md', 'Fixture CLI') + repo.write('LICENSE', 'MIT') + repo.write('CHANGELOG.md', 'Existing history\n') + expect(repo.git(['add', '.']).status).toBe(0) + expect(repo.git(['commit', '-qm', 'fixture']).status).toBe(0) + const output = join(repo.dir, 'artifact') + mkdirSync(output) + const plan = { + version: '4.1.1', + branch: 'main', + sourceSha: repo.git(['rev-parse', 'HEAD']).stdout.trim(), + notes: '[Posted by Jarvis]\n\n## 4.1.1\n\nMigration', + runId: '42', + runAttempt: '1', + } + writeFileSync(join(output, 'plan.json'), JSON.stringify(plan)) + const result = repo.run(process.execPath, ['scripts/release.mjs', 'pack', output]) + expect(result.status, result.stderr).toBe(0) + const metadata = JSON.parse(readFileSync(join(output, 'release.json'), 'utf8')) + expect(metadata).toMatchObject({ + ...plan, + name: '@todoist/comms-cli', + filename: 'todoist-comms-cli-4.1.1.tgz', + }) + const unpacked = join(repo.dir, 'unpacked') + mkdirSync(unpacked) + execFileSync('tar', ['-xzf', join(output, metadata.filename), '-C', unpacked]) + const packed = JSON.parse(readFileSync(join(unpacked, 'package/package.json'), 'utf8')) + expect(packed.scripts).toEqual({ postinstall: 'node scripts/postinstall.js' }) + expect(packed.publishConfig).toBeUndefined() + expect(packed.bin).toEqual({ tdc: 'dist/index.js' }) + expect(readFileSync(join(unpacked, 'package/CHANGELOG.md'), 'utf8')).toBe( + `${plan.notes}\n\nExisting history\n`, + ) + expect(readFileSync(join(unpacked, 'package/scripts/postinstall.js'), 'utf8')).toBe( + readFileSync(join(REPO_ROOT, 'scripts/postinstall.js'), 'utf8'), + ) + const cli = spawnSync(process.execPath, [join(unpacked, 'package/dist/index.js')], { + encoding: 'utf8', + }) + expect(cli.status).toBe(0) + expect(cli.stdout).toBe('@todoist/comms-cli 4.1.1\n') + expect( + execFileSync('tar', ['-tzf', join(output, metadata.filename)], { + encoding: 'utf8', + }), + ).not.toContain('scripts/release.mjs') + expect(JSON.parse(readFileSync(join(repo.dir, 'package.json'), 'utf8')).version).toBe( + '4.1.0', + ) + } finally { + repo.cleanup() + } + }, +) diff --git a/scripts/source-release-record.mjs b/scripts/source-release-record.mjs new file mode 100644 index 0000000..cc1926a --- /dev/null +++ b/scripts/source-release-record.mjs @@ -0,0 +1,228 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { mkdtempSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { resolve } from 'node:path' +import { stableSourceRecordAssets } from '../release.config.js' +import { REPOSITORY, SHA, VERSION } from './release-policy.mjs' + +function identity(metadata) { + assert.match(metadata.version, VERSION) + assert.match(metadata.sourceSha, SHA) + assert.match(metadata.sha256, /^[a-f0-9]{64}$/) + assert.match(metadata.runId, /^[1-9]\d*$/) + assert.match(metadata.runAttempt, /^[1-9]\d*$/) + assert.ok(metadata.notes.startsWith('[Posted by Jarvis]\n\n')) + const marker = `Comms-Release-Version: ${metadata.version}` + const artifact = `Comms-Release-Artifact: ${metadata.sourceSha}/${metadata.runId}/${metadata.runAttempt}/sha256:${metadata.sha256}` + return { + marker, + // This commit is AFTER the immutable build tag. Copying notes into its body can turn + // a quoted BREAKING CHANGE into another release. Full notes live in CHANGELOG/GitHub. + message: `chore(release): ${metadata.version} [skip ci]\n\n${marker}\n${artifact}\n`, + } +} + +// Update only release versions; preserve current-main dependencies and all prior changelog bytes. +export function sourceRecordFiles(metadata, files) { + const manifest = JSON.parse(files['package.json']) + const lock = JSON.parse(files['package-lock.json']) + assert.equal(manifest.name, '@todoist/comms-cli') + assert.equal(lock.name, manifest.name) + assert.equal(lock.packages?.['']?.name, manifest.name) + assert.equal(lock.version, manifest.version) + assert.equal(lock.packages[''].version, manifest.version) + assert.deepEqual( + lock.packages[''].dependencies, + manifest.dependencies, + 'Current main lock must be real and synchronized', + ) + assert.deepEqual( + lock.packages[''].devDependencies, + manifest.devDependencies, + 'Current main development lock must be synchronized', + ) + assert.match(manifest.version, VERSION) + assert.ok(!manifest.version.includes('-'), 'Stable main source version must be stable') + const parts = (version) => version.split('.').map(BigInt) + const current = parts(manifest.version) + const target = parts(metadata.version) + const changedPart = current.findIndex((part, i) => part !== target[i]) + assert.ok( + changedPart === -1 || current[changedPart] < target[changedPart], + 'Never downgrade current main source version', + ) + const heading = new RegExp( + `^#{1,6}[ \\t]+(?:\\[)?${metadata.version.replaceAll('.', '\\.')}($|\\]|[ \\t(])`, + 'm', + ) + assert.ok( + !heading.test(files['CHANGELOG.md']), + 'Release changelog already exists without a verified source record; inspect original history', + ) + const skill = files['skills/comms-cli/SKILL.md'] + const frontmatterEnd = skill.indexOf('\n---', 4) + assert.ok(skill.startsWith('---\n') && frontmatterEnd > 0, 'Missing skill metadata') + const versionLine = ` version: ${JSON.stringify(manifest.version)}` + const frontmatter = skill.slice(0, frontmatterEnd) + assert.equal(frontmatter.split(versionLine).length, 2, 'Skill version must match current main') + const updatedSkill = + frontmatter.replace(versionLine, ` version: ${JSON.stringify(metadata.version)}`) + + skill.slice(frontmatterEnd) + const result = { + ...files, + 'skills/comms-cli/SKILL.md': updatedSkill, + 'CHANGELOG.md': `${metadata.notes.trim()}\n\n${files['CHANGELOG.md']}`, + } + if (manifest.version !== metadata.version) { + manifest.version = metadata.version + lock.version = metadata.version + lock.packages[''].version = metadata.version + for (const [name, value] of [ + ['package.json', manifest], + ['package-lock.json', lock], + ]) { + const indent = files[name].match(/\n([ \t]+)"/)?.[1] || ' ' + result[name] = + `${JSON.stringify(value, null, indent)}${files[name].endsWith('\n') ? '\n' : ''}` + } + } + return result +} + +export function releaseChannelNote(existing, metadata, sourceRecord) { + assert.ok(Array.isArray(existing.channels), 'Invalid existing semantic-release channel note') + const channels = [...existing.channels] + const channel = metadata.branch === 'next' ? 'next' : null + if (!channels.includes(channel)) channels.push(channel) + if (metadata.branch === 'next') { + assert.equal(sourceRecord, undefined, 'Never record source files for next') + return { ...existing, channels } + } + assert.ok(sourceRecord, 'Stable source record must complete before channel recording') + assert.equal(sourceRecord.sourceSha, metadata.sourceSha) + assert.equal(sourceRecord.version, metadata.version) + if (existing.sourceRecord !== undefined) + assert.deepEqual( + existing.sourceRecord, + sourceRecord, + 'Never replace a different source record', + ) + return { ...existing, channels, sourceRecord } +} + +// No npm command, repack, tag mutation, force push or source checkout mutation is involved. +// A normal non-force push detects races; retries re-read main and locate the exact prior record. +export function recordStableSource( + metadata, + { + verifyOnly = false, + remote = `https://github.com/${REPOSITORY}.git`, + io = { execFileSync }, + } = {}, +) { + assert.ok(['main', 'next'].includes(metadata.branch)) + if (metadata.branch === 'next') { + assert.ok(metadata.version.includes('-next.')) + return undefined + } + assert.ok(!metadata.version.includes('-')) + const { marker, message } = identity(metadata) + const directory = mkdtempSync(resolve(tmpdir(), 'comms-cli-source-record-')) + const auth = [ + '-c', + 'http.https://github.com/.extraheader=', + '-c', + 'credential.helper=', + '-c', + 'credential.helper=!gh auth git-credential', + ] + const git = (args, options = {}) => + io.execFileSync('git', [...auth, ...args], { cwd: directory, encoding: 'utf8', ...options }) + const read = (ref) => + Object.fromEntries( + stableSourceRecordAssets.map((name) => [name, git(['show', `${ref}:${name}`])]), + ) + try { + git(['init', '--quiet']) + git(['fetch', '--quiet', '--no-tags', remote, 'refs/heads/main']) + const tip = git(['rev-parse', 'FETCH_HEAD']).trim() + git(['merge-base', '--is-ancestor', metadata.sourceSha, tip]) + const matches = git(['log', '--format=%H', '--fixed-strings', `--grep=${marker}`, tip]) + .trim() + .split('\n') + .filter(Boolean) + .filter((sha) => git(['show', '-s', '--format=%B', sha]).split('\n').includes(marker)) + assert.ok( + matches.length <= 1, + 'Multiple source release records; stop for maintainer inspection', + ) + const verify = (sha) => { + assert.match(sha, SHA) + const parents = git(['rev-list', '--parents', '-n', '1', sha]).trim().split(' ') + assert.equal(parents.length, 2, 'Source record must have exactly one parent') + git(['merge-base', '--is-ancestor', metadata.sourceSha, parents[1]]) + assert.equal( + git(['show', '-s', '--format=%B', sha]).trimEnd(), + message.trimEnd(), + 'Source record belongs to different original bytes; never replace it', + ) + const before = read(parents[1]) + const expected = sourceRecordFiles(metadata, before) + assert.deepEqual( + read(sha), + expected, + 'Source record content differs from original release plan', + ) + const paths = git(['diff-tree', '--no-commit-id', '--name-only', '-r', sha]) + .trim() + .split('\n') + .filter(Boolean) + .sort() + assert.deepEqual( + paths, + stableSourceRecordAssets.filter((name) => before[name] !== expected[name]).sort(), + 'Source record changed unrelated files', + ) + return { + sourceSha: metadata.sourceSha, + recordSha: sha, + version: metadata.version, + runId: metadata.runId, + runAttempt: metadata.runAttempt, + sha256: metadata.sha256, + } + } + if (matches.length) return verify(matches[0]) + assert.equal( + verifyOnly, + false, + `Stable ${metadata.version} source record is incomplete; retry the original publisher before building again`, + ) + const files = sourceRecordFiles(metadata, read(tip)) + git(['checkout', '--quiet', '--detach', tip]) + for (const name of stableSourceRecordAssets) + writeFileSync(resolve(directory, name), files[name]) + git(['add', '--', ...stableSourceRecordAssets]) + const messagePath = resolve(directory, '.git', 'release-message') + writeFileSync(messagePath, message) + git([ + '-c', + 'user.name=github-actions[bot]', + '-c', + 'user.email=41898282+github-actions[bot]@users.noreply.github.com', + 'commit', + '--quiet', + '--file', + messagePath, + ]) + const recordSha = git(['rev-parse', 'HEAD']).trim() + const record = verify(recordSha) + git(['push', remote, 'HEAD:refs/heads/main']) + git(['fetch', '--quiet', '--no-tags', remote, 'refs/heads/main']) + git(['merge-base', '--is-ancestor', recordSha, 'FETCH_HEAD']) + return record + } finally { + rmSync(directory, { recursive: true, force: true }) + } +} diff --git a/scripts/source-release-record.test.ts b/scripts/source-release-record.test.ts new file mode 100644 index 0000000..632b2c2 --- /dev/null +++ b/scripts/source-release-record.test.ts @@ -0,0 +1,487 @@ +import assert from 'node:assert/strict' +import { execFileSync, spawnSync } from 'node:child_process' +import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { describe, expect, it } from 'vitest' +import { isolatedEnv } from '../.agents/scripts/lib/test-repo.ts' +import { analyzeCommits, generateNotes } from './release-analysis.mjs' +import { recordStableSource, releaseChannelNote } from './source-release-record.mjs' + +// A disposable bare filesystem remote is the Git boundary; never the project remote/main. +function fixture(version = '4.1.0') { + const directory = mkdtempSync(join(tmpdir(), 'source-record-fixture-')) + const remote = join(directory, 'remote.git') + const work = join(directory, 'work') + mkdirSync(work) + mkdirSync(join(work, 'skills/comms-cli'), { recursive: true }) + const git = (args: string[], cwd = work) => + execFileSync('git', ['-c', 'commit.gpgSign=false', ...args], { + cwd, + env: isolatedEnv(), + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'pipe'], + }).trim() + git(['init', '--quiet', '--bare', remote]) + git(['init', '--quiet']) + git(['checkout', '-b', 'main']) + git(['config', 'user.name', 'Fixture']) + git(['config', 'user.email', 'fixture@example.com']) + const manifest = { + name: '@todoist/comms-cli', + version, + dependencies: { '@todoist/comms-sdk': '4.0.1' }, + userField: 'preserve me', + } + const lock = { + name: manifest.name, + version: manifest.version, + lockfileVersion: 3, + packages: { + '': { ...manifest }, + 'node_modules/example': { version: '1.0.0', integrity: 'fixture-original' }, + }, + } + const history = '## Existing user history\n\nDo not rewrite these bytes. \n' + for (const [name, data] of Object.entries({ + 'package.json': `${JSON.stringify(manifest, null, 4)}\n`, + 'package-lock.json': `${JSON.stringify(lock, null, 4)}\n`, + 'CHANGELOG.md': history, + 'skills/comms-cli/SKILL.md': `---\nname: comms-cli\nmetadata:\n version: ${JSON.stringify(version)}\n---\n# Keep current commands\n`, + 'user.txt': 'original', + })) + writeFileSync(join(work, name), data) + git(['add', '.']) + git(['commit', '--quiet', '-m', 'Original build source']) + git(['push', remote, 'HEAD:refs/heads/main']) + const sourceSha = git(['rev-parse', 'HEAD']) + const metadata = { + version: '4.1.1', + branch: 'main', + sourceSha, + sha256: 'a'.repeat(64), + runId: '42', + runAttempt: '2', + notes: '[Posted by Jarvis]\n\n## 4.1.1\n\n- Original release notes', + } + // A test-only tag proves commitback does not move the original source tag. + git(['update-ref', 'refs/tags/v4.1.1', sourceSha]) + git(['push', remote, 'refs/tags/v4.1.1']) + const events: string[][] = [] + const io = { + execFileSync(bin: string, args: string[], options: any) { + assert.equal(bin, 'git') + events.push(args) + return execFileSync(bin, ['-c', 'commit.gpgSign=false', ...args], { + ...options, + env: isolatedEnv(), + stdio: ['pipe', 'pipe', 'pipe'], + }) + }, + } + const record = (opts = {}) => recordStableSource(metadata, { remote, io, ...opts }) + const main = () => git(['rev-parse', 'refs/heads/main'], remote) + const file = (ref: string, name: string) => + execFileSync('git', ['show', `${ref}:${name}`], { + cwd: remote, + encoding: 'utf8', + env: isolatedEnv(), + }) + const userCommit = () => { + writeFileSync(join(work, 'user.txt'), 'later user edit') + git(['add', 'user.txt']) + git(['commit', '--quiet', '-m', 'Later user history']) + git(['push', remote, 'HEAD:refs/heads/main']) + return git(['rev-parse', 'HEAD']) + } + return { + directory, + remote, + work, + git, + metadata, + history, + events, + io, + record, + main, + file, + userCommit, + cleanup: () => rmSync(directory, { recursive: true, force: true }), + } +} + +describe('stable source release record', () => { + it('does not re-release a generated record containing real breaking-change release notes', async () => { + const f = fixture() + try { + const context = { + cwd: f.work, + logger: { log: () => {} }, + options: { repositoryUrl: 'https://github.com/todoist/comms-cli' }, + branch: { name: 'main' }, + lastRelease: { version: '4.1.0', gitTag: 'v4.1.0' }, + nextRelease: { version: '5.0.0', gitTag: 'v5.0.0' }, + commits: [ + { + hash: f.metadata.sourceSha, + message: + 'feat!: change format\n\nBREAKING CHANGE: BREAKING CHANGE: the response format is new', + }, + ], + } + f.metadata.version = '5.0.0' + f.metadata.notes = await generateNotes({}, context) + expect(f.metadata.notes).toContain('* BREAKING CHANGE: the response format is new') + const record = f.record() + assert.ok(record) + const message = f.git(['show', '-s', '--format=%B', record.recordSha], f.remote) + expect(f.file(record.recordSha, 'CHANGELOG.md')).toContain(f.metadata.notes.trim()) + await expect( + analyzeCommits( + {}, + { + ...context, + lastRelease: { version: '5.0.0' }, + commits: [{ hash: record.recordSha, message }], + }, + ), + ).resolves.toBeNull() + } finally { + f.cleanup() + } + }) + + it('handles the first migration release whose real source versions already equal the artifact', () => { + const f = fixture('4.1.1') + try { + const beforeManifest = f.file(f.main(), 'package.json') + const beforeLock = f.file(f.main(), 'package-lock.json') + const result = f.record() + assert.ok(result) + expect(f.file(result.recordSha, 'package.json')).toBe(beforeManifest) + expect(f.file(result.recordSha, 'package-lock.json')).toBe(beforeLock) + expect( + f.git( + ['diff-tree', '--no-commit-id', '--name-only', '-r', result.recordSha], + f.remote, + ), + ).toBe('CHANGELOG.md') + } finally { + f.cleanup() + } + }) + + it('records changelog and root versions after the original build while preserving user history and tag', () => { + const f = fixture() + try { + const parent = f.userCommit() + const result = f.record() + assert.ok(result) + expect(result.sourceSha).toBe(f.metadata.sourceSha) + expect(result.recordSha).toBe(f.main()) + expect(result.recordSha).not.toBe(f.metadata.sourceSha) + expect(f.git(['rev-parse', `${result.recordSha}^`], f.remote)).toBe(parent) + expect(f.file(result.recordSha, 'CHANGELOG.md')).toBe( + `${f.metadata.notes}\n\n${f.history}`, + ) + expect(JSON.parse(f.file(result.recordSha, 'package.json'))).toEqual({ + name: '@todoist/comms-cli', + version: '4.1.1', + dependencies: { '@todoist/comms-sdk': '4.0.1' }, + userField: 'preserve me', + }) + const lock = JSON.parse(f.file(result.recordSha, 'package-lock.json')) + expect(lock.version).toBe('4.1.1') + expect(lock.packages[''].version).toBe('4.1.1') + expect(lock.packages['node_modules/example']).toEqual({ + version: '1.0.0', + integrity: 'fixture-original', + }) + expect(f.file(result.recordSha, 'skills/comms-cli/SKILL.md')).toBe( + '---\nname: comms-cli\nmetadata:\n version: "4.1.1"\n---\n# Keep current commands\n', + ) + expect(f.file(result.recordSha, 'user.txt')).toBe('later user edit') + expect(f.git(['rev-parse', 'refs/tags/v4.1.1'], f.remote)).toBe(f.metadata.sourceSha) + expect(f.git(['show', '-s', '--format=%B', result.recordSha], f.remote)).toContain( + 'chore(release): 4.1.1 [skip ci]', + ) + expect( + f.events.some( + (args) => args.includes('--force') || args.includes('--force-with-lease'), + ), + ).toBe(false) + } finally { + f.cleanup() + } + }) + it('reuses a completed record without adding another changelog entry, commit or push', () => { + const f = fixture() + try { + const first = f.record() + assert.ok(first) + f.events.length = 0 + expect(f.record()).toEqual(first) + expect(f.events.some((args) => args.includes('commit') || args.includes('push'))).toBe( + false, + ) + expect(f.file(f.main(), 'CHANGELOG.md')).toBe(`${f.metadata.notes}\n\n${f.history}`) + } finally { + f.cleanup() + } + }) + it('recovers a lost main-push response without creating another source record', () => { + const f = fixture() + try { + let lost = false + const io = { + execFileSync(bin: string, args: string[], options: any) { + const result = f.io.execFileSync(bin, args, options) + if (args.includes('push') && !lost) { + lost = true + throw new Error('lost main push response') + } + return result + }, + } + expect(() => f.record({ io })).toThrow('lost main push response') + const accepted = f.main() + f.events.length = 0 + expect(f.record()?.recordSha).toBe(accepted) + expect(f.events.some((args) => args.includes('push'))).toBe(false) + } finally { + f.cleanup() + } + }) + it('stops on a concurrent main advance and retries on top of the new user commit without force', () => { + const f = fixture() + try { + let raced = false + let userSha = '' + const io = { + execFileSync(bin: string, args: string[], options: any) { + if (args.includes('push') && !raced) { + raced = true + userSha = f.userCommit() + } + return f.io.execFileSync(bin, args, options) + }, + } + expect(() => f.record({ io })).toThrow() + expect(f.main()).toBe(userSha) + const result = f.record() + assert.ok(result) + expect(f.git(['rev-parse', `${result.recordSha}^`], f.remote)).toBe(userSha) + expect(f.file(result.recordSha, 'skills/comms-cli/SKILL.md')).toBe( + '---\nname: comms-cli\nmetadata:\n version: "4.1.1"\n---\n# Keep current commands\n', + ) + expect(f.file(result.recordSha, 'user.txt')).toBe('later user edit') + } finally { + f.cleanup() + } + }) + it('keeps next completely excluded from changelog/version commitback', () => { + const f = fixture() + try { + f.metadata.branch = 'next' + f.metadata.version = '4.2.0-next.1' + expect(f.record()).toBeUndefined() + expect(f.events).toEqual([]) + expect(f.main()).toBe(f.metadata.sourceSha) + expect(f.file(f.main(), 'CHANGELOG.md')).toBe(f.history) + } finally { + f.cleanup() + } + }) + it('blocks a new baseline until the stable source record is complete', () => { + const f = fixture() + try { + expect(() => f.record({ verifyOnly: true })).toThrow(/source record is incomplete/) + expect(f.events.some((args) => args.includes('commit') || args.includes('push'))).toBe( + false, + ) + const result = f.record() + expect(f.record({ verifyOnly: true })).toEqual(result) + } finally { + f.cleanup() + } + }) + it('rejects a retry with a different original artifact identity', () => { + const f = fixture() + try { + f.record() + const accepted = f.main() + f.metadata.sha256 = 'b'.repeat(64) + expect(f.record).toThrow(/different original bytes/) + expect(f.main()).toBe(accepted) + } finally { + f.cleanup() + } + }) +}) + +describe('semantic release channels and separate source identity', () => { + const metadata = { branch: 'main', version: '4.1.1', sourceSha: 'a'.repeat(40) } + const record = { version: '4.1.1', sourceSha: metadata.sourceSha, recordSha: 'b'.repeat(40) } + it('preserves existing note fields/channels and records the generated commit separately', () => { + const existing = { channels: ['next'], userHistory: { preserve: true } } + const expected = { ...existing, channels: ['next', null], sourceRecord: record } + expect(releaseChannelNote(existing, metadata, record)).toEqual(expected) + expect(releaseChannelNote(expected, metadata, record)).toEqual(expected) + expect(existing).toEqual({ channels: ['next'], userHistory: { preserve: true } }) + }) + it('records next channel without creating a source record', () => { + expect( + releaseChannelNote( + { channels: [], custom: 1 }, + { ...metadata, branch: 'next' }, + undefined, + ), + ).toEqual({ channels: ['next'], custom: 1 }) + }) + it('refuses incomplete or conflicting stable source records before channel updates', () => { + expect(() => releaseChannelNote({ channels: [] }, metadata, undefined)).toThrow( + /must complete/, + ) + expect(() => + releaseChannelNote( + { channels: [null], sourceRecord: { ...record, recordSha: 'c'.repeat(40) } }, + metadata, + record, + ), + ).toThrow(/Never replace/) + expect(() => releaseChannelNote({ channels: 'invalid' }, metadata, record)).toThrow( + /Invalid/, + ) + }) +}) + +describe('actual recorder ordering with external GitHub and registry fixtures', () => { + it.each(['main', 'next'])( + 'finishes %s with the original artifact identity and idempotent source/channel recording', + (branch) => { + const f = fixture() + try { + const version = branch === 'main' ? '4.1.1' : '4.2.0-next.1' + const metadata = { + ...f.metadata, + branch, + version, + filename: `todoist-comms-cli-${version}.tgz`, + } + if (branch === 'next') { + f.git(['update-ref', `refs/tags/v${version}`, metadata.sourceSha]) + f.git(['push', f.remote, `refs/tags/v${version}`]) + } + const artifact = join(f.directory, 'artifact') + mkdirSync(artifact) + const manifest = JSON.stringify(metadata) + writeFileSync(join(artifact, 'release.json'), manifest) + writeFileSync(join(artifact, metadata.filename), 'original-tarball') + const release = { + id: 42, + tag_name: `v${version}`, + target_commitish: metadata.sourceSha, + draft: false, + prerelease: branch === 'next', + published_at: '2026-11-10T00:00:00Z', + body: metadata.notes, + } + const assets = [ + { + id: 100, + name: metadata.filename, + state: 'uploaded', + size: Buffer.byteLength('original-tarball'), + }, + { + id: 101, + name: 'release.json', + state: 'uploaded', + size: Buffer.byteLength(manifest), + }, + ] + const bin = join(f.directory, 'bin') + mkdirSync(bin) + const events = join(f.directory, 'events') + const realGit = execFileSync('which', ['git'], { encoding: 'utf8' }).trim() + const executable = (name: string, body: string) => + writeFileSync(join(bin, name), `#!${process.execPath}\n${body}\n`, { + mode: 0o755, + }) + const log = `const fs=require('node:fs'); const args=process.argv.slice(2); fs.appendFileSync(${JSON.stringify(events)},JSON.stringify({bin:require('node:path').basename(process.argv[1]),args})+'\\n');` + executable( + 'node', + log + + `if(args[1]==='check-registry') console.log(JSON.stringify({tags:{${branch === 'main' ? 'latest' : 'next'}:${JSON.stringify(version)}}})); else if(args[1]!=='verify') process.exit(91);`, + ) + executable( + 'git', + log + + `const r=require('node:child_process').spawnSync(${JSON.stringify(realGit)},['-c','commit.gpgSign=false',...args.map(x=>x==='https://github.com/todoist/comms-cli.git'?${JSON.stringify(f.remote)}:x)]); if(r.stdout)process.stdout.write(r.stdout);if(r.stderr)process.stderr.write(r.stderr);process.exit(r.status??1);`, + ) + executable( + 'gh', + log + + `const route=args.find(x=>x.startsWith('repos/')); if(args.includes('POST')||args.includes('PATCH')||args.includes('DELETE'))throw Error('unexpected GitHub write'); if(route?.includes('git/matching-refs')) console.log(JSON.stringify([{ref:'refs/tags/v${version}',object:{type:'commit',sha:'${metadata.sourceSha}'}}])); else if(route?.includes('/assets/100'))process.stdout.write('original-tarball');else if(route?.includes('/assets/101'))process.stdout.write(${JSON.stringify(manifest)});else if(route?.includes('/42/assets?'))console.log(JSON.stringify(${JSON.stringify(assets)}));else if(route?.includes('/releases?'))console.log(JSON.stringify([${JSON.stringify(release)}]));else if(route?.endsWith('/releases/42'))console.log(JSON.stringify(${JSON.stringify(release)}));else throw Error('unexpected route '+route);`, + ) + const output = join(f.directory, 'output') + const execute = () => + spawnSync(process.execPath, [resolve('scripts/record-release.mjs'), artifact], { + cwd: f.work, + encoding: 'utf8', + env: { + ...isolatedEnv(), + PATH: `${bin}:${process.env.PATH}`, + GITHUB_REPOSITORY: 'todoist/comms-cli', + GITHUB_OUTPUT: output, + }, + }) + const result = execute() + expect(result.status, result.stderr).toBe(0) + const accepted = f.main() + if (branch === 'main') { + expect(accepted).not.toBe(metadata.sourceSha) + expect(JSON.parse(f.file(accepted, 'package.json')).version).toBe('4.1.1') + expect(f.file(accepted, 'CHANGELOG.md')).toBe( + `${metadata.notes}\n\n${f.history}`, + ) + } else { + expect(accepted).toBe(metadata.sourceSha) + expect(f.file(accepted, 'CHANGELOG.md')).toBe(f.history) + } + expect(f.git(['rev-parse', `refs/tags/v${version}`], f.remote)).toBe( + metadata.sourceSha, + ) + const note = JSON.parse( + f.git(['notes', '--ref', 'refs/notes/semantic-release', 'show', `v${version}`]), + ) + expect(note.channels).toEqual([branch === 'main' ? null : 'next']) + if (branch === 'main') + expect(note.sourceRecord).toMatchObject({ + sourceSha: metadata.sourceSha, + recordSha: accepted, + }) + else expect(note.sourceRecord).toBeUndefined() + const commands = readFileSync(events, 'utf8') + .trim() + .split('\n') + .map((line) => JSON.parse(line)) + const firstGit = commands.findIndex((event) => event.bin === 'git') + const checks = commands + .slice(0, firstGit) + .filter((event) => event.bin === 'node' && event.args[1] === 'check-registry') + expect(checks.map((event) => event.args[3])).toEqual(['npmjs', 'github']) + expect(commands[firstGit - 1]).toMatchObject({ bin: 'gh' }) + expect(execute().status).toBe(0) + expect(f.main()).toBe(accepted) + expect(readFileSync(join(artifact, 'release.json'), 'utf8')).toBe(manifest) + expect(readFileSync(join(artifact, metadata.filename), 'utf8')).toBe( + 'original-tarball', + ) + } finally { + f.cleanup() + } + }, + ) +}) diff --git a/scripts/validate-source-run.mjs b/scripts/validate-source-run.mjs new file mode 100644 index 0000000..e8371bc --- /dev/null +++ b/scripts/validate-source-run.mjs @@ -0,0 +1,33 @@ +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { appendFileSync } from 'node:fs' +import { githubPages } from './github-pages.mjs' +import { REPOSITORY, validateSourceRun, readSourceRun } from './release-policy.mjs' + +assert.equal(process.env.GITHUB_REPOSITORY?.toLowerCase(), REPOSITORY) +assert.match(process.env.ARTIFACT_RUN_ID || '', /^\d+$/) +const api = (path) => + JSON.parse(execFileSync('gh', ['api', `repos/${REPOSITORY}/${path}`], { encoding: 'utf8' })) +const run = readSourceRun(api) +const workflow = api('actions/workflows/release.yml') +await validateSourceRun(run, workflow.id, process.env.RELEASE_SHA, async (base, head) => + api(`compare/${base}...${head}`), +) +const artifact = `release-${run.id}-${run.run_attempt}` +const artifacts = [...githubPages(`actions/runs/${run.id}/artifacts`, 'artifacts', api)].filter( + (item) => item.name === artifact && !item.expired, +) +if (process.env.GITHUB_EVENT_NAME === 'workflow_dispatch') + assert.equal( + artifacts.length, + 1, + 'Expected one retained artifact; never rebuild a published version', + ) +else assert.ok(artifacts.length <= 1) +appendFileSync(process.env.GITHUB_OUTPUT, `available=${artifacts.length === 1}\n`) + +appendFileSync( + process.env.GITHUB_OUTPUT, + `artifact=release-${run.id}-${run.run_attempt}\nbranch=${run.head_branch}\n`, +) +console.log(`Validated successful ${run.event} build ${run.id}, checkout ${run.head_sha}`) diff --git a/skills/comms-cli/SKILL.md b/skills/comms-cli/SKILL.md index 6091e53..cff66c4 100644 --- a/skills/comms-cli/SKILL.md +++ b/skills/comms-cli/SKILL.md @@ -13,6 +13,8 @@ Access Comms messaging via the `tdc` CLI. Use when the user asks about their Com ## Setup +Install with `npm install -g @todoist/comms-cli`. The executable remains `tdc`; `tdc update` follows the configured stable or next channel. + ```bash tdc auth login # OAuth login (standard write scopes) tdc auth login --read-only # OAuth login with read-only scope diff --git a/src/commands/account/account.test.ts b/src/commands/account/account.test.ts index 34165d4..969d7a1 100644 --- a/src/commands/account/account.test.ts +++ b/src/commands/account/account.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const storeMocks = vi.hoisted(() => ({ diff --git a/src/commands/account/index.ts b/src/commands/account/index.ts index 5ba6f08..d55e03f 100644 --- a/src/commands/account/index.ts +++ b/src/commands/account/index.ts @@ -1,10 +1,10 @@ -import { emitView } from '@doist/cli-core' +import { emitView } from '@todoist/cli-core' import { attachAccountCurrentCommand, attachAccountListCommand, attachAccountRemoveCommand, attachAccountUseCommand, -} from '@doist/cli-core/auth' +} from '@todoist/cli-core/auth' import chalk from 'chalk' import { Command } from 'commander' import { diff --git a/src/commands/auth/auth.test.ts b/src/commands/auth/auth.test.ts index ef84ea8..5b5a409 100644 --- a/src/commands/auth/auth.test.ts +++ b/src/commands/auth/auth.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { Command } from 'commander' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -74,8 +74,8 @@ vi.mock('../../lib/config.js', async (importOriginal) => { // src/lib/auth-provider.test.ts. attachLogoutCommand + attachStatusCommand // fall through to the real cli-core implementations so the integration is // exercised end-to-end. -vi.mock('@doist/cli-core/auth', async (importOriginal) => { - const actual = await importOriginal() +vi.mock('@todoist/cli-core/auth', async (importOriginal) => { + const actual = await importOriginal() return { ...actual, attachLoginCommand: vi.fn((parent, _options) => { @@ -104,8 +104,8 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { createInterface, type Interface } from 'node:readline' -import { attachLoginCommand, type AuthProvider } from '@doist/cli-core/auth' -import { CommsRequestError, type User } from '@doist/comms-sdk' +import { attachLoginCommand, type AuthProvider } from '@todoist/cli-core/auth' +import { CommsRequestError, type User } from '@todoist/comms-sdk' import { createWrappedCommsClient } from '../../lib/api.js' import { type CommsAccount, type CommsTokenStore } from '../../lib/auth-provider.js' import { getTokenRefreshOptions, TOKEN_ENV_VAR } from '../../lib/auth.js' diff --git a/src/commands/auth/helpers.ts b/src/commands/auth/helpers.ts index 5c8a3ad..ea6bfd6 100644 --- a/src/commands/auth/helpers.ts +++ b/src/commands/auth/helpers.ts @@ -1,4 +1,4 @@ -import type { TokenStorageResult } from '@doist/cli-core/auth' +import type { TokenStorageResult } from '@todoist/cli-core/auth' import chalk from 'chalk' import { createWrappedCommsClient } from '../../lib/api.js' import type { CommsAccount, CommsTokenStore } from '../../lib/auth-provider.js' diff --git a/src/commands/auth/index.ts b/src/commands/auth/index.ts index fb249f2..dd5d752 100644 --- a/src/commands/auth/index.ts +++ b/src/commands/auth/index.ts @@ -1,4 +1,4 @@ -import { attachRefreshTokenViewCommand, attachTokenViewCommand } from '@doist/cli-core/auth' +import { attachRefreshTokenViewCommand, attachTokenViewCommand } from '@todoist/cli-core/auth' import { Command, Option } from 'commander' import { CREDENTIAL_STORES, diff --git a/src/commands/auth/login.ts b/src/commands/auth/login.ts index f80d47d..ee63ac2 100644 --- a/src/commands/auth/login.ts +++ b/src/commands/auth/login.ts @@ -1,4 +1,4 @@ -import { attachLoginCommand } from '@doist/cli-core/auth' +import { attachLoginCommand } from '@todoist/cli-core/auth' import chalk from 'chalk' import { type Command, Option } from 'commander' import { renderError, renderSuccess } from '../../lib/auth-pages.js' diff --git a/src/commands/auth/logout.ts b/src/commands/auth/logout.ts index 5c72edb..19224e4 100644 --- a/src/commands/auth/logout.ts +++ b/src/commands/auth/logout.ts @@ -1,4 +1,4 @@ -import { attachLogoutCommand } from '@doist/cli-core/auth' +import { attachLogoutCommand } from '@todoist/cli-core/auth' import type { Command } from 'commander' import type { CommsAccount, CommsTokenStore } from '../../lib/auth-provider.js' import { logStoredTokenRemoval } from './helpers.js' diff --git a/src/commands/auth/status.ts b/src/commands/auth/status.ts index ebc2f1a..b825ece 100644 --- a/src/commands/auth/status.ts +++ b/src/commands/auth/status.ts @@ -1,5 +1,5 @@ -import { attachStatusCommand } from '@doist/cli-core/auth' -import { CommsRequestError, type User } from '@doist/comms-sdk' +import { attachStatusCommand } from '@todoist/cli-core/auth' +import { CommsRequestError, type User } from '@todoist/comms-sdk' import chalk from 'chalk' import type { Command } from 'commander' import { createWrappedCommsClient } from '../../lib/api.js' diff --git a/src/commands/auth/store-wrap.ts b/src/commands/auth/store-wrap.ts index 4c8bf36..3b00c7b 100644 --- a/src/commands/auth/store-wrap.ts +++ b/src/commands/auth/store-wrap.ts @@ -1,4 +1,4 @@ -import type { AccountRef } from '@doist/cli-core/auth' +import type { AccountRef } from '@todoist/cli-core/auth' import { findAccountInStore, type CommsTokenStore } from '../../lib/auth-provider.js' // Bridge the global `tdc --user ` (stripped by `src/index.ts`) into diff --git a/src/commands/changelog.test.ts b/src/commands/changelog.test.ts index 76bd8f4..bc8b396 100644 --- a/src/commands/changelog.test.ts +++ b/src/commands/changelog.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('node:fs/promises') @@ -63,7 +63,7 @@ describe('changelog wrapper', () => { const all = logSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n') expect(all).toContain( - `View full changelog: https://github.com/Doist/comms-cli/blob/v${packageJson.version}/CHANGELOG.md`, + `View full changelog: https://github.com/todoist/comms-cli/blob/v${packageJson.version}/CHANGELOG.md`, ) }) diff --git a/src/commands/changelog.ts b/src/commands/changelog.ts index a9e3a89..50cb083 100644 --- a/src/commands/changelog.ts +++ b/src/commands/changelog.ts @@ -1,6 +1,6 @@ import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' -import { registerChangelogCommand as registerCoreChangelogCommand } from '@doist/cli-core/commands' +import { registerChangelogCommand as registerCoreChangelogCommand } from '@todoist/cli-core/commands' import type { Command } from 'commander' import packageJson from '../../package.json' with { type: 'json' } @@ -9,7 +9,7 @@ const CHANGELOG_PATH = join(dirname(fileURLToPath(import.meta.url)), '..', '..', export function registerChangelogCommand(program: Command): void { registerCoreChangelogCommand(program, { path: CHANGELOG_PATH, - repoUrl: 'https://github.com/Doist/comms-cli', + repoUrl: 'https://github.com/todoist/comms-cli', version: packageJson.version, headingLevel: 'flexible', continuationIndent: true, diff --git a/src/commands/channel/channel.test.ts b/src/commands/channel/channel.test.ts index 5a277ea..aa64fc1 100644 --- a/src/commands/channel/channel.test.ts +++ b/src/commands/channel/channel.test.ts @@ -2,7 +2,7 @@ import { captureConsole, createTestProgram, describeEmptyMachineOutput, -} from '@doist/cli-core/testing' +} from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/channel/create.ts b/src/commands/channel/create.ts index 6158cb2..4a36daa 100644 --- a/src/commands/channel/create.ts +++ b/src/commands/channel/create.ts @@ -1,4 +1,4 @@ -import type { CreateChannelArgs } from '@doist/comms-sdk' +import type { CreateChannelArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' import { formatJson, printDryRun } from '../../lib/output.js' diff --git a/src/commands/channel/helpers.ts b/src/commands/channel/helpers.ts index 6d1cd72..f92a360 100644 --- a/src/commands/channel/helpers.ts +++ b/src/commands/channel/helpers.ts @@ -1,4 +1,4 @@ -import type { Channel } from '@doist/comms-sdk' +import type { Channel } from '@todoist/comms-sdk' import { getCommsClient, getCurrentWorkspaceId } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { diff --git a/src/commands/channel/list.ts b/src/commands/channel/list.ts index 7d4e112..66427b0 100644 --- a/src/commands/channel/list.ts +++ b/src/commands/channel/list.ts @@ -1,5 +1,5 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' -import type { Channel } from '@doist/comms-sdk' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' +import type { Channel } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { includePrivateChannels } from '../../lib/global-args.js' diff --git a/src/commands/channel/members.test.ts b/src/commands/channel/members.test.ts index 8a22606..c656bb9 100644 --- a/src/commands/channel/members.test.ts +++ b/src/commands/channel/members.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/channel/members.ts b/src/commands/channel/members.ts index cd60b85..aee1f18 100644 --- a/src/commands/channel/members.ts +++ b/src/commands/channel/members.ts @@ -1,5 +1,5 @@ -import { outputIds, resolveOutputMode } from '@doist/cli-core' -import { isRestrictedWorkspaceUser } from '@doist/comms-sdk' +import { outputIds, resolveOutputMode } from '@todoist/cli-core' +import { isRestrictedWorkspaceUser } from '@todoist/comms-sdk' import { getCurrentWorkspaceId, getWorkspaceGroups } from '../../lib/api.js' import type { ViewOptions } from '../../lib/options.js' import { colors, formatJson, formatNdjson, pluralize } from '../../lib/output.js' diff --git a/src/commands/channel/membership-helpers.ts b/src/commands/channel/membership-helpers.ts index a511969..ea1969a 100644 --- a/src/commands/channel/membership-helpers.ts +++ b/src/commands/channel/membership-helpers.ts @@ -1,4 +1,4 @@ -import type { Channel, Group, VisibleWorkspaceUser } from '@doist/comms-sdk' +import type { Channel, Group, VisibleWorkspaceUser } from '@todoist/comms-sdk' import { addUsersToChannel, getCommsClient, diff --git a/src/commands/channel/set.ts b/src/commands/channel/set.ts index b886640..ee3adef 100644 --- a/src/commands/channel/set.ts +++ b/src/commands/channel/set.ts @@ -1,4 +1,4 @@ -import type { User } from '@doist/comms-sdk' +import type { User } from '@todoist/comms-sdk' import { addUsersToChannel, getCommsClient, diff --git a/src/commands/channel/threads.test.ts b/src/commands/channel/threads.test.ts index 758810f..217bbe5 100644 --- a/src/commands/channel/threads.test.ts +++ b/src/commands/channel/threads.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/channel/threads.ts b/src/commands/channel/threads.ts index 8557389..54ccd5a 100644 --- a/src/commands/channel/threads.ts +++ b/src/commands/channel/threads.ts @@ -1,6 +1,6 @@ import { createHash } from 'node:crypto' -import { outputIds, resolveOutputMode } from '@doist/cli-core' -import type { ArchiveFilter, CommsApi, GetThreadsArgs, Thread } from '@doist/comms-sdk' +import { outputIds, resolveOutputMode } from '@todoist/cli-core' +import type { ArchiveFilter, CommsApi, GetThreadsArgs, Thread } from '@todoist/comms-sdk' import chalk from 'chalk' import { getCommsClient, getCurrentWorkspaceId } from '../../lib/api.js' import { formatRelativeDate } from '../../lib/dates.js' diff --git a/src/commands/channel/update.ts b/src/commands/channel/update.ts index 5bd2e12..ca45f2e 100644 --- a/src/commands/channel/update.ts +++ b/src/commands/channel/update.ts @@ -1,4 +1,4 @@ -import type { UpdateChannelArgs } from '@doist/comms-sdk' +import type { UpdateChannelArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' diff --git a/src/commands/comment/comment.test.ts b/src/commands/comment/comment.test.ts index 70e7819..10a9956 100644 --- a/src/commands/comment/comment.test.ts +++ b/src/commands/comment/comment.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram, mockStdin } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram, mockStdin } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/config/config.test.ts b/src/commands/config/config.test.ts index 1f9bd0a..9c5b247 100644 --- a/src/commands/config/config.test.ts +++ b/src/commands/config/config.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('chalk') @@ -21,7 +21,7 @@ vi.mock('../../lib/auth.js', async (importOriginal) => { } }) -import { SecureStoreUnavailableError } from '@doist/cli-core/auth' +import { SecureStoreUnavailableError } from '@todoist/cli-core/auth' import { STORED_ALAN, STORED_ELLIE } from '../../lib/__fixtures__/accounts.js' import { NoTokenError, probeApiToken } from '../../lib/auth.js' import { type Config, readConfigStrict, setConfig } from '../../lib/config.js' diff --git a/src/commands/config/view.ts b/src/commands/config/view.ts index 2498ca9..54ef817 100644 --- a/src/commands/config/view.ts +++ b/src/commands/config/view.ts @@ -1,4 +1,4 @@ -import { SecureStoreUnavailableError } from '@doist/cli-core/auth' +import { SecureStoreUnavailableError } from '@todoist/cli-core/auth' import chalk from 'chalk' import { type AuthProbeMetadata, diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index 1577165..a6b6b97 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -6,7 +6,7 @@ import { createTestProgram, describeEmptyMachineOutput, mockStdin, -} from '@doist/cli-core/testing' +} from '@todoist/cli-core/testing' import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' import { clearWorkspaceUserCache } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' @@ -173,7 +173,9 @@ const createProgram = () => createTestProgram(registerConversationCommand) // Shared setup for the --file suite: a fresh mock client wired into getCommsClient // plus a program. Tests asserting on output call captureConsole('log') themselves. function setupFileTest() { - const client = createClient({}) + const client = createClient({ + activeConversations: [createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z')], + }) apiMocks.getCommsClient.mockResolvedValue(client) return { client, program: createProgram() } } @@ -1412,7 +1414,7 @@ describe('conversation reply --file', () => { expect(client.attachments.upload).toHaveBeenCalledTimes(1) expect(client.attachments.upload).toHaveBeenCalledWith( - expect.objectContaining({ fileName: 'photo.png' }), + expect.objectContaining({ fileName: 'photo.png', workspaceId: 1 }), ) expect(client.conversationMessages.createMessage).toHaveBeenCalledWith( expect.objectContaining({ diff --git a/src/commands/conversation/helpers.ts b/src/commands/conversation/helpers.ts index 2ee4df5..ba49c7a 100644 --- a/src/commands/conversation/helpers.ts +++ b/src/commands/conversation/helpers.ts @@ -1,5 +1,5 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' -import type { Conversation } from '@doist/comms-sdk' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' +import type { Conversation } from '@todoist/comms-sdk' import chalk from 'chalk' import { buildUserNameMap, getCommsClient } from '../../lib/api.js' import { formatRelativeDate } from '../../lib/dates.js' diff --git a/src/commands/conversation/list.ts b/src/commands/conversation/list.ts index 723f134..3aca18b 100644 --- a/src/commands/conversation/list.ts +++ b/src/commands/conversation/list.ts @@ -1,4 +1,4 @@ -import { resolveOutputMode } from '@doist/cli-core' +import { resolveOutputMode } from '@todoist/cli-core' import { getCurrentWorkspaceId } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { resolveUserRefs, resolveWorkspaceRef } from '../../lib/refs.js' diff --git a/src/commands/conversation/reply.ts b/src/commands/conversation/reply.ts index 4947991..6bb0eb1 100644 --- a/src/commands/conversation/reply.ts +++ b/src/commands/conversation/reply.ts @@ -50,11 +50,12 @@ export async function replyToConversation( // Preflight the target before uploading so an invalid or forbidden // conversation fails fast instead of leaving an orphaned upload behind. - if (hasFiles) { - await client.conversations.getConversation(conversationId) - } - - const attachments = hasFiles ? await uploadAttachments(files) : undefined + const attachments = hasFiles + ? await uploadAttachments( + files, + (await client.conversations.getConversation(conversationId)).workspaceId, + ) + : undefined const message = await client.conversationMessages.createMessage({ conversationId, content: messageContent, diff --git a/src/commands/conversation/unread.ts b/src/commands/conversation/unread.ts index 2322e8b..f3821c5 100644 --- a/src/commands/conversation/unread.ts +++ b/src/commands/conversation/unread.ts @@ -1,4 +1,4 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' import chalk from 'chalk' import { buildUserNameMap, getCommsClient, getCurrentWorkspaceId } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' diff --git a/src/commands/conversation/view.ts b/src/commands/conversation/view.ts index 9da6504..648c98d 100644 --- a/src/commands/conversation/view.ts +++ b/src/commands/conversation/view.ts @@ -1,4 +1,4 @@ -import type { CommsApi, VisibleWorkspaceUser } from '@doist/comms-sdk' +import type { CommsApi, VisibleWorkspaceUser } from '@todoist/comms-sdk' import chalk from 'chalk' import { getCommsClient } from '../../lib/api.js' import { formatRelativeDate } from '../../lib/dates.js' diff --git a/src/commands/conversation/with.ts b/src/commands/conversation/with.ts index a305caa..e6dc3cb 100644 --- a/src/commands/conversation/with.ts +++ b/src/commands/conversation/with.ts @@ -1,4 +1,4 @@ -import { resolveOutputMode } from '@doist/cli-core' +import { resolveOutputMode } from '@todoist/cli-core' import { getCurrentWorkspaceId, getSessionUser, getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { resolveUserRefs, resolveWorkspaceRef } from '../../lib/refs.js' diff --git a/src/commands/doctor.test.ts b/src/commands/doctor.test.ts index a4f7622..279dbbe 100644 --- a/src/commands/doctor.test.ts +++ b/src/commands/doctor.test.ts @@ -1,5 +1,5 @@ import { readFile } from 'node:fs/promises' -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('chalk') diff --git a/src/commands/groups/groups.test.ts b/src/commands/groups/groups.test.ts index f934036..08f7b04 100644 --- a/src/commands/groups/groups.test.ts +++ b/src/commands/groups/groups.test.ts @@ -2,7 +2,7 @@ import { captureConsole, createTestProgram, describeEmptyMachineOutput, -} from '@doist/cli-core/testing' +} from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/groups/list.ts b/src/commands/groups/list.ts index 05fab8b..33a3f89 100644 --- a/src/commands/groups/list.ts +++ b/src/commands/groups/list.ts @@ -1,4 +1,4 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' import { getCurrentWorkspaceId, getWorkspaceGroups } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { ViewOptions } from '../../lib/options.js' diff --git a/src/commands/groups/view.ts b/src/commands/groups/view.ts index 2831b98..d79bdcb 100644 --- a/src/commands/groups/view.ts +++ b/src/commands/groups/view.ts @@ -1,4 +1,4 @@ -import { isRestrictedWorkspaceUser, type VisibleWorkspaceUser } from '@doist/comms-sdk' +import { isRestrictedWorkspaceUser, type VisibleWorkspaceUser } from '@todoist/comms-sdk' import { getCommsClient, getCurrentWorkspaceId } from '../../lib/api.js' import type { ViewOptions } from '../../lib/options.js' import { colors, formatJson, formatNdjson, pluralize } from '../../lib/output.js' diff --git a/src/commands/inbox.test.ts b/src/commands/inbox.test.ts index c8b811f..51c6705 100644 --- a/src/commands/inbox.test.ts +++ b/src/commands/inbox.test.ts @@ -2,7 +2,7 @@ import { captureConsole, createTestProgram, describeEmptyMachineOutput, -} from '@doist/cli-core/testing' +} from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/inbox.ts b/src/commands/inbox.ts index f738270..5cdd546 100644 --- a/src/commands/inbox.ts +++ b/src/commands/inbox.ts @@ -1,5 +1,5 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' -import type { ArchiveFilter } from '@doist/comms-sdk' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' +import type { ArchiveFilter } from '@todoist/comms-sdk' import chalk from 'chalk' import { Command, Option } from 'commander' import { getCommsClient, getCurrentWorkspaceId } from '../lib/api.js' diff --git a/src/commands/mentions.test.ts b/src/commands/mentions.test.ts index 6de7fee..d25252a 100644 --- a/src/commands/mentions.test.ts +++ b/src/commands/mentions.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const refsMocks = vi.hoisted(() => ({ diff --git a/src/commands/migrate/migrate.test.ts b/src/commands/migrate/migrate.test.ts index d2d6961..ebd6b3e 100644 --- a/src/commands/migrate/migrate.test.ts +++ b/src/commands/migrate/migrate.test.ts @@ -1,13 +1,13 @@ -import { captureConsole, createTestProgram, mockStdin } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram, mockStdin } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const sdkMocks = vi.hoisted(() => ({ fetchNewCommsUrls: vi.fn(), })) -vi.mock('@doist/comms-sdk', async (importActual) => ({ +vi.mock('@todoist/comms-sdk', async (importActual) => ({ // errors.ts reads helpers from the SDK, so only the network call is faked. - ...(await importActual()), + ...(await importActual()), fetchNewCommsUrls: sdkMocks.fetchNewCommsUrls, })) diff --git a/src/commands/migrate/urls.ts b/src/commands/migrate/urls.ts index d4dc078..a364784 100644 --- a/src/commands/migrate/urls.ts +++ b/src/commands/migrate/urls.ts @@ -1,4 +1,4 @@ -import { type CommsRequestError, fetchNewCommsUrls } from '@doist/comms-sdk' +import { type CommsRequestError, fetchNewCommsUrls } from '@todoist/comms-sdk' import { CliError } from '../../lib/errors.js' import { readContent } from '../../lib/input.js' import type { StdinOptions } from '../../lib/options.js' diff --git a/src/commands/msg/msg.test.ts b/src/commands/msg/msg.test.ts index 8bba256..6fddfc5 100644 --- a/src/commands/msg/msg.test.ts +++ b/src/commands/msg/msg.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram, mockStdin } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram, mockStdin } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/react.test.ts b/src/commands/react.test.ts index 2165b28..e1deaa8 100644 --- a/src/commands/react.test.ts +++ b/src/commands/react.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/search.test.ts b/src/commands/search.test.ts index 005685f..39c942f 100644 --- a/src/commands/search.test.ts +++ b/src/commands/search.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const refsMocks = vi.hoisted(() => ({ diff --git a/src/commands/skill/skill.test.ts b/src/commands/skill/skill.test.ts index b9cfecb..fc528a6 100644 --- a/src/commands/skill/skill.test.ts +++ b/src/commands/skill/skill.test.ts @@ -1,7 +1,7 @@ import { mkdir, readFile, rm, stat } from 'node:fs/promises' import { homedir, tmpdir } from 'node:os' import { join } from 'node:path' -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('chalk') diff --git a/src/commands/status/helpers.ts b/src/commands/status/helpers.ts index c62cedb..f9dfdb1 100644 --- a/src/commands/status/helpers.ts +++ b/src/commands/status/helpers.ts @@ -1,4 +1,4 @@ -import type { User } from '@doist/comms-sdk' +import type { User } from '@todoist/comms-sdk' import { formatJson } from '../../lib/output.js' /** The `{ status }` payload, or the whole user with `--full`, as every status command prints it. */ diff --git a/src/commands/status/set.ts b/src/commands/status/set.ts index 72508de..39b9b8c 100644 --- a/src/commands/status/set.ts +++ b/src/commands/status/set.ts @@ -1,4 +1,4 @@ -import type { SetStatusArgs } from '@doist/comms-sdk' +import type { SetStatusArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import { formatDateTime, parseLocalDate } from '../../lib/dates.js' import { parseDuration } from '../../lib/duration.js' diff --git a/src/commands/status/status.test.ts b/src/commands/status/status.test.ts index cccae04..5701ebc 100644 --- a/src/commands/status/status.test.ts +++ b/src/commands/status/status.test.ts @@ -1,4 +1,4 @@ -import { captureConsole, createTestProgram } from '@doist/cli-core/testing' +import { captureConsole, createTestProgram } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/thread/create.ts b/src/commands/thread/create.ts index f7a4004..276a8fe 100644 --- a/src/commands/thread/create.ts +++ b/src/commands/thread/create.ts @@ -81,7 +81,7 @@ export async function createThread( return } - const attachments = hasFiles ? await uploadAttachments(files) : undefined + const attachments = hasFiles ? await uploadAttachments(files, channel.workspaceId) : undefined const thread = await client.threads.createThread({ channelId, diff --git a/src/commands/thread/helpers.ts b/src/commands/thread/helpers.ts index 98e3ca6..8a6012d 100644 --- a/src/commands/thread/helpers.ts +++ b/src/commands/thread/helpers.ts @@ -1,4 +1,4 @@ -import type { CommsApi, Thread } from '@doist/comms-sdk' +import type { CommsApi, Thread } from '@todoist/comms-sdk' import chalk from 'chalk' import { getCommsClient, getWorkspaceGroups, getWorkspaceUsers } from '../../lib/api.js' import { formatRelativeDate } from '../../lib/dates.js' diff --git a/src/commands/thread/read.ts b/src/commands/thread/read.ts index a8b42da..ba78d67 100644 --- a/src/commands/thread/read.ts +++ b/src/commands/thread/read.ts @@ -1,4 +1,4 @@ -import type { Thread } from '@doist/comms-sdk' +import type { Thread } from '@todoist/comms-sdk' import type { MutationOptions, StdinOptions } from '../../lib/options.js' import { runThreadReadStateMutation } from './helpers.js' diff --git a/src/commands/thread/reply.ts b/src/commands/thread/reply.ts index 1397c8f..0dc1381 100644 --- a/src/commands/thread/reply.ts +++ b/src/commands/thread/reply.ts @@ -100,7 +100,7 @@ export async function replyToThread( return } - const attachments = hasFiles ? await uploadAttachments(files) : undefined + const attachments = hasFiles ? await uploadAttachments(files, thread.workspaceId) : undefined const notificationFields = notifyNone ? { groups: [], directMentions: [], directGroupMentions: [] } : resolved?.groups diff --git a/src/commands/thread/thread.test.ts b/src/commands/thread/thread.test.ts index ad43b08..2141eb6 100644 --- a/src/commands/thread/thread.test.ts +++ b/src/commands/thread/thread.test.ts @@ -6,8 +6,8 @@ import { captureStream, createTestProgram, mockStdin, -} from '@doist/cli-core/testing' -import { CommsApi, type CustomFetch, generateId } from '@doist/comms-sdk' +} from '@todoist/cli-core/testing' +import { CommsApi, type CustomFetch, generateId } from '@todoist/comms-sdk' import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ @@ -2354,7 +2354,7 @@ describe('thread reply --file', () => { expect(client.attachments.upload).toHaveBeenCalledTimes(1) expect(client.attachments.upload).toHaveBeenCalledWith( - expect.objectContaining({ fileName: 'diagram.png' }), + expect.objectContaining({ fileName: 'diagram.png', workspaceId: 10 }), ) expect(client.comments.createComment).toHaveBeenCalledWith( expect.objectContaining({ @@ -2489,6 +2489,9 @@ describe('thread create --file', () => { ]) expect(client.attachments.upload).toHaveBeenCalledTimes(2) + expect(client.attachments.upload).toHaveBeenCalledWith( + expect.objectContaining({ workspaceId: 10 }), + ) const args = client.threads.createThread.mock.calls[0][0] as { title: string content: string diff --git a/src/commands/thread/unread.ts b/src/commands/thread/unread.ts index dc70a17..d789863 100644 --- a/src/commands/thread/unread.ts +++ b/src/commands/thread/unread.ts @@ -1,4 +1,4 @@ -import type { CommsApi } from '@doist/comms-sdk' +import type { CommsApi } from '@todoist/comms-sdk' import { CliError } from '../../lib/errors.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' import { resolveCommentId } from '../../lib/refs.js' diff --git a/src/commands/thread/view.ts b/src/commands/thread/view.ts index f677198..6f81aa0 100644 --- a/src/commands/thread/view.ts +++ b/src/commands/thread/view.ts @@ -1,4 +1,4 @@ -import type { CommsApi } from '@doist/comms-sdk' +import type { CommsApi } from '@todoist/comms-sdk' import chalk from 'chalk' import { buildUserNameMap, getCommsClient } from '../../lib/api.js' import { formatRelativeDate } from '../../lib/dates.js' diff --git a/src/commands/update/index.ts b/src/commands/update/index.ts index 0367a34..8224fed 100644 --- a/src/commands/update/index.ts +++ b/src/commands/update/index.ts @@ -1,4 +1,4 @@ -import { registerUpdateCommand as registerCoreUpdateCommand } from '@doist/cli-core/commands' +import { registerUpdateCommand as registerCoreUpdateCommand } from '@todoist/cli-core/commands' import type { Command } from 'commander' import packageJson from '../../../package.json' with { type: 'json' } import { getConfigPath } from '../../lib/config.js' diff --git a/src/commands/update/update.test.ts b/src/commands/update/update.test.ts index 569256a..7caa1f2 100644 --- a/src/commands/update/update.test.ts +++ b/src/commands/update/update.test.ts @@ -17,7 +17,7 @@ describe('update wrapper', () => { }) afterEach(() => { - vi.doUnmock('@doist/cli-core/commands') + vi.doUnmock('@todoist/cli-core/commands') vi.doUnmock('../../lib/config.js') vi.resetModules() vi.restoreAllMocks() @@ -26,7 +26,7 @@ describe('update wrapper', () => { it('forwards comms-cli wiring (packageName, version, configPath, changelog hint, spinner) to cli-core', async () => { const registerCoreSpy = vi.fn() - vi.doMock('@doist/cli-core/commands', () => ({ + vi.doMock('@todoist/cli-core/commands', () => ({ registerUpdateCommand: registerCoreSpy, // Re-exported by src/lib/update.ts โ€” must still resolve when the // mocked module is loaded transitively via the wrapper. @@ -74,7 +74,7 @@ describe('update wrapper', () => { // Stable channel must resolve to the `latest` dist-tag โ€” but we don't // pin cli-core's exact registry URL shape; just assert the package // name + channel-appropriate tag are present. - expect(url).toContain(packageJson.name) + expect(url).toContain('@todoist/comms-cli') expect(url).toContain('latest') }) diff --git a/src/commands/user.test.ts b/src/commands/user.test.ts index 4bf05f9..506fd1a 100644 --- a/src/commands/user.test.ts +++ b/src/commands/user.test.ts @@ -2,7 +2,7 @@ import { captureConsole, createTestProgram, describeEmptyMachineOutput, -} from '@doist/cli-core/testing' +} from '@todoist/cli-core/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' const apiMocks = vi.hoisted(() => ({ diff --git a/src/commands/user.ts b/src/commands/user.ts index 771d9ea..85f4d9d 100644 --- a/src/commands/user.ts +++ b/src/commands/user.ts @@ -1,4 +1,4 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' import chalk from 'chalk' import { Command } from 'commander' import { diff --git a/src/commands/view.test.ts b/src/commands/view.test.ts index 1b78f80..d3924e4 100644 --- a/src/commands/view.test.ts +++ b/src/commands/view.test.ts @@ -1,4 +1,4 @@ -import { createTestProgram } from '@doist/cli-core/testing' +import { createTestProgram } from '@todoist/cli-core/testing' import { Command } from 'commander' import { beforeEach, describe, expect, it, vi } from 'vitest' diff --git a/src/commands/workspace.ts b/src/commands/workspace.ts index 161e3a0..9669a47 100644 --- a/src/commands/workspace.ts +++ b/src/commands/workspace.ts @@ -1,4 +1,4 @@ -import { outputIds, printEmpty, resolveOutputMode } from '@doist/cli-core' +import { outputIds, printEmpty, resolveOutputMode } from '@todoist/cli-core' import chalk from 'chalk' import { Command } from 'commander' import { fetchWorkspaces, getCurrentWorkspaceId } from '../lib/api.js' diff --git a/src/index.ts b/src/index.ts index 3372702..0fb43d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node -import { stripUserFlag } from '@doist/cli-core' +import { stripUserFlag } from '@todoist/cli-core' import { CommanderError, type Command, program } from 'commander' import pkg from '../package.json' with { type: 'json' } import { configureCommandOutput } from './lib/command-output.js' diff --git a/src/lib/api.test.ts b/src/lib/api.test.ts index 2320335..78299ea 100644 --- a/src/lib/api.test.ts +++ b/src/lib/api.test.ts @@ -1,4 +1,4 @@ -import { CommsRequestError } from '@doist/comms-sdk' +import { CommsRequestError } from '@todoist/comms-sdk' import { beforeEach, describe, expect, it, vi } from 'vitest' import packageJson from '../../package.json' with { type: 'json' } @@ -14,10 +14,10 @@ const sdkMocks = vi.hoisted(() => ({ addGroupUsers: vi.fn(), })) -vi.mock('@doist/comms-sdk', async (importActual) => { +vi.mock('@todoist/comms-sdk', async (importActual) => { // Only the client is faked. The error class and the helpers that inspect it // stay real, because the status mapping under test depends on them agreeing. - const actual = await importActual() + const actual = await importActual() class CommsApi { channels = { deleteChannel: sdkMocks.deleteChannel } attachments = { upload: sdkMocks.uploadAttachment } @@ -214,7 +214,11 @@ describe('wrapResult โ€” central 403 translation', () => { const client = createWrappedCommsClient('test-token') await expect( - client.attachments.upload({ file: new Blob(['x']), fileName: 'x.png' }), + client.attachments.upload({ + file: new Blob(['x']), + fileName: 'x.png', + workspaceId: 42, + }), ).rejects.toMatchObject({ code: 'INSUFFICIENT_SCOPE', message: 'This action requires permissions your current token does not have.', @@ -232,7 +236,11 @@ describe('wrapResult โ€” central 403 translation', () => { const client = createWrappedCommsClient('test-token') await expect( - client.attachments.upload({ file: new Blob(['x']), fileName: 'x.png' }), + client.attachments.upload({ + file: new Blob(['x']), + fileName: 'x.png', + workspaceId: 42, + }), ).rejects.toThrow('READ_ONLY') // The SDK upload must never fire when the write-guard rejects. expect(sdkMocks.uploadAttachment).not.toHaveBeenCalled() diff --git a/src/lib/api.ts b/src/lib/api.ts index 0610801..bed6a71 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -8,7 +8,7 @@ import { type User, type Workspace, type WorkspaceUser, -} from '@doist/comms-sdk' +} from '@todoist/comms-sdk' import { getApiTokenSnapshot } from './auth.js' import { getConfig, updateConfig } from './config.js' import { diff --git a/src/lib/attachments.test.ts b/src/lib/attachments.test.ts index 7a2ac5d..d384273 100644 --- a/src/lib/attachments.test.ts +++ b/src/lib/attachments.test.ts @@ -51,9 +51,19 @@ describe('uploadAttachments', () => { const client = createClient() apiMocks.getCommsClient.mockResolvedValue(client) - const result = await uploadAttachments([fileA, fileB]) + const result = await uploadAttachments([fileA, fileB], 42) expect(client.attachments.upload).toHaveBeenCalledTimes(2) + expect(client.attachments.upload).toHaveBeenNthCalledWith(1, { + file: expect.any(Blob), + fileName: 'a.png', + workspaceId: 42, + }) + expect(client.attachments.upload).toHaveBeenNthCalledWith(2, { + file: expect.any(Blob), + fileName: 'b.pdf', + workspaceId: 42, + }) expect(result.map((a) => a.fileName)).toEqual(['a.png', 'b.pdf']) }) @@ -62,7 +72,7 @@ describe('uploadAttachments', () => { apiMocks.getCommsClient.mockResolvedValue(client) await expect( - uploadAttachments([fileA, join(tmpDir, 'missing.png'), fileB]), + uploadAttachments([fileA, join(tmpDir, 'missing.png'), fileB], 42), ).rejects.toMatchObject({ code: 'FILE_NOT_FOUND' }) expect(client.attachments.upload).not.toHaveBeenCalled() diff --git a/src/lib/attachments.ts b/src/lib/attachments.ts index 08fd1f4..a979b35 100644 --- a/src/lib/attachments.ts +++ b/src/lib/attachments.ts @@ -1,4 +1,4 @@ -import type { Attachment } from '@doist/comms-sdk' +import type { Attachment } from '@todoist/comms-sdk' import { getCommsClient } from './api.js' import { openLocalFileAsBlob } from './local-file.js' @@ -45,13 +45,16 @@ export async function validateAttachmentFiles(files: string[]): Promise { * uploaded-but-unreferenced attachments behind. Uploads then run with bounded * concurrency while the returned array preserves the input order. */ -export async function uploadAttachments(files: string[]): Promise { +export async function uploadAttachments( + files: string[], + workspaceId: number, +): Promise { // Validate every path first so a bad one fails before we upload anything. const opened = await Promise.all(files.map((file) => openLocalFileAsBlob({ file }))) const client = await getCommsClient() return mapWithConcurrency(opened, MAX_UPLOAD_CONCURRENCY, ({ blob, fileName }) => - client.attachments.upload({ file: blob, fileName }), + client.attachments.upload({ file: blob, fileName, workspaceId }), ) } diff --git a/src/lib/auth-pages.ts b/src/lib/auth-pages.ts index e24e46e..c5565ea 100644 --- a/src/lib/auth-pages.ts +++ b/src/lib/auth-pages.ts @@ -296,7 +296,7 @@ export function renderSuccess(): string {