diff --git a/tests/plugins/test_scm_request_processor.py b/tests/plugins/test_scm_request_processor.py index 676e2e8..1830c65 100644 --- a/tests/plugins/test_scm_request_processor.py +++ b/tests/plugins/test_scm_request_processor.py @@ -2263,6 +2263,86 @@ class TestCreateNewBranch: reason="Processed", ) + @patch("toddlers.plugins.scm_request_processor.TemporaryDirectory") + @patch("toddlers.plugins.scm_request_processor.git") + @patch("toddlers.plugins.scm_request_processor.fedora_account") + def test_create_new_branch_plugin_enabled_failure( + self, mock_fedora_account, mock_git, mock_temp_dir + ): + """ + Assert that plugin will be disabled even when there is failure during + branch creation. + """ + issue = {"id": 100, "user": {"name": "zlopez"}} + + repo = "repo" + default_branch = "rawhide" + branch = "f36" + namespace = "rpms" + action = "new_branch" + sls = {"rawhide": "2050-06-01"} + json = { + "repo": repo, + "branch": branch, + "namespace": namespace, + "action": action, + "sls": sls, + } + self.toddler.dist_git.get_project_contributors.return_value = { + "users": {"admin": [], "commit": [], "collaborators": []}, + "groups": {"admin": ["group"], "commit": [], "collaborators": []}, + } + self.toddler.dist_git.get_branches.return_value = [] + self.toddler.dist_git.get_default_branch.return_value = default_branch + self.toddler.dist_git._pagure_url = "https://fp.o" + self.toddler.dist_git.get_plugins.return_value = [ + scm_request_processor.PLUGIN_NAME + ] + self.toddler.dist_git.new_branch.side_effect = Exception() + mock_fedora_account.user_member_of.return_value = True + + mock_dir = MagicMock() + mock_dir.__enter__.return_value = "dir" + mock_temp_dir.return_value = mock_dir + + mock_git_repo = Mock() + mock_git_repo.first_commit.return_value = "SHA256" + + mock_git.clone_repo.return_value = mock_git_repo + + with pytest.raises(Exception): + self.toddler.create_new_branch(issue, json) + + # Asserts + self.toddler.dist_git.get_project_contributors.assert_called_with( + namespace, repo + ) + + self.toddler.dist_git.get_branches.assert_called_with(namespace, repo) + + mock_fedora_account.user_member_of.assert_called_with( + mock_fedora_account.get_user_by_username(), "group" + ) + + mock_git.clone_repo.assert_called_with( + "{0}/{1}/{2}".format(self.toddler.dist_git._pagure_url, namespace, repo), + "dir", + ) + + self.toddler.dist_git.get_default_branch.assert_called_with(namespace, repo) + + mock_git_repo.first_commit.assert_called_with(default_branch) + self.toddler.dist_git.disable_plugin.assert_called_with( + namespace, repo, scm_request_processor.PLUGIN_NAME + ) + + self.toddler.dist_git.new_branch.assert_called_with( + namespace, repo, branch, from_commit="SHA256" + ) + self.toddler.dist_git.enable_plugin.assert_called_with( + namespace, repo, scm_request_processor.PLUGIN_NAME + ) + @patch("toddlers.plugins.scm_request_processor.fedora_account") def test_create_new_branch_create_git_branch_disabled(self, mock_fedora_account): """ diff --git a/toddlers/plugins/scm_request_processor.py b/toddlers/plugins/scm_request_processor.py index ccb26ab..b7b07b2 100644 --- a/toddlers/plugins/scm_request_processor.py +++ b/toddlers/plugins/scm_request_processor.py @@ -931,12 +931,14 @@ class SCMRequestProcessor(ToddlerBase): if PLUGIN_NAME in plugins: plugin_enabled = True self.dist_git.disable_plugin(namespace, repo, PLUGIN_NAME) - self.dist_git.new_branch( - namespace, repo, branch_name, from_commit=commit - ) - # Re-enable the plugin - if plugin_enabled: - self.dist_git.enable_plugin(namespace, repo, PLUGIN_NAME) + try: + self.dist_git.new_branch( + namespace, repo, branch_name, from_commit=commit + ) + finally: + # Re-enable the plugin + if plugin_enabled: + self.dist_git.enable_plugin(namespace, repo, PLUGIN_NAME) new_branch_comment = ( "The branch was created in git. It " "may take up to 10 minutes before you have "