Skip to content

Commit 79080d7

Browse files
fix(harvester): address review feedback
1 parent eaa6ae9 commit 79080d7

6 files changed

Lines changed: 37 additions & 29 deletions

File tree

application/tests/harvester_test/diff_pipeline_test.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import UTC, datetime
2+
import subprocess
23
import time
34
import unittest
45

@@ -22,6 +23,23 @@ def test_pipeline_benchmark(self):
2223
"ASVS",
2324
"master",
2425
)
26+
client.sync()
27+
28+
head_commit = client.get_current_commit_sha()
29+
30+
previous_commit = subprocess.run(
31+
[
32+
"git",
33+
"-C",
34+
str(client.get_local_path()),
35+
"rev-parse",
36+
"HEAD~1",
37+
],
38+
check=True,
39+
capture_output=True,
40+
text=True,
41+
timeout=300,
42+
).stdout.strip()
2543

2644
retriever = DiffRetriever(client)
2745
parser = DiffParser()
@@ -30,14 +48,14 @@ def test_pipeline_benchmark(self):
3048
start = time.perf_counter()
3149

3250
diff = retriever.get_diff(
33-
"a79c0184",
34-
"122d9e0969465a6041e16c806a0464b35deea444",
51+
previous_commit,
52+
head_commit,
3553
)
3654

3755
blocks = parser.parse(
3856
diff,
3957
repository="OWASP/ASVS",
40-
commit_sha="122d9e0969465a6041e16c806a0464b35deea444",
58+
commit_sha=head_commit,
4159
committed_at=datetime.now(UTC),
4260
)
4361

application/tests/harvester_test/diff_retriever_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DiffRetrieverTests(unittest.TestCase):
1111
@patch("application.utils.harvester.diff_retriever.subprocess.run")
1212
def test_get_diff(self, mock_run):
1313
mock_run.return_value = MagicMock(
14-
stdout="diff --git a/README.md b/README.md\n",
14+
stdout=b"diff --git a/README.md b/README.md\n",
1515
)
1616

1717
client = MagicMock()
@@ -39,15 +39,14 @@ def test_get_diff(self, mock_run):
3939
"def456",
4040
],
4141
capture_output=True,
42-
text=True,
4342
check=True,
4443
timeout=300,
4544
)
4645

4746
@patch("application.utils.harvester.diff_retriever.subprocess.run")
4847
def test_large_diff_raises(self, mock_run):
4948
mock_run.return_value = MagicMock(
50-
stdout="A" * (51 * 1024 * 1024),
49+
stdout=b"A" * (51 * 1024 * 1024),
5150
)
5251

5352
client = MagicMock()

application/tests/harvester_test/git_repository_client_test.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,6 @@ def test_sync_fetches_when_repository_exists(self, mock_run):
8989

9090
mock_fetch.assert_called_once()
9191

92-
mock_run.assert_called_once_with(
93-
[
94-
"git",
95-
"-C",
96-
str(client.get_local_path()),
97-
"reset",
98-
"--hard",
99-
"origin/main",
100-
],
101-
check=True,
102-
capture_output=True,
103-
text=True,
104-
timeout=300,
105-
)
106-
10792
@patch("application.utils.harvester.git_repository_client.subprocess.run")
10893
def test_fetch_runs_git_command(self, mock_run):
10994
client = GitRepositoryClient(
@@ -130,6 +115,7 @@ def test_checkout_runs_git_command(self, mock_run):
130115
"-C",
131116
str(client.get_local_path()),
132117
"checkout",
118+
"--",
133119
"main",
134120
],
135121
check=True,

application/utils/harvester/checkpoint_store.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ def load(self, repository_id: str) -> RepositoryCheckpoint | None:
2222
.filter_by(repository_id=repository_id)
2323
.first()
2424
)
25+
2526
if record is None:
2627
return None
28+
2729
return RepositoryCheckpoint(
2830
repository_id=record.repository_id,
2931
last_processed_commit=record.last_processed_commit,
@@ -53,6 +55,7 @@ def save(self, checkpoint: RepositoryCheckpoint) -> None:
5355
)
5456
.first()
5557
)
58+
5659
if canonical_conflict is not None:
5760
session.rollback()
5861
raise ValueError("duplicate canonical source identity")

application/utils/harvester/diff_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ def parse(
4444

4545
continue
4646

47-
if line.startswith("+++"):
47+
if line.startswith("+++ b/") or line.startswith("++/dev/null"):
4848
continue
4949

50-
if line.startswith("---"):
50+
if line.startswith("--- a/") or line.startswith("--- /dev/null"):
5151
continue
5252

5353
if line.startswith("@@"):
5454
continue
5555

56-
if line.startswith("+") and not line.startswith("+++"):
56+
if line.startswith("+"):
5757
added_lines.append(line[1:])
5858

5959
if current_file is not None:

application/utils/harvester/diff_retriever.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,23 @@ def get_diff(self, base_commit: str, target_commit: str = "HEAD") -> str:
5656
],
5757
check=True,
5858
capture_output=True,
59-
text=True,
6059
timeout=300,
6160
)
6261
except subprocess.CalledProcessError as exc:
63-
logger.error("Failed to retrieve diff: %s", exc.stderr)
62+
logger.error(
63+
"Failed to retrieve diff: %s",
64+
exc.stderr.decode("utf-8", errors="replace"),
65+
)
6466
raise
6567

66-
diff = result.stdout
68+
diff_bytes = result.stdout
6769

68-
diff_size = len(diff.encode("utf-8"))
70+
diff_size = len(diff_bytes)
6971

7072
if diff_size > self.MAX_DIFF_SIZE_BYTES:
7173
raise ValueError(
7274
f"Diff size ({diff_size} bytes) exceeds "
7375
f"maximum supported size ({self.MAX_DIFF_SIZE_BYTES} bytes)."
7476
)
7577

76-
return diff
78+
return diff_bytes.decode("utf-8", errors="replace")

0 commit comments

Comments
 (0)