From 00aed529cf22796fdae0d23f844221df640cc7c2 Mon Sep 17 00:00:00 2001 From: mprahl Date: Feb 23 2018 03:01:35 +0000 Subject: Use PDC instead of Bodhi to get the active release branches Fixes #187 Signed-off-by: mprahl --- diff --git a/fedpkg/cli.py b/fedpkg/cli.py index a203b91..e8ff6be 100644 --- a/fedpkg/cli.py +++ b/fedpkg/cli.py @@ -462,7 +462,7 @@ suggest_reboot=False raise rpkgError('You must specify a branch if you are not in ' 'a git repository') - bodhi_url = config.get('{0}.bodhi'.format(name), 'url') + pdc_url = config.get('{0}.pdc'.format(name), 'url') if branch: if is_epel(branch): assert_valid_epel_package(module_name, branch) @@ -474,7 +474,7 @@ suggest_reboot=False 'Only characters, numbers, periods, dashes, ' 'underscores, and pluses are allowed in module branch ' 'names') - release_branches = get_release_branches(bodhi_url) + release_branches = get_release_branches(pdc_url) if branch in release_branches: if service_levels: raise rpkgError( @@ -489,14 +489,13 @@ suggest_reboot=False # If service levels were provided, verify them if service_levels: - pdc_url = config.get('{0}.pdc'.format(name), 'url') sl_dict = sl_list_to_dict(service_levels) verify_sls(pdc_url, sl_dict) pagure_url = config.get('{0}.pagure'.format(name), 'url') pagure_token = get_pagure_token(config, name) if all_releases: - release_branches = get_release_branches(bodhi_url) + release_branches = get_release_branches(pdc_url) branches = [b for b in release_branches if re.match(r'^(f\d+)$', b)] else: diff --git a/fedpkg/utils.py b/fedpkg/utils.py index d241308..b3d704c 100644 --- a/fedpkg/utils.py +++ b/fedpkg/utils.py @@ -18,7 +18,6 @@ from six.moves.urllib.parse import urlencode from six.moves.configparser import NoSectionError, NoOptionError import requests from requests.exceptions import ConnectionError -from fedora.client.bodhi import Bodhi2Client from pyrpkg import rpkgError @@ -97,26 +96,55 @@ def new_pagure_issue(url, token, title, body): url.rstrip('/'), rv.json()['issue']['id']) -def get_release_branches(bodhi_url): +def get_release_branches(url): """ - Get the active Fedora release branches from Bodhi - :param bodhi_url: a string of the URL to Bodhi + Get the active Fedora release branches from PDC + :param url: a string of the URL to PDC :return: a set containing the active Fedora release branches """ - bodhi = Bodhi2Client(bodhi_url) branches = set() - page = 1 + api_url = '{0}/rest_api/v1/product-versions/'.format(url.rstrip('/')) + query_args = { + 'fields': ['short', 'version'], + 'active': True + } while True: - rv = bodhi.send_request('releases', auth=False, params={'page': page}) - for release in rv['releases']: - if release['state'] == 'current': - branches.add(release['branch']) - if page < rv['pages']: - page += 1 - else: - break + try: + rv = requests.get(api_url, params=query_args, timeout=60) + except ConnectionError as error: + error_msg = ('The connection to PDC failed while trying to get ' + 'the active release branches. The error was: {0}' + .format(str(error))) + raise rpkgError(error_msg) - return branches + if not rv.ok: + base_error_msg = ('The following error occurred while trying to ' + 'get the active release branches in PDC: {0}') + raise rpkgError(base_error_msg.format(rv.text)) + + rv_json = rv.json() + for product_version in rv_json['results']: + # If the version is not a digit we can ignore it (e.g. rawhide) + if not product_version['version'].isdigit(): + continue + + if product_version['short'] == 'epel': + prefix = 'epel' + if product_version['version'] == '6': + prefix = 'el' + branches.add('{0}{1}'.format( + prefix, product_version['version'])) + elif product_version['short'] == 'fedora': + branches.add('f{0}'.format(product_version['version'])) + + if rv_json['next']: + # Clear the query_args because they are baked into the "next" URL + query_args = {} + api_url = rv_json['next'] + else: + # We've gone through every page, so we can return the found + # branches + return branches def sl_list_to_dict(sls): diff --git a/test/test_utils.py b/test/test_utils.py index cff22bd..7ca395f 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -115,29 +115,29 @@ class TestUtils(CliTestCase): assert str(e) == ('The SL "{0}" must expire on June 1st or ' 'December 1st'.format(eol)) - @patch('fedpkg.utils.Bodhi2Client') - def test_get_release_branches(self, mock_bodhi): + @patch('requests.get') + def test_get_release_branches(self, mock_request_get): """Test that get_release_branches returns all the active Fedora release branches. """ - mock_bodhi_client = Mock() - mock_bodhi_client.send_request.return_value = { - u'page': 1, - u'pages': 1, - u'releases': [ - {'state': 'current', 'branch': 'el6'}, - {'state': 'archived', 'branch': 'f24'}, - {'state': 'current', 'branch': 'epel7'}, - {'state': 'current', 'branch': 'f25'}, - {'state': 'archived', 'branch': 'el5'}, - {'state': 'archived', 'branch': 'f23'}, - {'state': 'current', 'branch': 'f26'}, - {'state': 'pending', 'branch': 'f27m'}, - {'state': 'current', 'branch': 'f27'} - ], - u'rows_per_page': 20, - u'total': 11} - mock_bodhi.return_value = mock_bodhi_client - expected = set(['el6', 'epel7', 'f25', 'f26', 'f27']) - actual = utils.get_release_branches('http://bodhi.local') + mock_rv = Mock() + mock_rv.ok = True + # This abbreviated data returned from the product-versions PDC API + mock_rv.json.return_value = { + 'count': 7, + 'next': None, + 'previous': None, + 'results': [ + {'short': 'epel', 'version': '6'}, + {'short': 'epel', 'version': '7'}, + {'short': 'fedora', 'version': '25'}, + {'short': 'fedora', 'version': '26'}, + {'short': 'fedora', 'version': '27'}, + {'short': 'fedora', 'version': '28'}, + {'short': 'fedora', 'version': 'rawhide'} + ] + } + mock_request_get.return_value = mock_rv + expected = set(['el6', 'epel7', 'f25', 'f26', 'f27', 'f28']) + actual = utils.get_release_branches('http://pdc.local') self.assertEqual(expected, actual)