Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple views defined in ony file #1

Merged
merged 1 commit into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
jenkins-view-builder
====================

jenkins-view-builder is a command line tool for managing jenkins views with yaml files and back your views with SCM.
jenkins-view-builder is a command line tool for managing jenkins views with yaml files and back
your views with SCM.

Installation
---

Install jenkins-view-builder at a system level or in an isolated virtualenv by running the following command:
Install jenkins-view-builder at a system level or in an isolated virtualenv by running the
following command:

pip install jenkins-view-builder

Usage
---

There are several types of views in jenkins
There are several types of views in jenkins:
* Build Pipeline View
* Nested View
* Dashboard
* List View
* Radiator

jenkins-view-builder as of now supports **List View**, **Build Pipeline View** and **Nested View** with support for other views coming soon. Examples of yaml files are in the `tests/fixtures` folder. Views are specified as yaml files and given to the jenkins-view-builder to upload to jenkins. Say, you have the following **List View** view in a yaml file
jenkins-view-builder as of now supports **List View**, **Build Pipeline View** and **Nested View**
with support for other views coming soon. Examples of yaml files are in the `tests/fixtures`
folder. Views are specified as yaml files and given to the jenkins-view-builder to upload to
jenkins. Say, you have the following **List View** view in a yaml file

- view:
type: list
Expand All @@ -36,8 +41,16 @@ jenkins-view-builder as of now supports **List View**, **Build Pipeline View** a
- status
- weather
recurse: False
- view:
type: list
name: second
description: Second ply jobs
columns:
- status
recurse: False

jenkins-view-builder can create this view in jenkins. jenkins-view-builder needs a jenkins config file which tells it how to connect to jenkins. The config file looks like this
jenkins-view-builder can create this view in jenkins. jenkins-view-builder needs a jenkins config
file which tells it how to connect to jenkins. The config file looks like this

[jenkins]
user=user
Expand All @@ -48,11 +61,14 @@ Once that is ready, we are all set to create the view in jenkins using the follo

jenkins-view-builder update --conf path-to-jenkins-config-file path-to-view-yaml-file

There should be feedback on stdout on what the tool is doing. update command is capable of determining if the view already exists and if it does then it just updates it.
There should be feedback on stdout on what the tool is doing. update command is capable of
determining if the view already exists and if it does then it just updates it.

It is also possible to test the view to make sure that jenkins-view-builder is creating the correct xml that it would post to jenkins. This can be done using the following command
It is also possible to test the view to make sure that jenkins-view-builder is creating the correct
xml that it would post to jenkins. This can be done using the following command

jenkins-view-builder test path-to-view-yaml-file

Running this command will spit out the generated xml in the `out` folder of the current working directory. If the output looks good, the `update` command can be used to upload the view.
Running this command will spit out the generated xml in the `out` folder of the current working
directory. If the output looks good, the `update` command can be used to upload the view.

12 changes: 9 additions & 3 deletions builder/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ def take_action(self, parsed_args):
self.log.debug(yaml)

try:
name, xml = convert_to_xml(yaml)
xmls = convert_to_xml(yaml)
except Exception as e:
raise(e)
if isinstance(xmls[0], str):
name, xml = xmls
with open(os.path.join(out_dir, name + ".xml"), 'wb') as xml_file:
xml_file.write(xml)
else:
for name, xml in xmls:
with open(os.path.join(out_dir, name + ".xml"), 'wb') as xml_file:
xml_file.write(xml)

with open(os.path.join(out_dir, name + ".xml"), 'wb') as xml_file:
xml_file.write(xml)
15 changes: 12 additions & 3 deletions builder/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ def read_update(self, config, view_yaml):
with open(view_yaml, 'r') as yaml_file:
yaml = yaml_file.read()
self.log.debug(yaml)
name, xml = convert_to_xml(yaml)
self.log.debug(xml)
update(config, name, xml)
try:
xmls = convert_to_xml(yaml)
except Exception as e:
raise(e)
if isinstance(xmls[0], str):
name, xml = xmls
self.log.debug(xml)
update(config, name, xml)
else:
for name, xml in xmls:
self.log.debug(xml)
update(config, name, xml)

def take_action(self, parsed_args):
self.log.info("Updating view data in Jenkins")
Expand Down
18 changes: 12 additions & 6 deletions builder/converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import xml.etree.ElementTree as ET


# for reuse and avoid refactoring all current test functions,
# 2 types of results:
# - only one item in ret, return as a tuple, not list
# - multiple items in ret, return a tuple list
def convert_to_xml(yaml_str):
yaml_dict = yaml.load(yaml_str)
yaml_view = yaml_dict[0]['view']
view_name = yaml_view['name']

xml = convert_yaml_dict_to_xml(yaml_view)
xml_str = ET.tostring(xml, encoding="us-ascii")
return (view_name, six.b("<?xml version=\"1.0\" ?>") + xml_str)
ret = []
for item in yaml_dict:
yaml_view = item['view']
view_name = yaml_view['name']
xml = convert_yaml_dict_to_xml(yaml_view)
xml_str = six.b("<?xml version=\"1.0\" ?>\n") + ET.tostring(xml, encoding="us-ascii")
ret.append((view_name, xml_str))
return ret[0] if (len(ret) == 1) else ret


def convert_yaml_dict_to_xml(yaml_dict):
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

PROJECT = 'jenkins-view-builder'

VERSION = '0.7.1'
VERSION = '0.7.2'

try:
from setuptools import setup, find_packages
Expand All @@ -10,15 +10,15 @@

config = {
'name': 'jenkins-view-builder',
'version': '0.7.1',
'version': '0.7.2',

'description': 'Build jenkins views in YAML',

'author': 'Piyush Srivastava',
'author_email': '[email protected]',

'url': 'https://github.com/piyush0101/jenkins-view-builder',
'download_url': 'https://github.com/piyush0101/jenkins-view-builder/archive/0.7.1.tar.gz',
'download_url': 'https://github.com/piyush0101/jenkins-view-builder/archive/0.7.2.tar.gz',

'classifiers': [],

Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/multi_list_view.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- view:
type: list
name: monsanto
description: Merge ply jobs
jobs:
- Merge-nova-Ply
- Merge-Config-Ply
- Merge-bark-Ply
columns:
- status
- weather
recurse: False
- view:
type: list
name: second
description: Second ply jobs
jobs:
- Second-nova-Ply
columns:
- status
- weather
recurse: False
15 changes: 15 additions & 0 deletions tests/test_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ def test_should_have_a_name_tag_in_view_xml(self):

self.assertEqual(name.text, 'monsanto')

def test_should_have_multi_name_tags_in_view_xml(self):
yaml_file = open(os.path.join(self.file_path, 'multi_list_view.yaml'), 'r')

yaml = yaml_file.read()
names = []

for name, xml_view in convert_to_xml(yaml):
xml_root = ET.fromstring(xml_view)
name = xml_root.find('name')
print name.text
names.append(name.text)

self.assertListEqual(names, ['monsanto',
'second'])

def test_should_have_jobs_in_view_xml(self):
yaml_file = open(os.path.join(self.file_path, 'list_view.yaml'), 'r')

Expand Down