diff --git a/jenkins_jobs/cli/subcommand/update.py b/jenkins_jobs/cli/subcommand/update.py index 374089dc1..2099bd9f3 100644 --- a/jenkins_jobs/cli/subcommand/update.py +++ b/jenkins_jobs/cli/subcommand/update.py @@ -81,6 +81,14 @@ class UpdateSubCommand(base.BaseSubCommand): help="update existing jobs only", ) + update.add_argument( + "--enabled-only", + action="store_true", + default=False, + dest="enabled_only", + help="update enabled jobs only", + ) + update_type = update.add_mutually_exclusive_group() update_type.add_argument( "-j", @@ -133,6 +141,24 @@ class UpdateSubCommand(base.BaseSubCommand): builder, xml_jobs, xml_views = self._generate_xmljobs(options, jjb_config) + if options.enabled_only: + # filter out jobs which are disabled + xml_jobs_filtered = [] + for xml_job in xml_jobs: + el = xml_job.xml.find("./disabled") + if el is not None: + if el.text == "true": + continue + xml_jobs_filtered.append(xml_job) + + logging.info( + "Will only deploy enabled jobs " + "(skipping {} disabled jobs)".format( + len(xml_jobs) - len(xml_jobs_filtered) + ) + ) + xml_jobs = xml_jobs_filtered + if options.update == "jobs": jobs, num_updated_jobs = builder.update_jobs( xml_jobs, diff --git a/tests/cmd/fixtures/cmd-002.yaml b/tests/cmd/fixtures/cmd-002.yaml index f844cc763..70d9e66e0 100644 --- a/tests/cmd/fixtures/cmd-002.yaml +++ b/tests/cmd/fixtures/cmd-002.yaml @@ -1,16 +1,20 @@ - job-template: + disabled: false name: 'bar001' description: 'My first job' - job-template: + disabled: false name: 'bar002' description: 'My second job' - job-template: + disabled: false name: 'baz001' description: 'My third job' - job-template: + disabled: true name: 'bam001' description: 'My fourth job' diff --git a/tests/cmd/subcommands/test_update.py b/tests/cmd/subcommands/test_update.py index 3cd7de392..70cc207c4 100644 --- a/tests/cmd/subcommands/test_update.py +++ b/tests/cmd/subcommands/test_update.py @@ -45,6 +45,29 @@ def test_update_jobs(mocker, fixtures_dir, default_config_file, execute_jenkins_ ) +def test_update_jobs_enabled_only( + mocker, fixtures_dir, default_config_file, execute_jenkins_jobs +): + """ + Test update_job is called with --enabled-only + """ + mocker.patch("jenkins_jobs.builder.jenkins.Jenkins.job_exists") + mocker.patch("jenkins_jobs.builder.jenkins.Jenkins.get_all_jobs") + reconfig_job = mocker.patch("jenkins_jobs.builder.jenkins.Jenkins.reconfig_job") + + path = fixtures_dir / "cmd-002.yaml" + args = ["--conf", default_config_file, "update", "--enabled-only", str(path)] + + execute_jenkins_jobs(args) + + reconfig_job.assert_has_calls( + [mock.call(job_name, mock.ANY) for job_name in ["bar001", "bar002", "baz001"]], + any_order=True, + ) + # make sure that there are only those 3 calls checked before + assert len(reconfig_job.call_args_list) == 3 + + def test_update_jobs_decode_job_output( mocker, fixtures_dir, default_config_file, execute_jenkins_jobs ):