From 5117d87e8c7b9272f1ac5ef59f2f05afc32c9715 Mon Sep 17 00:00:00 2001
From: kendallpcdoctor <kendall@pc-doctor.com>
Date: Thu, 25 Jul 2019 11:01:30 -0700
Subject: [PATCH] Add support for two new plugins, and a new version of one

Added new support for the following plugins:
Perforce plugin: https://wiki.jenkins.io/display/JENKINS/Perforce+Plugin
InfluxDB plugin: https://wiki.jenkins.io/display/JENKINS/InfluxDB+Plugin

Added support for v3.0 of the following plugin
https://wiki.jenkins.io/display/JENKINS/Priority+Sorter+Plugin

Change-Id: If109863a91710c08b34dee622496e551a7b3b520
---
 jenkins_jobs/modules/project_matrix.py        |  11 ++
 jenkins_jobs/modules/properties.py            |  12 +-
 jenkins_jobs/modules/publishers.py            |  22 ++++
 jenkins_jobs/modules/scm.py                   | 109 ++++++++++++++++++
 .../fixtures/matrix-axis-p4-strategy-file.xml |  14 +++
 .../matrix-axis-p4-strategy-file.yaml         |   4 +
 .../priority_sorter003.plugins_info.yaml      |   3 +
 .../fixtures/priority_sorter003.xml           |   9 ++
 .../fixtures/priority_sorter003.yaml          |   3 +
 tests/publishers/fixtures/influxdb-full.xml   |  12 ++
 tests/publishers/fixtures/influxdb-full.yaml  |   7 ++
 .../publishers/fixtures/influxdb-minimal.xml  |  12 ++
 .../publishers/fixtures/influxdb-minimal.yaml |   3 +
 tests/scm/fixtures/p4-001.xml                 |  25 ++++
 tests/scm/fixtures/p4-001.yaml                |  10 ++
 tests/scm/fixtures/p4-002.xml                 |  38 ++++++
 tests/scm/fixtures/p4-002.yaml                |  21 ++++
 tests/scm/fixtures/p4-003.xml                 |  41 +++++++
 tests/scm/fixtures/p4-003.yaml                |  11 ++
 19 files changed, 366 insertions(+), 1 deletion(-)
 create mode 100644 tests/general/fixtures/matrix-axis-p4-strategy-file.xml
 create mode 100644 tests/general/fixtures/matrix-axis-p4-strategy-file.yaml
 create mode 100644 tests/properties/fixtures/priority_sorter003.plugins_info.yaml
 create mode 100644 tests/properties/fixtures/priority_sorter003.xml
 create mode 100644 tests/properties/fixtures/priority_sorter003.yaml
 create mode 100644 tests/publishers/fixtures/influxdb-full.xml
 create mode 100644 tests/publishers/fixtures/influxdb-full.yaml
 create mode 100644 tests/publishers/fixtures/influxdb-minimal.xml
 create mode 100644 tests/publishers/fixtures/influxdb-minimal.yaml
 create mode 100644 tests/scm/fixtures/p4-001.xml
 create mode 100644 tests/scm/fixtures/p4-001.yaml
 create mode 100644 tests/scm/fixtures/p4-002.xml
 create mode 100644 tests/scm/fixtures/p4-002.yaml
 create mode 100644 tests/scm/fixtures/p4-003.xml
 create mode 100644 tests/scm/fixtures/p4-003.yaml

diff --git a/jenkins_jobs/modules/project_matrix.py b/jenkins_jobs/modules/project_matrix.py
index a96b7f00a..13c677215 100644
--- a/jenkins_jobs/modules/project_matrix.py
+++ b/jenkins_jobs/modules/project_matrix.py
@@ -130,6 +130,8 @@ class Matrix(jenkins_jobs.modules.base.Base):
             'hudson.matrix.DefaultMatrixExecutionStrategyImpl',
         'yaml-strategy':
             'org.jenkinsci.plugins.yamlaxis.YamlMatrixExecutionStrategy',
