Skip to content

Save the user's GitHub organization#117

Open
abetomo wants to merge 1 commit into
enactic:mainfrom
abetomo:save-github-orgs
Open

Save the user's GitHub organization#117
abetomo wants to merge 1 commit into
enactic:mainfrom
abetomo:save-github-orgs

Conversation

@abetomo

@abetomo abetomo commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

This is used to configure permissions for each organization.

Copilot AI review requested due to automatic review settings July 16, 2026 03:15
Comment thread app/crud.py

def _upsert_organizations(
*, session: Session, organizations: list[dict] | None
) -> list[GitHubOrganization]:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:org so 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 GitHubOrganization rows and associate them to UserGitHub.

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.

Comment thread app/routers/login.py Outdated
Comment thread app/crud.py Outdated
Comment thread app/crud.py Outdated
Comment thread app/crud.py
Comment thread app/crud.py
Comment on lines +60 to +90
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this remove left organizations?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

SQLAlchemy handles the update nicely.
I added unit tests to confirm this.

This is used to configure permissions for each organization.
@abetomo
abetomo force-pushed the save-github-orgs branch from 9f331d9 to 4351131 Compare July 16, 2026 04:37
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.

3 participants