You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0ff95c2
Browse filesBrowse the repository at this point in the historyBrowse files
feat(cli): Pick the stored account from the target org or server (#1528)
Stacked on #1526. Covers the automatic-selection half of #1396: with
several stored accounts, commands use the account that fits what they
target, so users don't have to `sentry auth switch` back and forth.
**How the account is chosen** (per invocation, most specific first)
1. **Organization.** If the target org isn't in the active account's
cached org list, but another stored account has used that org, that
account is pinned for the rest of the invocation. This hooks into
`resolveOrgRegion` / `resolveEffectiveOrg`, so it covers the ~55
org-scoped call sites and DSN `oNNNN` ids.
2. **Server.** An explicit `SENTRY_HOST`/`SENTRY_URL`, `.sentryclirc`,
or a pasted Sentry URL for another account's server selects the stored
account on that server. Previously this was a `HostScopeError`.
3. **Active account** otherwise.
**Guardrails**
- The stored active account never changes. The selection lives in
memory, keyed by the invocation's env object, so library-mode calls are
isolated from each other.
- One invocation never mixes identities: the first pin wins.
- An account is only chosen for hosts its token is already trusted for,
using the same SaaS-equivalence rule as `token-host.ts`.
- If the active account has no cached org list (for example right after
`auth switch`), it is kept. An account that simply hasn't used the org
yet can't be overridden by one that has.
- An explicit server limits candidates to accounts on that server. The
persisted `defaults.url` counts as soft: when the pinned account is
elsewhere, it yields to that account's host.
- `SENTRY_FORCE_ENV_TOKEN` is never overridden.
**Storage**
Schema v19 adds `org_regions.account_id`. Writes record the account
whose credentials fetched the row, following in-process refresh-token
rotation. The migration backfills existing rows by matching credential
identity. The org→account mapping is warm right away for new logins,
because login already warms the org cache.
Memoized credential state (token, fingerprint, row, stored-creds flag)
is now keyed by selection, and user info reads and writes go to the
account in effect. Account management (`switch`, `logout`, `list`) still
works on the stored active account.
**Not covered**
- An org that no stored account has used yet still goes to the active
account. There is no retry-on-403 across accounts.
- When several non-active accounts know the same org, the most recently
used one wins, with no prompt.
<!-- junior-request-attribution:start -->
via **byk**.
<!-- junior-request-attribution:end -->
<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC010884QHLG%3A1792178653.828039 -->
--
[View Junior
Session](https://junior-prod.sentry.dev/conversations/slack%3AC010884QHLG%3A1792178653.828039)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC010884QHLG%3A1792178653.828039/?project=4510944073809921)
<!-- junior-session-footer:end -->
---------
Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Burak Yigit Kaya <burak.kaya@sentry.io>
Copy file name to clipboardExpand all lines: apps/cli-docs/src/content/docs/agent-guidance.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -325,7 +325,7 @@ When querying the Events API (directly or via `sentry api`), valid dataset value
325
325
## Common Mistakes
326
326
327
327
-**Wrong issue ID format**: Use `PROJECT-123` (short ID), not the numeric ID `123456789`. The short ID includes the project prefix.
328
-
-**Pre-authenticating unnecessarily**: Don't run `sentry auth login` before every command. The CLI detects missing/expired auth and prompts automatically. Only run `sentry auth login` to add an account; use `sentry auth switch` to change which stored account is used.
328
+
-**Pre-authenticating unnecessarily**: Don't run `sentry auth login` before every command. The CLI detects missing/expired auth and prompts automatically. Only run `sentry auth login` to add an account. With several stored accounts, commands pick the one that knows the target organization or server automatically; use `sentry auth switch`only to change the default.
329
329
-**Missing `--json` for piping**: Human-readable output includes formatting. Use `--json` when parsing output programmatically.
330
330
-**Specifying org/project when not needed**: Auto-detection resolves org/project from `.sentryclirc` config files, DSNs, env vars, and directory names. Let it work first — only add `<org>/<project>` if the CLI says it can't detect the target or detects the wrong one.
331
331
-**Manually discovering the project before running a command**: Don't list the orgs you belong to, then list every project in each, to match the local checkout to a project — the CLI already does exactly this resolution internally on each command. Skip the fan-out and run the command directly; correct the target afterwards only if the output shows the wrong org/project.
Copy file name to clipboardExpand all lines: packages/cli/plugins/sentry-cli/skills/sentry-cli/SKILL.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -337,7 +337,7 @@ When querying the Events API (directly or via `sentry api`), valid dataset value
337
337
### Common Mistakes
338
338
339
339
-**Wrong issue ID format**: Use `PROJECT-123` (short ID), not the numeric ID `123456789`. The short ID includes the project prefix.
340
-
-**Pre-authenticating unnecessarily**: Don't run `sentry auth login` before every command. The CLI detects missing/expired auth and prompts automatically. Only run `sentry auth login` to add an account; use `sentry auth switch` to change which stored account is used.
340
+
-**Pre-authenticating unnecessarily**: Don't run `sentry auth login` before every command. The CLI detects missing/expired auth and prompts automatically. Only run `sentry auth login` to add an account. With several stored accounts, commands pick the one that knows the target organization or server automatically; use `sentry auth switch`only to change the default.
341
341
-**Missing `--json` for piping**: Human-readable output includes formatting. Use `--json` when parsing output programmatically.
342
342
-**Specifying org/project when not needed**: Auto-detection resolves org/project from `.sentryclirc` config files, DSNs, env vars, and directory names. Let it work first — only add `<org>/<project>` if the CLI says it can't detect the target or detects the wrong one.
343
343
-**Manually discovering the project before running a command**: Don't list the orgs you belong to, then list every project in each, to match the local checkout to a project — the CLI already does exactly this resolution internally on each command. Skip the fan-out and run the command directly; correct the target afterwards only if the output shows the wrong org/project.
Copy file name to clipboardExpand all lines: packages/cli/plugins/sentry-cli/skills/sentry-cli/references/auth.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Authenticate with Sentry
22
22
**Flags:**
23
23
-`--token <value> - Authenticate using an API token instead of OAuth`
24
24
-`--timeout <value> - Timeout for OAuth flow in seconds (default: 900) - (default: "900")`
25
-
-`--force - Replace the active account's login without prompting, instead of adding another account`
25
+
-`--force - Replace the stored login for this server without prompting, instead of adding another account`
26
26
-`--url <value> - Sentry instance URL to authenticate against (e.g. https://sentry.example.com). Required for self-hosted; defaults to SaaS (https://sentry.io).`
27
27
-`--read-only - Request only read-only OAuth scopes (project:read, org:read, event:read, member:read, team:read). Useful for handing tokens to AI agents or CI jobs that should not be able to mutate Sentry state.`
28
28
-`-s, --scope <value>... - Request specific OAuth scopes (repeatable, comma-separated). E.g. --scope project:read --scope org:read. Overrides the default scope set.`
0 commit comments