refactor(api): Centralize project id-or-slug discrimination#117461
Closed
JoshFerge wants to merge 1 commit into
Closed
refactor(api): Centralize project id-or-slug discrimination#117461JoshFerge wants to merge 1 commit into
JoshFerge wants to merge 1 commit into
Conversation
Extract the "is this value an ID or a slug?" decision into a single coerce_id_or_slug() helper so ProjectIdOrSlugField and parse_id_or_slug_params share one rule instead of each re-deriving the isdecimal()/-1-sentinel branching. Adds a docstring noting the relationship to the generic IdOrSlugLookup (slug__id_or_slug) DB lookup. Behavior is unchanged: the field still layers strict slug validation on top of the shared rule, and the parser still skips empty values. https://claude.ai/code/session_014ZqyxEfBjS5hjG62L8tACF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft / experiment stacked on #117445 (base =
feat/project-id-or-slug-foundation, so this diff is just the refactor). Explores the reuse opportunity raised in review: the "is this value an ID or a slug?" decision was being re-derived in several spots.What
Extracts a single
coerce_id_or_slug()helper insrc/sentry/api/helpers/projects.pyas the one source of truth for theisdecimal()+-1sentinel branching. BothProjectIdOrSlugField.to_internal_valueandparse_id_or_slug_paramsnow call it instead of each re-implementing the rule.Why
Before this, the same discrimination lived in three places:
IdOrSlugLookup.as_sql— the genericslug__id_or_slugDB lookup —str(...).isdecimal()ProjectIdOrSlugField.to_internal_value—data.isdecimal() or data == str(ALL_ACCESS_PROJECT_ID)parse_id_or_slug_params— the same check againThis collapses the two new copies into one. The generic
IdOrSlugLookupis intentionally left alone: it runs at the SQL layer across many models (Team, Org, SentryApp, DocIntegration, …) and deliberately has no notion of the project-specific-1/$allsentinels, so coupling it to project constants would be wrong. The new helper's docstring records that relationship.Behavior
Unchanged. The field still layers strict slug-format validation (
MIXED_SLUG_REGEX) on top of the shared rule, and the parser still silently skips empty values without validating slug format. Validated equivalent to the pre-refactor implementation across every case in the existing tests (tests/sentry/api/helpers/test_projects.py,tests/sentry/api/serializers/rest_framework/test_project.py) plus a fuzz battery of id / slug / sentinel / edge inputs.Out of scope (possible follow-ups)
_validate_sluginProjectFieldstill routes through the field and then rejects ints; could be expressed more directly, but left as-is to keep behavior identical.str(...).isdecimal()organization id-or-slug checks inbases/organization.pyare a separate concept and not touched here.https://claude.ai/code/session_014ZqyxEfBjS5hjG62L8tACF
Generated by Claude Code