diff --git a/.github/linters/.isort.cfg b/.github/linters/.isort.cfg new file mode 100644 index 0000000..c76db01 --- /dev/null +++ b/.github/linters/.isort.cfg @@ -0,0 +1,2 @@ +[isort] +profile = black diff --git a/evergreen.py b/evergreen.py index 45b5632..e900fc4 100644 --- a/evergreen.py +++ b/evergreen.py @@ -63,12 +63,14 @@ def main(): # pragma: no cover # If dry_run is set, just print the dependabot file if dry_run: if follow_up_type == "issue": - print("\tEligible for configuring dependabot.") - count_eligible += 1 - print("\tConfiguration:\n" + dependabot_file) + skip = check_pending_issues_for_duplicates(title, repo) + if not skip: + print("\tEligible for configuring dependabot.") + count_eligible += 1 + print("\tConfiguration:\n" + dependabot_file) if follow_up_type == "pull": # Try to detect if the repo already has an open pull request for dependabot - skip = check_pending_pulls_for_duplicates(repo) + skip = check_pending_pulls_for_duplicates(title, repo) if not skip: print("\tEligible for configuring dependabot.") count_eligible += 1 @@ -78,15 +80,16 @@ def main(): # pragma: no cover # Get dependabot security updates enabled if possible if not is_dependabot_security_updates_enabled(repo.owner, repo.name, token): enable_dependabot_security_updates(repo.owner, repo.name, token) - if follow_up_type == "issue": - count_eligible += 1 - issue = repo.create_issue(title, body) - print("\tCreated issue " + issue.html_url) + skip = check_pending_issues_for_duplicates(title, repo) + if not skip: + count_eligible += 1 + issue = repo.create_issue(title, body) + print("\tCreated issue " + issue.html_url) else: count_eligible += 1 # Try to detect if the repo already has an open pull request for dependabot - skip = check_pending_pulls_for_duplicates(repo) + skip = check_pending_pulls_for_duplicates(title, repo) # Create a dependabot.yaml file, a branch, and a PR if not skip: @@ -144,18 +147,30 @@ def get_repos_iterator(organization, repository_list, github_connection): return repos -def check_pending_pulls_for_duplicates(repo) -> bool: +def check_pending_pulls_for_duplicates(title, repo) -> bool: """Check if there are any open pull requests for dependabot and return the bool skip""" pull_requests = repo.pull_requests(state="open") skip = False for pull_request in pull_requests: - if pull_request.head.ref.startswith("dependabot-"): + if pull_request.head.ref.startswith(title): print("\tPull request already exists: " + pull_request.html_url) skip = True break return skip +def check_pending_issues_for_duplicates(title, repo) -> bool: + """Check if there are any open issues for dependabot and return the bool skip""" + issues = repo.issues(state="open") + skip = False + for issue in issues: + if issue.title.startswith(title): + print("\tIssue already exists: " + issue.html_url) + skip = True + break + return skip + + def commit_changes(title, body, repo, dependabot_file): """Commit the changes to the repo and open a pull reques and return the pull request object""" default_branch = repo.default_branch diff --git a/test_evergreen.py b/test_evergreen.py index 95bb4b3..cea3152 100644 --- a/test_evergreen.py +++ b/test_evergreen.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch from evergreen import ( + check_pending_issues_for_duplicates, check_pending_pulls_for_duplicates, commit_changes, enable_dependabot_security_updates, @@ -233,7 +234,7 @@ def test_check_pending_pulls_for_duplicates_no_duplicates(self): mock_pull_request.head.ref = "not-dependabot-branch" mock_repo.pull_requests.return_value = [mock_pull_request] - result = check_pending_pulls_for_duplicates(mock_repo) + result = check_pending_pulls_for_duplicates("dependabot-branch", mock_repo) # Assert that the function returned the expected result self.assertEqual(result, False) @@ -245,7 +246,39 @@ def test_check_pending_pulls_for_duplicates_with_duplicates(self): mock_pull_request.head.ref = "dependabot-branch" mock_repo.pull_requests.return_value = [mock_pull_request] - result = check_pending_pulls_for_duplicates(mock_repo) + result = check_pending_pulls_for_duplicates( + mock_pull_request.head.ref, mock_repo + ) + + # Assert that the function returned the expected result + self.assertEqual(result, True) + + +class TestCheckPendingIssuesForDuplicates(unittest.TestCase): + """Test the check_pending_Issues_for_duplicates function.""" + + def test_check_pending_issues_for_duplicates_no_duplicates(self): + """Test the check_pending_Issues_for_duplicates function where there are no duplicates to be found.""" + mock_issue = MagicMock() + mock_issue.title = "Other Issue" + mock_issue.issues.return_value = [mock_issue] + + result = check_pending_issues_for_duplicates("Enable Dependabot", mock_issue) + + mock_issue.issues.assert_called_once_with(state="open") + + # Assert that the function returned the expected result + self.assertEqual(result, False) + + def test_check_pending_issues_for_duplicates_with_duplicates(self): + """Test the check_pending_issues_for_duplicates function where there are duplicates to be found.""" + mock_issue = MagicMock() + mock_issue.title = "Enable Dependabot" + mock_issue.issues.return_value = [mock_issue] + + result = check_pending_issues_for_duplicates("Enable Dependabot", mock_issue) + + mock_issue.issues.assert_called_once_with(state="open") # Assert that the function returned the expected result self.assertEqual(result, True)