From b267c4f8e3229088ba740d0362552ba65347c20b Mon Sep 17 00:00:00 2001 From: jmeridth Date: Tue, 2 Apr 2024 01:11:20 -0500 Subject: [PATCH] fix: add tests and use fromisoformat to parse string date - [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 --- evergreen.py | 24 +++++++++++++------ test_evergreen.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/evergreen.py b/evergreen.py index 6f25542..2b61a9c 100644 --- a/evergreen.py +++ b/evergreen.py @@ -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) @@ -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}", diff --git a/test_evergreen.py b/test_evergreen.py index 189375c..c467f87 100644 --- a/test_evergreen.py +++ b/test_evergreen.py @@ -2,6 +2,7 @@ import unittest import uuid +from datetime import datetime, timezone from unittest.mock import MagicMock, patch import requests @@ -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, ) @@ -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()