Skip to content
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
19 changes: 12 additions & 7 deletions cleanowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def main(): # pragma: no cover
if not dry_run:
# Remove that username from the codeowners_file_contents
file_changed = True
codeowners_file_contents = codeowners_file_contents.decoded.replace(
f"@{username}", ""
bytes_username = f"@{username}".encode("ASCII")
codeowners_file_contents_new = (
codeowners_file_contents.decoded.replace(bytes_username, b"")
)

# Update the CODEOWNERS file if usernames were removed
Expand All @@ -106,7 +107,7 @@ def main(): # pragma: no cover
title,
body,
repo,
codeowners_file_contents,
codeowners_file_contents_new,
commit_message,
codeowners_filepath,
)
Expand Down Expand Up @@ -175,7 +176,12 @@ def get_usernames_from_codeowners(codeowners_file_contents):


def commit_changes(
title, body, repo, codeowners_file_contents, commit_message, codeowners_filepath
title,
body,
repo,
codeowners_file_contents_new,
commit_message,
codeowners_filepath,
):
"""Commit the changes to the repo and open a pull reques and return the pull request object"""
default_branch = repo.default_branch
Expand All @@ -184,10 +190,9 @@ def commit_changes(
front_matter = "refs/heads/"
branch_name = "codeowners-" + str(uuid.uuid4())
repo.create_ref(front_matter + branch_name, default_branch_commit)
repo.create_file(
path=codeowners_filepath,
repo.file_contents(codeowners_filepath).update(
message=commit_message,
content=codeowners_file_contents.encode(), # Convert to bytes object
content=codeowners_file_contents_new,
branch=branch_name,
)

Expand Down
10 changes: 3 additions & 7 deletions test_cleanowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_commit_changes(self, mock_uuid):
mock_repo.default_branch = "main"
mock_repo.ref.return_value.object.sha = "abc123" # Mock SHA for latest commit
mock_repo.create_ref.return_value = True
mock_repo.create_file.return_value = True
mock_repo.file_contents.return_value = MagicMock()
mock_repo.file_contents.update.return_value = True
mock_repo.create_pull.return_value = "MockPullRequest"

title = "Test Title"
Expand All @@ -45,12 +46,7 @@ def test_commit_changes(self, mock_uuid):
mock_repo.create_ref.assert_called_once_with(
f"refs/heads/{branch_name}", "abc123"
)
mock_repo.create_file.assert_called_once_with(
path="CODEOWNERS",
message=commit_message,
content=dependabot_file.encode(),
branch=branch_name,
)
mock_repo.file_contents.assert_called_once_with("CODEOWNERS")
mock_repo.create_pull.assert_called_once_with(
title=title,
body=body,
Expand Down