: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): fall back from stale user github token (#57606)
Alessandro Pogliaghia47643c52dcf
diff --git a/products/tasks/backend/temporal/process_task/tests/test_utils.py b/products/tasks/backend/temporal/process_task/tests/test_utils.pyindex 763b1d68f569..9fbf6cfaef6c 100644--- a/products/tasks/backend/temporal/process_task/tests/test_utils.py+++ b/products/tasks/backend/temporal/process_task/tests/test_utils.py@@ -346,8 +346,8 @@ class TestGetSandboxGitHubToken(TestCase):("cached_token_wins", "ghu_cached", True, "ghu_user", None, "ghu_cached"),("identity_token", None, True, "ghu_user", None, "ghu_user"),("missing_identity_falls_back_to_team_token", None, False, None, "missing", "ghs_team"),- ("identity_requires_reauthorization", None, True, None, "reauthorization", None),- ("identity_without_token_requires_reauthorization", None, True, None, "empty_token", None),+ ("identity_reauthorization_falls_back_to_team_token", None, True, None, "reauthorization", "ghs_team"),+ ("identity_without_token_falls_back_to_team_token", None, True, None, "empty_token", "ghs_team"),])@patch("products.tasks.backend.temporal.process_task.utils.get_cached_github_user_token")@@ -376,23 +376,14 @@ class TestGetSandboxGitHubToken(TestCase):identity.get_usable_user_access_token.return_value = identity_tokenmock_get_identity.return_value = identity if has_identity else None- if error_case in ("reauthorization", "empty_token"):- with self.assertRaises(ReauthorizationRequired):- get_sandbox_github_token(- 123,- run_id="run-1",- state={"pr_authorship_mode": "user"},- created_by=creator,- )- else:- mock_get_github_token.return_value = expected_token- result = get_sandbox_github_token(- 123,- run_id="run-1",- state={"pr_authorship_mode": "user"},- created_by=creator,- )- assert result == expected_token+ mock_get_github_token.return_value = expected_token+ result = get_sandbox_github_token(+ 123,+ run_id="run-1",+ state={"pr_authorship_mode": "user"},+ created_by=creator,+ )+ assert result == expected_tokenmock_cached.assert_called_once_with("run-1")if cached_token:@@ -407,11 +398,43 @@ class TestGetSandboxGitHubToken(TestCase):)if has_identity:identity.get_usable_user_access_token.assert_called_once()- if error_case == "missing":+ if error_case in ("missing", "reauthorization", "empty_token"):mock_get_github_token.assert_called_once_with(123)else:mock_get_github_token.assert_not_called()+ @parameterized.expand(+ [+ ("reauthorization",),+ ("empty_token",),+ ]+ )+ @patch("products.tasks.backend.temporal.process_task.utils.get_cached_github_user_token")+ @patch("products.tasks.backend.temporal.process_task.utils.get_user_github_integration")+ def test_user_authorship_requires_reauthorization_without_team_fallback(+ self,+ error_case: str,+ mock_get_identity: MagicMock,+ mock_cached: MagicMock,+ ) -> None:+ from posthog.models.user_integration import ReauthorizationRequired++ mock_cached.return_value = None+ identity = MagicMock()+ if error_case == "reauthorization":+ identity.get_usable_user_access_token.side_effect = ReauthorizationRequired("reauthorize GitHub")+ else:+ identity.get_usable_user_access_token.return_value = None+ mock_get_identity.return_value = identity++ with self.assertRaises(ReauthorizationRequired):+ get_sandbox_github_token(+ None,+ run_id="run-1",+ state={"pr_authorship_mode": "user"},+ created_by=MagicMock(name="creator"),+ )+@patch("products.tasks.backend.temporal.process_task.utils.get_github_token")def test_bot_authorship_uses_installation_token(self, mock_get_github_token) -> None:mock_get_github_token.return_value = "ghs_bot"diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.pyindex 5f7134dcfff5..f1b4422ebaf3 100644--- a/products/tasks/backend/temporal/process_task/utils.py+++ b/products/tasks/backend/temporal/process_task/utils.py@@ -527,13 +527,20 @@ def get_sandbox_github_token(raise ReauthorizationRequired(f"User-authored run {run_id} requires a linked GitHub account with repo access.")- else:+ elif github_integration_id is None:token = user_github_integration.get_usable_user_access_token()if token is None:raise ReauthorizationRequired(f"User-authored run {run_id} requires a linked GitHub account with repo access.")return token+ else:+ try:+ token = user_github_integration.get_usable_user_access_token()+ except ReauthorizationRequired:+ token = None+ if token is not None:+ return tokenif github_integration_id is None:return None