Skip to content

Commit 4b14174

Browse files
authored
fix: handle non-base64 encodings for github file contents (#139)
1 parent ee52248 commit 4b14174

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/lgtm_ai/git_client/github.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def get_diff_from_url(self, pr_url: PRUrl) -> PRDiff:
5757
try:
5858
parsed_diff = parse_diff_patch(metadata=metadata, diff_text=file.patch or "")
5959
except GitDiffParseError:
60-
logger.exception("Failed to parse diff patch for file %s, will skip it", file.filename)
60+
logger.exception(
61+
"Failed to parse diff patch for file %s, will skip it",
62+
file.filename,
63+
)
6164
continue
6265
parsed.append(parsed_diff)
6366

@@ -119,7 +122,11 @@ def get_issue_content(self, issues_url: HttpUrl, issue_id: str) -> IssueContent
119122
repo = _get_repo_from_issues_url(self.client, issues_url)
120123
issue = repo.get_issue(int(issue_id))
121124
except (github.GithubException, ValueError) as err:
122-
logger.error("Failed to retrieve the issue content from GitHub for issue %s: %s", issue_id, err)
125+
logger.warning(
126+
"Failed to retrieve the issue content from GitHub for issue %s: %s",
127+
issue_id,
128+
err,
129+
)
123130
return None
124131

125132
return IssueContent(
@@ -146,7 +153,7 @@ def get_file_contents(self, pr_url: PRUrl, file_path: str, branch_name: ContextB
146153
try:
147154
file_contents = repo.get_contents(file_path, ref=pr.head.ref if branch_name == "source" else pr.base.ref)
148155
except github.GithubException as err:
149-
logger.error(
156+
logger.warning(
150157
"Failed to retrieve file %s from GitHub branch %s, error: %s",
151158
file_path,
152159
branch_name,
@@ -168,9 +175,9 @@ def get_file_contents(self, pr_url: PRUrl, file_path: str, branch_name: ContextB
168175
)
169176
return None
170177
decoded_chunk_content = decoded_bytes.decode("utf-8")
171-
except (binascii.Error, UnicodeDecodeError):
172-
logger.error(
173-
"Failed to decode file %s from GitHub sha: %s, ignoring...",
178+
except (binascii.Error, UnicodeDecodeError, AssertionError):
179+
logger.warning(
180+
"Failed to get decoded content for file %s from branch %s, ignoring...",
174181
file_path,
175182
branch_name,
176183
)

src/lgtm_ai/git_client/gitlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_issue_content(self, issues_url: HttpUrl, issue_id: str) -> IssueContent
7171
project = _get_project_from_issues_url(self.client, issues_url)
7272
issue = project.issues.get(issue_id)
7373
except (gitlab.exceptions.GitlabError, ValueError):
74-
logger.error("Failed to retrieve the issue content from GitLab for issue %s", issue_id)
74+
logger.warning("Failed to retrieve the issue content from GitLab for issue %s", issue_id)
7575
return None
7676

7777
return IssueContent(

src/lgtm_ai/review/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def get_code_context(self, target: PRUrl | LocalRepository, pr_diff: PRDiff) ->
4747
context = PRCodeContext(file_contents=[])
4848
branch: ContextBranch = "source"
4949
for file_path in pr_diff.changed_files:
50+
logger.debug("Fetching content for file %s", file_path)
5051
if self._git_client and isinstance(target, PRUrl):
5152
branch = "source"
5253
content = self._git_client.get_file_contents(file_path=file_path, pr_url=target, branch_name=branch)

0 commit comments

Comments
 (0)