Skip to content

Commit

Permalink
Merge pull request #26 from chrheg/main
Browse files Browse the repository at this point in the history
feat: Add check for existing issues
  • Loading branch information
zkoppert authored Jan 29, 2024
2 parents 90b159d + 0758ac7 commit 92d440d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/linters/.isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[isort]
profile = black
37 changes: 26 additions & 11 deletions evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions test_evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 92d440d

Please sign in to comment.