From 6ee8b537c26336c6f67219a8b1ec529dd6daf846 Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Mar 02 2020 10:58:18 +0000 Subject: Clone config customization for namespaces After the 'clone' operation, a git config is updated with 'git-bz' config values. Now, these values can be customized based on the namespace of the cloned repository. Each namespace has its own key 'clone_config_' in config section. For namespaces, that missing this config ('tests' namespace is a good example), any of these values aren't updated. A repository has to be namespaced ('distgit_namespaced = True') for the config to be applied. Config file of x-pkg has to be modified. JIRA: COMPOSE-2638 Fixes: https://pagure.io/fedpkg/issue/231 Signed-off-by: Ondrej Nosek --- diff --git a/etc/rpkg/rpkg.conf b/etc/rpkg/rpkg.conf index 9b6b944..c3c71be 100644 --- a/etc/rpkg/rpkg.conf +++ b/etc/rpkg/rpkg.conf @@ -9,5 +9,5 @@ anongiturl = git://localhost/%(module)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(module)s diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 6ee2348..ac08653 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -200,8 +200,11 @@ class Commands(object): self.debug = False # Set an attribute verbose self.verbose = False - # Config to set after cloning - self.clone_config = None + # Configs to set after cloning (namespaces have its own config) + # These are initialized when detected in config, examples: + # self.clone_config_rpms = ... + # self.clone_config_modules = ... + # self.clone_config_container = ... # Git namespacing for more than just rpm build artifacts self.distgit_namespaced = distgit_namespaced # Kerberos realms used for username detection @@ -1613,10 +1616,22 @@ class Commands(object): # Inject ourselves as the git credential helper for https pushing conf_git.config('credential.helper', ' '.join(find_me() + ['gitcred'])) conf_git.config('credential.useHttpPath', 'true') - # Set any other clone_config - if self.clone_config: + + # skip repositories that are not namespaced + if not self.distgit_namespaced: + return + + # use "rpms" as a default namespace if omitted + namespace = repo.split("/")[0] if "/" in repo else "rpms" + # Has current namespace its own clone config? + selected_clone_config = None + if hasattr(self, "clone_config_{}".format(namespace)): + selected_clone_config = getattr(self, "clone_config_{}".format(namespace)) + + # Valid configuration is non-empty string + if selected_clone_config: base_repo = self.get_base_repo(repo) - clone_config = self.clone_config.strip() % { + selected_clone_config = selected_clone_config.strip() % { # base_repo will be just the repository name with namespace # stripped. 'repo': base_repo, @@ -1630,9 +1645,10 @@ class Commands(object): 'base_module': base_repo, 'module': repo } - for confline in clone_config.splitlines(): + for confline in selected_clone_config.splitlines(): if confline: - conf_git.config(*confline.split()) + # maxsplit=1 because value in clone_config can contain whitespaces + conf_git.config(*confline.split(None, 1)) def _add_git_excludes(self, conf_dir): """ diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index f416914..8defa8c 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -314,17 +314,14 @@ class cliClient(object): self._cmd.lookaside_request_params = items.get('lookaside_request_params') self._cmd.dry_run = self.args.dry_run - clone_config = items.get('clone_config') - self._cmd.clone_config = clone_config - if clone_config: - if '%(base_module)s' in clone_config: - self.log.warning( - 'Format argument base_module is deprecated in clone ' - 'config. Please use "repo" instead.') - if '%(module)s' in clone_config: - self.log.warning( - 'Format argument module is deprecated in clone config. ' - 'Please use "ns_repo" instead.') + # search config for keys "clone_config_{namespace}" and stores its values + # to object(s): self._cmd.clone_config_{namespace} + for key in items.keys(): + if re.match(r"^clone_config_\w+$", key): + clone_config = items.get(key) + if not clone_config: + self.log.debug("No clone config is set for '{}'".format(key)) + setattr(self._cmd, key, clone_config) # This function loads the extra stuff once we figure out what site # we are diff --git a/tests/commands/__init__.py b/tests/commands/__init__.py index 0c121a6..a93ea9d 100644 --- a/tests/commands/__init__.py +++ b/tests/commands/__init__.py @@ -29,7 +29,7 @@ class CommandTestCase(unittest.TestCase): self.gitbaseurl = 'TODO' self.kojiprofile = 'TODO' self.build_client = 'TODO' - self.clone_config = ''' + self.clone_config_rpms = ''' bz.default-component %(module)s sendemail.to %(module)s-owner@fedoraproject.org ''' diff --git a/tests/commands/test_clone.py b/tests/commands/test_clone.py index c530689..3739ac5 100644 --- a/tests/commands/test_clone.py +++ b/tests/commands/test_clone.py @@ -23,16 +23,16 @@ class CommandCloneTestCase(CommandTestCase): self.anongiturl, self.branchre, self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet) - cmd.clone_config = CLONE_CONFIG + cmd.clone_config_rpms = CLONE_CONFIG cmd.clone(self.module, anon=True) moduledir = os.path.join(self.path, self.module) self.assertTrue(os.path.isdir(os.path.join(moduledir, '.git'))) confgit = git.Git(moduledir) self.assertIn('gitcred', confgit.config('credential.helper')) - self.assertEqual(confgit.config('bz.default-component'), self.module) - self.assertEqual(confgit.config('sendemail.to'), - "%s-owner@fedoraproject.org" % self.module) + # there is no namespace used, therefore additional clone config was skipped + self.assertRaises(git.exc.GitCommandError, confgit.config, 'bz.default-component') + self.assertRaises(git.exc.GitCommandError, confgit.config, 'sendemail.to') def test_clone_anonymous_with_namespace(self): self.module = 'rpms/module1' @@ -43,7 +43,7 @@ class CommandCloneTestCase(CommandTestCase): self.anongiturl, self.branchre, self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet, distgit_namespaced=True) - cmd.clone_config = CLONE_CONFIG + cmd.clone_config_rpms = CLONE_CONFIG cmd.clone(self.module, anon=True) moduledir = os.path.join(self.path, 'module1') @@ -135,7 +135,7 @@ class CommandCloneTestCase(CommandTestCase): self.anongiturl, self.branchre, self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet, distgit_namespaced=True) - cmd.clone_config = 'bz.default-component %(base_module)s' + cmd.clone_config_rpms = 'bz.default-component %(base_module)s' cmd.clone(self.module, anon=True) moduledir = os.path.join(self.path, 'module1') @@ -201,7 +201,7 @@ class CommandCloneTestCase(CommandTestCase): self.anongiturl, self.branchre, self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet) - cmd.clone_config = CLONE_CONFIG + cmd.clone_config_rpms = CLONE_CONFIG cmd.clone_with_dirs(self.module, anon=True) moduledir_base = os.path.join(self.path, self.module) @@ -210,9 +210,9 @@ class CommandCloneTestCase(CommandTestCase): self.assertTrue(os.path.isdir(os.path.join(moduledir, '.git'))) confgit = git.Git(moduledir) self.assertIn('gitcred', confgit.config('credential.helper')) - self.assertEqual(confgit.config('bz.default-component'), self.module) - self.assertEqual(confgit.config('sendemail.to'), - "%s-owner@fedoraproject.org" % self.module) + # there is no namespace used, therefore additional clone config was skipped + self.assertRaises(git.exc.GitCommandError, confgit.config, 'bz.default-component') + self.assertRaises(git.exc.GitCommandError, confgit.config, 'sendemail.to') def test_clone_with_dirs_anonymous_git_excludes(self): self.make_new_git(self.module, diff --git a/tests/commands/test_push.py b/tests/commands/test_push.py index e8c92d8..3057cad 100644 --- a/tests/commands/test_push.py +++ b/tests/commands/test_push.py @@ -52,7 +52,7 @@ class CommandPushTestCase(CommandTestCase): self.anongiturl, self.branchre, self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet) - cmd.clone_config = CLONE_CONFIG + cmd.clone_config_rpms = CLONE_CONFIG cmd.clone(self.module, anon=True) cmd.path = os.path.join(self.path, self.module) os.chdir(os.path.join(self.path, self.module)) @@ -84,7 +84,7 @@ class TestPushWithPatches(CommandTestCase): self.kojiprofile, self.build_client, self.user, self.dist, self.target, self.quiet) - self.cmd.clone_config = CLONE_CONFIG + self.cmd.clone_config_rpms = CLONE_CONFIG self.cmd.clone(self.module, anon=True) self.cmd.path = os.path.join(self.path, self.module) os.chdir(os.path.join(self.path, self.module)) diff --git a/tests/fixtures/rpkg-container-own-config.conf b/tests/fixtures/rpkg-container-own-config.conf index 504d5a5..a40f430 100644 --- a/tests/fixtures/rpkg-container-own-config.conf +++ b/tests/fixtures/rpkg-container-own-config.conf @@ -7,7 +7,7 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s [rpkg.container-build] diff --git a/tests/fixtures/rpkg-deprecated-kojiconfig.conf b/tests/fixtures/rpkg-deprecated-kojiconfig.conf index dad93cd..232afe9 100644 --- a/tests/fixtures/rpkg-deprecated-kojiconfig.conf +++ b/tests/fixtures/rpkg-deprecated-kojiconfig.conf @@ -7,5 +7,5 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiconfig = /path/to/koji.conf build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s diff --git a/tests/fixtures/rpkg-greenwave.conf b/tests/fixtures/rpkg-greenwave.conf index 55069d5..bdf0227 100644 --- a/tests/fixtures/rpkg-greenwave.conf +++ b/tests/fixtures/rpkg-greenwave.conf @@ -7,7 +7,7 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s [rpkg.mbs] @@ -19,4 +19,4 @@ oidc_client_secret = notsecret oidc_scopes = openid,https://id.fedoraproject.org/scope/groups,https://mbs.fedoraproject.org/oidc/submit-build [rpkg.greenwave] -url = http://greenwave.localhost/ \ No newline at end of file +url = http://greenwave.localhost/ diff --git a/tests/fixtures/rpkg-has-distgit-namespaces.conf b/tests/fixtures/rpkg-has-distgit-namespaces.conf index 08b2306..4745bd2 100644 --- a/tests/fixtures/rpkg-has-distgit-namespaces.conf +++ b/tests/fixtures/rpkg-has-distgit-namespaces.conf @@ -7,7 +7,7 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s distgit_namespaced = True distgit_namespaces = rpms modules containers diff --git a/tests/fixtures/rpkg-ns.conf b/tests/fixtures/rpkg-ns.conf index 63ff75e..6a1e3da 100644 --- a/tests/fixtures/rpkg-ns.conf +++ b/tests/fixtures/rpkg-ns.conf @@ -7,6 +7,6 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s distgit_namespaced = True diff --git a/tests/fixtures/rpkg.conf b/tests/fixtures/rpkg.conf index 241194e..6f42081 100644 --- a/tests/fixtures/rpkg.conf +++ b/tests/fixtures/rpkg.conf @@ -7,7 +7,7 @@ anongiturl = git://localhost/%(repo)s branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji -clone_config = +clone_config_rpms = bz.default-component %(repo)s [rpkg.mbs]