From 1ead727a2d6980855aaa1c7c302dce0483d5ea6c Mon Sep 17 00:00:00 2001 From: Vsevolod Fedorov Date: Mon, 25 Mar 2024 13:03:17 +0300 Subject: [PATCH] Story 2011049: Add --force flag to update-all command Without it, using this command in scripts is impossible. Change-Id: Ifd44fe0171a7753c9a09e9b05047ce2965c183f3 --- doc/source/changelog.rst | 1 + jenkins_jobs/cli/subcommand/delete_all.py | 8 ++++++- tests/cmd/subcommands/test_delete_all.py | 26 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 2e5eaed4c..b8e50a054 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -8,6 +8,7 @@ Features added ~~~~~~~~~~~~~~ * `--delete-old` flag for `update` cli command is now deletes obsolete views also, not only jobs. +* Add `--force` flag to `delete-all` cli command to allow it's usage in scripts. Bugs fixed ~~~~~~~~~~ diff --git a/jenkins_jobs/cli/subcommand/delete_all.py b/jenkins_jobs/cli/subcommand/delete_all.py index bee23cf86..8b47030ef 100644 --- a/jenkins_jobs/cli/subcommand/delete_all.py +++ b/jenkins_jobs/cli/subcommand/delete_all.py @@ -52,6 +52,12 @@ class DeleteAllSubCommand(base.BaseSubCommand): default=False, help="delete only views", ) + delete_all.add_argument( + "-f", + "--force", + action="store_true", + help="Do not ask interactively for confirmation. Ba cautious!", + ) def execute(self, options, jjb_config): builder = JenkinsManager(jjb_config) @@ -68,7 +74,7 @@ class DeleteAllSubCommand(base.BaseSubCommand): else: reach.update(("jobs", "views")) - if not utils.confirm( + if not options.force and not utils.confirm( "Sure you want to delete *ALL* {} from Jenkins " "server?\n(including those not managed by Jenkins " "Job Builder)".format(" AND ".join(reach)) diff --git a/tests/cmd/subcommands/test_delete_all.py b/tests/cmd/subcommands/test_delete_all.py index c3683c471..717eccc27 100644 --- a/tests/cmd/subcommands/test_delete_all.py +++ b/tests/cmd/subcommands/test_delete_all.py @@ -44,3 +44,29 @@ def test_delete_all_abort(mocker, default_config_file, execute_jenkins_jobs): args = ["--conf", default_config_file, "delete-all"] with pytest.raises(SystemExit): execute_jenkins_jobs(args) + + +def test_delete_all_forced(mocker, default_config_file, execute_jenkins_jobs): + """ + Test handling the deletion of a job and a view with --force flag. + """ + + delete_jobs = mocker.patch( + "jenkins_jobs.cli.subcommand.base.JenkinsManager.delete_all_jobs" + ) + delete_views = mocker.patch( + "jenkins_jobs.cli.subcommand.base.JenkinsManager.delete_all_views" + ) + get_jobs = mocker.patch("jenkins_jobs.builder.jenkins.Jenkins.get_all_jobs") + get_jobs.return_value = [{"name": name} for name in ["job-1"]] + get_views = mocker.patch("jenkins_jobs.builder.jenkins.Jenkins.get_views") + get_views.return_value = [{"name": name} for name in ["view-1"]] + input = mocker.patch("jenkins_jobs.utils.input", return_value="n") + + args = ["--conf", default_config_file, "delete-all", "--force"] + execute_jenkins_jobs(args) + + input.assert_not_called() + + assert delete_jobs.call_count == 1 + assert delete_views.call_count == 1