-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(integrations): Clarify which endpoints gets called #113196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,19 +65,7 @@ def build_repository_config( | |
| "integration_id": data["integration_id"], | ||
| } | ||
|
|
||
| def compare_commits( | ||
| self, repo: Repository, start_sha: str | None, end_sha: str | ||
| ) -> Sequence[Mapping[str, Any]]: | ||
| def eval_commits(client: Any) -> Sequence[Mapping[str, Any]]: | ||
| # use config name because that is kept in sync via webhooks | ||
| name = repo.config["name"] | ||
| if start_sha is None: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the difference between calling one endpoint vs the other one. |
||
| res = client.get_last_commits(name, end_sha) | ||
| return self._format_commits(client, name, res[:20]) | ||
| else: | ||
| commits = client.compare_commits(name, start_sha, end_sha) | ||
| return self._format_commits(client, name, commits) | ||
|
|
||
| def _get_installation_and_client(self, repo: Repository) -> tuple[IntegrationInstallation, Any]: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're adding two new functions and they will share this logic. |
||
| integration_id = repo.integration_id | ||
| if integration_id is None: | ||
| raise NotImplementedError("GitHub apps requires an integration id to fetch commits") | ||
|
|
@@ -90,13 +78,39 @@ def eval_commits(client: Any) -> Sequence[Mapping[str, Any]]: | |
| ) | ||
|
|
||
| installation = integration.get_installation(organization_id=repo.organization_id) | ||
| client = installation.get_client() | ||
| return installation, installation.get_client() | ||
|
|
||
| def fetch_recent_commits(self, repo: Repository, end_sha: str) -> Sequence[Mapping[str, Any]]: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one focuses on getting the last 20 commits. Not an idempotent call. |
||
| installation, client = self._get_installation_and_client(repo) | ||
| # use config name because that is kept in sync via webhooks | ||
| name = repo.config["name"] | ||
|
|
||
| try: | ||
| commits = client.get_last_commits(name, end_sha) | ||
| return self._format_commits(client, name, commits[:20]) | ||
|
Comment on lines
+89
to
+90
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: could we update the client to take a pagination size of 20 vs retrieving 30 and trimming the number down? https://docs.github.com/en/rest/commits/commits?apiVersion=2026-03-10#list-commits-on-a-repository |
||
| except Exception as e: | ||
| installation.raise_error(e) | ||
|
|
||
| def fetch_commits_for_compare_range( | ||
| self, repo: Repository, start_sha: str, end_sha: str | ||
| ) -> Sequence[Mapping[str, Any]]: | ||
| installation, client = self._get_installation_and_client(repo) | ||
| # use config name because that is kept in sync via webhooks | ||
| name = repo.config["name"] | ||
|
|
||
| try: | ||
| return eval_commits(client) | ||
| commits = client.compare_commits(name, start_sha, end_sha) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an idempotent call. In the next PR I will be moving the caching logic here. |
||
| return self._format_commits(client, name, commits) | ||
| except Exception as e: | ||
| installation.raise_error(e) | ||
|
|
||
| def compare_commits( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for backward compatibility. |
||
| self, repo: Repository, start_sha: str | None, end_sha: str | ||
| ) -> Sequence[Mapping[str, Any]]: | ||
| if start_sha is None: | ||
| return self.fetch_recent_commits(repo, end_sha) | ||
| return self.fetch_commits_for_compare_range(repo, start_sha, end_sha) | ||
|
|
||
| def _format_commits( | ||
| self, | ||
| client: Any, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,13 +101,13 @@ def get_github_compare_commits_cache_key( | |
| return f"fetch-commits:compare-commits:v1:{digest}" | ||
|
|
||
|
|
||
| def fetch_compare_commits( | ||
| def fetch_commits_for_compare_range( | ||
| *, | ||
| cache_enabled: bool, | ||
| repo: Repository, | ||
| provider: Any, | ||
| is_integration_repo_provider: bool, | ||
| start_sha: str | None, | ||
| start_sha: str, | ||
| end_sha: str, | ||
| user: RpcUser | None, | ||
| lifecycle: Any, | ||
|
|
@@ -127,7 +127,11 @@ def fetch_compare_commits( | |
| lifecycle.add_extra("compare_commits_cache_enabled", False) | ||
|
|
||
| if is_integration_repo_provider: | ||
| repo_commits = provider.compare_commits(repo, start_sha, end_sha) | ||
| # New method to decouple the provider from the task | ||
| if hasattr(provider, "fetch_commits_for_compare_range"): | ||
| repo_commits = provider.fetch_commits_for_compare_range(repo, start_sha, end_sha) | ||
| else: | ||
| repo_commits = provider.compare_commits(repo, start_sha, end_sha) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For backward compatibility. |
||
| else: | ||
| # XXX: This only works for plugins that support actor context | ||
| repo_commits = provider.compare_commits(repo, start_sha, end_sha, actor=user) | ||
|
|
@@ -141,6 +145,23 @@ def fetch_compare_commits( | |
| return repo_commits | ||
|
|
||
|
|
||
| def fetch_recent_commits( | ||
| *, | ||
| repo: Repository, | ||
| provider: Any, | ||
| is_integration_repo_provider: bool, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be standardized across provider clients consistently? Mainly asking out of ignorance for GitLab and Bitbucket's APIs. |
||
| end_sha: str, | ||
| user: RpcUser | None, | ||
| ) -> list[dict[str, Any]]: | ||
| if is_integration_repo_provider: | ||
| if hasattr(provider, "fetch_recent_commits"): | ||
| return provider.fetch_recent_commits(repo, end_sha) | ||
| return provider.compare_commits(repo, None, end_sha) | ||
|
|
||
| # XXX: This only works for plugins that support actor context | ||
| return provider.compare_commits(repo, None, end_sha, actor=user) | ||
|
|
||
|
|
||
| def get_repo_and_provider_for_ref( | ||
| *, | ||
| release: Release, | ||
|
|
@@ -260,23 +281,31 @@ def fetch_commits( | |
| } | ||
| ) | ||
| try: | ||
| provider_name = repo.provider | ||
| compare_commits_cache_enabled = ( | ||
| github_compare_commits_cache_feature_enabled | ||
| and isinstance(provider_name, str) | ||
| and provider_name in GITHUB_CACHEABLE_REPOSITORY_PROVIDERS | ||
| and start_sha is not None | ||
| ) | ||
| repo_commits = fetch_compare_commits( | ||
| cache_enabled=compare_commits_cache_enabled, | ||
| repo=repo, | ||
| provider=provider, | ||
| is_integration_repo_provider=is_integration_repo_provider, | ||
| start_sha=start_sha, | ||
| end_sha=end_sha, | ||
| user=user, | ||
| lifecycle=lifecycle, | ||
| ) | ||
| if start_sha is None: | ||
| repo_commits = fetch_recent_commits( | ||
| repo=repo, | ||
| provider=provider, | ||
| is_integration_repo_provider=is_integration_repo_provider, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixAdd a Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is sort of an anti-pattern anyway. Function calls like this shouldn't necessarily be coupled to the metrics in the top-level tasks imo. |
||
| end_sha=end_sha, | ||
| user=user, | ||
| ) | ||
| else: | ||
| provider_name = repo.provider | ||
| compare_commits_cache_enabled = ( | ||
| github_compare_commits_cache_feature_enabled | ||
| and isinstance(provider_name, str) | ||
| and provider_name in GITHUB_CACHEABLE_REPOSITORY_PROVIDERS | ||
| ) | ||
| repo_commits = fetch_commits_for_compare_range( | ||
| cache_enabled=compare_commits_cache_enabled, | ||
| repo=repo, | ||
| provider=provider, | ||
| is_integration_repo_provider=is_integration_repo_provider, | ||
| start_sha=start_sha, | ||
| end_sha=end_sha, | ||
| user=user, | ||
| lifecycle=lifecycle, | ||
| ) | ||
| except NotImplementedError: | ||
| pass | ||
| except IntegrationResourceNotFoundError: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function would call two different endpoints:
compare/and sometimescommit/.In a sense, we're not doing any comparison of commits, thus, even making the name more confusing.