Add _projects to convert project list to dictionary

It would be much easier for job authors to directly access projects
via their canonical name, e.g.

 {{ zuul.projects['project'].src_dir }}

Currently they have to iterate the existing list, try to match the
project name and pull out the src_dir, which is all fairly ugly in
ansible/jinja.

Filters were proposed ( I6f3d0d8f8171cf654a4cf7a356ba72c16e260288),
but discussions indicated it would be better to change the interface.

This adds _projects as the dictionary view.  We can

 1. translate existing callers to the dictionary view _projects
    (and test)
 2. convert projects == _projects
 3. translate existing callers back to projects
 4. remove _projects

Needed-By: Id9a7c137ca5bed25d81087201091157c8401576a
Needed-By: I430277369f9ecb2ecc0a31f795c72bba83bcecff
Change-Id: I4476b9d4915d107e29b91229287865bff0ada305
This commit is contained in:
Ian Wienand 2017-10-18 08:21:19 +11:00
parent b0a95abc92
commit d61d98e567
2 changed files with 20 additions and 2 deletions

View File

@ -249,6 +249,13 @@ of item.
A boolean indicating whether this project appears in the
:attr:`job.required-projects` list for this job.
.. var:: _projects
:type: dict
The same as ``projects`` but a dictionary indexed by the
``name`` value of each entry. ``projects`` will be converted to
this.
.. var:: tenant
The name of the current Zuul tenant.

View File

@ -187,6 +187,7 @@ class ExecutorClient(object):
and item.change.newrev != '0' * 40):
zuul_params['newrev'] = item.change.newrev
zuul_params['projects'] = [] # Set below
zuul_params['_projects'] = {} # transitional to convert to dict
zuul_params['items'] = []
for i in all_items:
d = dict()
@ -270,14 +271,24 @@ class ExecutorClient(object):
projects.add(project)
for p in projects:
zuul_params['projects'].append(dict(
zuul_params['_projects'][p.canonical_name] = (dict(
name=p.name,
short_name=p.name.split('/')[-1],
canonical_hostname=p.canonical_hostname,
# Duplicate this into the dict too, so that iterating
# project.values() is easier for callers
canonical_name=p.canonical_name,
canonical_hostname=p.canonical_hostname,
src_dir=os.path.join('src', p.canonical_name),
required=(p in required_projects),
))
# We are transitioning "projects" from a list to a dict
# indexed by canonical name, as it is much easier to access
# values in ansible. Existing callers are converted to
# "_projects", then once "projects" is unused we switch it,
# then convert callers back. Finally when "_projects" is
# unused it will be removed.
for cn, p in zuul_params['_projects'].items():
zuul_params['projects'].append(p)
build = Build(job, uuid)
build.parameters = params