Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Feb 15, 2024
1 parent 9af72a0 commit 074f23e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion coverage_comment/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __getattr__(self, attr):


class GitHub:

"""
GitHub client.
"""
Expand All @@ -61,6 +60,8 @@ def __getattr__(self, attr):
def _http(self, method, path, *, bytes=False, **kw):
_method = method.lower()
requests_kwargs = {}
headers = kw.pop("headers", {})
header_kwargs = {"headers": headers} if headers else {}
if _method == "get" and kw:
requests_kwargs = {"params": kw}

Expand All @@ -71,6 +72,7 @@ def _http(self, method, path, *, bytes=False, **kw):
_method.upper(),
path,
timeout=TIMEOUT,
**header_kwargs,
**requests_kwargs,
)
if bytes:
Expand Down
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
9 changes: 9 additions & 0 deletions tests/unit/test_github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def test_github_client__get(session, gh):
assert gh.repos("a/b").issues().get(a=1) == {"foo": "bar"}


def test_github_client__get_headers(session, gh):
session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})(
json={"foo": "bar"},
headers={"X-foo": "yay"},
)

assert gh.repos("a/b").issues().get(a=1, headers={"X-foo": "yay"}) == {"foo": "bar"}


def test_github_client__post_non_json(session, gh):
session.register("POST", "/repos/a/b/issues", timeout=60, json={"a": 1})()

Expand Down

0 comments on commit 074f23e

Please sign in to comment.