Add get_job_info_regex method to get info about a set of jobs
The get_job_info_regex method allows a user to get a list of jobs information that match by the job name. Change-Id: I1cfccdef0bbd81a039725a1ed63eae077d610c4b
This commit is contained in:
@@ -47,6 +47,7 @@ See examples at :doc:`example`
|
||||
|
||||
import base64
|
||||
import json
|
||||
import re
|
||||
import socket
|
||||
|
||||
import six
|
||||
@@ -219,6 +220,22 @@ class Jenkins(object):
|
||||
raise JenkinsException(
|
||||
"Could not parse JSON info for job[%s]" % name)
|
||||
|
||||
def get_job_info_regex(self, pattern, depth=0):
|
||||
'''Get a list of jobs information that contain names which match the
|
||||
regex pattern.
|
||||
|
||||
:param pattern: regex pattern, ``str``
|
||||
:param depth: JSON depth, ``int``
|
||||
:returns: List of jobs info, ``list``
|
||||
'''
|
||||
result = []
|
||||
jobs = self.get_jobs()
|
||||
for job in jobs:
|
||||
if re.search(pattern, job['name']):
|
||||
result.append(self.get_job_info(job['name'], depth=depth))
|
||||
|
||||
return result
|
||||
|
||||
def get_job_name(self, name):
|
||||
'''Return the name of a job using the API.
|
||||
|
||||
|
||||
@@ -542,6 +542,26 @@ class JenkinsTest(unittest.TestCase):
|
||||
jenkins_mock.call_args[0][0].get_full_url(),
|
||||
u'http://example.com/job/Test%20Job/api/json?depth=0')
|
||||
|
||||
@patch.object(jenkins.Jenkins, 'jenkins_open')
|
||||
def test_get_job_info_regex(self, jenkins_mock):
|
||||
jobs = [
|
||||
{u'name': u'my-job-1'},
|
||||
{u'name': u'my-job-2'},
|
||||
{u'name': u'your-job-1'},
|
||||
{u'name': u'Your-Job-1'},
|
||||
{u'name': u'my-project-1'},
|
||||
]
|
||||
job_info_to_return = {u'jobs': jobs}
|
||||
jenkins_mock.return_value = json.dumps(job_info_to_return)
|
||||
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
|
||||
|
||||
self.assertEqual(len(j.get_job_info_regex('her-job')), 0)
|
||||
self.assertEqual(len(j.get_job_info_regex('my-job-1')), 1)
|
||||
self.assertEqual(len(j.get_job_info_regex('my-job')), 2)
|
||||
self.assertEqual(len(j.get_job_info_regex('job')), 3)
|
||||
self.assertEqual(len(j.get_job_info_regex('project')), 1)
|
||||
self.assertEqual(len(j.get_job_info_regex('[Yy]our-[Jj]ob-1')), 2)
|
||||
|
||||
@patch.object(jenkins.Jenkins, 'jenkins_open')
|
||||
def test_get_job_info__None(self, jenkins_mock):
|
||||
jenkins_mock.return_value = None
|
||||
|
||||
Reference in New Issue
Block a user