Skip to content

Commit

Permalink
fix: add tests and use fromisoformat to parse string date
Browse files Browse the repository at this point in the history
- [x] change from splitting string to using datetime.datetime.fromisoformat
  since the format from GitHub API for a repositories created_at date
  is ISO 8601 (example: 2024-04-03T05:00:00Z)
- [x] write tests to handle if the repo.created_at is a string or a datetime

According to the github3.py library, the repository's [created_at date is
a datetime.datetime obj](https://github.com/sigmavirus24/github3.py/blob/3bb730f11a70ab84f9f64835442dc4c9c62638ea/src/github3/repos/repo.py#L2938-L2941).

This makes me wonder how we're getting a string.

Signed-off-by: jmeridth <[email protected]>
  • Loading branch information
jmeridth committed Apr 2, 2024
1 parent ceff005 commit b267c4f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
24 changes: 17 additions & 7 deletions evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ def main(): # pragma: no cover
except github3.exceptions.NotFoundError:
pass

repo_createdat_date_object = datetime.strptime(
repo.created_at.split("T")[0], "%Y-%m-%d"
)
if created_after_date and repo_createdat_date_object.replace(
tzinfo=None
) < datetime.strptime(created_after_date, "%Y-%m-%d"):
if is_repo_created_date_before(repo.created_at, created_after_date):
continue

print("Checking " + repo.full_name)
Expand Down Expand Up @@ -164,8 +159,23 @@ def main(): # pragma: no cover
print("Done. " + str(count_eligible) + " repositories were eligible.")


def is_repo_created_date_before(repo_created_at, created_after_date):
"""Check if the repository was created before the created_after_date"""
repo_created_at_date = repo_created_at
if isinstance(repo_created_at, str):
repo_created_at_date = datetime.fromisoformat(repo_created_at)
repo_created_at_date = repo_created_at_date.replace(tzinfo=None)
if created_after_date and repo_created_at_date < datetime.strptime(
created_after_date, "%Y-%m-%d"
):
return True
return False


def is_dependabot_security_updates_enabled(owner, repo, access_token):
"""Check if Dependabot security updates are enabled at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library"""
"""Check if Dependabot security updates are enabled at the
/repos/:owner/:repo/automated-security-fixes endpoint using the requests library
"""
url = f"https://api.github.com/repos/{owner}/{repo}/automated-security-fixes"
headers = {
"Authorization": f"Bearer {access_token}",
Expand Down
60 changes: 60 additions & 0 deletions test_evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import unittest
import uuid
from datetime import datetime, timezone
from unittest.mock import MagicMock, patch

import requests
Expand All @@ -15,6 +16,7 @@
get_global_project_id,
get_repos_iterator,
is_dependabot_security_updates_enabled,
is_repo_created_date_before,
link_item_to_project,
)

Expand Down Expand Up @@ -579,5 +581,63 @@ def test_link_item_to_project_request_exception(self, mock_post):
self.assertIsNone(result)


class TestIsRepoCreateDateBeforeCreatedAfterDate(unittest.TestCase):
"""Test the is_repo_create_date_before_created_after_date function in evergreen.py"""

def test_is_repo_create_date_is_date_and_before_created_after_date(self):
"""Test the repo.created_at date is before created_after_date."""
repo_created_at = datetime(2020, 1, 1, 5, 0, tzinfo=timezone.utc)
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertTrue(result)

def test_is_repo_create_date_is_date_and_after_created_after_date(self):
"""Test the repo.created_at date is after created_after_date."""
repo_created_at = datetime(2022, 1, 1, 5, 0, tzinfo=timezone.utc)
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertFalse(result)

def test_is_repo_created_date_is_date_and_no_time_zone(self):
"""Test the repo.created_at date is after created_after_date."""
repo_created_at = datetime(2020, 1, 1, 5)
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertTrue(result)

def test_is_repo_create_date_is_string_and_before_created_after_date(self):
"""Test the repo.created_at date is before created_after_date."""
repo_created_at = "2020-01-01T05:00:00Z"
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertTrue(result)

def test_is_repo_create_date_is_string_and_after_created_after_date(self):
"""Test the repo.created_at date is after created_after_date."""
repo_created_at = "2022-01-01T05:00:00Z"
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertFalse(result)

def test_is_repo_created_date_is_string_and_no_time_zone(self):
"""Test the repo.created_at date is after created_after_date."""
repo_created_at = "2020-01-01T05:00:00Z"
created_after_date = "2021-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertTrue(result)


if __name__ == "__main__":
unittest.main()

0 comments on commit b267c4f

Please sign in to comment.