: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(github): show a clean message when automatic webhook setup fails (#117439)
Tom Owers99b3b0bad547
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.pyindex c059ea44ef6c..9f08723570e3 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py@@ -28,6 +28,7 @@ from posthog.egress.github.transport import (raise_if_github_rate_limited,)from posthog.egress.limiter.policies import Priority+from posthog.temporal.common.logger import get_loggerfrom products.warehouse_sources.backend.temporal.data_imports.pipelines.core.batcher import Batcherfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.base import (@@ -84,6 +85,8 @@ _WEBHOOK_PERMISSION_HINT = ("or repository webhook access on the GitHub app installation")+LOGGER = get_logger(__name__)+class GithubRetryableError(Exception):pass@@ -2060,6 +2063,18 @@ def _is_repo_hook_permission_error(response: requests.Response) -> bool:return response.status_code in (403, 404)+def _webhook_failure_message(action: Literal["create", "update"], response: requests.Response | None = None) -> str:+ """User-facing message for a repo webhook call that failed, without GitHub's status or raw body."""+ # GitHub rejects a hook past its per-repo limit with a 422 whose errors[] message reads+ # 'The "push" event cannot have more than 20 hooks'.+ if response is not None and "cannot have more than" in response.text.lower():+ return (+ "This repository has reached GitHub's webhook limit. Delete an unused webhook in the "+ "repository settings and retry, or set it up manually following the steps below."+ )+ return f"GitHub couldn't {action} the webhook. Retry, or set it up manually following the steps below."++def create_repo_webhook(token: str,repo: str,@@ -2105,7 +2120,8 @@ def create_repo_webhook(error="GitHub rate limit reached while creating the repository webhook; please retry shortly.",)except requests.exceptions.RequestException as e:- return WebhookCreationResult(success=False, error=f"Failed to create webhook automatically: {e}")+ LOGGER.warning("Failed to reach GitHub to create a repository webhook", error=str(e))+ return WebhookCreationResult(success=False, error=_webhook_failure_message("create"))if response.status_code in (200, 201):# GitHub never echoes the secret back, so we return the one we generated as@@ -2122,9 +2138,10 @@ def create_repo_webhook(),)- return WebhookCreationResult(- success=False, error=f"Failed to create webhook automatically: {response.status_code} {response.text}"+ LOGGER.warning(+ "GitHub rejected a repository webhook create", status_code=response.status_code, body=response.text[:500])+ return WebhookCreationResult(success=False, error=_webhook_failure_message("create", response))def ensure_repo_webhook(@@ -2152,7 +2169,8 @@ def ensure_repo_webhook(),)if error is not None:- return WebhookCreationResult(success=False, error=f"Failed to create webhook automatically: {error}")+ LOGGER.warning("Failed to list repository webhooks before creating one", error=error)+ return WebhookCreationResult(success=False, error=_webhook_failure_message("create"))hook = _match_hook_by_url(hooks or [], webhook_url)if hook is None:@@ -2184,7 +2202,8 @@ def ensure_repo_webhook(error="GitHub rate limit reached while updating the repository webhook; please retry shortly.",)except requests.exceptions.RequestException as e:- return WebhookCreationResult(success=False, error=f"Failed to update webhook automatically: {e}")+ LOGGER.warning("Failed to reach GitHub to update a repository webhook", error=str(e))+ return WebhookCreationResult(success=False, error=_webhook_failure_message("update"))if response.ok:return WebhookCreationResult(success=True, extra_inputs={"signing_secret": secret})@@ -2196,9 +2215,10 @@ def ensure_repo_webhook("Add it and reconnect, or set up the webhook manually following the steps below."),)- return WebhookCreationResult(- success=False, error=f"Failed to update webhook automatically: {response.status_code} {response.text}"+ LOGGER.warning(+ "GitHub rejected a repository webhook update", status_code=response.status_code, body=response.text[:500])+ return WebhookCreationResult(success=False, error=_webhook_failure_message("update", response))def _list_repo_hooks(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.pyindex d0d8e2250127..227c60107ebb 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py@@ -961,7 +961,7 @@ If automatic creation failed with a permissions error, the fix depends on how yo# against it, and the failed repos' manual setup instructions tell the user to reuse it.return WebhookCreationResult(success=False,- error="Failed to create the webhook in some repositories — " + "; ".join(failures),+ error="Couldn't create the webhook in some repositories. " + "; ".join(failures),extra_inputs={"signing_secret": secret} if any_success else {},)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_source_class.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_source_class.pyindex 438ae7ab8ee8..a5568deb3ee5 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_source_class.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_source_class.py@@ -12,6 +12,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.intfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (ScriptedResponse,SourceDriver,+ scripted_network,)from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.github import (GithubAuthMethodConfig,@@ -823,3 +824,31 @@ class TestGithubSource:assert result.error is not None and "c/d: boom" in result.error# The repo that did get a hook verifies against this secret, so it must still persist.assert result.extra_inputs.get("signing_secret")++ @pytest.mark.parametrize(+ "rejection, expected_message",+ [+ (+ ScriptedResponse(+ status=422,+ json={+ "message": "Validation Failed",+ "errors": [{"resource": "Hook", "message": 'The "push" event cannot have more than 20 hooks'}],+ },+ ),+ "This repository has reached GitHub's webhook limit.",+ ),+ (+ ScriptedResponse(status=422, json={"message": "Validation Failed", "errors": [{"code": "custom"}]}),+ "GitHub couldn't create the webhook.",+ ),+ ],+ )+ def test_create_webhook_rejection_shows_a_clean_message(self, rejection, expected_message):+ with scripted_network([ScriptedResponse(status=200, json=[]), rejection]):+ result = self.source.create_webhook(_pat_config(repository="o/r"), "https://hooks/x", self.team_id)++ assert result.success is False+ assert result.error is not None and expected_message in result.error+ assert "422" not in result.error+ assert "Validation Failed" not in result.error