Skip to content

Commit bdec7dd

Browse files
committed
Move get_file_content logic to revision scope
1 parent 1598779 commit bdec7dd

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

bot/code_review_bot/__init__.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,9 @@ def hash(self):
198198
# format `obj-x86_64-pc-linux-gnu`
199199
file_content = None
200200
if "/obj-" not in self.path:
201-
if settings.mercurial_cache_checkout:
202-
logger.debug("Using the local repository to build issue's hash")
203-
try:
204-
with (settings.mercurial_cache_checkout / self.path).open() as f:
205-
file_content = f.read()
206-
except (FileNotFoundError, IsADirectoryError):
207-
logger.warning(
208-
"Failed to find issue's related file", path=self.path
209-
)
210-
file_content = None
211-
else:
212-
try:
213-
# Load all the lines affected by the issue
214-
file_content = self.revision.load_file(self.path)
215-
except ValueError:
216-
# Build the hash with an empty content in case the path is erroneous
217-
file_content = None
218-
except requests.exceptions.HTTPError as e:
219-
if e.response.status_code == 404:
220-
logger.warning(
221-
"Failed to download a file with an issue", path=self.path
222-
)
223-
224-
# We still build the hash with empty content
225-
file_content = None
226-
else:
227-
# When encountering another HTTP error, raise the issue
228-
raise
201+
file_content = self.revision.get_file_content(
202+
self.path, settings.mercurial_cache_checkout
203+
)
229204

230205
if file_content is None:
231206
self._hash = None

bot/code_review_bot/revisions/base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
from abc import ABC
88
from datetime import timedelta
9+
from pathlib import Path
910

1011
import rs_parsepatch
1112
import structlog
@@ -165,6 +166,25 @@ def contains(self, issue):
165166
lines = set(range(issue.line, issue.line + issue.nb_lines))
166167
return not lines.isdisjoint(modified_lines)
167168

169+
def get_file_content(
170+
self, file_path: str, local_cache_repository: Path | None = None
171+
):
172+
if local_cache_repository:
173+
logger.debug("Using the local repository to build issue's hash")
174+
try:
175+
with (local_cache_repository / file_path).open() as f:
176+
file_content = f.read()
177+
except (FileNotFoundError, IsADirectoryError):
178+
logger.warning("Failed to find issue's related file", path=file_path)
179+
file_content = None
180+
else:
181+
try:
182+
file_content = self.load_file(file_path)
183+
except ValueError:
184+
# The path is erroneous, consider as empty content
185+
file_content = None
186+
return file_content
187+
168188
@property
169189
def has_clang_files(self):
170190
"""

bot/code_review_bot/revisions/phabricator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,17 @@ def load_file(self, path):
362362
)
363363
logger.info("Downloading HGMO file", url=url)
364364

365-
response = requests.get(url, headers=GetAppUserAgent())
366-
response.raise_for_status()
365+
try:
366+
response = requests.get(url, headers=GetAppUserAgent())
367+
response.raise_for_status()
368+
except requests.exceptions.HTTPError as e:
369+
if e.response.status_code == 404:
370+
logger.warning("Failed to download file", path=self.path)
371+
# Consider as empty content if the file is not found
372+
return None
373+
else:
374+
# When encountering another HTTP error, raise the issue
375+
raise e
367376

368377
# Store in cache
369378
content = response.content.decode("utf-8")

0 commit comments

Comments
 (0)