From 587740ec757e46bc971110582e2e9f3f134b26b3 Mon Sep 17 00:00:00 2001 From: Jan Zerebecki Date: Wed, 16 Oct 2019 14:53:15 +0200 Subject: Support nested views Change-Id: I13532a16efc6e970ab5a7c021ec4d77be98d3de8 --- doc/source/view_nested.rst | 7 +++ jenkins_jobs/modules/view_nested.py | 93 +++++++++++++++++++++++++++++++++++ setup.cfg | 1 + tests/base.py | 32 ++++++++++-- tests/views/fixtures/view_nested.xml | 24 +++++++++ tests/views/fixtures/view_nested.yaml | 8 +++ tests/views/test_views.py | 7 +++ 7 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 doc/source/view_nested.rst create mode 100644 jenkins_jobs/modules/view_nested.py create mode 100644 tests/views/fixtures/view_nested.xml create mode 100644 tests/views/fixtures/view_nested.yaml diff --git a/doc/source/view_nested.rst b/doc/source/view_nested.rst new file mode 100644 index 00000000..a0e989fa --- /dev/null +++ b/doc/source/view_nested.rst @@ -0,0 +1,7 @@ +.. _view_nested: + +Nested View +=========== + +.. automodule:: view_nested + :members: diff --git a/jenkins_jobs/modules/view_nested.py b/jenkins_jobs/modules/view_nested.py new file mode 100644 index 00000000..02388e5f --- /dev/null +++ b/jenkins_jobs/modules/view_nested.py @@ -0,0 +1,93 @@ +# Copyright 2019 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 nested module handles creating Jenkins Nested views. + +To create a nested view specify ``nested`` in the ``view-type`` attribute +to the :ref:`view_nested` 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) + * **views** (`list`): The views to nest. + * **default-view** (`str`): Name of the view to use as the default from the + nested ones. (the first one by default) + * **columns** (`list`): List of columns to be shown in view. (default empty + list) + +Example: + + .. literalinclude:: + /../../tests/views/fixtures/view_nested.yaml + +""" + +import xml.etree.ElementTree as XML +import jenkins_jobs.modules.base +import jenkins_jobs.modules.helpers as helpers +from jenkins_jobs.xml_config import XmlViewGenerator + +COLUMN_DICT = { + "status": "hudson.views.StatusColumn", + "weather": "hudson.views.WeatherColumn", +} + + +class Nested(jenkins_jobs.modules.base.Base): + def root_xml(self, data): + root = XML.Element("hudson.plugins.nested__view.NestedView") + + mapping = [ + ("name", "name", None), + ("description", "description", ""), + ("filter-executors", "filterExecutors", False), + ("filter-queue", "filterQueue", False), + ] + helpers.convert_mapping_to_xml(root, data, mapping, fail_required=True) + + XML.SubElement(root, "properties", {"class": "hudson.model.View$PropertyList"}) + + v_xml = XML.SubElement(root, "views") + views = data.get("views", []) + + xml_view_generator = XmlViewGenerator(self.registry) + xml_views = xml_view_generator.generateXML(views) + + for xml_job in xml_views: + v_xml.append(xml_job.xml) + + d_xml = XML.SubElement(root, "defaultView") + d_xml.text = data.get("default-view", views[0]["name"]) + + c_xml = XML.SubElement(root, "columns") + # there is a columns element in a columns element + c_xml = XML.SubElement(c_xml, "columns") + columns = data.get("columns", []) + + for column in columns: + if column in COLUMN_DICT: + XML.SubElement(c_xml, COLUMN_DICT[column]) + else: + raise ValueError( + "Unsupported column %s is not one of: %s." + % (column, ", ".join(COLUMN_DICT.keys())) + ) + + return root diff --git a/setup.cfg b/setup.cfg index e183d323..89f9a494 100644 --- a/setup.cfg +++ b/setup.cfg @@ -64,6 +64,7 @@ jenkins_jobs.projects = jenkins_jobs.views = all=jenkins_jobs.modules.view_all:All list=jenkins_jobs.modules.view_list:List + nested=jenkins_jobs.modules.view_nested:Nested pipeline=jenkins_jobs.modules.view_pipeline:Pipeline jenkins_jobs.builders = raw=jenkins_jobs.modules.general:raw diff --git a/tests/base.py b/tests/base.py index 660a3af7..f828c773 100644 --- a/tests/base.py +++ b/tests/base.py @@ -18,10 +18,12 @@ # under the License. import doctest +import configparser import io import json import logging import os +import pkg_resources import re import xml.etree.ElementTree as XML @@ -45,6 +47,7 @@ from jenkins_jobs.modules import project_multibranch from jenkins_jobs.modules import project_multijob from jenkins_jobs.modules import view_all from jenkins_jobs.modules import view_list +from jenkins_jobs.modules import view_nested from jenkins_jobs.modules import view_pipeline from jenkins_jobs.parser import YamlParser from jenkins_jobs.registry import ModuleRegistry @@ -169,7 +172,8 @@ class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase): scenarios = [] fixtures_path = None - def test_yaml_snippet(self): + @mock.patch("pkg_resources.iter_entry_points") + def test_yaml_snippet(self, mock): if not self.in_filename: return @@ -187,6 +191,24 @@ class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase): self.addDetail("plugins-info", text_content(str(plugins_info))) parser = YamlParser(jjb_config) + e = pkg_resources.EntryPoint.parse + d = pkg_resources.Distribution() + config = configparser.ConfigParser() + config.read(os.path.dirname(__file__) + "/../setup.cfg") + groups = {} + for key in config["entry_points"]: + groups[key] = list() + for line in config["entry_points"][key].split("\n"): + if "" == line.strip(): + continue + groups[key].append(e(line, dist=d)) + + def mock_iter_entry_points(group, name=None): + return ( + entry for entry in groups[group] if name is None or name == entry.name + ) + + mock.side_effect = mock_iter_entry_points registry = ModuleRegistry(jjb_config, plugins_info) registry.set_parser_data(parser.data) @@ -213,11 +235,13 @@ class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase): if "view-type" in yaml_content: if yaml_content["view-type"] == "all": - project = view_all.All(None) + project = view_all.All(registry) elif yaml_content["view-type"] == "list": - project = view_list.List(None) + project = view_list.List(registry) + elif yaml_content["view-type"] == "nested": + project = view_nested.Nested(registry) elif yaml_content["view-type"] == "pipeline": - project = view_pipeline.Pipeline(None) + project = view_pipeline.Pipeline(registry) else: raise InvalidAttributeError("view-type", yaml_content["view-type"]) diff --git a/tests/views/fixtures/view_nested.xml b/tests/views/fixtures/view_nested.xml new file mode 100644 index 00000000..757990ac --- /dev/null +++ b/tests/views/fixtures/view_nested.xml @@ -0,0 +1,24 @@ + + + NestedViewTest + + false + false + + + + All + + false + false + + + + All + + + + + + + diff --git a/tests/views/fixtures/view_nested.yaml b/tests/views/fixtures/view_nested.yaml new file mode 100644 index 00000000..6a33c3ac --- /dev/null +++ b/tests/views/fixtures/view_nested.yaml @@ -0,0 +1,8 @@ +name: NestedViewTest +view-type: nested +views: + - name: All + view-type: all +columns: + - status + - weather diff --git a/tests/views/test_views.py b/tests/views/test_views.py index 700b174d..62435590 100644 --- a/tests/views/test_views.py +++ b/tests/views/test_views.py @@ -15,6 +15,7 @@ import os from jenkins_jobs.modules import view_all from jenkins_jobs.modules import view_list +from jenkins_jobs.modules import view_nested from jenkins_jobs.modules import view_pipeline from tests import base @@ -31,6 +32,12 @@ class TestCaseModuleViewList(base.BaseScenariosTestCase): klass = view_list.List +class TestCaseModuleViewNested(base.BaseScenariosTestCase): + fixtures_path = os.path.join(os.path.dirname(__file__), "fixtures") + scenarios = base.get_scenarios(fixtures_path) + klass = view_nested.Nested + + class TestCaseModuleViewPipeline(base.BaseScenariosTestCase): fixtures_path = os.path.join(os.path.dirname(__file__), "fixtures") scenarios = base.get_scenarios(fixtures_path) -- cgit