: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): say what to check when a repository is not accessible (#95743)
Tom Owers70009009bb35
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 19cc4a67ae75..b27de9dc59dd 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@@ -361,6 +361,12 @@ def _should_stop_desc(return any(_is_older_than_cutoff(item.get(incremental_field), cutoff) for item in data if item)+# GitHub answers 404 both for a repository that doesn't exist and for one the token can't see, so+# the reason can't tell the two apart. The source layer matches this text to append the next step+# once for the whole batch instead of repeating it per repository.+REPOSITORY_NOT_ACCESSIBLE_REASON = "not found or not accessible"++def validate_credentials(personal_access_token: str, repository: str, api_version: str = GITHUB_DEFAULT_API_VERSION) -> tuple[bool, str | None]:@@ -392,7 +398,7 @@ def validate_credentials(return False, "Invalid personal access token"if response.status_code == 404:- return False, f"Repository '{repository}' not found or not accessible"+ return False, f"Repository '{repository}' {REPOSITORY_NOT_ACCESSIBLE_REASON}"try:body = response.json()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 e98d840ab13f..354bea59afe1 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@@ -50,6 +50,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_from products.warehouse_sources.backend.temporal.data_imports.sources.github.github import (_ORG_PERMISSION_REASON,ORG_SCOPED_ENDPOINTS,+ REPOSITORY_NOT_ACCESSIBLE_REASON,GithubEgressIdentity,GithubResumeConfig,check_org_endpoint_permission,@@ -133,6 +134,17 @@ GITHUB_WEBHOOK_EVENT_CHECKLIST: str = "\n".join(# needs its own reshaping branch before its webhook rows would match what the poll path writes.+_REPOSITORY_ACCESS_GUIDANCE = "Check the spelling and that your token has read access."+++def _join_validation_failures(failures: list[str]) -> str:+ """Join the per-repository failures, with one next step for the whole list rather than one each."""+ joined = "; ".join(failures)+ if any(REPOSITORY_NOT_ACCESSIBLE_REASON in failure for failure in failures):+ return f"{joined}. {_REPOSITORY_ACCESS_GUIDANCE}"+ return joined++@SourceRegistry.registerclass GithubSource(ResumableSource[GithubSourceConfig, GithubResumeConfig],@@ -645,9 +657,9 @@ If automatic creation failed with a permissions error, the fix depends on how yo# A 401 is token-level — probing further repos yields the same answer.if message == "Invalid personal access token":return False, message- failures.append(message or f"Repository '{repository}' not found or not accessible")+ failures.append(message or f"Repository '{repository}' {REPOSITORY_NOT_ACCESSIBLE_REASON}")if failures:- return False, "; ".join(failures)+ return False, _join_validation_failures(failures)return True, Noneexcept Exception as e:# `_get_access_token` and the OAuth mixin raise deterministic config/credential errorsdiff --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 6dedab231c14..bbc94361470c 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@@ -642,6 +642,36 @@ class TestGithubSource:assert valid is Falseassert message is not None and "a/missing" in message+ assert message.count("read access") == 1++ @mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.github.source.validate_github_credentials"+ )+ def test_validate_credentials_states_the_next_step_once_for_several_repos(self, mock_validate):+ # Guards against the next step moving back into the per-repo reason, where N inaccessible+ # repos would read as the same sentence N times.+ mock_validate.side_effect = [+ (False, "Repository 'a/one' not found or not accessible"),+ (False, "Repository 'a/two' not found or not accessible"),+ ]++ valid, message = self.source.validate_credentials(_pat_config(repositories=["a/one", "a/two"]), self.team_id)++ assert valid is False+ assert message is not None+ assert "a/one" in message and "a/two" in message+ assert message.count("read access") == 1++ @mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.github.source.validate_github_credentials"+ )+ def test_validate_credentials_omits_repo_guidance_for_other_failures(self, mock_validate):+ mock_validate.side_effect = [(False, "Validation failed")]++ valid, message = self.source.validate_credentials(_pat_config(repositories=["a/one"]), self.team_id)++ assert valid is False+ assert message == "Validation failed"@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.github.source.validate_github_credentials"