Save the user's GitHub organization#117
Conversation
|
|
||
| def _upsert_organizations( | ||
| *, session: Session, organizations: list[dict] | None | ||
| ) -> list[GitHubOrganization]: |
There was a problem hiding this comment.
https://docs.sqlalchemy.org/en/21/orm/session_state_management.html#merging
session.merge() looks usable here, but it identifies rows by primary key.
Our current primary key is a sequential ID (to match the other tables), so it doesn't work for this.
It looks like it would work if we set github_id as the primary key.
There was a problem hiding this comment.
Pull request overview
Adds GitHub organization persistence during GitHub OAuth login so the app can later configure/compute per-organization permissions for a signed-in user.
Changes:
- Expands GitHub OAuth scope to include
read:orgso org membership can be queried. - Fetches the user’s GitHub organizations during the OAuth callback and passes them into user create/update.
- Adds CRUD logic to upsert
GitHubOrganizationrows and associate them toUserGitHub.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| app/routers/login.py | Requests org-read scope and fetches /user/orgs during OAuth callback to collect organizations. |
| app/crud.py | Adds organization upsert + relationship assignment in create_user / update_user_github. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def _upsert_organizations( | ||
| *, session: Session, organizations: list[dict] | None | ||
| ) -> list[GitHubOrganization]: | ||
| if organizations is None: | ||
| return [] | ||
|
|
||
| existing = { | ||
| org.github_id: org | ||
| for org in session.exec( | ||
| select(GitHubOrganization).where( | ||
| GitHubOrganization.github_id.in_( | ||
| [org["github_id"] for org in organizations] | ||
| ) | ||
| ) | ||
| ).all() | ||
| } | ||
| github_organizations = [] | ||
| new_organizations = [] | ||
| for org in organizations: | ||
| organization = existing.get(org["github_id"]) | ||
| if organization is None: | ||
| organization = GitHubOrganization( | ||
| github_id=org["github_id"], login=org["login"] | ||
| ) | ||
| new_organizations.append(organization) | ||
| else: | ||
| # It is updated at commit. | ||
| organization.login = org["login"] | ||
| github_organizations.append(organization) | ||
| session.add_all(new_organizations) | ||
| return github_organizations |
There was a problem hiding this comment.
Does this remove left organizations?
There was a problem hiding this comment.
SQLAlchemy handles the update nicely.
I added unit tests to confirm this.
This is used to configure permissions for each organization.
9f331d9 to
4351131
Compare
This is used to configure permissions for each organization.