Vsevolod Fedorov af9e03ec08 Rewrite YAML parser
Rewrite YAML parser, YAML objects and parameters expansion logic to
enable better control over expansion logic.
Broken backward compatilibity:
* More agressive parameter expansion. This may lead to parameters
  expanded in places where they were not expanded before.
* Top-level elements, which is not known to parser (such as 'job',
  'view', 'project' etc), are now lead to parse failures.
  Prepend them with underscore to be ignored by parser.
* Files included using '!include-raw:' elements and having formatting in
  it's path ('lazy-loaded' in previous implementation) are now expanded
  too.
  Use '!include-raw-escape:' for them instead.
  See changes in these tests for examples:
    tests/yamlparser/job_fixtures/lazy-load-jobs-multi001.yaml
    tests/yamlparser/job_fixtures/lazy-load-jobs-multi002.yaml
    tests/yamlparser/job_fixtures/lazy-load-jobs001.yaml
* Parameters with template value using itself were substituted as is.
  For example: "timer: '{timer}'" was expanded to "{timer}". Now it
  leads to recursive parameter error.
  See changes in this test for example:
    tests/yamlparser/job_fixtures/parameter_name_reuse_default.*
    ->
    tests/yamlparser/error_fixtures/parameter_name_reuse_default.*
* When job group includes a job which was never declared, it was just
  ignored. Now it fails: job is missing.
  See changes in this test for example:
    tests/yamlparser/job_fixtures/job_group_includes_missing_job.*
    ->
    tests/yamlparser/error_fixtures/job_group_includes_missing_job.*

Change-Id: Ief4e515f065a1b9e0f74fe06d7e94fa77d69f273
2023-02-28 20:16:57 +03:00

202 lines
5.3 KiB
Python

# Copyright 2012 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
The Zuul module adds jobs parameters to manually run a build as Zuul would
have. It is entirely optional, Zuul 2.0+ pass the parameters over Gearman.
.. _expected by Zuul: \
https://opendev.org/zuul/zuul/src/tag/2.6.0/doc/source/launchers.rst#user-content-zuul-parameters
"""
import jenkins_jobs.modules.base
def zuul():
"""yaml: zuul
Configure this job to be triggered by Zuul.
Adds parameters describing the change triggering the build such as the
branch name, change number and patchset.
See parameters `expected by Zuul`_.
Example::
triggers:
- zuul
"""
def zuul_post():
"""yaml: zuul-post
Configure this post-merge job to be triggered by Zuul.
Adds parameters describing the reference update triggering the build, which
are the previous and next revisions in full (40 hexadecimal sha1) and short
form.
See parameters `expected by Zuul`_.
Example::
triggers:
- zuul-post
"""
ZUUL_PARAMETERS = [
{
"string": {
"description": "Zuul provided key to link builds with Gerrit events",
"name": "ZUUL_UUID",
}
},
{
"string": {
"description": "Zuul provided key to link builds with Gerrit"
" events (deprecated use ZUUL_UUID instead)",
"name": "UUID",
}
},
{
"string": {
"description": "Zuul pipeline triggering this job",
"name": "ZUUL_PIPELINE",
}
},
{
"string": {
"description": "URL of Zuul's git repos accessible to workers",
"name": "ZUUL_URL",
}
},
{
"string": {
"description": "Branch name of triggering project",
"name": "ZUUL_PROJECT",
}
},
{
"string": {
"description": "Branch name of triggering change",
"name": "ZUUL_BRANCH",
}
},
{
"string": {
"description": "List of dependent changes to merge",
"name": "ZUUL_CHANGES",
}
},
{
"string": {
"description": "Reference for the merged commit(s) to use",
"name": "ZUUL_REF",
}
},
{
"string": {
"description": "The commit SHA1 at the head of ZUUL_REF",
"name": "ZUUL_COMMIT",
}
},
{"string": {"description": "List of included changes", "name": "ZUUL_CHANGE_IDS"}},
{"string": {"description": "ID of triggering change", "name": "ZUUL_CHANGE"}},
{
"string": {
"description": "Patchset of triggering change",
"name": "ZUUL_PATCHSET",
}
},
{
"string": {
"description": "Zuul considered this job voting or not",
"name": "ZUUL_VOTING",
}
},
]
ZUUL_POST_PARAMETERS = [
{
"string": {
"description": "Zuul provided key to link builds with Gerrit events",
"name": "ZUUL_UUID",
}
},
{
"string": {
"description": "Zuul provided key to link builds with Gerrit"
" events (deprecated use ZUUL_UUID instead)",
"name": "UUID",
}
},
{
"string": {
"description": "Zuul pipeline triggering this job",
"name": "ZUUL_PIPELINE",
}
},
{
"string": {
"description": "URL of Zuul's git repos accessible to workers",
"name": "ZUUL_URL",
}
},
{
"string": {
"description": "Branch name of triggering project",
"name": "ZUUL_PROJECT",
}
},
{
"string": {
"description": "Name of updated reference triggering this job",
"name": "ZUUL_REF",
}
},
{
"string": {
"description": "Name of updated reference triggering this job",
"name": "ZUUL_REFNAME",
}
},
{"string": {"description": "Old SHA at this reference", "name": "ZUUL_OLDREV"}},
{"string": {"description": "New SHA at this reference", "name": "ZUUL_NEWREV"}},
{
"string": {
"description": "Shortened new SHA at this reference",
"name": "ZUUL_SHORT_NEWREV",
}
},
]
class Zuul(jenkins_jobs.modules.base.Base):
sequence = 0
def amend_job_dict(self, job):
triggers = job.get("triggers", [])
if "zuul" not in triggers and "zuul-post" not in triggers:
return False
parameters = job.setdefault("parameters", [])
if "zuul" in triggers:
parameters.extend(ZUUL_PARAMETERS)
triggers.remove("zuul")
if "zuul-post" in triggers:
parameters.extend(ZUUL_POST_PARAMETERS)
triggers.remove("zuul-post")
return True