Skip to content

Commit b16205b

Browse files
authored
Merge pull request #360 from py-cov-action/huge-datafile
2 parents 9af72a0 + eb7d81b commit b16205b

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

coverage_comment/github_client.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def __getattr__(self, attr):
4747

4848

4949
class GitHub:
50-
5150
"""
5251
GitHub client.
5352
"""
@@ -58,9 +57,18 @@ def __init__(self, session: httpx.Client):
5857
def __getattr__(self, attr):
5958
return _Callable(self, "/%s" % attr)
6059

61-
def _http(self, method, path, *, bytes=False, **kw):
60+
def _http(
61+
self,
62+
method: str,
63+
path: str,
64+
*,
65+
bytes: bool = False,
66+
headers: dict[str, str] | None = None,
67+
**kw,
68+
):
6269
_method = method.lower()
6370
requests_kwargs = {}
71+
header_kwargs = {"headers": headers} if headers else {}
6472
if _method == "get" and kw:
6573
requests_kwargs = {"params": kw}
6674

@@ -71,6 +79,7 @@ def _http(self, method, path, *, bytes=False, **kw):
7179
_method.upper(),
7280
path,
7381
timeout=TIMEOUT,
82+
**header_kwargs,
7483
**requests_kwargs,
7584
)
7685
if bytes:
@@ -93,9 +102,13 @@ def _http(self, method, path, *, bytes=False, **kw):
93102

94103
def response_contents(
95104
response: httpx.Response,
96-
) -> JsonObject | bytes:
105+
) -> JsonObject | str | bytes:
97106
if response.headers.get("content-type", "").startswith("application/json"):
98107
return response.json(object_hook=JsonObject)
108+
if response.headers.get("content-type", "").startswith(
109+
"application/vnd.github.raw+json"
110+
):
111+
return response.text
99112
return response.content
100113

101114

coverage_comment/storage.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import base64
43
import contextlib
54
import pathlib
65

@@ -120,11 +119,16 @@ def get_datafile_contents(
120119
) -> str | None:
121120
contents_path = github.repos(repository).contents(str(files.DATA_PATH))
122121
try:
123-
response = contents_path.get(ref=branch)
122+
response = contents_path.get(
123+
ref=branch,
124+
# If we don't pass this header, the format of the answer will depend on
125+
# the size of the file. With the header, we're sure to get the raw content.
126+
headers={"Accept": "application/vnd.github.raw+json"},
127+
)
124128
except github_client.NotFound:
125129
return None
126130

127-
return base64.b64decode(response.content).decode()
131+
return response
128132

129133

130134
def get_raw_file_url(

tests/integration/test_main.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import base64
43
import json
54
import os
65
import pathlib
@@ -232,7 +231,7 @@ def test_action__pull_request__store_comment_not_targeting_default(
232231
session.register(
233232
"GET",
234233
"/repos/py-cov-action/foobar/contents/data.json",
235-
)(json={"content": base64.b64encode(payload.encode()).decode()})
234+
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
236235

237236
# Who am I
238237
session.register("GET", "/user")(json={"login": "foo"})
@@ -293,7 +292,7 @@ def test_action__pull_request__post_comment(
293292
session.register(
294293
"GET",
295294
"/repos/py-cov-action/foobar/contents/data.json",
296-
)(json={"content": base64.b64encode(payload.encode()).decode()})
295+
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
297296

298297
# Who am I
299298
session.register("GET", "/user")(json={"login": "foo"})
@@ -355,7 +354,7 @@ def test_action__push__non_default_branch(
355354
session.register(
356355
"GET",
357356
"/repos/py-cov-action/foobar/contents/data.json",
358-
)(json={"content": base64.b64encode(payload.encode()).decode()})
357+
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
359358

360359
session.register(
361360
"GET",
@@ -444,7 +443,7 @@ def test_action__push__non_default_branch__no_pr(
444443
session.register(
445444
"GET",
446445
"/repos/py-cov-action/foobar/contents/data.json",
447-
)(json={"content": base64.b64encode(payload.encode()).decode()})
446+
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
448447

449448
session.register(
450449
"GET",
@@ -498,7 +497,7 @@ def test_action__pull_request__force_store_comment(
498497
session.register(
499498
"GET",
500499
"/repos/py-cov-action/foobar/contents/data.json",
501-
)(json={"content": base64.b64encode(payload.encode()).decode()})
500+
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
502501

503502
git.register("git fetch origin main --depth=1000")()
504503
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

tests/unit/test_github_client.py

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ def test_github_client__get(session, gh):
1313
assert gh.repos("a/b").issues().get(a=1) == {"foo": "bar"}
1414

1515

16+
def test_github_client__get_text(session, gh):
17+
session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})(
18+
text="foobar", headers={"content-type": "application/vnd.github.raw+json"}
19+
)
20+
21+
assert gh.repos("a/b").issues().get(a=1) == "foobar"
22+
23+
24+
def test_github_client__get_bytes(session, gh):
25+
session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})(
26+
text="foobar", headers={"content-type": "application/vnd.github.raw+json"}
27+
)
28+
29+
assert gh.repos("a/b").issues().get(a=1, bytes=True) == b"foobar"
30+
31+
32+
def test_github_client__get_headers(session, gh):
33+
session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})(
34+
json={"foo": "bar"},
35+
headers={"X-foo": "yay"},
36+
)
37+
38+
assert gh.repos("a/b").issues().get(a=1, headers={"X-foo": "yay"}) == {"foo": "bar"}
39+
40+
1641
def test_github_client__post_non_json(session, gh):
1742
session.register("POST", "/repos/a/b/issues", timeout=60, json={"a": 1})()
1843

tests/unit/test_storage.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import base64
43
import pathlib
54

65
import pytest
@@ -142,9 +141,8 @@ def test_get_datafile_contents__not_found(gh, session):
142141

143142

144143
def test_get_datafile_contents(gh, session):
145-
payload = base64.b64encode(b"yay").decode()
146144
session.register("GET", "/repos/foo/bar/contents/data.json", params={"ref": "baz"})(
147-
json={"content": payload}
145+
text="yay", headers={"content-type": "application/vnd.github.raw+json"}
148146
)
149147

150148
result = storage.get_datafile_contents(

0 commit comments

Comments
 (0)