:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
refactor(user): simplify GitHub login source selection (#100269)
Paul D'Ambraec5b34519a73
diff --git a/posthog/models/user.py b/posthog/models/user.pyindex 8fe1ac994813..7ed911e1b2a4 100644--- a/posthog/models/user.py+++ b/posthog/models/user.py@@ -487,16 +487,7 @@ class User(AbstractUser, UUIDTClassicModel, ModelActivityMixin): # type: ignoreself.save(update_fields=["current_team"])return self.current_team- def get_github_login(self) -> str | None:- """Resolve this user's GitHub login.-- Precedence:- 1. `UserIntegration` (kind=github) — user's own GitHub integration- 2. `UserSocialAuth` (provider=github) — OAuth login linkage when no GitHub user integration exists- 3. Team-level `Integration` (kind=github) `connecting_user_github_login` — identity stored on the- team's GitHub integration (e.g. captured at install). Still a supported integration path,- lowest precedence as an identity fallback when (1)/(2) do not yield a GitHub username.- """+ def _get_github_login_from_user_integration(self) -> str | None:from posthog.models.user_integration import UserGitHubIntegrationprefetched_user_integrations = getattr(self, "_prefetched_github_user_integrations", None)@@ -510,6 +501,9 @@ class User(AbstractUser, UUIDTClassicModel, ModelActivityMixin): # type: ignoreif login:return login+ return None++ def _get_github_login_from_social_auth(self) -> str | None:for sa in self.social_auth.all():if sa.provider != "github":continue@@ -521,27 +515,42 @@ class User(AbstractUser, UUIDTClassicModel, ModelActivityMixin): # type: ignoreif login:return str(login)- # Team-level GitHub integration: connecting_user_github_login from install / configuration.+ return None++ def _get_github_login_from_team_integration(self) -> str | None:prefetched_integrations = getattr(self, "_prefetched_github_integrations", None)- if prefetched_integrations is not None:- for integration in prefetched_integrations:- login = (integration.config or {}).get("connecting_user_github_login")+ team_github_integrations = (+ prefetched_integrations+ if prefetched_integrations is not None+ else self.integration_set.filter(kind="github")+ .exclude(config__connecting_user_github_login=None)+ .only("config")+ .order_by("id")[:1]+ )+ for integration in team_github_integrations:+ if isinstance(integration.config, dict):+ login = integration.config.get("connecting_user_github_login")if login:return str(login)- else:- team_github_integration = (- self.integration_set.filter(kind="github")- .exclude(config__connecting_user_github_login=None)- .only("config")- .first()- )- if team_github_integration and isinstance(team_github_integration.config, dict):- login_val = team_github_integration.config.get("connecting_user_github_login")- if login_val:- return str(login_val)return None+ def get_github_login(self) -> str | None:+ """Resolve this user's GitHub login.++ Precedence:+ 1. `UserIntegration` (kind=github) — user's own GitHub integration+ 2. `UserSocialAuth` (provider=github) — OAuth login linkage when no GitHub user integration exists+ 3. Team-level `Integration` (kind=github) `connecting_user_github_login` — identity stored on the+ team's GitHub integration (e.g. captured at install). Still a supported integration path,+ lowest precedence as an identity fallback when (1)/(2) do not yield a GitHub username.+ """+ return (+ self._get_github_login_from_user_integration()+ or self._get_github_login_from_social_auth()+ or self._get_github_login_from_team_integration()+ )+def join(self,*,