| 1 |
# Copyright 2019 Openstack Foundation |
| 2 |
|
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
""" |
| 16 |
The view nested module handles creating Jenkins Nested views. |
| 17 |
|
| 18 |
To create a nested view specify ``nested`` in the ``view-type`` attribute |
| 19 |
to the :ref:`view_nested` definition. |
| 20 |
|
| 21 |
:View Parameters: |
| 22 |
* **name** (`str`): The name of the view. |
| 23 |
* **view-type** (`str`): The type of view. |
| 24 |
* **description** (`str`): A description of the view. (default '') |
| 25 |
* **filter-executors** (`bool`): Show only executors that can |
| 26 |
execute the included views. (default false) |
| 27 |
* **filter-queue** (`bool`): Show only included jobs in builder |
| 28 |
queue. (default false) |
| 29 |
* **views** (`list`): The views to nest. |
| 30 |
* **default-view** (`str`): Name of the view to use as the default from the |
| 31 |
nested ones. (the first one by default) |
| 32 |
* **columns** (`list`): List of columns to be shown in view. (default empty |
| 33 |
list) |
| 34 |
|
| 35 |
Example: |
| 36 |
|
| 37 |
.. literalinclude:: |
| 38 |
/../../tests/views/fixtures/view_nested.yaml |
| 39 |
|
| 40 |
""" |
| 41 |
|
| 42 |
import xml.etree.ElementTree as XML |
| 43 |
import jenkins_jobs.modules.base |
| 44 |
import jenkins_jobs.modules.helpers as helpers |
| 45 |
from jenkins_jobs.root_base import JobViewData |
| 46 |
from jenkins_jobs.xml_config import XmlViewGenerator |
| 47 |
|
| 48 |
COLUMN_DICT = { |
| 49 |
"status": "hudson.views.StatusColumn", |
| 50 |
"weather": "hudson.views.WeatherColumn", |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
class Nested(jenkins_jobs.modules.base.Base): |
| 55 |
def root_xml(self, data): |
| 56 |
root = XML.Element("hudson.plugins.nested__view.NestedView") |
| 57 |
|
| 58 |
mapping = [ |
| 59 |
("name", "name", None), |
| 60 |
("description", "description", ""), |
| 61 |
("filter-executors", "filterExecutors", False), |
| 62 |
("filter-queue", "filterQueue", False), |
| 63 |
] |
| 64 |
helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True) |
| 65 |
|
| 66 |
XML.SubElement(root, "properties", {"class": "hudson.model.View$PropertyList"}) |
| 67 |
|
| 68 |
v_xml = XML.SubElement(root, "views") |
| 69 |
views = data.get("views", []) |
| 70 |
view_data_list = [JobViewData(v) for v in views] |
| 71 |
|
| 72 |
xml_view_generator = XmlViewGenerator(self.registry) |
| 73 |
xml_views = xml_view_generator.generateXML(view_data_list) |
| 74 |
|
| 75 |
for xml_job in xml_views: |
| 76 |
v_xml.append(xml_job.xml) |
| 77 |
|
| 78 |
d_xml = XML.SubElement(root, "defaultView") |
| 79 |
d_xml.text = data.get("default-view", views[0]["name"]) |
| 80 |
|
| 81 |
c_xml = XML.SubElement(root, "columns") |
| 82 |
# there is a columns element in a columns element |
| 83 |
c_xml = XML.SubElement(c_xml, "columns") |
| 84 |
columns = data.get("columns", []) |
| 85 |
|
| 86 |
for column in columns: |
| 87 |
if column in COLUMN_DICT: |
| 88 |
XML.SubElement(c_xml, COLUMN_DICT[column]) |
| 89 |
else: |
| 90 |
raise ValueError( |
| 91 |
"Unsupported column %s is not one of: %s." |
| 92 |
% (column, ", ".join(COLUMN_DICT.keys())) |
| 93 |
) |
| 94 |
|
| 95 |
return root |