Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Set the following environment variables:

```py
from github_api_cache import GithubClient

# Use the default token, if one is set in the environment.
client = GithubClient()
```
Expand Down Expand Up @@ -129,9 +130,9 @@ Set expiry on specific url patterns (falls back to `expire_after` if no match fo

```py
urls_expire_after = {
'*/pulls': 60, # expire requests to get pull requests after 60 secs
'*/branches': 60 * 5, # expire requests to get branches after 5 mins
'*/commits': 30, # expire requests to get commits after 30 secs
"*/pulls": 60, # expire requests to get pull requests after 60 secs
"*/branches": 60 * 5, # expire requests to get branches after 5 mins
"*/commits": 30, # expire requests to get commits after 30 secs
}
client = GithubClient(urls_expire_after=urls_expire_after)
```
Expand Down
12 changes: 6 additions & 6 deletions osgithub/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import json
from base64 import b64decode
from datetime import datetime, timezone
from datetime import datetime
from os import environ
from pathlib import Path

Expand Down Expand Up @@ -367,11 +367,11 @@ def get_last_updated(self, path, ref):
"""
commits = self.get_commits_for_file(path, ref, number_of_commits=1)
last_commit_date = commits[0]["commit"]["committer"]["date"]
dt = datetime.strptime(last_commit_date, "%Y-%m-%dT%H:%M:%SZ")
# we know GitHub is giving us a UTC timezone because the string ends in
# Z, but Python's strptime can't consume that with it's %Z operator so
# we're matching it literally and then setting the timezone to UTC.
return dt.replace(tzinfo=timezone.utc)
# GitHub returns UTC timestamps with a trailing Z; normalize it to an
# explicit UTC offset so strptime can create an aware datetime with %z.
return datetime.strptime(
last_commit_date.replace("Z", "+00:00"), "%Y-%m-%dT%H:%M:%S%z"
)

def get_readme(self, tag="main"):
"""
Expand Down
38 changes: 19 additions & 19 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,25 @@ pyyaml==6.0.3 \
--hash=sha256:fa160448684b4e94d80416c0fa4aac48967a969efe22931448d853ada8baf926 \
--hash=sha256:fc09d0aa354569bc501d4e787133afc08552722d3ab34836a80547331bb5d4a0
# via pre-commit
ruff==0.15.22 \
--hash=sha256:11c1c715af53a09f714e011106bffc419751ec8232fcb5da42173284ea3fec6f \
--hash=sha256:1877d63b9d24ed278744f1523fd11b85540566d54641f97c566d7d9dc5ca5296 \
--hash=sha256:1e0dd1b2e4d3d585f897a0d137cbf4eaf6223bef4e8ce34d6bb12556c5f9249e \
--hash=sha256:225dbf095a87f1d9f90f5fd7924d2613ee452a75a4308c63a8f50f761787aa7c \
--hash=sha256:365523eb91d9224e1bcb03b022fbf0facb8f9e23792a2c53d9d4b3924bdbdebb \
--hash=sha256:3f15175b1fb580126f58285a5dae6b2ea89000136d980c64499211f116b54809 \
--hash=sha256:44423e73493737f5e7c5b41d475483898ff37afcdae38bc3da5085e29af1c2d8 \
--hash=sha256:62d425005c1835eb24e2ee4161cb90e8db263415f4a71c8c72c33abaa6c0c224 \
--hash=sha256:630479b18625f5ffc373f77603a22a9f8ac0acd7ff0501178b5db28ec71e9c64 \
--hash=sha256:72af58b951b0ae395935ae79763dc349bc0eb706319d28f7a33ad2cfb3cfc178 \
--hash=sha256:742a29cf29bddb7c8327895d6a10e0e6c5b38a96dd407af9b5d0857f809c0576 \
--hash=sha256:9be63ba1eb936acd2d1342fb8337c356353706fce233b2a15a09a97037e6acde \
--hash=sha256:a1606c510bd7215680d32efab38965f7cdec3ef69f5170a3f4791404ffdd5262 \
--hash=sha256:b82c6482946e9eda7ff2e091d25b8bad3f718684e1916d41bd56873cee05b697 \
--hash=sha256:e1168075b72158510839f250027659cdd78476f40507dd517892304c41318661 \
--hash=sha256:e5ba0e4a13fd14abbed2a77b517a3911290c6c6c59ef67784328d1668fab76cf \
--hash=sha256:e8b9b3f8779a4f08c969defc3c8c35abffaa757e601ed5ae66d6d1db6519969a \
--hash=sha256:fabfd168afdf29fee5be98b831efa9683c94d7c5a3b58b9ce5a2e38444589a74
ruff==0.16.0 \
--hash=sha256:0ff4a79ce3ec0172f3241943835de1c4cb4e2dcd07f0f8c2d02603dbbbee4b17 \
--hash=sha256:14296fedcd2705c77ab8235439278bbb38f285cf7da5528b00b3e330c3d4872d \
--hash=sha256:28ea2b7df8ebf7f9da6b7d47b230ab48f387c0a29be3b474c4d0740e197bb9af \
--hash=sha256:33a3dfac8c35f81498dea9181bccc2f4c4bc8f1521a1dd9406e77643e0f0fb09 \
--hash=sha256:3c954b1d580bfa035b41654f7858cc7e71d5fc3ac5b723dd62bd9133830ed522 \
--hash=sha256:429c117f022bf481fabd9d551e7a3952b24c65e6ef44337ea09d90bebef14472 \
--hash=sha256:48044c678e9cb8698246c99b14aaccfa6601dea7379eb48a6f8f73f7a6d86cd0 \
--hash=sha256:4f11a8d11010301d0a398a2fdef67691feca7294da6aef55e2150e8fa2cd520b \
--hash=sha256:6e364e5ed22ed8dc05082fd78e35308618260907ac2d3c1d637b2e682415b6c9 \
--hash=sha256:7aa0959bad8eb8bef50340154fc9b58678dae31fa4293afa38b44b6e552c0213 \
--hash=sha256:7fab76fa065c873f41ff744347c6e77bcc3dfec4bcc754dc26b63d23c0f7f5fb \
--hash=sha256:a5237a0bda500d30d81b8e07a6973a5cbc772864cbf746ae2f4e8a2e01c9f4ed \
--hash=sha256:a9b50c55e263103586b3dcf5f73d479eb8cb5fdb6098fec59a62891dab653717 \
--hash=sha256:d327b8fc113a1d4421a04f3839d3752057c8dd1ee320223a6f3f52d04ada462a \
--hash=sha256:e01c21d10eb1b29f47b7454e1f4056db9a3f0260c646aa88457c610291db9f81 \
--hash=sha256:e460aafd5495ec89efaa6ced2e4a9a581116451e1c88b9d37ef497e0f8e93982 \
--hash=sha256:e5115729eb08c585e5121978ba5d5b60caeae394ce21b9fb5e6cd33a1c6c9b1e \
--hash=sha256:e95c448fca1fb2a18372a9440926c5a6ee789639bb975c72e7ae6d0b04218ab4
# via -r requirements.dev.in
tomli==2.4.1 \
--hash=sha256:01f520d4f53ef97964a240a035ec2a869fe1a37dde002b57ebc4417a27ccd853 \
Expand Down
46 changes: 31 additions & 15 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_github_repo_get_pull_requests(httpretty, state):
register_uri(
httpretty,
"repos/test/foo/pulls",
queryparams=dict(state=state, page=1, per_page=30),
queryparams={"state": state, "page": 1, "per_page": 30},
body=pull_requests[state],
)
pulls = repo.get_pull_requests(state)
Expand All @@ -146,7 +146,7 @@ def test_github_repo_get_open_pull_request_count(httpretty):
register_uri(
httpretty,
"repos/test/foo/pulls",
queryparams=dict(state="open", page=1, per_page=30),
queryparams={"state": "open", "page": 1, "per_page": 30},
body=pull_requests,
)
assert repo.open_pull_request_count == 2
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_github_repo_get_multipage_pull_request_count(httpretty):
register_uri(
httpretty,
"repos/test/foo/pulls",
queryparams=dict(state="open", page=i, per_page=30),
queryparams={"state": "open", "page": i, "per_page": 30},
body=pull_requests,
)
last_page_pull_requests = [
Expand All @@ -209,7 +209,7 @@ def test_github_repo_get_multipage_pull_request_count(httpretty):
register_uri(
httpretty,
"repos/test/foo/pulls",
queryparams=dict(state="open", page=3, per_page=30),
queryparams={"state": "open", "page": 3, "per_page": 30},
body=last_page_pull_requests,
)

Expand Down Expand Up @@ -242,15 +242,19 @@ def test_github_repo_get_contents_single_file(httpretty):
register_uri(
httpretty,
"repos/test/foo/contents/test-folder/test-file.html",
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body=reponse_json,
)

commits_response = [{"commit": {"committer": {"date": "2021-03-01T10:00:00Z"}}}]
register_uri(
httpretty,
"repos/test/foo/commits",
queryparams=dict(sha="main", path="test-folder/test-file.html", per_page=1),
queryparams={
"sha": "main",
"path": "test-folder/test-file.html",
"per_page": 1,
},
body=commits_response,
)

Expand Down Expand Up @@ -280,7 +284,11 @@ def test_github_repo_get_last_updated(httpretty):
register_uri(
httpretty,
"repos/test/foo/commits",
queryparams=dict(sha="main", path="test-folder/test-file.html", per_page=1),
queryparams={
"sha": "main",
"path": "test-folder/test-file.html",
"per_page": 1,
},
body=commits_response,
)

Expand Down Expand Up @@ -327,7 +335,7 @@ def test_github_repo_get_contents_exceptions(
register_uri(
httpretty,
"repos/test/foo/contents/test-folder/test-file.html",
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
status=status_code,
body=body,
)
Expand Down Expand Up @@ -368,7 +376,7 @@ def test_github_repo_matching_file_from_parent_contents(
register_uri(
httpretty,
"repos/test/foo/contents/test-folder",
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body=response_json,
)

Expand Down Expand Up @@ -401,7 +409,7 @@ def test_github_repo_get_contents_folder(httpretty):
register_uri(
httpretty,
"repos/test/foo/contents/test-folder",
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body=response_json,
)
contents = repo.get_contents("test-folder", ref="main")
Expand Down Expand Up @@ -443,7 +451,7 @@ def test_github_repo_get_contents_from_git_blob(httpretty):
register_uri(
httpretty,
"repos/test/foo/contents/test-folder",
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body=response_json,
)

Expand All @@ -463,7 +471,11 @@ def test_github_repo_get_contents_from_git_blob(httpretty):
register_uri(
httpretty,
"repos/test/foo/commits",
queryparams=dict(sha="main", path="test-folder/test-file.html", per_page=1),
queryparams={
"sha": "main",
"path": "test-folder/test-file.html",
"per_page": 1,
},
body=commits_response,
)

Expand Down Expand Up @@ -499,7 +511,7 @@ def test_github_repo_get_contents_too_large_file(httpretty):
httpretty,
"repos/test/foo/contents/test-folder/test-file.html",
status=200,
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body={
"name": "test-file.html",
"path": "test-folder/test-file.html",
Expand All @@ -515,7 +527,7 @@ def test_github_repo_get_contents_too_large_file(httpretty):
httpretty,
"repos/test/foo/contents/test-folder",
status=200,
queryparams=dict(ref="main"),
queryparams={"ref": "main"},
body=[
{
"name": "test-file.html",
Expand Down Expand Up @@ -547,7 +559,11 @@ def test_github_repo_get_contents_too_large_file(httpretty):
register_uri(
httpretty,
"repos/test/foo/commits",
queryparams=dict(sha="main", path="test-folder/test-file.html", per_page=1),
queryparams={
"sha": "main",
"path": "test-folder/test-file.html",
"per_page": 1,
},
body=commits_response,
)

Expand Down