Skip to content

fix(init): resolve numeric org ID from DSN and prompt when Sentry already configured#532

Merged
betegon merged 6 commits intomainfrom
fix/init-dsn-existing-project-detection
Mar 23, 2026
Merged

fix(init): resolve numeric org ID from DSN and prompt when Sentry already configured#532
betegon merged 6 commits intomainfrom
fix/init-dsn-existing-project-detection

Conversation

@betegon
Copy link
Member

@betegon betegon commented Mar 23, 2026

Summary

  • Bug fix (sentry init only): When `sentry init .` is run in a project with an existing Sentry DSN, the CLI extracts a numeric org ID (e.g. `4507492088676352`) from the DSN. If the org belonged to a different Sentry account, or the org regions cache was empty after a fresh install, this numeric ID was passed directly to `listTeams()` → 404 → confusing error: "Organization '4507492088676352' not found."

    Fix scoped to `resolveOrgSlug` in `local-ops.ts` (only used by sentry init): when the prefetched org is a raw numeric string, look it up in the org regions cache (`getOrgByNumericId`). If found → use the real slug. If not (empty cache or inaccessible org) → fall through to `listOrganizations()` so the user selects from their accessible orgs.

  • UX improvement: Before creating a new project, `sentry init` now checks if the codebase already has a Sentry DSN that resolves to an accessible project. If so, prompts: "Found an existing Sentry project (org/project). Use it or create a new one?" — avoids silently creating a duplicate when Sentry is already configured.

Scope: no impact on other commands

The numeric ID fallback in `resolveOrgFromDsn` is intentionally unchanged. Commands like `sentry issue list`, `sentry trace view`, and `sentry event view` rely on DSN org auto-detection and work correctly with numeric IDs (the Sentry API accepts them for read operations). Only `sentry init`'s project-creation path gets the narrower fix.

How the original bug was triggered

Fresh CLI install → `sentry auth login` (`warmOrgCache()` fires in background, may not complete before process exits) → immediately `sentry init .` in a project with a Sentry DSN → empty org regions cache → numeric org ID used directly → 404.

Test plan

  • Replicate bug: clear SQLite cache, `sentry auth login`, immediately `sentry init .` in a project with DSN → no more numeric-ID error, falls through to org selection
  • DSN from accessible org: `sentry init .` → prompt "Found existing project (org/slug). Use it?"
  • `--yes` flag: `sentry init . --yes` with accessible DSN → auto-uses existing, no prompt
  • Other commands unaffected: `sentry issue list` in a project with DSN still auto-detects org from DSN
  • Unit tests: `bun test test/isolated/resolve-target.test.ts`

🤖 Generated with Claude Code

…eady configured

When `sentry init .` is run in a project with an existing Sentry DSN, the CLI
was extracting the numeric org ID (e.g. `4507492088676352`) from the DSN and
passing it directly to the API as an org slug. If the org belonged to a
different Sentry account (or the org regions cache was empty after a fresh
install), this caused a confusing 404 error: "Organization '4507492088676352'
not found."

Fix the numeric ID fallback in `resolveOrgFromDsn`: instead of returning the
raw numeric ID, look it up in the org regions cache via `getOrgByNumericId`.
If found, use the real slug. If not (empty cache or inaccessible org), return
null so the caller falls through to `listOrganizations()` for proper
interactive org selection.

Also add a UX improvement: before creating a new Sentry project, detect
whether the codebase already has a Sentry DSN that resolves to an accessible
project. If so, prompt the user: "Found an existing Sentry project. Use it
or create a new one?" This avoids silently creating a duplicate project when
Sentry is already configured.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 23, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (install) Support SENTRY_VERSION env var for version pinning by BYK in #537

Bug Fixes 🐛

Event

  • Detect org/ISSUE-SHORT-ID in event view single-arg path (CLI-9K) by BYK in #529
  • Auto-redirect issue short IDs in event view (CLI-JR) by BYK in #524

Other

  • (api) Strip api/0/ prefix and exclude NodeSystemError integration (CLI-K1) by BYK in #523
  • (dashboard) Add missing datasets to agent guidance by betegon in #522
  • (docs) Overscroll popup — curl command + click-to-copy by betegon in #531
  • (init) Resolve numeric org ID from DSN and prompt when Sentry already configured by betegon in #532
  • (polling) Move spinner from stderr to stdout to prevent consola collision by BYK in #533
  • (telemetry) Set sentry.org tag in issue explain and plan commands by BYK in #534
  • Handle invalid URLs gracefully in response cache (CLI-GC) by BYK in #528
  • Avoid double-prefixing in buildCommandHint for slashed args (CLI-8C) by BYK in #527
  • Handle full short IDs and numeric IDs in multi-slash issue args (CLI-KC, CLI-B6) by BYK in #526
  • Auto-recovery for wrong entity types across commands (CLI-G6, CLI-K6, CLI-JR) by BYK in #525

Documentation 📚

  • (init) Add documentation and experimental notice for sentry init by betegon in #530

Internal Changes 🔧

  • Regenerate skill files by github-actions[bot] in 22b5281d

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 23, 2026

Codecov Results 📊

126 passed | Total: 126 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 1024 uncovered lines.
✅ Project coverage is 96.02%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
resolve-target.ts 90.24% ⚠️ 69 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    95.81%    96.02%    +0.21%
==========================================
  Files          185       185         —
  Lines        25681     25757       +76
  Branches         0         0         —
==========================================
+ Hits         24605     24733      +128
- Misses        1076      1024       -52
- Partials         0         0         —

Generated by Codecov Action

The previous fix to resolveOrgFromDsn was too broad — returning null
broke DSN org auto-detection for read commands like `sentry issue list`
that work fine with numeric org IDs.

Revert resolveOrgFromDsn to its original behavior. Instead, handle the
numeric ID resolution in resolveOrgSlug (local-ops.ts), which is only
used by sentry init's project creation path. When the prefetched org is
a raw numeric string, attempt to resolve it to a slug via the org regions
cache (getOrgByNumericId). On cache miss — empty cache or org from a
different Sentry account — fall through to listOrganizations() so the
user can select from their accessible orgs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
betegon and others added 2 commits March 23, 2026 18:31
…ect detection

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Extract `promptForExistingProject` helper to reduce cognitive complexity
- Move numeric org ID regex to top-level constant
- Wrap single-statement if-returns in block statements
- Simplify double-negation logical expression
- Fix import ordering and template literals in test file
- Apply formatter line-wrapping

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Reuse `resolveDsnByPublicKey` from resolve-target.ts instead of
duplicating the getCachedProjectByDsnKey → findProjectByDsnKey →
setCachedProjectByDsnKey pattern. Addresses cursor bot review comment.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

…error formatting

Errors thrown by tryGetExistingProject (e.g. non-404 ApiErrors) now reach
formatLocalOpError instead of bubbling up as raw error.message strings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@betegon betegon merged commit 48a9a8f into main Mar 23, 2026
22 checks passed
@betegon betegon deleted the fix/init-dsn-existing-project-detection branch March 23, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant