Commit 48a9a8f
fix(init): resolve numeric org ID from DSN and prompt when Sentry already configured (#532)
## 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](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent f2eaabb commit 48a9a8f
File tree
3 files changed
+342
-8
lines changed- src/lib
- init
- test/lib/init
3 files changed
+342
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
| |||
675 | 678 | | |
676 | 679 | | |
677 | 680 | | |
678 | | - | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
679 | 694 | | |
680 | 695 | | |
681 | 696 | | |
| |||
744 | 759 | | |
745 | 760 | | |
746 | 761 | | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
747 | 841 | | |
748 | 842 | | |
749 | 843 | | |
| |||
774 | 868 | | |
775 | 869 | | |
776 | 870 | | |
777 | | - | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
778 | 880 | | |
779 | 881 | | |
780 | 882 | | |
| |||
786 | 888 | | |
787 | 889 | | |
788 | 890 | | |
789 | | - | |
| 891 | + | |
790 | 892 | | |
791 | 893 | | |
792 | 894 | | |
| |||
796 | 898 | | |
797 | 899 | | |
798 | 900 | | |
799 | | - | |
| 901 | + | |
800 | 902 | | |
801 | 903 | | |
802 | 904 | | |
803 | 905 | | |
804 | 906 | | |
805 | 907 | | |
806 | | - | |
| 908 | + | |
807 | 909 | | |
808 | 910 | | |
809 | 911 | | |
810 | 912 | | |
811 | 913 | | |
812 | | - | |
| 914 | + | |
813 | 915 | | |
814 | 916 | | |
815 | | - | |
| 917 | + | |
816 | 918 | | |
817 | 919 | | |
818 | 920 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
248 | | - | |
| 248 | + | |
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
| |||
0 commit comments