diff --git a/cleanowners.py b/cleanowners.py index a700db8..f7fc22f 100644 --- a/cleanowners.py +++ b/cleanowners.py @@ -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 @@ -106,7 +107,7 @@ def main(): # pragma: no cover title, body, repo, - codeowners_file_contents, + codeowners_file_contents_new, commit_message, codeowners_filepath, ) @@ -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 @@ -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, ) diff --git a/test_cleanowners.py b/test_cleanowners.py index 6ffcc1b..792ce1a 100644 --- a/test_cleanowners.py +++ b/test_cleanowners.py @@ -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" @@ -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,