Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.

feat(client): Pass the request's org scope to CredentialProvider.current - #129

Merged
BYK merged 1 commit into
mainfrom
feat/credential-request-scope
Nov 5, 2026
Merged

BYK merged 1 commit into
mainfrom
feat/credential-request-scope

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

createApiTransport now passes CredentialProvider.current the request it picks a credential for: { url, scope? }. scope is the organization (and project, when present) the URL path targets. A new export, parseApiPathScope, parses it.

This lets a host with several logins pick the one that can reach the org at the shared request layer. The alternative is having every caller resolve the org first. The motivating bug is getsentry/toolkit#1803: sentry api organizations/<org>/... uses the invocation's current account even when another stored login can reach <org>. Typed CLI commands avoid this only because they call selectAccountForOrg before building the request.

parseApiPathScope reads these paths:

  • organizations/{org}/
  • organizations/{org}/projects/{project}/
  • projects/{org}/{project}/
  • teams/{org}/

It accepts a full URL (it finds /api/0/ anywhere in the path, so self-hosted prefixes work) or a relative endpoint. It returns undefined for unscoped paths, list endpoints, schema placeholders, and encoded or dot segments. The logic comes from the CLI's parseOrgProjectFromApiPath, which can now delegate to it.

Compatibility: existing providers that declare current() with no arguments still type-check and behave the same. The credential must stay valid for the URL's origin, because the URL is already built. The JSDoc says so.

The toolkit side, which consumes this, is a draft until this ships in a release.

via @BYK.

--

View Session in Sentry

createApiTransport now calls current({ url, scope }), where scope is the
organization (and project) the URL path targets, parsed by the new
parseApiPathScope helper. Hosts with several logins can pick the one that
can reach that organization at the shared request layer instead of in every
command.

Refs getsentry/toolkit#1803

