Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add check for existing issues #26

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
chrheg marked this conversation as resolved.
Show resolved Hide resolved
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