summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/modules/view_list.py
blob: 5c1e1e5f030580f39b7043e8604862c14997d343 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# Copyright 2015 Openstack Foundation

# 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 view list module handles creating Jenkins List views.

To create a list view specify ``list`` in the ``view-type`` attribute
to the :ref:`view_list` definition.

:View Parameters:
    * **name** (`str`): The name of the view.
    * **view-type** (`str`): The type of view.
    * **description** (`str`): A description of the view. (default '')
    * **filter-executors** (`bool`): Show only executors that can
      execute the included views. (default false)
    * **filter-queue** (`bool`): Show only included jobs in builder
      queue. (default false)
    * **job-name** (`list`): List of jobs to be included.
    * **job-filters** (`dict`): Job filters to be included. Requires
      :jenkins-plugins:`View Job Filters <view-job-filters>`

        * **most-recent** (`dict`)
            :most-recent:
                * **max-to-include** (`int`): Maximum number of jobs
                  to include. (default 0)
                * **check-start-time** (`bool`): Check job start
                  time. (default false)

        * **build-duration** (`dict`)
            :build-duration:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **build-duration-type** ('str'): Duration of the
                  build. (default Latest)
                * **amount-type**: ('str'): Duration in hours,
                  days or builds. (default Hours)
                * **amount**: ('int'): How far back to check.
                  (default 0)
                * **less-than**: ('bool'): Check build duration
                  less than or more than. (default True)
                * **build-duration-minutes**: ('int'): Build
                  duration minutes. (default 0)

        * **build-trend** (`dict`)
            :build-trend:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **build-trend-type** ('str'): Duration of the
                  build. (default Latest)
                * **amount-type**: ('str'): Duration in hours,
                  days or builds. (default Hours)
                * **amount**: ('int'): How far back to check.
                  (default 0)
                * **status**: ('str'): Job status.
                  (default Completed)

        * **job-status** (`dict`)
            :job-status:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **unstable** ('bool'): Jobs with status
                  unstable. (default False)
                * **failed** ('bool'): Jobs with status
                  failed. (default False)
                * **aborted** ('bool'): Jobs with status
                  aborted. (default False)
                * **disabled** ('bool'): Jobs with status
                  disabled. (default False)
                * **stable** ('bool'): Jobs with status
                  stable. (default False)

        * **fallback** (`dict`)
            :fallback:
                * **fallback-type** ('str'): Fallback type to include/exclude
                  for all jobs in a view, if no jobs have been included by
                  previous filters. (default REMOVE_ALL_IF_ALL_INCLUDED)

        * **build-status** (`dict`)
            :build-status:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **never-built** ('bool'): Jobs that are never
                  built. (default False)
                * **building** ('bool'): Jobs that are being
                  built. (default False)
                * **in-build-queue** ('bool'): Jobs that are in
                  the build queue. (default False)

        * **user-relevence** (`dict`)
            :user-relevence:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **build-count** ('str'): Count of builds.
                  (default AtLeastOne)
                * **amount-type**: ('str'): Duration in hours,
                  days or builds. (default Hours)
                * **amount**: ('int'): How far back to check.
                  (default 0)
                * **match-user-id** ('bool'): Jobs matching
                  user-id. (default False)
                * **match-user-fullname** ('bool'): Jobs
                  matching user fullname. (default False)
                * **ignore-case** ('bool'): Ignore case.
                  (default False)
                * **ignore-whitespace** ('bool'): Ignore
                  whitespace. (default False)
                * **ignore-non-alphaNumeric** ('bool'): Ignore
                  non-alphaNumeric. (default False)
                * **match-builder** ('bool'): Jobs matching
                  builder. (default False)
                * **match-email** ('bool'): Jobs matching
                  email. (default False)
                * **match-scm-changes** ('bool'): Jobs matching
                  scm changes. (default False)

        * **regex-job** (`dict`)
            :regex-job:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **regex-name** ('str'): Regular expression name.
                  (default '')
                * **regex** ('str'): Regular expression. (default '')

        * **job-tpye** (`dict`)
            :job-type:
                * **match-type** ('str'): Jobs that match a filter to include.
                  (default includeMatched)
                * **job-type** ('str'): Type of Job.
                  (default hudson.model.FreeStyleProject)

        * **parameter** (`dict`)
            :parameter:
                * **match-type** ('str'): Jobs that match a filter to include.
                  (default includeMatched)
                * **name** ('str'): Job name to match. (default '')
                * **value** ('str'): Value to match. (default '')
                * **desc** ('str'): Description to match. (default '')
                * **use-default-value** ('bool'): Use default value.
                  (default False)
                * **match-builds-in-progress** ('bool'): Match build in
                  progress. (default False)
                * **match-all-builds** ('bool'): Match all builds.
                  (default False)
                * **max-builds-to-match** ('int'): Maximum builds to match.
                  (default 0)

        * **other-views** (`dict`)
            :other-views:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)
                * **view-name** ('str'): View name.
                  (default select a view other than this one)

        * **scm** (`dict`)
            :scm:
                * **match-type** ('str'): Jobs that match a filter to include.
                  (default includeMatched)
                * **scm-type** ('str'): Type of SCM.
                  (default hudson.scm.NullSCM)

        * **secured-job** (`dict`)
            :secured-job:
                * **match-type** ('str'): Jobs that match a filter
                  to include. (default includeMatched)

        * **user-permissions** (`dict`)
            :user-permissions:
                * **match-type** ('str'): Jobs that match a filter to include.
                  (default includeMatched)
                * **configure** ('bool'): User with configure permissions.
                  (default false)
                * **amount-type**: ('bool'): User with build permissions.
                  (default false)
                * **amount**: ('bool'): User with workspace permissions.
                  (default false)
                * **permission-check**: ('str'): Match user permissions.
                  (default MustMatchAll)

        * **upstream-downstream** (`dict`)
            :upstream-downstream:
                * **include-upstream** ('bool'): Jobs that match upstream.
                  (default False)
                * **include-downstream** ('bool'): Jobs that match downstream.
                  (default False)
                * **recursive** ('bool'): Jobs that are recursive.
                  (default False)
                * **exclude-originals** ('bool'): Jobs that are originals.
                  (default False)

        * **unclassified** (`dict`)
            :unclassified:
                * **match-type** ('str'): Jobs that match a filter to include.
                  (default includeMatched)

    * **columns** (`list`): List of columns to be shown in view.
    * **regex** (`str`): . Regular expression for selecting jobs
      (optional)
    * **recurse** (`bool`): Recurse in subfolders.(default false)
    * **status-filter** (`bool`): Filter job list by enabled/disabled
      status. (optional)

Example:

    .. literalinclude::
        /../../tests/views/fixtures/view_list001.yaml

Example:

    .. literalinclude::
        /../../tests/views/fixtures/view_list002.yaml
"""

import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base
import jenkins_jobs.modules.helpers as helpers
import jenkins_jobs.modules.view_jobfilters as view_jobfilters

COLUMN_DICT = {
    "status": "hudson.views.StatusColumn",
    "weather": "hudson.views.WeatherColumn",
    "job": "hudson.views.JobColumn",
    "last-success": "hudson.views.LastSuccessColumn",
    "last-failure": "hudson.views.LastFailureColumn",
    "last-duration": "hudson.views.LastDurationColumn",
    "build-button": "hudson.views.BuildButtonColumn",
    "last-stable": "hudson.views.LastStableColumn",
    "robot-list": "hudson.plugins.robot.view.RobotListViewColumn",
    "find-bugs": "hudson.plugins.findbugs.FindBugsColumn",
    "jacoco": "hudson.plugins.jacococoveragecolumn.JaCoCoColumn",
    "git-branch": "hudson.plugins.git.GitBranchSpecifierColumn",
    "schedule-build": "org.jenkinsci.plugins.schedulebuild.ScheduleBuildButtonColumn",
    "priority-sorter": "jenkins.advancedqueue.PrioritySorterJobColumn",
    "build-filter": "hudson.views.BuildFilterColumn",
    "desc": "jenkins.branch.DescriptionColumn",
    "policy-violations": "com.sonatype.insight.ci.hudson.QualityColumn "
    'plugin="sonatype-clm-ci"',
    "member-graph-view": "com.barchart.jenkins.cascade.GraphViewColumn "
    'plugin="maven-release-cascade"',
    "extra-tests-total": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>2</testResultFormat>",
    ],
    "extra-tests-failed": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>3</testResultFormat>",
    ],
    "extra-tests-passed": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>4</testResultFormat>",
    ],
    "extra-tests-skipped": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>5</testResultFormat>",
    ],
    "extra-tests-format-0": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>0</testResultFormat>",
    ],
    "extra-tests-format-1": [
        ["jenkins.plugins.extracolumns.TestResultColumn", {"plugin": "extra-columns"}],
        "<testResultFormat>1</testResultFormat>",
    ],
    "extra-build-description": [
        [
            "jenkins.plugins.extracolumns.BuildDescriptionColumn",
            {"plugin": "extra-columns"},
        ],
        "<columnWidth>3</columnWidth>",
        "<forceWidth>false</forceWidth>",
    ],
    "extra-build-parameters": [
        [
            "jenkins.plugins.extracolumns.BuildParametersColumn",
            {"plugin": "extra-columns"},
        ],
        "<singlePara>false</singlePara>",
        "<parameterName/>",
    ],
    "extra-last-user-name": "jenkins.plugins.extracolumns.UserNameColumn"
    ' plugin="extra-columns"',
    "extra-last-output": "jenkins.plugins.extracolumns.LastBuildConsoleColumn"
    ' plugin="extra-columns"',
    "extra-workspace-link": "jenkins.plugins.extracolumns.WorkspaceColumn "
    'plugin="extra-columns"',
    "extra-configure-button": "jenkins.plugins.extracolumns.ConfigureProjectColumn"
    ' plugin="extra-columns"',
}
DEFAULT_COLUMNS = [
    "status",
    "weather",
    "job",
    "last-success",
    "last-failure",
    "last-duration",
    "build-button",
]


class List(jenkins_jobs.modules.base.Base):
    sequence = 0

    def root_xml(self, data, sectioned=False):
        name = "hudson.model.ListView"
        if sectioned:
            name = "hudson.plugins.sectioned__view.ListViewSection"
        root = XML.Element(name)

        mapping = [("name", "name", None)]
        if not sectioned:
            mapping.extend(
                [
                    ("description", "description", ""),
                    ("filter-executors", "filterExecutors", False),
                    ("filter-queue", "filterQueue", False),
                ]
            )
        helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True)

        if not sectioned:
            XML.SubElement(
                root, "properties", {"class": "hudson.model.View$PropertyList"}
            )

        jn_xml = XML.SubElement(root, "jobNames")
        jobnames = data.get("job-name", None)
        XML.SubElement(
            jn_xml, "comparator", {"class": "hudson.util.CaseInsensitiveComparator"}
        )
        if jobnames is not None:
            # Job names must be sorted in the xml
            jobnames = sorted(jobnames, key=str.lower)
            for jobname in jobnames:
                XML.SubElement(jn_xml, "string").text = str(jobname)

        job_filter_xml = XML.SubElement(root, "jobFilters")
        jobfilters = data.get("job-filters", [])

        for jobfilter in jobfilters:
            filter = getattr(view_jobfilters, jobfilter.replace("-", "_"))
            filter(job_filter_xml, jobfilters.get(jobfilter))

        if sectioned:
            mapping = [
                ("width", "width", "FULL", ["FULL", "HALF", "THIRD", "TWO_THIRDS"]),
                ("alignment", "alignment", "CENTER", ["CENTER", "LEFT", "RIGHT"]),
            ]
            helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True)

        c_xml = XML.SubElement(root, "columns")
        columns = data.get("columns", DEFAULT_COLUMNS)

        for column in columns:
            if isinstance(column, dict):
                if "extra-build-parameter" in column:
                    p_name = column["extra-build-parameter"]
                    x = XML.SubElement(
                        c_xml,
                        "jenkins.plugins.extracolumns.BuildParametersColumn",
                        plugin="extra-columns",
                    )
                    x.append(XML.fromstring("<singlePara>true</singlePara>"))
                    x.append(
                        XML.fromstring("<parameterName>%s</parameterName>" % p_name)
                    )
            else:
                if column in COLUMN_DICT:
                    if isinstance(COLUMN_DICT[column], list):
                        x = XML.SubElement(
                            c_xml,
                            COLUMN_DICT[column][0][0],
                            **COLUMN_DICT[column][0][1]
                        )
                        for tag in COLUMN_DICT[column][1:]:
                            x.append(XML.fromstring(tag))
                    else:
                        XML.SubElement(c_xml, COLUMN_DICT[column])
        mapping = [("regex", "includeRegex", None)]
        if not sectioned:
            mapping.extend(
                [("recurse", "recurse", False), ("status-filter", "statusFilter", None)]
            )
        helpers.convert_mapping_to_xml(root, data, mapping, fail_required=False)

        return root