-
Notifications
You must be signed in to change notification settings - Fork 0
Added additional metadata to diff output #46
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
bdf76be
45ca911
ce0aac8
7222aee
a3170a1
f1daf32
d94df0b
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from importlib.metadata import PackageNotFoundError, version | ||
|
|
||
| try: | ||
| __version__ = version("diff-poetry-lock") | ||
| except PackageNotFoundError: | ||
| __version__ = "dev" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import base64 | ||
|
|
||
| import requests | ||
| from loguru import logger | ||
| from pydantic import BaseModel, Field, parse_obj_as | ||
| from requests import Response | ||
|
|
||
| from diff_poetry_lock.settings import PrLookupConfigurable, Settings | ||
|
|
||
|
|
@@ -30,6 +31,7 @@ class GithubApi: | |
| def __init__(self, settings: Settings) -> None: | ||
| self.s = settings | ||
| self.session = requests.session() | ||
| self._ref_hash_cache: dict[str, str] = {} | ||
| if isinstance(self.s, PrLookupConfigurable): | ||
| self.s.set_pr_lookup_service(self) | ||
|
|
||
|
|
@@ -45,7 +47,7 @@ def post_comment(self, comment: str) -> None: | |
| logger.debug("Posting comment to PR #{}", self.s.pr_num) | ||
| r = self.session.post( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/issues/{self.s.pr_num}/comments", | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github+json"}, | ||
| headers=self.api_headers(), | ||
| json={"body": f"{MAGIC_COMMENT_IDENTIFIER}{comment}"}, | ||
| timeout=10, | ||
| ) | ||
|
|
@@ -56,7 +58,7 @@ def update_comment(self, comment_id: int, comment: str) -> None: | |
| logger.debug("Updating comment {}", comment_id) | ||
| r = self.session.patch( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/issues/comments/{comment_id}", | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github+json"}, | ||
| headers=self.api_headers(), | ||
| json={"body": f"{MAGIC_COMMENT_IDENTIFIER}{comment}"}, | ||
| timeout=10, | ||
| ) | ||
|
|
@@ -74,7 +76,7 @@ def list_comments(self) -> list[GithubComment]: | |
| r = self.session.get( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/issues/{self.s.pr_num}/comments", | ||
| params={"per_page": 100, "page": page}, | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github+json"}, | ||
| headers=self.api_headers(), | ||
| timeout=10, | ||
| ) | ||
| r.raise_for_status() | ||
|
|
@@ -84,28 +86,47 @@ def list_comments(self) -> list[GithubComment]: | |
| logger.debug("Found %d comments", len(all_comments)) | ||
| return [c for c in all_comments if c.is_diff_comment()] | ||
|
|
||
| def get_file(self, ref: str) -> Response: | ||
| def get_file(self, ref: str) -> bytes: | ||
| logger.debug("Fetching {} from ref {}", self.s.lockfile_path, ref) | ||
|
|
||
| r = self.session.get( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/contents/{self.s.lockfile_path}", | ||
| params={"ref": ref}, | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github.raw"}, | ||
| headers=self.api_headers(), | ||
| timeout=10, | ||
| stream=True, | ||
| ) | ||
| logger.debug("Response status: {}", r.status_code) | ||
|
|
||
| if r.status_code == 404: | ||
| raise FileNotFoundError(self.s.lockfile_path) from RepoFileRetrievalError(self.s.repository, ref) | ||
| r.raise_for_status() | ||
| return r | ||
| file_obj = r.json() | ||
|
|
||
| resolved_hash = str(file_obj.get("sha", "")).strip() | ||
| if resolved_hash: | ||
| self._ref_hash_cache[ref] = resolved_hash | ||
| logger.debug("Cached commit hash for ref {} from contents sha", ref) | ||
|
|
||
| encoded_content = file_obj.get("content", "") | ||
|
Comment on lines
+103
to
+110
Collaborator
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. thought: This is 100% fine for now but we're inching closer to wanting/needing some kind of abstraction, like a Github API library. Octocat was the go-to in the Ruby ecosystem. I don't know what's the analogue in the Python world.
Collaborator
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. Sure. Created a new issue to track and implement it in #47 |
||
| if not isinstance(encoded_content, str): | ||
| msg = "Invalid content returned from GitHub contents API" | ||
| raise TypeError(msg) | ||
|
|
||
| return base64.b64decode(encoded_content) | ||
colindean marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def resolve_commit_hash(self, ref: str) -> str: | ||
| if cached_hash := self._ref_hash_cache.get(ref): | ||
| logger.debug("Using cached commit hash for ref {}", ref) | ||
| return cached_hash | ||
|
|
||
| logger.warning("No cached commit hash for ref {}, falling back to ref", ref) | ||
| return ref | ||
|
|
||
| def delete_comment(self, comment_id: int) -> None: | ||
| logger.debug("Deleting comment {}", comment_id) | ||
| r = self.session.delete( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/issues/comments/{comment_id}", | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github+json"}, | ||
| headers=self.api_headers(), | ||
| ) | ||
| logger.debug("Response status: {}", r.status_code) | ||
| r.raise_for_status() | ||
|
|
@@ -122,7 +143,7 @@ def find_pr_for_branch(self, branch_ref: str) -> str: | |
| r = self.session.get( | ||
| f"{self.s.api_url}/repos/{self.s.repository}/pulls", | ||
| params={"head": head, "state": "open"}, | ||
| headers={"Authorization": f"token {self.s.token}", "Accept": "application/vnd.github+json"}, | ||
| headers=self.api_headers(), | ||
| timeout=10, | ||
| ) | ||
| logger.debug("Response status: {}", r.status_code) | ||
|
|
@@ -137,6 +158,13 @@ def find_pr_for_branch(self, branch_ref: str) -> str: | |
| logger.debug("No open PR found for branch {}", branch) | ||
| return "" | ||
|
|
||
| def api_headers(self) -> dict[str, str]: | ||
| return self.request_headers(self.s.token) | ||
|
|
||
| @staticmethod | ||
| def request_headers(token: str) -> dict[str, str]: | ||
| return {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"} | ||
|
|
||
| def upsert_comment(self, existing_comment: GithubComment | None, comment: str | None) -> None: | ||
| if existing_comment is None and comment is None: | ||
| return | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.