From 54815aa8436eeaf045fdeed4dc3932f870e728ee Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Jun 30 2017 17:45:12 +0000 Subject: Query PDC to get the supported branches for gitolite. --- diff --git a/dist_git_auth.py b/dist_git_auth.py index db089dc..2dd310b 100644 --- a/dist_git_auth.py +++ b/dist_git_auth.py @@ -13,13 +13,14 @@ from __future__ import print_function import logging import os +import dogpile.cache +import pdc_client import werkzeug if 'PAGURE_CONFIG' not in os.environ \ and os.path.exists('/etc/pagure/pagure.cfg'): os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg' - import pagure # noqa: E402 from pagure import APP # noqa: E402 from pagure.lib import model # noqa: E402 @@ -28,12 +29,38 @@ from pagure.lib.git_auth import Gitolite3Auth, _read_file # noqa: E402 logging.config.dictConfig(APP.config.get('LOGGING') or {'version': 1}) _log = logging.getLogger(__name__) +cache = dogpile.cache.make_region().configure( + 'dogpile.cache.memory', + expiration_time=600, +) + + _blacklist = ''' - f[0-9][0-9] = @all - epel[0-9] = @all - epel[0-9][0-9] = @all - el[0-9] = @all - olpc[0-9] = @all''' +namespace2pdctype = { + 'rpms': 'rpm', + 'modules': 'module', + 'container': 'container', +} + +@cache.cache_on_arguments() +def get_supported_branches(namespace, package): + default_url = 'https://pdc.fedoraproject.org/rest_api/v1/' + url = pagure.APP.config.get('PDC_URL', default_url) + pdc = pdc_client.PDCClient(url, develop=True) + kwargs = dict( + global_component=package, + type=namespace2pdctype[namespace], + active=True, # Not EOL. + ) + branches = pdc.get_paged(pdc['component-branches'], **kwargs) + return [branch['name'] for branch in branches] + + class DistGitoliteAuth(Gitolite3Auth): """ A dist-git's gitolite authentication module. """ @@ -83,12 +110,21 @@ class DistGitoliteAuth(Gitolite3Auth): if repos not in ['tickets/', 'requests/']: config.append(' R = @all') - if repos == '': - config.append(_blacklist) - access = 'RWC' if project.is_fork: access = 'RW+' + + if repos == '': + # First, whitelist the supported branches from PDC + for branch in get_supported_branches(project.namespace, project.name): + config.append(' %s %s = %s' % (access, branch, project.user.user)) + for user in project.committers: + if user != project.user: + config.append(' %s = %s' % (access, branch, user.user)) + + # Then, blacklist a pattern over that (after). + config.append(_blacklist) + if project.committer_groups: config.append(' %s+ = @%s' % (access, ' @'.join( [ @@ -101,6 +137,7 @@ class DistGitoliteAuth(Gitolite3Auth): for user in project.committers: if user != project.user: config.append(' %s = %s' % (access, user.user)) + for deploykey in project.deploykeys: access = 'R' if deploykey.pushaccess: diff --git a/dist_git_auth_tests.py b/dist_git_auth_tests.py index d0a2c05..fbf026f 100644 --- a/dist_git_auth_tests.py +++ b/dist_git_auth_tests.py @@ -1,16 +1,21 @@ from __future__ import print_function + import tempfile import os +import mock + # These are the tests from the pagure/ git repo. # Run with PYTHONPATH=.:/path/to/pagure/checkout nosetests dist_git_auth_tests.py import tests -from dist_git_auth import DistGitoliteAuth +import dist_git_auth expected = """ repo test R = @all + RWC master = pingou + RWC f9000 = pingou - f[0-9][0-9] = @all - epel[0-9] = @all - epel[0-9][0-9] = @all @@ -23,6 +28,8 @@ repo requests/test repo test2 R = @all + RWC master = pingou + RWC f9000 = pingou - f[0-9][0-9] = @all - epel[0-9] = @all - epel[0-9][0-9] = @all @@ -35,6 +42,8 @@ repo requests/test2 repo somenamespace/test3 R = @all + RWC master = pingou + RWC f9000 = pingou - f[0-9][0-9] = @all - epel[0-9] = @all - epel[0-9][0-9] = @all @@ -63,12 +72,20 @@ class DistGitoliteAuthTestCase(tests.Modeltests): pass super(DistGitoliteAuthTestCase, self).tearDown() - def test_write_gitolite_acls(self): + @mock.patch('dist_git_auth.get_supported_branches') + def test_write_gitolite_acls(self, get_supported_branches): + get_supported_branches.return_value = ['master', 'f9000'] print("Initializing DB.") tests.create_projects(self.session) print("Generating %r" % self.configfile) - DistGitoliteAuth.write_gitolite_acls(self.session, self.configfile) + dist_git_auth.DistGitoliteAuth.write_gitolite_acls( + self.session, self.configfile) print("Checking the contents of %r" % self.configfile) with open(self.configfile, 'r') as f: contents = f.read() self.assertMultiLineEqual(contents.strip(), expected.strip()) + + def test_get_supported_branches(self): + expected = ['master', 'f26', 'f25', 'f24', 'el6'] + actual = dist_git_auth.get_supported_branches('rpms', 'nethack') + self.assertEquals(set(actual), set(expected))