Skip to content

Commit fa9133f

Browse files
committed
Change of approach: let github compute the diff
1 parent 654f214 commit fa9133f

File tree

7 files changed

+94
-76
lines changed

7 files changed

+94
-76
lines changed

coverage_comment/coverage.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,7 @@ def get_diff_coverage_info(
286286
)
287287

288288

289-
def get_added_lines(
290-
git: subprocess.Git, base_ref: str
291-
) -> dict[pathlib.Path, list[int]]:
292-
# --unified=0 means we don't get any context lines for chunk, and we
293-
# don't merge chunks. This means the headers that describe line number
294-
# are always enough to derive what line numbers were added.
295-
git.fetch("origin", base_ref, "--depth=1000")
296-
diff = git.diff("--unified=0", "FETCH_HEAD...HEAD")
297-
return parse_diff_output(diff)
298-
299-
300-
def parse_diff_output(diff: str) -> dict[pathlib.Path, list[int]]:
289+
def get_added_lines(diff: str) -> dict[pathlib.Path, list[int]]:
301290
current_file: pathlib.Path | None = None
302291
added_filename_prefix = "+++ b/"
303292
result: dict[pathlib.Path, list[int]] = {}

coverage_comment/github.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,19 @@ def append_to_file(content: str, filepath: pathlib.Path):
254254

255255
def add_job_summary(content: str, github_step_summary: pathlib.Path):
256256
append_to_file(content=content, filepath=github_step_summary)
257+
258+
259+
def get_pr_diff(github: github_client.GitHub, repository: str, pr_number: int) -> str:
260+
"""
261+
Get the diff of a pull request.
262+
"""
263+
return github.repos(repository).pulls(f"{pr_number}.diff").get()
264+
265+
266+
def get_branch_diff(
267+
github: github_client.GitHub, repository: str, base_branch: str, head_branch: str
268+
) -> str:
269+
"""
270+
Get the diff of branch.
271+
"""
272+
return github.repos(repository).compare(f"{base_branch}...{head_branch}.diff").get()

coverage_comment/main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,20 @@ def process_pr(
132132
)
133133
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch
134134

135-
added_lines = coverage_module.get_added_lines(git=git, base_ref=base_ref)
135+
if config.GITHUB_BRANCH_NAME:
136+
diff = github.get_branch_diff(
137+
github=gh,
138+
repository=config.GITHUB_REPOSITORY,
139+
base_branch=base_ref,
140+
head_branch=config.GITHUB_BRANCH_NAME,
141+
)
142+
elif config.GITHUB_PR_NUMBER:
143+
diff = github.get_pr_diff(
144+
github=gh,
145+
repository=config.GITHUB_REPOSITORY,
146+
pr_number=config.GITHUB_PR_NUMBER,
147+
)
148+
added_lines = coverage_module.get_added_lines(diff=diff)
136149
diff_coverage = coverage_module.get_diff_coverage_info(
137150
coverage=coverage, added_lines=added_lines
138151
)