+        'p4-strategy':
+            'org.jenkinsci.plugins.p4.matrix.MatrixOptions'
     }
 
     def root_xml(self, data):
@@ -202,6 +204,15 @@ class Matrix(jenkins_jobs.modules.base.Base):
 
             XML.SubElement(ex_r, 'excludeKey').text = exclude_key
 
+        elif strategy_name == 'p4-strategy':
+            XML.SubElement(ex_r, 'runSequentially').text = (
+                str(strategy.get('sequential', False)).lower()
+            )
+
+            XML.SubElement(ex_r, 'buildParent').text = (
+                str(strategy.get('build-parent', False)).lower()
+            )
+
         ax_root = XML.SubElement(root, 'axes')
         for axis_ in data.get('axes', []):
             axis = axis_['axis']
diff --git a/jenkins_jobs/modules/properties.py b/jenkins_jobs/modules/properties.py
index 8e06385eb..1898a051c 100644
--- a/jenkins_jobs/modules/properties.py
+++ b/jenkins_jobs/modules/properties.py
@@ -580,7 +580,17 @@ def priority_sorter(registry, xml_parent, data):
     plugin_info = registry.get_plugin_info('PrioritySorter')
     version = pkg_resources.parse_version(plugin_info.get('version', '0'))
 
-    if version >= pkg_resources.parse_version("2.0"):
+    if version >= pkg_resources.parse_version("3.0"):
+        priority_sorter_tag = XML.SubElement(
+            xml_parent,
+            'jenkins.advancedqueue.jobinclusion.'
+            'strategy.JobInclusionJobProperty')
+
+        mapping = [
+            ('use', 'useJobGroup', True),
+            ('priority', 'jobGroupName', None)
+        ]
+    elif version >= pkg_resources.parse_version("2.0"):
         priority_sorter_tag = XML.SubElement(xml_parent,
                                              'jenkins.advancedqueue.priority.'
                                              'strategy.PriorityJobProperty')
diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py
index 393e530f5..8c3484761 100644
--- a/jenkins_jobs/modules/publishers.py
+++ b/jenkins_jobs/modules/publishers.py
@@ -40,6 +40,28 @@ from jenkins_jobs.modules import hudson_model
 import jenkins_jobs.modules.helpers as helpers
 
 
