From dbbca01833954ce1f36646ad0905f57519939f22 Mon Sep 17 00:00:00 2001 From: sbussetti Date: Mon, 26 Nov 2018 15:57:13 -0500 Subject: [PATCH] Adds support for Property Strategies Multibranch pipeline jobs also support "Property Strategy" elements. These are fairly limited, but do contain the very useful "Suppress automatic SCM triggering" trait. This commit adds support for defining these for the "All branches get the same properties" strategy, and could be easily amended to also support the "Named branches get different properties" strategy in a future update. Change-Id: I3d9281657e341260a23f357f6e247793379b0eed Signed-off-by: sbussetti --- jenkins_jobs/modules/project_multibranch.py | 87 +++++++++++++++++++ .../fixtures/scm_bitbucket_full.xml | 10 +++ .../fixtures/scm_bitbucket_full.yaml | 4 + .../multibranch/fixtures/scm_gerrit_full.xml | 10 +++ .../multibranch/fixtures/scm_gerrit_full.yaml | 4 + tests/multibranch/fixtures/scm_git_full.xml | 10 +++ tests/multibranch/fixtures/scm_git_full.yaml | 4 + .../multibranch/fixtures/scm_github_full.xml | 10 +++ .../multibranch/fixtures/scm_github_full.yaml | 4 + 9 files changed, 143 insertions(+) diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index 24cc23b00..1c331b091 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -325,6 +325,10 @@ def bitbucket_scm(xml_parent, data): discovered initially or a change from the previous revision has been detected. (optional) Refer to :func:`~build_strategies `. + :arg dict property-strategies: Provides control over how to build a branch + (like to disable SCM triggering or to override the pipeline durability) + (optional) + Refer to :func:`~property_strategies `. :arg bool local-branch: Check out to matching local branch If given, checkout the revision to build as HEAD on this branch. If selected, then the branch name is computed from the remote branch @@ -485,6 +489,9 @@ def bitbucket_scm(xml_parent, data): helpers.convert_mapping_to_xml( dbr, data, dbr_mapping, fail_required=True) + if data.get('property-strategies', None): + property_strategies(xml_parent, data) + if data.get('build-strategies', None): build_strategies(xml_parent, data) @@ -564,6 +571,10 @@ def gerrit_scm(xml_parent, data): discovered initially or a change from the previous revision has been detected. (optional) Refer to :func:`~build_strategies `. + :arg dict property-strategies: Provides control over how to build a branch + (like to disable SCM triggering or to override the pipeline durability) + (optional) + Refer to :func:`~property_strategies `. Minimal Example: @@ -621,6 +632,9 @@ def gerrit_scm(xml_parent, data): '.RefSpecsSCMSourceTrait_-RefSpecTemplate')) XML.SubElement(e, 'value').text = x + if data.get('property-strategies', None): + property_strategies(xml_parent, data) + if data.get('build-strategies', None): build_strategies(xml_parent, data) @@ -648,6 +662,10 @@ def git_scm(xml_parent, data): discovered initially or a change from the previous revision has been detected. (optional) Refer to :func:`~build_strategies `. + :arg dict property-strategies: Provides control over how to build a branch + (like to disable SCM triggering or to override the pipeline durability) + (optional) + Refer to :func:`~property_strategies `. :extensions: @@ -722,6 +740,9 @@ def git_scm(xml_parent, data): 'jenkins.scm.impl.trait.RegexSCMHeadFilterTrait') XML.SubElement(rshf, 'regex').text = data.get('head-filter-regex') + if data.get('property-strategies', None): + property_strategies(xml_parent, data) + if data.get('build-strategies', None): build_strategies(xml_parent, data) @@ -774,6 +795,10 @@ def github_scm(xml_parent, data): discovered initially or a change from the previous revision has been detected. (optional) Refer to :func:`~build_strategies `. + :arg dict property-strategies: Provides control over how to build a branch + (like to disable SCM triggering or to override the pipeline durability) + (optional) + Refer to :func:`~property_strategies `. :extensions: @@ -943,6 +968,9 @@ def github_scm(xml_parent, data): helpers.convert_mapping_to_xml( dpro, data, dpro_mapping, fail_required=True) + if data.get('property-strategies', None): + property_strategies(xml_parent, data) + if data.get('build-strategies', None): build_strategies(xml_parent, data) @@ -1106,3 +1134,62 @@ def build_strategies(xml_parent, data): nb['wildcards-name'], wildcards_name_mapping, fail_required=False) + + +def property_strategies(xml_parent, data): + """Configure Basic Branch Property Strategies. + + Requires the :jenkins-wiki:`Branch API Plugin `. + + :arg dict property-strategies: Definition of property strategies. + + * **all-branches** (list): A list of property strategy definitions + for use with all branches. + + * **suppress-scm-triggering** (bool): Suppresses automatic SCM + triggering (optional) + * **pipeline-branch-durability-override** (str): Set a custom + branch speed/durability level. Valid values: + performance-optimized, survivable-nonatomic, or + max-survivability (optional) Requires the :jenkins-wiki: + `Pipeline Multibranch Plugin ` + """ + + # Valid options for the pipeline branch durability override. + pbdo_map = collections.OrderedDict([ + ("max-survivability", "MAX_SURVIVABILITY"), + ("performance-optimized", "PERFORMANCE_OPTIMIZED"), + ("survivable-nonatomic", "SURVIVABLE_NONATOMIC"), + ]) + basic_property_strategies = 'jenkins.branch' + workflow_multibranch = 'org.jenkinsci.plugins.workflow.multibranch' + dbps = XML.SubElement(xml_parent, 'strategy', { + 'class': ''.join([basic_property_strategies, + '.DefaultBranchPropertyStrategy'])}) + prop_strats = data.get('property-strategies', None) + + if prop_strats: + props_elem = XML.SubElement(dbps, 'properties', { + 'class': 'java.util.Arrays$ArrayList'}) + props_elem = XML.SubElement(props_elem, 'a', { + 'class': ''.join([ + basic_property_strategies, '.BranchProperty-array'])}) + + for dbs_list in prop_strats.get('all-branches', None): + + if dbs_list.get('suppress-scm-triggering', False): + XML.SubElement(props_elem, ''.join([ + basic_property_strategies, '.NoTriggerBranchProperty'])) + + pbdo_val = dbs_list.get( + 'pipeline-branch-durability-override', None) + if pbdo_val: + if not pbdo_map.get(pbdo_val): + raise InvalidAttributeError( + 'pipeline-branch-durability-override', + pbdo_val, + pbdo_map.keys()) + pbdo_elem = XML.SubElement(props_elem, ''.join([ + workflow_multibranch, '.DurabilityHintBranchProperty']), { + 'plugin': 'workflow-multibranch'}) + XML.SubElement(pbdo_elem, 'hint').text = pbdo_map.get(pbdo_val) diff --git a/tests/multibranch/fixtures/scm_bitbucket_full.xml b/tests/multibranch/fixtures/scm_bitbucket_full.xml index c73893351..3a32f7c11 100644 --- a/tests/multibranch/fixtures/scm_bitbucket_full.xml +++ b/tests/multibranch/fixtures/scm_bitbucket_full.xml @@ -104,6 +104,16 @@ + + + + + + MAX_SURVIVABILITY + + + + 86400000 diff --git a/tests/multibranch/fixtures/scm_bitbucket_full.yaml b/tests/multibranch/fixtures/scm_bitbucket_full.yaml index 73c116e08..f203fb6eb 100644 --- a/tests/multibranch/fixtures/scm_bitbucket_full.yaml +++ b/tests/multibranch/fixtures/scm_bitbucket_full.yaml @@ -19,6 +19,10 @@ scm: filter-by-name-wildcard: includes: '*' excludes: 'master' + property-strategies: + all-branches: + - suppress-scm-triggering: true + - pipeline-branch-durability-override: max-survivability build-strategies: - tags: ignore-tags-newer-than: 1 diff --git a/tests/multibranch/fixtures/scm_gerrit_full.xml b/tests/multibranch/fixtures/scm_gerrit_full.xml index 2c511acc5..5ba878b0a 100644 --- a/tests/multibranch/fixtures/scm_gerrit_full.xml +++ b/tests/multibranch/fixtures/scm_gerrit_full.xml @@ -49,6 +49,16 @@ + + + + + + MAX_SURVIVABILITY + + + + 86400000 diff --git a/tests/multibranch/fixtures/scm_gerrit_full.yaml b/tests/multibranch/fixtures/scm_gerrit_full.yaml index 15f8eb18f..36040cb5b 100644 --- a/tests/multibranch/fixtures/scm_gerrit_full.yaml +++ b/tests/multibranch/fixtures/scm_gerrit_full.yaml @@ -7,6 +7,10 @@ scm: credentials-id: secret ignore-on-push-notifications: true refspecs: 'refs/heads/*' + property-strategies: + all-branches: + - suppress-scm-triggering: true + - pipeline-branch-durability-override: max-survivability build-strategies: - tags: ignore-tags-newer-than: 1 diff --git a/tests/multibranch/fixtures/scm_git_full.xml b/tests/multibranch/fixtures/scm_git_full.xml index e54352176..837f0e55c 100644 --- a/tests/multibranch/fixtures/scm_git_full.xml +++ b/tests/multibranch/fixtures/scm_git_full.xml @@ -81,6 +81,16 @@ + + + + + + MAX_SURVIVABILITY + + + + 86400000 diff --git a/tests/multibranch/fixtures/scm_git_full.yaml b/tests/multibranch/fixtures/scm_git_full.yaml index 71a93278c..5e2a28f54 100644 --- a/tests/multibranch/fixtures/scm_git_full.yaml +++ b/tests/multibranch/fixtures/scm_git_full.yaml @@ -9,6 +9,10 @@ scm: ignore-on-push-notifications: true discover-tags: true head-filter-regex: 'master|\d+\.\d+' + property-strategies: + all-branches: + - suppress-scm-triggering: true + - pipeline-branch-durability-override: max-survivability build-strategies: - tags: ignore-tags-newer-than: 1 diff --git a/tests/multibranch/fixtures/scm_github_full.xml b/tests/multibranch/fixtures/scm_github_full.xml index 769d066f3..a74e2e6d4 100644 --- a/tests/multibranch/fixtures/scm_github_full.xml +++ b/tests/multibranch/fixtures/scm_github_full.xml @@ -93,6 +93,16 @@ + + + + + + MAX_SURVIVABILITY + + + + 86400000 diff --git a/tests/multibranch/fixtures/scm_github_full.yaml b/tests/multibranch/fixtures/scm_github_full.yaml index 33a477c3b..9b74e8377 100644 --- a/tests/multibranch/fixtures/scm_github_full.yaml +++ b/tests/multibranch/fixtures/scm_github_full.yaml @@ -14,6 +14,10 @@ scm: discover-pr-forks-trust: everyone discover-pr-origin: both discover-tags: true + property-strategies: + all-branches: + - suppress-scm-triggering: true + - pipeline-branch-durability-override: max-survivability build-strategies: - tags: ignore-tags-newer-than: 1