Skip to content

Commit

Permalink
Merge pull request #210 from github/fix-update-existing-logic
Browse files Browse the repository at this point in the history
fix: update_existing logic
  • Loading branch information
zkoppert committed Aug 30, 2024
2 parents ff2f3ff + 9e1ea3a commit d6e090a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
21 changes: 12 additions & 9 deletions evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,19 @@ def main(): # pragma: no cover
filename_list = [".github/dependabot.yaml", ".github/dependabot.yml"]
dependabot_filename_to_use = filename_list[0] # Default to the first filename
for filename in filename_list:
existing_config = check_existing_config(repo, filename, update_existing)
existing_config = check_existing_config(repo, filename)
if existing_config:
dependabot_filename_to_use = filename
break

if existing_config and not update_existing:
print(
"Skipping "
+ repo.full_name
+ " (dependabot file already exists and update_existing is False)"
)
continue

if created_after_date and is_repo_created_date_before(
repo.created_at, created_after_date
):
Expand Down Expand Up @@ -207,15 +215,14 @@ def is_dependabot_security_updates_enabled(owner, repo, access_token):
return False


def check_existing_config(repo, filename, update_existing):
def check_existing_config(repo, filename):
"""
Check if the dependabot file already exists in the
repository and return the existing config if it does
Args:
repo (github3.repos.repo.Repository): The repository to check
filename (str): The dependabot configuration filename to check
update_existing (bool): Whether to update existing dependabot configuration files
Returns:
github3.repos.contents.Contents | None: The existing config if it exists, otherwise None
Expand All @@ -224,14 +231,10 @@ def check_existing_config(repo, filename, update_existing):
try:
existing_config = repo.file_contents(filename)
if existing_config.size > 0:
if not update_existing:
print(
"Skipping " + repo.full_name + " (dependabot file already exists)"
)
return None
return existing_config
except github3.exceptions.NotFoundError:
pass
return existing_config
return None


def enable_dependabot_security_updates(owner, repo, access_token):
Expand Down
18 changes: 2 additions & 16 deletions test_evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def test_check_existing_config_with_existing_config(self):
filename = "dependabot.yaml"
mock_repo.file_contents.return_value.size = 5

result = check_existing_config(mock_repo, filename, True)
result = check_existing_config(mock_repo, filename)

self.assertIsNotNone(result)

Expand All @@ -676,21 +676,7 @@ def test_check_existing_config_without_existing_config(self):
mock_response
)

result = check_existing_config(mock_repo, "dependabot.yml", True)

self.assertIsNone(result)

def test_check_existing_config_with_existing_config_without_update_existing_set(
self,
):
"""
Test the case where there is an existing configuration but UPDATE_EXISTING is False
"""
mock_repo = MagicMock()
filename = "dependabot.yaml"
mock_repo.file_contents.return_value.size = 5

result = check_existing_config(mock_repo, filename, False)
result = check_existing_config(mock_repo, "dependabot.yml")

self.assertIsNone(result)

Expand Down

0 comments on commit d6e090a

Please sign in to comment.