Support nested views

Change-Id: I13532a16efc6e970ab5a7c021ec4d77be98d3de8
This commit is contained in:
Jan Zerebecki 2019-10-16 14:53:15 +02:00
parent da1e227c8c
commit 587740ec75
7 changed files with 168 additions and 4 deletions

View File

@ -0,0 +1,7 @@
.. _view_nested:
Nested View
===========
.. automodule:: view_nested
:members:

View File

@ -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

View File

@ -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

View File

@ -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"])

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<hudson.plugins.nested__view.NestedView>
<name>NestedViewTest</name>
<description/>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<views>
<hudson.model.AllView>
<name>All</name>
<description/>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
</hudson.model.AllView>
</views>
<defaultView>All</defaultView>
<columns>
<columns>
<hudson.views.StatusColumn/>
<hudson.views.WeatherColumn/>
</columns>
</columns>
</hudson.plugins.nested__view.NestedView>

View File

@ -0,0 +1,8 @@
name: NestedViewTest
view-type: nested
views:
- name: All
view-type: all
columns:
- status
- weather

View File

@ -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)