Make get_job_info fetch_all_builds work with jobs in folders

Make sure to use fullName from the API results to correctly
compute the URL for the additional API request.

Change-Id: I7ca1f2e15a6c40883ba376b182c2c991ca76e8b2
Closes-Bug:#1828051
This commit is contained in:
Frank Lichtenheld 2019-05-07 16:55:48 +02:00
parent a5615d04f8
commit 190fbbb5ad
2 changed files with 36 additions and 3 deletions

View File

@ -432,7 +432,7 @@ class Jenkins(object):
all_builds_loaded = (oldest_loaded_build_number == first_build_number)
if all_builds_loaded:
return data
folder_url, short_name = self._get_job_folder(data["name"])
folder_url, short_name = self._get_job_folder(data["fullName"])
response = self.jenkins_open(requests.Request(
'GET', self._build_url(ALL_BUILDS, locals())
))
@ -440,7 +440,7 @@ class Jenkins(object):
data["builds"] = json.loads(response)["allBuilds"]
else:
raise JenkinsException('Could not fetch all builds from job[%s]' %
data["name"])
data["fullName"])
return data
def get_job_info(self, name, depth=0, fetch_all_builds=False):

View File

@ -35,7 +35,8 @@ class JenkinsGetJobInfoTest(JenkinsJobsTestBase):
u'user': u'unknown',
u'firstBuild': {u'number': 4},
u'builds': [{u'number': 5}],
u'name': u'Test Job'
u'name': u'Test Job',
u'fullName': u'Test Job'
}
all_builds_to_return = {u'allBuilds': [{u'number': 4},
{u'number': 5}]}
@ -75,6 +76,38 @@ class JenkinsGetJobInfoTest(JenkinsJobsTestBase):
self.make_url('job/a%20Folder/job/Test%20Job/api/json?depth=0'))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_all_builds_in_folder(self, jenkins_mock):
job_info_to_return = {
u'building': False,
u'msg': u'test',
u'revision': 66,
u'user': u'unknown',
u'firstBuild': {u'number': 4},
u'builds': [{u'number': 5}],
u'name': u'Test Job',
u'fullName': u'a Folder/Test Job'
}
all_builds_to_return = {u'allBuilds': [{u'number': 4},
{u'number': 5}]}
jenkins_mock.side_effect = [json.dumps(job_info_to_return),
json.dumps(all_builds_to_return)]
job_info = self.j.get_job_info(u'a Folder/Test Job', fetch_all_builds=True)
expected = dict(job_info_to_return)
expected["builds"] = [{u'number': 4}, {u'number': 5}]
self.assertEqual(job_info, expected)
self.assertEqual(
jenkins_mock.call_args_list[0][0][0].url,
self.make_url('job/a%20Folder/job/Test%20Job/api/json?depth=0'))
self.assertEqual(
jenkins_mock.call_args_list[1][0][0].url,
self.make_url(
'job/a%20Folder/job/Test%20Job/api/json?tree=allBuilds[number,url]'))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_regex(self, jenkins_mock):
jobs = [