From 210460273918e68ab90fb6f7173cfe0f7acf26f5 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sat, 10 Mar 2018 15:21:46 -0500 Subject: [PATCH] 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 Signed-off-by: Sorin Sbarnea Signed-off-by: Thanh Ha --- jenkins_jobs/modules/project_multibranch.py | 83 +++++++++++++++++++ .../multibranch/fixtures/scm_gerrit_full.xml | 60 ++++++++++++++ .../multibranch/fixtures/scm_gerrit_full.yaml | 8 ++ .../fixtures/scm_gerrit_minimal.xml | 63 ++++++++++++++ .../fixtures/scm_gerrit_minimal.yaml | 5 ++ 5 files changed, 219 insertions(+) create mode 100644 tests/multibranch/fixtures/scm_gerrit_full.xml create mode 100644 tests/multibranch/fixtures/scm_gerrit_full.yaml create mode 100644 tests/multibranch/fixtures/scm_gerrit_minimal.xml create mode 100644 tests/multibranch/fixtures/scm_gerrit_minimal.yaml diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index 479f7ed46..ae7d6df3b 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -37,6 +37,9 @@ Plugins required: * **bitbucket** (`dict`): Refer to :func:`~bitbucket_scm ` for documentation. + * **gerrit** (`dict`): Refer to + :func:`~gerrit_scm ` for documentation. + * **git** (`dict`): Refer to :func:`~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 + `. + + :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 diff --git a/tests/multibranch/fixtures/scm_gerrit_full.xml b/tests/multibranch/fixtures/scm_gerrit_full.xml new file mode 100644 index 000000000..e14588ae1 --- /dev/null +++ b/tests/multibranch/fixtures/scm_gerrit_full.xml @@ -0,0 +1,60 @@ + + + + + + All + false + false + + + + + + + + + + + false + + + + + + + true + -1 + -1 + + + + + + + 1-1-1-1-1 + https://review.gerrithub.io/johndoe/foo + secret + * + + true + + + + + + refs/heads/* + + + + + + + + + + + + Jenkinsfile + + diff --git a/tests/multibranch/fixtures/scm_gerrit_full.yaml b/tests/multibranch/fixtures/scm_gerrit_full.yaml new file mode 100644 index 000000000..373bffca0 --- /dev/null +++ b/tests/multibranch/fixtures/scm_gerrit_full.yaml @@ -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/*' diff --git a/tests/multibranch/fixtures/scm_gerrit_minimal.xml b/tests/multibranch/fixtures/scm_gerrit_minimal.xml new file mode 100644 index 000000000..03ad96cb7 --- /dev/null +++ b/tests/multibranch/fixtures/scm_gerrit_minimal.xml @@ -0,0 +1,63 @@ + + + + + + All + false + false + + + + + + + + + + + false + + + + + + + true + -1 + -1 + + + + + + + 1-1-1-1-1 + https://review.gerrithub.io/johndoe/foo + + * + + true + + + + + + +refs/changes/*:refs/remotes/@{remote}/* + + + +refs/heads/*:refs/remotes/@{remote}/* + + + + + + + + + + + + Jenkinsfile + + diff --git a/tests/multibranch/fixtures/scm_gerrit_minimal.yaml b/tests/multibranch/fixtures/scm_gerrit_minimal.yaml new file mode 100644 index 000000000..bad9c191a --- /dev/null +++ b/tests/multibranch/fixtures/scm_gerrit_minimal.yaml @@ -0,0 +1,5 @@ +name: 'demo-multibranch-gerrit-min' +project-type: multibranch +scm: + - gerrit: + url: 'https://review.gerrithub.io/johndoe/foo'