Skip to content

Conversation

@TkDodo
Copy link
Collaborator

@TkDodo TkDodo commented Aug 5, 2025

No description provided.

@github-actions github-actions bot added Scope: Frontend Automatically applied to PRs that change frontend components Scope: Backend Automatically applied to PRs that change backend components labels Aug 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2025

🚨 Warning: This pull request contains Frontend and Backend changes!

It's discouraged to make changes to Sentry's Frontend and Backend in a single pull request. The Frontend and Backend are not atomically deployed. If the changes are interdependent of each other, they must be separated into two pull requests and be made forward or backwards compatible, such that the Backend or Frontend can be safely deployed independently.

Have questions? Please ask in the #discuss-dev-infra channel.

@codecov
Copy link

codecov bot commented Aug 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #97155      +/-   ##
==========================================
- Coverage   81.36%   79.12%   -2.25%     
==========================================
  Files        8535     8643     +108     
  Lines      378006   393114   +15108     
  Branches    23839    24577     +738     
==========================================
+ Hits       307571   311042    +3471     
- Misses      70068    81706   +11638     
+ Partials      367      366       -1     

@getsantry
Copy link
Contributor

getsantry bot commented Sep 23, 2025

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@TkDodo
Copy link
Collaborator Author

TkDodo commented Sep 30, 2025

@TkDodo TkDodo closed this Sep 30, 2025
@TkDodo TkDodo deleted the tkdodo/feat/api-url-generation branch September 30, 2025 15:40
ryan953 added a commit that referenced this pull request Oct 15, 2025
This script is wired into pre-commit, so that whenever a `urls.py` file
changes in `sentry` we re-build the list in typescript.
For `getsentry`, because it's a separate repo, we can't have the same
automation to keep the files in sync :(. So that list is broken out into
it's own file and will need to manually be managed for now.

### Pre commit
I triggered the pre-commit step on any urls.py file becaues we have our
main `src/sentry/api/urls.py` file, but also a few places that
contribute imports like `src/sentry/workflow_engine/endpoints/urls.py`
and `src/sentry/notifications/platform/api/endpoints/urls.py`. I'd
rather not have these extra urls.py file at all, and have all api routes
be defined in one py file. But even if i go fix it now, someone is bound
to add an import to import their route definitions again in the future.

Ok, so we have a pre-commit. It will generate/update the ts file. 

### CI tasks

Changing the TS file does not trigger a full eslint/jest run in the
frontend CI. Also it does not add the "Scope: Frontend" label to the PR.
Only the TypeScript command is executed when the generated file changes.
This will catch cases where someone trys to remove an api that is still
in use (assuming all api call sites are statically typed, which they are
not yet). Adding a new api endpoint in python will trigger TS, but
shouldn't ever fail.

If someone manages to add a new endpoint and the ts file doesn't
re-generate then it won't be the end of the world. In the short term
we'll just re-gen the file.

### Testing the Script

There's about ~800 routes that the script identified. I got an llm to
generate all the regexp that splits url patterns into N strings. It
appears to be handling cases where a url segment is named, like
`/(?P<issue_id>[^/]+)/` or has alternates like `/(issue|group)/`.
With these examples working i didn't look through all 800 urls to
validate them, I also didn't look for anything that's missing. As we
statically type all our frontend code using this list, if something is
wrong we can revisit the script and fix it up.

There are however tests (llm generated) that are passing. 

### Running the script

The generated file has the command embedded inside it, and we can also
see from the pre-commit-config: `python3 -m
tools.api_urls_to_typescript` will do it.

It can also be run with a flag:
- `python3 -m tools.api_urls_to_typescript list` -> output to the
terminal a list of urls, no formatting

Related to #97155

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Add Python tool to generate TS typings for API URLs from Django URL
patterns, commit the generated file, add tests, and wire into pre-commit
and CI filters.
> 
> - **Tools / Codegen**:
> - Add `tools/api_urls_to_typescript.py` to derive routes from Django
`urlpatterns` and emit `static/app/api/urlpatterns.generated.ts` (with
`KnownApiUrls` and `MaybeApiPath`).
> - Supports listing routes via `python3 -m tools.api_urls_to_typescript
list`.
> - **Generated Artifacts**:
> - Add `static/app/api/urlpatterns.generated.ts` containing the
enumerated API paths.
> - **Tests**:
> - Add `tests/tools/test_api_urls_to_typescript.py` covering
`regexp_to_routes` (named groups, alternates, optional parts, complex
patterns).
> - **Pre-commit**:
> - Add `gen-ts-api-urls` hook to regenerate TS routes on `urls.py`
changes.
> - **CI / Filters**:
> - Update `.github/file-filters.yml` to broaden TS/JS globs and exclude
`*.generated.ts`; add frontend workflow anchor and include in triggers.
> - Update `frontend.yml` job outputs to expose
`typecheckable_rules_changed`.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
82c5d6a. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components Scope: Frontend Automatically applied to PRs that change frontend components WIP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants