Add Gerrit SCM Support to MultiBranch project

Refers to:
https://wiki.jenkins.io/display/JENKINS/Gerrit+Code+Review+Plugin

Change-Id: Ib0c3a6d179e11b91c7a34f3385fd947413291785
Co-Authored-By: Thanh Ha <zxiiro@linux.com>
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
Signed-off-by: Thanh Ha <zxiiro@linux.com>
This commit is contained in:
Sorin Sbarnea 2018-03-10 15:21:46 -05:00
parent 3b120c3bab
commit 2104602739
5 changed files with 219 additions and 0 deletions

View File

@ -37,6 +37,9 @@ Plugins required:
* **bitbucket** (`dict`): Refer to
:func:`~bitbucket_scm <bitbucket_scm>` for documentation.
* **gerrit** (`dict`): Refer to
:func:`~gerrit_scm <gerrit_scm>` for documentation.
* **git** (`dict`): Refer to
:func:`~git_scm <git_scm>` for documentation.
@ -66,6 +69,7 @@ import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base
import jenkins_jobs.modules.helpers as helpers
import uuid
import six
from jenkins_jobs.errors import InvalidAttributeError
@ -231,6 +235,7 @@ class WorkflowMultiBranch(jenkins_jobs.modules.base.Base):
valid_scm = [
'bitbucket',
'gerrit',
'git',
'github',
]
@ -242,6 +247,9 @@ class WorkflowMultiBranch(jenkins_jobs.modules.base.Base):
if scm == 'bitbucket':
bitbucket_scm(bs, scm_data[scm])
elif scm == 'gerrit':
gerrit_scm(bs, scm_data[scm])
elif scm == 'git':
git_scm(bs, scm_data[scm])
@ -319,6 +327,81 @@ def bitbucket_scm(xml_parent, data):
XML.SubElement(source, 'traits')
def gerrit_scm(xml_parent, data):
"""Configure Gerrit SCM
Requires the :jenkins-wiki:`Gerrit Code Review Plugin
<Gerrit+Code+Review+Plugin>`.
:arg str url: The git url. (required)
:arg str credentials-id: The credential to use to connect to the GIT URL.
:arg bool ignore-on-push-notifications: If a job should not trigger upon
push notifications. (default false)
:arg list(str) refspecs: Which refspecs to look for.
(default ``['+refs/changes/*:refs/remotes/@{remote}/*',
'+refs/heads/*:refs/remotes/@{remote}/*']``)
:arg str includes: Comma-separated list of branches to be included.
(default '*')
:arg str excludes: Comma-separated list of branches to be excluded.
(default '')
Minimal Example:
.. literalinclude::
/../../tests/multibranch/fixtures/scm_gerrit_minimal.yaml
Full Example:
.. literalinclude::
/../../tests/multibranch/fixtures/scm_gerrit_full.yaml
"""
source = XML.SubElement(xml_parent, 'source', {
'class': 'jenkins.plugins.gerrit.GerritSCMSource',
'plugin': 'gerrit',
})
source_mapping = [
('', 'id', str(uuid.uuid4())),
('url', 'remote', None),
('credentials-id', 'credentialsId', ''),
('includes', 'includes', '*'),
('excludes', 'excludes', ''),
('ignore-on-push-notifications', 'ignoreOnPushNotifications', True),
]
helpers.convert_mapping_to_xml(
source, data, source_mapping, fail_required=True)
source_mapping_optional = [
('api-uri', 'apiUri', None),
]
helpers.convert_mapping_to_xml(
source, data, source_mapping_optional, fail_required=False)
# Traits
traits = XML.SubElement(source, 'traits')
XML.SubElement(traits,
'jenkins.plugins.gerrit.traits.ChangeDiscoveryTrait')
# Refspec Trait
refspec_trait = XML.SubElement(
traits, 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait', {
'plugin': 'git',
}
)
templates = XML.SubElement(refspec_trait, 'templates')
refspecs = data.get('refspecs', [
'+refs/changes/*:refs/remotes/@{remote}/*',
'+refs/heads/*:refs/remotes/@{remote}/*',
])
# convert single string to list
if isinstance(refspecs, six.string_types):
refspecs = [refspecs]
for x in refspecs:
e = XML.SubElement(
templates, ('jenkins.plugins.git.traits'
'.RefSpecsSCMSourceTrait_-RefSpecTemplate'))
XML.SubElement(e, 'value').text = x
def git_scm(xml_parent, data):
"""Configure Git SCM

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject plugin="workflow-multibranch">
<properties/>
<views>
<hudson.model.AllView>
<name>All</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../../.."/>
</hudson.model.AllView>
</views>
<viewsTabBar class="hudson.views.DefaultViewsTabBar"/>
<folderViews class="jenkins.branch.MultiBranchProjectViewHolder" plugin="branch-api">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</folderViews>
<healthMetrics>
<com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric plugin="cloudbees-folder">
<nonRecursive>false</nonRecursive>
</com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric>
</healthMetrics>
<icon class="jenkins.branch.MetadataActionFolderIcon" plugin="branch-api">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="cloudbees-folder">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
</orphanedItemStrategy>
<triggers/>
<sources class="jenkins.branch.MultiBranchProject$BranchSourceList" plugin="branch-api">
<data>
<jenkins.branch.BranchSource>
<source class="jenkins.plugins.gerrit.GerritSCMSource" plugin="gerrit">
<id>1-1-1-1-1</id>
<remote>https://review.gerrithub.io/johndoe/foo</remote>
<credentialsId>secret</credentialsId>
<includes>*</includes>
<excludes/>
<ignoreOnPushNotifications>true</ignoreOnPushNotifications>
<traits>
<jenkins.plugins.gerrit.traits.ChangeDiscoveryTrait/>
<jenkins.plugins.git.traits.RefSpecsSCMSourceTrait plugin="git">
<templates>
<jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
<value>refs/heads/*</value>
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
</templates>
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait>
</traits>
</source>
</jenkins.branch.BranchSource>
</data>
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</sources>
<factory class="org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
<scriptPath>Jenkinsfile</scriptPath>
</factory>
</org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject>

View File

@ -0,0 +1,8 @@
name: 'demo-multibranch-gerrit-min'
project-type: multibranch
scm:
- gerrit:
url: 'https://review.gerrithub.io/johndoe/foo'
credentials-id: secret
ignore-on-push-notifications: true
refspecs: 'refs/heads/*'

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject plugin="workflow-multibranch">
<properties/>
<views>
<hudson.model.AllView>
<name>All</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../../.."/>
</hudson.model.AllView>
</views>
<viewsTabBar class="hudson.views.DefaultViewsTabBar"/>
<folderViews class="jenkins.branch.MultiBranchProjectViewHolder" plugin="branch-api">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</folderViews>
<healthMetrics>
<com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric plugin="cloudbees-folder">
<nonRecursive>false</nonRecursive>
</com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric>
</healthMetrics>
<icon class="jenkins.branch.MetadataActionFolderIcon" plugin="branch-api">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="cloudbees-folder">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
</orphanedItemStrategy>
<triggers/>
<sources class="jenkins.branch.MultiBranchProject$BranchSourceList" plugin="branch-api">
<data>
<jenkins.branch.BranchSource>
<source class="jenkins.plugins.gerrit.GerritSCMSource" plugin="gerrit">
<id>1-1-1-1-1</id>
<remote>https://review.gerrithub.io/johndoe/foo</remote>
<credentialsId/>
<includes>*</includes>
<excludes/>
<ignoreOnPushNotifications>true</ignoreOnPushNotifications>
<traits>
<jenkins.plugins.gerrit.traits.ChangeDiscoveryTrait/>
<jenkins.plugins.git.traits.RefSpecsSCMSourceTrait plugin="git">
<templates>
<jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
<value>+refs/changes/*:refs/remotes/@{remote}/*</value>
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
<jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
<value>+refs/heads/*:refs/remotes/@{remote}/*</value>
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate>
</templates>
</jenkins.plugins.git.traits.RefSpecsSCMSourceTrait>
</traits>
</source>
</jenkins.branch.BranchSource>
</data>
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</sources>
<factory class="org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
<scriptPath>Jenkinsfile</scriptPath>
</factory>
</org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject>

View File

@ -0,0 +1,5 @@
name: 'demo-multibranch-gerrit-min'
project-type: multibranch
scm:
- gerrit:
url: 'https://review.gerrithub.io/johndoe/foo'