-
Notifications
You must be signed in to change notification settings - Fork 14
github: add GitHubAPI, GitHubAPIClient classes (bug 1989637) #536
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
Merged
zzzeid
merged 1 commit into
zeid/bug-1989635-github-pr-pilot
from
zeid/bug-1989637-github-api-client
Oct 22, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import requests | ||
| from django.conf import settings | ||
| from simple_github import AppAuth, AppInstallationAuth | ||
|
|
||
| from lando.main.models.repo import Repo | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class GitHubAPI: | ||
| """A simple wrapper that authenticates with and communicates with the GitHub API.""" | ||
|
|
||
| GITHUB_BASE_URL = "https://api.github.com" | ||
|
|
||
| def __init__(self, repo: Repo): | ||
|
zzzeid marked this conversation as resolved.
|
||
| repo_owner = repo._github_repo_org | ||
| repo_name = repo.git_repo_name | ||
|
|
||
| self.session = requests.Session() | ||
| self.session.headers.update( | ||
| { | ||
| "Authorization": f"Bearer {self._get_token(repo_owner, repo_name)}", | ||
| "User-Agent": settings.HTTP_USER_AGENT, | ||
| "Accept": "application/vnd.github+json", | ||
| "X-GitHub-Api-Version": "2022-11-28", | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _get_token(repo_owner: str, repo_name: str) -> str | None: | ||
| """Obtain a fresh GitHub token to push to the specified repo. | ||
|
|
||
| This relies on GITHUB_APP_ID and GITHUB_APP_PRIVKEY to be set in the | ||
| environment. Returns None if those are missing. | ||
|
|
||
| The app with ID GITHUB_APP_ID needs to be enabled for the target repo. | ||
|
|
||
| """ | ||
| app_id = settings.GITHUB_APP_ID | ||
| private_key = settings.GITHUB_APP_PRIVKEY | ||
|
|
||
| if not app_id or not private_key: | ||
| logger.warning( | ||
| f"Missing GITHUB_APP_ID or GITHUB_APP_PRIVKEY to authenticate against GitHub repo {repo_owner}/{repo_name}", | ||
| ) | ||
| return None | ||
|
|
||
| app_auth = AppAuth( | ||
| app_id, | ||
| private_key, | ||
| ) | ||
| session = AppInstallationAuth(app_auth, repo_owner, repositories=[repo_name]) | ||
| return asyncio.run(session.get_token()) | ||
|
|
||
| def get(self, path: str, *args, **kwargs) -> dict: | ||
| """Send a GET request to the GitHub API with given args and kwargs.""" | ||
| url = f"{self.GITHUB_BASE_URL}/{path}" | ||
| return self.session.get(url, *args, **kwargs) | ||
|
|
||
| def post(self, path: str, *args, **kwargs) -> dict: | ||
| """Send a POST request to the GitHub API with given args and kwargs.""" | ||
| url = f"self.GITHUB_BASE_URL/{path}" | ||
| return self.session.post(url, *args, **kwargs) | ||
|
|
||
|
|
||
| class GitHubAPIClient: | ||
| """A convenience client that provides various methods to interact with the GitHub API.""" | ||
|
|
||
| client = None | ||
|
|
||
| def __init__(self, repo: Repo): | ||
| self.client = GitHubAPI(repo) | ||
| self.repo = repo | ||
| self.repo_base_url = ( | ||
| f"repos/{self.repo._github_repo_org}/{self.repo.git_repo_name}" | ||
| ) | ||
|
|
||
| def _get(self, path: str, *args, **kwargs) -> dict: | ||
| result = self.client.get(path, *args, **kwargs) | ||
| return result.json() | ||
|
|
||
| def list_pull_requests(self) -> list: | ||
| """List all pull requests in the repo.""" | ||
| return self._get(f"{self.repo_base_url}/pulls") | ||
|
|
||
| def get_pull_request(self, pull_number: int) -> dict: | ||
| """Get a specific pull request from the repo.""" | ||
| return self._get(f"{self.repo_base_url}/pulls/{pull_number}") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would no longer be needed with #604.