Merge "Add load-branch to tenant configuration"

This commit is contained in:
Zuul 2020-03-06 19:41:35 +00:00 committed by Gerrit Code Review
commit 8e940b21c0
6 changed files with 72 additions and 1 deletions

View File

@ -130,6 +130,18 @@ configuration. Some examples of tenant definitions are:
The items in the list follow the same format described in
**untrusted-projects**.
.. attr:: <project>
The config-projects have an additional config option that
may be specified optionally.
.. attr:: load-branch
:default: master
Define which branch is loaded from a config project. By
default config projects load Zuul configuration only
from the master branch.
.. attr:: untrusted-projects
A list of projects to be treated as untrusted in this tenant.

View File

@ -0,0 +1,6 @@
---
features:
- |
The tenant configuration now supports loading a different branch than
master from config projects.
See :attr:`tenant.config-projects.<project>.load-branch`.

View File

@ -0,0 +1,9 @@
- tenant:
name: tenant-one
source:
gerrit:
config-projects:
- common-config:
load-branch: stable
untrusted-projects:
- org/project1

View File

@ -331,6 +331,41 @@ class TestTenantExcludeAll(TenantParserTestCase):
"No error should have been accumulated")
class TestTenantConfigBranches(ZuulTestCase):
tenant_config_file = 'config/tenant-parser/simple.yaml'
def _validate_job(self, job, branch):
tenant_one = self.sched.abide.tenants.get('tenant-one')
jobs = tenant_one.layout.getJobs(job)
self.assertEquals(len(jobs), 1)
self.assertIn(jobs[0].source_context.branch, branch)
def test_tenant_config_load_branch(self):
"""
Tests that when specifying branches for a project only those branches
are parsed.
"""
# Job must be defined in master
common_job = 'common-config-job'
self._validate_job(common_job, 'master')
self.log.debug('Creating branches')
self.create_branch('common-config', 'stable')
self.create_branch('common-config', 'feat_x')
self.sched.reconfigure(self.config)
# Job must be defined in master
self._validate_job(common_job, 'master')
# Reconfigure with load-branch stable for common-config
self.newTenantConfig('config/tenant-parser/branch.yaml')
self.sched.reconfigure(self.config)
# Now job must be defined on stable branch
self._validate_job(common_job, 'stable')
class TestSplitConfig(ZuulTestCase):
tenant_config_file = 'config/split-config/main.yaml'

View File

@ -1430,6 +1430,7 @@ class TenantParser(object):
'shadow': to_list(str),
'exclude-unprotected-branches': bool,
'extra-config-paths': to_list(str),
'load-branch': str,
}}
project = vs.Any(str, project_dict)
@ -1642,6 +1643,7 @@ class TenantParser(object):
project_include = current_include
shadow_projects = []
project_exclude_unprotected_branches = None
project_load_branch = None
else:
project_name = list(conf.keys())[0]
project = source.getProject(project_name)
@ -1666,6 +1668,8 @@ class TenantParser(object):
if not x.endswith('/')])
extra_config_dirs = tuple([x[:-1] for x in extra_config_paths
if x.endswith('/')])
project_load_branch = conf[project_name].get(
'load-branch', None)
tenant_project_config = model.TenantProjectConfig(project)
tenant_project_config.load_classes = frozenset(project_include)
@ -1674,6 +1678,7 @@ class TenantParser(object):
project_exclude_unprotected_branches
tenant_project_config.extra_config_files = extra_config_files
tenant_project_config.extra_config_dirs = extra_config_dirs
tenant_project_config.load_branch = project_load_branch
return tenant_project_config
@ -1814,8 +1819,10 @@ class TenantParser(object):
untrusted_projects_config = model.UnparsedConfig()
for project in tenant.config_projects:
tpc = tenant.project_configs.get(project.canonical_name)
branch = tpc.load_branch if tpc.load_branch else 'master'
branch_cache = abide.getUnparsedBranchCache(
project.canonical_name, 'master')
project.canonical_name, branch)
tpc = tenant.project_configs[project.canonical_name]
unparsed_branch_config = branch_cache.get(tpc)

View File

@ -3360,6 +3360,8 @@ class TenantProjectConfig(object):
self.extra_config_files = ()
# The list of paths to look for extra zuul config dirs
self.extra_config_dirs = ()
# Load config from a different branch if this is a config project
self.load_branch = None
class ProjectPipelineConfig(ConfigObject):