Skip to content

Commit

Permalink
Merge pull request #44 from opensafely-core/last-updated-datetime
Browse files Browse the repository at this point in the history
feat: switch get_last_updated from date -> datetime
  • Loading branch information
ghickman authored Apr 19, 2022
2 parents fadf421 + bb483b4 commit 2b8b5cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions osgithub/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""
import json
from base64 import b64decode
from datetime import datetime
from datetime import datetime, timezone
from os import environ
from pathlib import Path

Expand Down Expand Up @@ -364,18 +364,22 @@ def get_commits_for_file(self, path, ref, number_of_commits=1):

def get_last_updated(self, path, ref):
"""
Finds the date of the last commit for a file
Finds the datetime of the last commit for a file
Args:
path (str): path to the file in the repo
ref (str): branch/tag/sha
Returns:
str: HTML from readme (at ROOT)
datetime: a datetime instance of the last commit's committed date
"""
commits = self.get_commits_for_file(path, ref, number_of_commits=1)
last_commit_date = commits[0]["commit"]["committer"]["date"]
return datetime.strptime(last_commit_date, "%Y-%m-%dT%H:%M:%SZ").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)

def get_readme(self, tag="main"):
"""
Expand Down
8 changes: 5 additions & 3 deletions tests/test_github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from base64 import b64encode
from datetime import date
from datetime import datetime, timezone
from os import environ

import pytest
Expand Down Expand Up @@ -285,7 +285,7 @@ def test_github_repo_get_last_updated(httpretty):
)

last_updated = repo.get_last_updated(path="test-folder/test-file.html", ref="main")
assert last_updated == date(2021, 3, 1)
assert last_updated == datetime(2021, 3, 1, 10, 0, 0, tzinfo=timezone.utc)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -546,7 +546,9 @@ def test_github_repo_get_contents_too_large_file(httpretty):

content_file = repo.get_contents("test-folder/test-file.html", ref="main")
assert content_file.decoded_content == str_content
assert content_file.last_updated == date(2021, 3, 1)
assert content_file.last_updated == datetime(
2021, 3, 1, 10, 0, 0, tzinfo=timezone.utc
)


def test_github_repo_get_readme(httpretty):
Expand Down

0 comments on commit 2b8b5cc

Please sign in to comment.