Skip to content

Commit eb7d81b

Browse files
committed
Storage: when retrieving datafile, use application/vnd.github.raw+json
1 parent 94446be commit eb7d81b

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

coverage_comment/storage.py

Lines changed: 7 additions & 3 deletions
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

Lines changed: 5 additions & 6 deletions
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_storage.py

Lines changed: 1 addition & 3 deletions
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)