: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
fix(tasks): skip errored GitHub installs in repo selection (#69336)
Harley Alexander83ee7b00b65d
diff --git a/products/signals/backend/test/test_resolve_team_github_integration.py b/products/signals/backend/test/test_resolve_team_github_integration.pyindex c172b2816518..ce0b9c94f4c8 100644--- a/products/signals/backend/test/test_resolve_team_github_integration.py+++ b/products/signals/backend/test/test_resolve_team_github_integration.py@@ -5,7 +5,7 @@ import pytestfrom django.utils import timezonefrom posthog.models import Organization, Team, User-from posthog.models.integration import GitHubIntegration, Integration+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, GitHubIntegration, Integrationfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.user_integration import UserGitHubIntegration, UserIntegration@@ -312,6 +312,30 @@ def test_keeps_user_integration_with_empty_unsynced_repository_cache(organizatioassert resolved.integration.id == integration.id+def test_skips_team_integration_whose_token_refresh_permanently_failed(organization, team):+ # An install uninstalled/suspended on GitHub's side keeps `errors=TOKEN_REFRESH_FAILED`.+ # Re-selecting it makes repo discovery storm GitHub with doomed refreshes, so it must be+ # skipped in favour of a healthy fallback.+ user = _create_user("[email protected]", organization)+ errored = _create_team_integration(team, integration_id="team-dead")+ Integration.objects.filter(pk=errored.pk).update(errors=ERROR_TOKEN_REFRESH_FAILED)+ user_int = _create_user_integration(user, integration_id="user-1")++ resolved = resolve_team_github_integration(team.id)++ assert isinstance(resolved, UserGitHubIntegration)+ assert resolved.integration.id == user_int.id+++def test_returns_none_when_only_team_github_has_failed_token_refresh(team):+ errored = _create_team_integration(team, integration_id="team-dead")+ Integration.objects.filter(pk=errored.pk).update(errors=ERROR_TOKEN_REFRESH_FAILED)++ assert resolve_team_github_integration(team.id) is None++@pytest.mark.django_dbdef test_prefers_org_account_over_user_account(organization, team):# Org accounts sort before User accounts on `config__account__type` (alphabetical).diff --git a/products/tasks/backend/logic/repo_selection/agent.py b/products/tasks/backend/logic/repo_selection/agent.pyindex 44aee2cdc8b7..b6b1899764c2 100644--- a/products/tasks/backend/logic/repo_selection/agent.py+++ b/products/tasks/backend/logic/repo_selection/agent.py@@ -8,7 +8,7 @@ from typing import TYPE_CHECKINGfrom django.db.models import Case, IntegerField, Value, Whenfrom posthog.models.github_integration_base import GitHubIntegrationBase-from posthog.models.integration import GitHubIntegration, Integration+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, GitHubIntegration, Integrationfrom posthog.models.integration_repository_cache import GitHubRepositoryFullCachefrom posthog.models.organization import OrganizationMembershipfrom posthog.models.team.team import Team@@ -112,6 +112,9 @@ def resolve_team_github_integration(Integration.objects.filter(team_id=team_id, kind="github")# Skip integrations whose installation has been synced and confirmed empty (0 repos).exclude(repository_cache=[], repository_cache_updated_at__isnull=False)+ # Skip installs whose token refresh is permanently failing (uninstalled/suspended on+ # GitHub's side): re-selecting one makes repo discovery storm GitHub with doomed refreshes.+ .exclude(errors=ERROR_TOKEN_REFRESH_FAILED)# Prioritize orgs vs users (alphabetically), then oldest first.order_by("config__account__type", "created_at", "id").first()