+def influx_db(registry, xml_parent, data):
+    """yaml: influx-db
+    Requires the Jenkins :jenkins-wiki: `Influx DB
+    <Influx+DB+Plugin>`.
+    """
+
+    influx_db = XML.SubElement(xml_parent,
+        'jenkinsci.plugins.influxdb.InfluxDbPublisher',
+        {'plugin': 'influx-db'})
+
+    mapping = [
+        ('selected-target', 'selectedTarget', ''),
+        ('custom-project-name', 'customProjectName', ''),
+        ('custom-prefix', 'customPrefix', ''),
+        ('jenkins-env-parameter-field', 'jenkinsEnvParameterField', ''),
+        ('jenkins-env-parameter-tag', 'jenkinsEnvParameterTag', '')
+    ]
+
+    helpers.convert_mapping_to_xml(
+        influx_db, data, mapping, fail_required=True)
+
+
 def allure(registry, xml_parent, data):
     """yaml: allure
 
diff --git a/jenkins_jobs/modules/scm.py b/jenkins_jobs/modules/scm.py
index 6c68dc158..baa4f94c1 100644
--- a/jenkins_jobs/modules/scm.py
+++ b/jenkins_jobs/modules/scm.py
@@ -45,6 +45,115 @@ import jenkins_jobs.modules.base
 import jenkins_jobs.modules.helpers as helpers
 
 
+def p4(registry, xml_parent, data):
+    r"""yaml: p4
+    Specifies the Perforce (P4) repository for this job
+    Requires the Jenkins :jenkins-wiki:`P4 Plugin <P4+Plugin>`.
+    """
+    scm = XML.SubElement(xml_parent, 'scm',
+                        {'class': 'org.jenkinsci.plugins.p4.PerforceScm',
+                        'plugin': 'p4'})
+
+    XML.SubElement(scm, 'credential').text = data.get('credential')
+
+    p4_construct_workspace(scm, data)
+
+    p4_construct_populate(scm, data)
+
+
+def p4_construct_workspace(xml_parent, data):
+    workspace = None
+
+    workspace_mapping = [
+        ('workspace-charset', 'charset', 'none'),
+        ('workspace-pin-host', 'pinHost', False),
+        ('workspace-name', 'name', ''),
+        ('workspace-cleanup', 'cleanup', None)
+    ]
+
+    if data.get('workspace-type') == 'Static':
+        workspace = XML.SubElement(xml_parent, 'workspace',
+            {'class':
+            'org.jenkinsci.plugins.p4.workspace.StaticWorkspaceImpl'})
+    elif data.get('workspace-type') == 'Manual':
+        workspace = XML.SubElement(xml_parent, 'workspace',
+            {'class':
+            'org.jenkinsci.plugins.p4.workspace.ManualWorkspaceImpl'})
+
+        spec = XML.SubElement(workspace, 'spec')
+
+        spec_mapping = [
+            ('spec-allwrite', 'allwrite', False),
+            ('spec-clobber', 'clobber', False),
+            ('spec-compress', 'compress', False),
+            ('spec-locked', 'locked', False),
+            ('spec-modtime', 'modtime', False),
+            ('spec-rmdir', 'rmdir', False),
+            ('spec-line', 'line', ''),
+            ('spec-view', 'view', ''),
+            ('spec-type', 'type', ''),
+            ('spec-backup', 'backup', False),
+            ('spec-stream-name', 'streamName', '')
+        ]
+
+        helpers.convert_mapping_to_xml(
+            spec, data, spec_mapping, fail_required=False)
+
+    if 'view-mask' in data.keys():
+        filter_node = XML.SubElement(xml_parent, 'filter')
+
+        view_mask = XML.SubElement(filter_node,
+            'org.jenkinsci.plugins.p4.filters.FilterViewMaskImpl')
+
+        view_mask_mapping = [
+            ('view-mask', 'viewMask', None)
+        ]
+
+        helpers.convert_mapping_to_xml(
+            view_mask, data, view_mask_mapping, fail_required=False)
+
+    helpers.convert_mapping_to_xml(
+        workspace, data, workspace_mapping, fail_required=False)
+
+
+def p4_construct_populate(xml_parent, data):
+    populate = None
+
+    populate_mapping = [
+        ('populate-have-list', 'have', False),
+        ('populate-force-sync', 'force', False),
+        ('populate-modtime', 'modtime', False),
+        ('populate-quiet', 'quiet', False),
+        ('populate-label', 'pin', None),
+        ('populate-revert', 'revert', None),
+        ('populate-replace', 'replace', None),
+        ('populate-delete', 'delete', None),
+        ('populate-tidy', 'tidy', None)
+    ]
+
+    parallel_mapping = [
+        ('parallel-enabled', 'enable', False),
+        ('parallel-threads', 'threads', '4'),
+        ('parallel-minfiles', 'minfiles', '1'),
+        ('parallel-minbytes', 'minbytes', '1024')
+    ]
+
+    if data.get('populate-type') == 'SyncOnly':
+        populate = XML.SubElement(xml_parent, 'populate',
+            {'class': 'org.jenkinsci.plugins.p4.populate.SyncOnlyImpl'})
+    elif data.get('populate-type') == 'AutoClean':
+        populate = XML.SubElement(xml_parent, 'populate',
+            {'class': 'org.jenkinsci.plugins.p4.populate.AutoCleanImpl'})
+
+    helpers.convert_mapping_to_xml(
+        populate, data, populate_mapping, fail_required=False)
+
+    parallel = XML.SubElement(populate, 'parallel')
+
+    helpers.convert_mapping_to_xml(
+        parallel, data, parallel_mapping, fail_required=False)
+
+
 def git(registry, xml_parent, data):
     r"""yaml: git
     Specifies the git SCM repository for this job.
