Add config option --enabled-only

That option can be used when calling the update subcommand. If the
option is set, only jobs which are enabled will be updated.
That is useful of there are a lot of disabled jobs which are not
required.

Change-Id: Ie2f268e2356a3b7e0a334a331696ca81e09790aa
This commit is contained in:
Thomas Bechtold 2023-02-21 12:44:05 +01:00
parent de86b2c130
commit 5e61fbaf00
3 changed files with 53 additions and 0 deletions

View File

@ -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,

View File

@ -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'

View File

@ -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
):