Add a "list" subcommand

Add a list subcommand that allows listing of all the jobs defined in
yaml configuration, or on the Jenkins master, with the option of taking
glob parameters to allow matching some job names.

Also supports the recursive and exclude path control options.

  jenkins-jobs list 'wikimedia-fundraising*'

  jenkins-jobs list -r -p configs/ 'wikimedia-fundraising*'

Co-Authored-By: Darragh Bailey <dbailey@hpe.com>
Co-Authored-By: Sorin Sbarnea <ssbarnea@redhat.com>
Change-Id: I897b9ed35561e455dc6b89c3bacec74b54777903
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
This commit is contained in:
Marc Abramowitz 2014-04-05 23:13:11 -07:00 committed by Thanh Ha
parent 72dfe41186
commit 8d4671e3a7
No known key found for this signature in database
GPG Key ID: B0CB27E00DA095AA
4 changed files with 161 additions and 1 deletions

View File

@ -81,7 +81,7 @@ def create_parser():
subparser = parser.add_subparsers(
dest='command',
help="update, test or delete job")
help="update, test, list or delete job")
extension_manager = extension.ExtensionManager(
namespace='jjb.cli.subcommands',

View File

@ -0,0 +1,74 @@
#!/usr/bin/env python
# Copyright (C) 2018 Sorin Sbarnea
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import sys
import jenkins_jobs.cli.subcommand.base as base
import jenkins_jobs.utils as utils
import jenkins_jobs.builder as builder
import jenkins_jobs.parser as parser
import jenkins_jobs.registry as registry
def list_duplicates(seq):
seen = set()
return set(x for x in seq if x in seen or seen.add(x))
class ListSubCommand(base.BaseSubCommand):
def parse_args(self, subparser):
list = subparser.add_parser('list', help="List jobs")
self.parse_option_recursive_exclude(list)
list.add_argument('names',
help='name(s) of job(s)',
nargs='*',
default=None)
list.add_argument('-p', '--path', default=None,
help='path to YAML file or directory')
def execute(self, options, jjb_config):
self.jjb_config = jjb_config
self.jenkins = builder.JenkinsManager(jjb_config)
jobs = self.get_jobs(options.names, options.path)
logging.info("Matching jobs: %d", len(jobs))
stdout = utils.wrap_stream(sys.stdout)
for job in jobs:
stdout.write((job + '\n').encode('utf-8'))
def get_jobs(self, jobs_glob=None, fn=None):
if fn:
r = registry.ModuleRegistry(self.jjb_config,
self.jenkins.plugins_list)
p = parser.YamlParser(self.jjb_config)
p.load_files(fn)
p.expandYaml(r, jobs_glob)
jobs = [j['name'] for j in p.jobs]
else:
jobs = [j['name'] for j in self.jenkins.get_jobs()
if not jobs_glob or parser.matches(j['name'], jobs_glob)]
jobs = sorted(jobs)
for duplicate in list_duplicates(jobs):
logging.warning("Found duplicate job name '%s', likely bug.",
duplicate)
logging.debug("Builder.get_jobs: returning %r", jobs)
return jobs

View File

@ -48,6 +48,7 @@ jjb.cli.subcommands =
delete=jenkins_jobs.cli.subcommand.delete:DeleteSubCommand
delete-all=jenkins_jobs.cli.subcommand.delete_all:DeleteAllSubCommand
get-plugins-info=jenkins_jobs.cli.subcommand.get_plugins_info:GetPluginsInfoSubCommand
list=jenkins_jobs.cli.subcommand.list:ListSubCommand
jenkins_jobs.projects =
externaljob=jenkins_jobs.modules.project_externaljob:ExternalJob
flow=jenkins_jobs.modules.project_flow:Flow

View File

@ -0,0 +1,85 @@
#!/usr/bin/env python
# Copyright (C) 2018 Sorin Sbarnea
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
import os
from testscenarios.testcase import TestWithScenarios
from tests.base import mock
from tests.cmd.test_cmd import CmdTestsBase
@mock.patch('jenkins_jobs.builder.JenkinsManager.get_plugins_info',
mock.MagicMock)
class ListFromJenkinsTests(TestWithScenarios, CmdTestsBase):
scenarios = [
('single',
dict(jobs=['job1'], globs=[], found=['job1'])),
('multiple',
dict(jobs=['job1', 'job2'], globs=[], found=['job1', 'job2'])),
('multiple_with_glob',
dict(jobs=['job1', 'job2', 'job3'], globs=["job[1-2]"],
found=['job1', 'job2'])),
('multiple_with_multi_glob',
dict(jobs=['job1', 'job2', 'job3', 'job4'],
globs=["job1", "job[24]"],
found=['job1', 'job2', 'job4'])),
]
@mock.patch('jenkins_jobs.builder.JenkinsManager.get_jobs')
def test_list(self, get_jobs_mock):
def _get_jobs():
return [{'name': name} for name in self.jobs]
get_jobs_mock.side_effect = _get_jobs
console_out = io.BytesIO()
args = ['--conf', self.default_config_file, 'list'] + self.globs
with mock.patch('sys.stdout', console_out):
self.execute_jenkins_jobs_with_args(args)
self.assertEqual(console_out.getvalue().decode('utf-8').rstrip(),
('\n'.join(self.found)))
@mock.patch('jenkins_jobs.builder.JenkinsManager.get_plugins_info',
mock.MagicMock)
class ListFromYamlTests(TestWithScenarios, CmdTestsBase):
scenarios = [
('all',
dict(globs=[], found=['bam001', 'bar001', 'bar002', 'baz001'])),
('some',
dict(globs=["*am*", "*002", "bar001"],
found=['bam001', 'bar001', 'bar002'])),
]
def test_list(self):
path = os.path.join(self.fixtures_path, 'cmd-002.yaml')
console_out = io.BytesIO()
with mock.patch('sys.stdout', console_out):
self.execute_jenkins_jobs_with_args(
['--conf',
self.default_config_file,
'list',
'-p',
path] + self.globs)
self.assertEqual(console_out.getvalue().decode('utf-8').rstrip(),
('\n'.join(self.found)))