diff --git a/evergreen.py b/evergreen.py index 51513ac..6719c59 100644 --- a/evergreen.py +++ b/evergreen.py @@ -80,9 +80,7 @@ def main(): # pragma: no cover except github3.exceptions.NotFoundError: pass - if created_after_date and repo.created_at.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) @@ -161,8 +159,18 @@ def main(): # pragma: no cover print("Done. " + str(count_eligible) + " repositories were eligible.") +def is_repo_created_date_before(repo_created_at: str, created_after_date: str): + """Check if the repository was created before the created_after_date""" + repo_created_at_date = datetime.fromisoformat(repo_created_at).replace(tzinfo=None) + return created_after_date and repo_created_at_date < datetime.strptime( + created_after_date, "%Y-%m-%d" + ) + + 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..15aafcf 100644 --- a/test_evergreen.py +++ b/test_evergreen.py @@ -15,6 +15,7 @@ get_global_project_id, get_repos_iterator, is_dependabot_security_updates_enabled, + is_repo_created_date_before, link_item_to_project, ) @@ -579,5 +580,45 @@ 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_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_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_has_no_time_zone(self): + """Test the repo.created_at date is after created_after_date.""" + repo_created_at = "2020-01-01" + created_after_date = "2021-01-01" + + result = is_repo_created_date_before(repo_created_at, created_after_date) + + self.assertTrue(result) + + def test_is_created_after_date_is_empty_string(self): + """Test the repo.created_at date is after created_after_date.""" + repo_created_at = "2020-01-01" + created_after_date = "" + + result = is_repo_created_date_before(repo_created_at, created_after_date) + + self.assertFalse(result) + + if __name__ == "__main__": unittest.main()