Skip to content

Commit 3efb5dd

Browse files
committed
Change of approach: let github compute the diff
1 parent 66208c7 commit 3efb5dd

File tree

7 files changed

+92
-76
lines changed

7 files changed

+92
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,27 @@ 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 (
264+
github.repos(repository)
265+
.pulls(pr_number)
266+
.get(headers={"Accept": "application/vnd.github.v3.diff"})
267+
)
268+
269+
270+
def get_branch_diff(
271+
github: github_client.GitHub, repository: str, base_branch: str, head_branch: str
272+
) -> str:
273+
"""
274+
Get the diff of branch.
275+
"""
276+
return (
277+
github.repos(repository)
278+
.compare(f"{base_branch}...{head_branch}")
279+
.get(headers={"Accept": "application/vnd.github.v3.diff"})
280+
)

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,29 @@ 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(
451+
"GET",
452+
"/repos/foo/bar/pulls/123",
453+
headers={"Accept": "application/vnd.github.v3.diff"},
454+
)(text="diff --git a/foo.py b/foo.py...")
455+
456+
result = github.get_pr_diff(github=gh, repository="foo/bar", pr_number=123)
457+
458+
assert result == "diff --git a/foo.py b/foo.py..."
459+
460+
461+
def test_get_branch_diff(gh, session):
462+
session.register(
463+
"GET",
464+
"/repos/foo/bar/compare/main...feature",
465+
headers={"Accept": "application/vnd.github.v3.diff"},
466+
)(text="diff --git a/foo.py b/foo.py...")
467+
468+
result = github.get_branch_diff(
469+
github=gh, repository="foo/bar", base_branch="main", head_branch="feature"
470+
)
471+
472+
assert result == "diff --git a/foo.py b/foo.py..."

tests/integration/test_main.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ 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")(text=DIFF_STDOUT)
7979

8080
result = main.action(
8181
config=pull_request_config(
@@ -155,8 +155,8 @@ def checker(payload):
155155
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
156156
)(status_code=403)
157157

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)
158+
# What is the diff of the PR
159+
session.register("GET", "/repos/py-cov-action/foobar/pulls/2")(text=DIFF_STDOUT)
160160

161161
result = main.action(
162162
config=pull_request_config(
@@ -202,8 +202,8 @@ def test_action__pull_request__post_comment(
202202
# Are there already comments
203203
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])
204204

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

208208
comment = None
209209

@@ -249,8 +249,11 @@ def test_action__push__non_default_branch(
249249
session.register("GET", "/repos/py-cov-action/foobar")(
250250
json={"default_branch": "main", "visibility": "public"}
251251
)
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)
252+
253+
# What is the diff of the `other` branch
254+
session.register("GET", "/repos/py-cov-action/foobar/compare/main...other")(
255+
text=DIFF_STDOUT
256+
)
254257

255258
payload = json.dumps({"coverage": 30.00})
256259
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -338,8 +341,10 @@ def test_action__push__non_default_branch__no_pr(
338341
session.register("GET", "/repos/py-cov-action/foobar")(
339342
json={"default_branch": "main", "visibility": "public"}
340343
)
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)
344+
# What is the diff of the `other` branch
345+
session.register("GET", "/repos/py-cov-action/foobar/compare/main...other")(
346+
text=DIFF_STDOUT
347+
)
343348

344349
payload = json.dumps({"coverage": 30.00})
345350
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -402,8 +407,8 @@ def test_action__pull_request__force_store_comment(
402407
"/repos/py-cov-action/foobar/contents/data.json",
403408
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
404409

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

408413
result = main.action(
409414
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
@@ -433,8 +438,8 @@ def test_action__pull_request__post_comment__no_marker(
433438
"/repos/py-cov-action/foobar/contents/data.json",
434439
)(status_code=404)
435440

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

439444
result = main.action(
440445
config=pull_request_config(COMMENT_TEMPLATE="""foo"""),
@@ -458,8 +463,8 @@ def test_action__pull_request__annotations(
458463
"/repos/py-cov-action/foobar/contents/data.json",
459464
)(status_code=404)
460465

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

464469
# Who am I
465470
session.register("GET", "/user")(json={"login": "foo"})
@@ -500,8 +505,8 @@ def test_action__pull_request__post_comment__template_error(
500505
"/repos/py-cov-action/foobar/contents/data.json",
501506
)(status_code=404)
502507

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

506511
result = main.action(
507512
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)