Skip to content

: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.

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.py
index 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 pytest
from django.utils import timezone
from posthog.models import Organization, Team, User
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, GitHubIntegration, Integration
from posthog.models.organization import OrganizationMembership
from posthog.models.user_integration import UserGitHubIntegration, UserIntegration
@@ -312,6 +312,30 @@ def test_keeps_user_integration_with_empty_unsynced_repository_cache(organizatio
assert 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_db
def 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.py
index 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_CHECKING
from django.db.models import Case, IntegerField, Value, When
from posthog.models.github_integration_base import GitHubIntegrationBase
-from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, GitHubIntegration, Integration
from posthog.models.integration_repository_cache import GitHubRepositoryFullCache
from posthog.models.organization import OrganizationMembership
from 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()