summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/modules/zuul.py
blob: af011d662588d838307c56ffad61d8f25fbac969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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 itertools
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 handle_data(self, job_data):
        changed = False
        jobs = itertools.chain(
            job_data.get("job", {}).values(), job_data.get("job-template", {}).values()
        )
        for job in jobs:
            triggers = job.get("triggers")
            if not triggers:
                continue

            if "zuul" not in job.get("triggers", []) and "zuul-post" not in job.get(
                "triggers", []
            ):
                continue
            if "parameters" not in job:
                job["parameters"] = []
            if "zuul" in job.get("triggers", []):
                job["parameters"].extend(ZUUL_PARAMETERS)
                job["triggers"].remove("zuul")
            if "zuul-post" in job.get("triggers", []):
                job["parameters"].extend(ZUUL_POST_PARAMETERS)
                job["triggers"].remove("zuul-post")
            changed = True
        return changed