tests/integration/test_coverage.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/integration/test_github.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,25 @@ def test_annotations(capsys):
444444
::endgroup::"""
445445
output = capsys.readouterr()
446446
assert output.err.strip() == expected
447+
448+
449+
def test_get_pr_diff(gh, session):
450+
session.register("GET", "/repos/foo/bar/pulls/123.diff")(
451+
text="diff --git a/foo.py b/foo.py..."
452+
)
453+
454+
result = github.get_pr_diff(github=gh, repository="foo/bar", pr_number=123)
455+
456+
assert result == "diff --git a/foo.py b/foo.py..."
457+
458+
459+
def test_get_branch_diff(gh, session):
460+
session.register("GET", "/repos/foo/bar/compare/main...feature.diff")(
461+
text="diff --git a/foo.py b/foo.py..."
462+
)
463+
464+
result = github.get_branch_diff(
465+
github=gh, repository="foo/bar", base_branch="main", head_branch="feature"
466+
)
467+
468+
assert result == "diff --git a/foo.py b/foo.py..."

tests/integration/test_main.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ def checker(payload):
7474
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
7575
)(status_code=403)
7676

77-
git.register("git fetch origin main --depth=1000")()
78-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
77+
# What is the diff of the PR
78+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
79+
text=DIFF_STDOUT
80+
)
7981

8082
result = main.action(
8183
config=pull_request_config(
@@ -155,8 +157,10 @@ def checker(payload):
155157
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
156158
)(status_code=403)
157159

158-
git.register("git fetch origin foo --depth=1000")(stdout=DIFF_STDOUT)
159-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
160+
# What is the diff of the PR
161+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
162+
text=DIFF_STDOUT
163+
)
160164

161165
result = main.action(
162166
config=pull_request_config(
@@ -202,8 +206,10 @@ def test_action__pull_request__post_comment(
202206
# Are there already comments
203207
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])
204208

205-
git.register("git fetch origin main --depth=1000")()
206-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
209+
# What is the diff of the PR
210+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
211+
text=DIFF_STDOUT
212+
)
207213

208214
comment = None
209215

@@ -249,8 +255,11 @@ def test_action__push__non_default_branch(
249255
session.register("GET", "/repos/py-cov-action/foobar")(
250256
json={"default_branch": "main", "visibility": "public"}
251257
)
252-
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
253-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
258+
259+
# What is the diff of the `other` branch
260+
session.register("GET", "/repos/py-cov-action/foobar/compare/main...other.diff")(
261+
text=DIFF_STDOUT
262+
)
254263

255264
payload = json.dumps({"coverage": 30.00})
256265
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -338,8 +347,10 @@ def test_action__push__non_default_branch__no_pr(
338347
session.register("GET", "/repos/py-cov-action/foobar")(
339348
json={"default_branch": "main", "visibility": "public"}
340349
)
341-
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
342-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
350+
# What is the diff of the `other` branch
351+
session.register("GET", "/repos/py-cov-action/foobar/compare/main...other.diff")(
352+
text=DIFF_STDOUT
353+
)
343354

344355
payload = json.dumps({"coverage": 30.00})
345356
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -402,8 +413,10 @@ def test_action__pull_request__force_store_comment(
402413
"/repos/py-cov-action/foobar/contents/data.json",
403414
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
404415

405-
git.register("git fetch origin main --depth=1000")()
406-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
416+
# What is the diff of the PR
417+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
418+
text=DIFF_STDOUT
419+
)
407420

408421
result = main.action(
409422
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
@@ -433,8 +446,10 @@ def test_action__pull_request__post_comment__no_marker(
433446
"/repos/py-cov-action/foobar/contents/data.json",
434447
)(status_code=404)
435448

436-
git.register("git fetch origin main --depth=1000")()
437-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
449+
# What is the diff of the PR
450+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
451+
text=DIFF_STDOUT
452+
)
438453

439454
result = main.action(
440455
config=pull_request_config(COMMENT_TEMPLATE="""foo"""),
@@ -458,8 +473,10 @@ def test_action__pull_request__annotations(
458473
"/repos/py-cov-action/foobar/contents/data.json",
459474
)(status_code=404)
460475

461-
git.register("git fetch origin main --depth=1000")()
462-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
476+
# What is the diff of the PR
477+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
478+
text=DIFF_STDOUT
479+
)
463480

464481
# Who am I
465482
session.register("GET", "/user")(json={"login": "foo"})
@@ -500,8 +517,10 @@ def test_action__pull_request__post_comment__template_error(
500517
"/repos/py-cov-action/foobar/contents/data.json",
501518
)(status_code=404)
502519

503-
git.register("git fetch origin main --depth=1000")()
504-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
520+
# What is the diff of the PR
521+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2.diff")(
522+
text=DIFF_STDOUT
523+
)
505524

506525
result = main.action(
507526
config=pull_request_config(COMMENT_TEMPLATE="""{%"""),

tests/unit/test_coverage.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,6 @@ def test_get_diff_coverage_info(make_coverage_obj, added_lines, update_obj, expe
304304
assert result == expected
305305

306306

307-
def test_get_added_lines(git):
308-
diff = (
309-
"""+++ b/README.md\n@@ -1,2 +1,3 @@\n-# coverage-comment\n+coverage-comment\n"""
310-
)
311-
git.register("git fetch origin main --depth=1000")()
312-
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
313-
assert coverage.get_added_lines(git=git, base_ref="main") == {
314-
pathlib.Path("README.md"): [1, 2, 3]
315-
}
316-
317-
318307
@pytest.mark.parametrize(
319308
"line_number_diff_line, expected",
320309
[
@@ -327,7 +316,7 @@ def test_parse_line_number_diff_line(git, line_number_diff_line, expected):
327316
assert result == expected
328317

329318

330-
def test_parse_diff_output(git):
319+
def test_get_added_lines(git):
331320
diff = """diff --git a/action.yml b/action.yml
332321
deleted file mode 100644
333322
index 42249d1..0000000
@@ -365,13 +354,13 @@ def test_parse_diff_output(git):
365354
"""
366355
git.register("git fetch origin main --depth=1000")()
367356
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
368-
assert coverage.parse_diff_output(diff=diff) == {
357+
assert coverage.get_added_lines(diff=diff) == {
369358
pathlib.Path("README.md"): [1, 3, 4, 5, 6],
370359
pathlib.Path("foo.txt"): [1],
371360
}
372361

373362

374-
def test_parse_diff_output__error(git):
363+
def test_get_added_lines__error(git):
375364
diff = """
376365
@@ -0,0 +1,1 @@
377366
+name: Python Coverage Comment
@@ -381,4 +370,4 @@ def test_parse_diff_output__error(git):
381370
git.register("git fetch origin main --depth=1000")()
382371
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
383372
with pytest.raises(ValueError):
384-
coverage.parse_diff_output(diff=diff)
373+
coverage.get_added_lines(diff=diff)

0 commit comments

Comments
 (0)