diff --git a/tests/general/fixtures/matrix-axis-p4-strategy-file.xml b/tests/general/fixtures/matrix-axis-p4-strategy-file.xml
new file mode 100644
index 000000000..de485efa8
--- /dev/null
+++ b/tests/general/fixtures/matrix-axis-p4-strategy-file.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<matrix-project>
+  <executionStrategy class="org.jenkinsci.plugins.p4.matrix.MatrixOptions">
+    <runSequentially>false</runSequentially>
+    <buildParent>false</buildParent>
+  </executionStrategy>
+  <axes/>
+  <actions/>
+  <keepDependencies>false</keepDependencies>
+  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
+  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
+  <concurrentBuild>false</concurrentBuild>
+  <canRoam>true</canRoam>
+</matrix-project>
diff --git a/tests/general/fixtures/matrix-axis-p4-strategy-file.yaml b/tests/general/fixtures/matrix-axis-p4-strategy-file.yaml
new file mode 100644
index 000000000..3fd84a210
--- /dev/null
+++ b/tests/general/fixtures/matrix-axis-p4-strategy-file.yaml
@@ -0,0 +1,4 @@
+project-type: matrix
+p4-strategy:
+  sequential: false
+  build-parent: false
diff --git a/tests/properties/fixtures/priority_sorter003.plugins_info.yaml b/tests/properties/fixtures/priority_sorter003.plugins_info.yaml
new file mode 100644
index 000000000..2d581816b
--- /dev/null
+++ b/tests/properties/fixtures/priority_sorter003.plugins_info.yaml
@@ -0,0 +1,3 @@
+- longName: 'Jenkins Priority Sorter Plugin'
+  shortName: 'PrioritySorter'
+  version: '3.0'
diff --git a/tests/properties/fixtures/priority_sorter003.xml b/tests/properties/fixtures/priority_sorter003.xml
new file mode 100644
index 000000000..876ff7b5a
--- /dev/null
+++ b/tests/properties/fixtures/priority_sorter003.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <properties>
+    <jenkins.advancedqueue.jobinclusion.strategy.JobInclusionJobProperty>
+      <useJobGroup>true</useJobGroup>
+      <jobGroupName>Priority 3</jobGroupName>
+    </jenkins.advancedqueue.jobinclusion.strategy.JobInclusionJobProperty>
+  </properties>
+</project>
diff --git a/tests/properties/fixtures/priority_sorter003.yaml b/tests/properties/fixtures/priority_sorter003.yaml
new file mode 100644
index 000000000..9153de676
--- /dev/null
+++ b/tests/properties/fixtures/priority_sorter003.yaml
@@ -0,0 +1,3 @@
+properties:
+  - priority-sorter:
+      priority: Priority 3
diff --git a/tests/publishers/fixtures/influxdb-full.xml b/tests/publishers/fixtures/influxdb-full.xml
new file mode 100644
index 000000000..b08e0bfd9
--- /dev/null
+++ b/tests/publishers/fixtures/influxdb-full.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <publishers>
+    <jenkinsci.plugins.influxdb.InfluxDbPublisher plugin="influx-db">
+      <selectedTarget>selectedtarget</selectedTarget>
+      <customProjectName>customprojectname</customProjectName>
+      <customPrefix>customprefix</customPrefix>
+      <jenkinsEnvParameterField>jenkinsenvparameterfield</jenkinsEnvParameterField>
+      <jenkinsEnvParameterTag>jenkinsenvparametertag</jenkinsEnvParameterTag>
+    </jenkinsci.plugins.influxdb.InfluxDbPublisher>
+  </publishers>
+</project>
diff --git a/tests/publishers/fixtures/influxdb-full.yaml b/tests/publishers/fixtures/influxdb-full.yaml
new file mode 100644
index 000000000..219c5d476
--- /dev/null
+++ b/tests/publishers/fixtures/influxdb-full.yaml
@@ -0,0 +1,7 @@
+publishers:
+  - influx-db:
+      selected-target: selectedtarget
+      custom-project-name: customprojectname
+      custom-prefix: customprefix
+      jenkins-env-parameter-field: jenkinsenvparameterfield
+      jenkins-env-parameter-tag: jenkinsenvparametertag
diff --git a/tests/publishers/fixtures/influxdb-minimal.xml b/tests/publishers/fixtures/influxdb-minimal.xml
new file mode 100644
index 000000000..1ea806f8d
--- /dev/null
+++ b/tests/publishers/fixtures/influxdb-minimal.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <publishers>
+    <jenkinsci.plugins.influxdb.InfluxDbPublisher plugin="influx-db">
+      <selectedTarget>selectedtarget</selectedTarget>
+      <customProjectName/>
+      <customPrefix/>
+      <jenkinsEnvParameterField/>
+      <jenkinsEnvParameterTag/>
+    </jenkinsci.plugins.influxdb.InfluxDbPublisher>
+  </publishers>
+</project>
diff --git a/tests/publishers/fixtures/influxdb-minimal.yaml b/tests/publishers/fixtures/influxdb-minimal.yaml
new file mode 100644
index 000000000..fd5871c07
--- /dev/null
+++ b/tests/publishers/fixtures/influxdb-minimal.yaml
@@ -0,0 +1,3 @@
+publishers:
+  - influx-db:
+      selected-target: selectedtarget
diff --git a/tests/scm/fixtures/p4-001.xml b/tests/scm/fixtures/p4-001.xml
new file mode 100644
index 000000000..30620d74a
--- /dev/null
+++ b/tests/scm/fixtures/p4-001.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <scm class="org.jenkinsci.plugins.p4.PerforceScm" plugin="p4">
+    <credential>credential</credential>
+    <workspace class="org.jenkinsci.plugins.p4.workspace.StaticWorkspaceImpl">
+      <charset>none</charset>
+      <pinHost>false</pinHost>
+      <name>workspacename</name>
+    </workspace>
+    <populate class="org.jenkinsci.plugins.p4.populate.SyncOnlyImpl">
+      <have>true</have>
+      <force>false</force>
+      <modtime>false</modtime>
+      <quiet>true</quiet>
+      <pin>populatelabel</pin>
+      <revert>false</revert>
+      <parallel>
+        <enable>false</enable>
+        <threads>4</threads>
+        <minfiles>1</minfiles>
+        <minbytes>1024</minbytes>
+      </parallel>
+    </populate>
+  </scm>
+</project>
diff --git a/tests/scm/fixtures/p4-001.yaml b/tests/scm/fixtures/p4-001.yaml
new file mode 100644
index 000000000..af3aa5f26
--- /dev/null
+++ b/tests/scm/fixtures/p4-001.yaml
@@ -0,0 +1,10 @@
+scm:
+  - p4:
+      credential: credential
+      workspace-type: Static
+      workspace-name: workspacename
+      populate-type: SyncOnly
+      populate-have-list: true
+      populate-quiet: true
+      populate-revert: false
+      populate-label: populatelabel
diff --git a/tests/scm/fixtures/p4-002.xml b/tests/scm/fixtures/p4-002.xml
new file mode 100644
index 000000000..01de25914
--- /dev/null
+++ b/tests/scm/fixtures/p4-002.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <scm class="org.jenkinsci.plugins.p4.PerforceScm" plugin="p4">
+    <credential>credential</credential>
+    <workspace class="org.jenkinsci.plugins.p4.workspace.ManualWorkspaceImpl">
+      <spec>
+        <allwrite>true</allwrite>
+        <clobber>true</clobber>
+        <compress>true</compress>
+        <locked>true</locked>
+        <modtime>true</modtime>
+        <rmdir>true</rmdir>
+        <line>specline</line>
+        <view>specview</view>
+        <type>spectype</type>
+        <backup>true</backup>
+        <streamName>specstreamname</streamName>
+      </spec>
+      <charset>none</charset>
+      <pinHost>false</pinHost>
+      <name>workspacename</name>
+    </workspace>
+    <populate class="org.jenkinsci.plugins.p4.populate.AutoCleanImpl">
+      <have>true</have>
+      <force>false</force>
+      <modtime>false</modtime>
+      <quiet>true</quiet>
+      <pin>populatelabel</pin>
+      <revert>false</revert>
+      <parallel>
+        <enable>false</enable>
+        <threads>4</threads>
+        <minfiles>1</minfiles>
+        <minbytes>1024</minbytes>
+      </parallel>
+    </populate>
+  </scm>
+</project>
diff --git a/tests/scm/fixtures/p4-002.yaml b/tests/scm/fixtures/p4-002.yaml
new file mode 100644
index 000000000..5c1c32eee
--- /dev/null
+++ b/tests/scm/fixtures/p4-002.yaml
@@ -0,0 +1,21 @@
+scm:
+ - p4:
+     credential: credential
+     workspace-type: Manual
+     workspace-name: workspacename
+     populate-type: AutoClean
+     populate-have-list: True
+     populate-quiet: True
+     populate-revert: False
+     populate-label: populatelabel
+     spec-allwrite: True
+     spec-clobber: True
+     spec-compress: True
+     spec-locked: True
+     spec-modtime: True
+     spec-rmdir: True
+     spec-line: specline
+     spec-view: specview
+     spec-type: spectype
+     spec-backup: True
+     spec-stream-name: specstreamname
diff --git a/tests/scm/fixtures/p4-003.xml b/tests/scm/fixtures/p4-003.xml
new file mode 100644
index 000000000..d5d58d50d
--- /dev/null
+++ b/tests/scm/fixtures/p4-003.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <scm class="org.jenkinsci.plugins.p4.PerforceScm" plugin="p4">
+    <credential>credential</credential>
+    <workspace class="org.jenkinsci.plugins.p4.workspace.ManualWorkspaceImpl">
+      <spec>
+        <allwrite>false</allwrite>
+        <clobber>false</clobber>
+        <compress>false</compress>
+        <locked>false</locked>
+        <modtime>false</modtime>
+        <rmdir>false</rmdir>
+        <line/>
+        <view/>
+        <type/>
+        <backup>false</backup>
+        <streamName/>
+      </spec>
+      <charset>none</charset>
+      <pinHost>false</pinHost>
+      <name>workspacename</name>
+    </workspace>
+    <filter>
+      <org.jenkinsci.plugins.p4.filters.FilterViewMaskImpl>
+        <viewMask>viewmask</viewMask>
+      </org.jenkinsci.plugins.p4.filters.FilterViewMaskImpl>
+    </filter>
+    <populate class="org.jenkinsci.plugins.p4.populate.SyncOnlyImpl">
+      <have>false</have>
+      <force>false</force>
+      <modtime>false</modtime>
+      <quiet>false</quiet>
+      <parallel>
+        <enable>true</enable>
+        <threads>2</threads>
+        <minfiles>5</minfiles>
+        <minbytes>500</minbytes>
+      </parallel>
+    </populate>
+  </scm>
+</project>
diff --git a/tests/scm/fixtures/p4-003.yaml b/tests/scm/fixtures/p4-003.yaml
new file mode 100644
index 000000000..cfefeb08e
--- /dev/null
+++ b/tests/scm/fixtures/p4-003.yaml
@@ -0,0 +1,11 @@
+scm:
+  - p4:
+      credential: credential
+      workspace-type: Manual
+      workspace-name: workspacename
+      populate-type: SyncOnly
+      view-mask: viewmask
+      parallel-enabled: True
+      parallel-threads: 2
+      parallel-minfiles: 5
+      parallel-minbytes: 500