Skip to content

Commit

Permalink
Storage: when retrieving datafile, use application/vnd.github.raw+json
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Feb 15, 2024
1 parent 94446be commit eb7d81b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 7 additions & 3 deletions coverage_comment/storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import base64
import contextlib
import pathlib

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

return base64.b64decode(response.content).decode()
return response


def get_raw_file_url(
Expand Down
11 changes: 5 additions & 6 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import base64
import json
import os
import pathlib
Expand Down Expand Up @@ -232,7 +231,7 @@ def test_action__pull_request__store_comment_not_targeting_default(
session.register(
"GET",
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})

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

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

session.register(
"GET",
Expand Down Expand Up @@ -444,7 +443,7 @@ def test_action__push__non_default_branch__no_pr(
session.register(
"GET",
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})

session.register(
"GET",
Expand Down Expand Up @@ -498,7 +497,7 @@ def test_action__pull_request__force_store_comment(
session.register(
"GET",
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})

git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import base64
import pathlib

import pytest
Expand Down Expand Up @@ -142,9 +141,8 @@ def test_get_datafile_contents__not_found(gh, session):


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

result = storage.get_datafile_contents(
Expand Down

0 comments on commit eb7d81b

Please sign in to comment.