Co-Authored-By: BYK <[email protected]>
@BYK
BYK merged commit 97e1f0a into main Nov 5, 2026
11 checks passed
@BYK
BYK deleted the feat/credential-request-scope branch November 5, 2026 10:03
sentry-junior Bot added a commit to getsentry/toolkit that referenced this pull request Nov 5, 2026
1.26.0 adds parseApiPathScope and passes the request's org scope to
CredentialProvider.current (getsentry/sentry-api-schema#129), which
the account routing in this branch depends on.

Co-Authored-By: BYK <[email protected]>
BYK added a commit to getsentry/toolkit that referenced this pull request Nov 5, 2026
… org (#1976)

Fixes #1803.

`sentry api organizations/<org>/...` used the invocation's current
account even when another stored login could reach `<org>`. Typed
commands avoid this only because `resolveOrgRegion` calls
`selectAccountForOrg` before building the request.

This moves the routing into the shared request layer.
getsentry/sentry-api-schema#129 makes `createApiTransport` pass
`CredentialProvider.current({ url, scope })`, where `scope` is the org
(and project) the URL path targets. The CLI changes:

- **`sentry-client.ts`**: for unpinned transports, `current` now runs
`selectAccountForOrg(scope.organization)` before reading the credential.
It is limited to accounts on the request's server, because the URL is
already built. A numeric org id is matched as an id, as in `region.ts`.
- **`getDefaultSdkConfig`** no longer pins the credential. Its only
caller is `sentry api`'s `prepareRawApiRequest`, so without this the
transport could not route that request. The base URL still comes from
the current credential, routing stays on that server, and
`assertCredentialScope` still guards every send.
- **`selectAccountForOrg`** gets an optional `server` that limits
candidates, the env token included, to that server.
- **`parseOrgProjectFromApiPath`** delegates to `parseApiPathScope` from
`@sentry/api/client`, so the CLI and the transport read org scope the
same way.
- The `auth` docs fragment mentions `sentry api` paths under
organization-based selection.

Existing rules still hold. One invocation never mixes accounts: once an
org has picked an account, later requests are no-ops. Pinned
credentials, such as region discovery and `withStoredAccount`, are not
re-routed. Forced env tokens are never overridden.

**Draft because:** the CLI needs a published `@sentry/api` release that
includes getsentry/sentry-api-schema#129. This PR still needs the
catalog bump in `pnpm-workspace.yaml`, currently `1.20.0`. Against
`1.20.0`, `current: async (request) => …` and the `CredentialRequest`
import do not type-check.

**Not covered:** `sentry api` to an org that only an account on a
*different* server can reach. The URL's host is fixed before the
transport runs, so routing there would need the command to pick the
account before building the URL. That's a CLI-layer change; I left it
out to keep the fix in `@sentry/api`.

Verification, run with a local build of the sentry-api-schema branch
installed in place of `@sentry/[email protected]`:
- `pnpm run typecheck` and `pnpm run lint` in `packages/cli` pass.
- New tests in `test/lib/db/account-routing.test.ts` cover routing by
path org, unscoped requests staying on the active account, and the
same-server limit. The first fails without the transport change.
- `pnpm run test:unit` passes, except for 6 permission-based tests
(completions, upgrade lock, resilient DB, code scanner, file-changes)
that also fail on clean `main` in my sandbox.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3Aad43917b182be622bccdd8f7 -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3Aad43917b182be622bccdd8f7/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
BYK added a commit that referenced this pull request Nov 5, 2026
…BaseUrl (#130)

This adds a per-request base URL hook to the generated client, so a host
can send each org-scoped request to the org's region or server
**before** the URL is built. Follow-up to #129, which gave
`CredentialProvider.current` the path's scope. That only helps pick a
credential for a URL that's already built.

```ts
const sentry = createSentryClient({
  baseUrl: "https://sentry.io",
  resolveBaseUrl: async ({ baseUrl, scope }) =>
    scope ? await regionUrlFor(scope.organization) : baseUrl,
});
await sentry.listOrganizationIssues({ path: { organization_id_or_slug: "acme" } });
// → https://us.sentry.io/api/0/organizations/acme/issues/
```

## What changes

- **`Config.resolveBaseUrl`** (types `BaseUrlRequest` and
`BaseUrlResolver`, exported from the root). It receives `{ baseUrl, url,
scope? }`:
  - `baseUrl` is the configured base URL, or `''` when none is set.
  - `url` is the built path and query.
- `scope` is `parseApiPathScope(url)`, and is left out for unscoped
paths.

Its result replaces `baseUrl` for that one request, with trailing
slashes removed. It runs once per request, including `prepareRequest`,
after `requestValidator` and before request interceptors. A per-call
`resolveBaseUrl` replaces the configured one. If it throws, the request
rejects with that error unchanged and `fetch` is never called.
- The new step lives in `scripts/generate-base-url-resolver.mjs`. It
patches the pinned Fetch client in the same way
`generate-request-preparation.mjs` does.
- **`parseApiPathScope`** moves to `lib/api-client/api-path-scope.ts`, a
module with no imports, so the generated client can use it without
bundling `errors.ts`. `api-path.ts` re-exports it, so the public API
doesn't change.

The host still owns region discovery, caching, and trust. The client
only asks the host which base URL to use.

## Consumer

The toolkit CLI will use this to replace `resolveOrgRegion` calls at
each call site and the `getDefaultSdkConfig({ path })` workaround from
getsentry/toolkit#1978. I built that change against a local build of
this branch. CLI `tsc`, lint, and the affected tests pass.

## Tests

`test/base-url-resolution.test.ts` covers:
- An org-scoped operation goes to the resolved base URL. The resolver
gets the base URL, path, query, and scope.
- A project scope is passed through. Unscoped paths get no `scope` key.
- Raw `prepareRequest` URLs are resolved, including when no `baseUrl` is
configured.
- Ordering: the resolver runs after validation, before interceptors, and
once per request.
- A per-call resolver overrides the configured one.
- A resolver that throws rejects the request before any fetch.

`pnpm build`, `pnpm typecheck`, and `pnpm test` (632 tests) pass.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3Aad43917b182be622bccdd8f7 -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3Aad43917b182be622bccdd8f7/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant