From cd8c8bfbb70d03d41a8da63e6edfec4e6b57218b Mon Sep 17 00:00:00 2001 From: Piotr Falkowski Date: Thu, 30 Mar 2023 12:31:52 +0200 Subject: [PATCH] Added configuration for Suppress SCM Triggering In version 2.1045.v4ec3ed07b_e4f Branch API Plugin added options to configure suppressing SCM trigger. Previously this feature could be only turned on or off, now it's possible to select what to suppress and on which branches. (see https://github.com/jenkinsci/branch-api-plugin/pull/244) This commit adds support for this option by allowing dictionary syntax for `suppress-scm-triggering` element: ``` - suppress-scm-triggering: suppression-strategy: suppress-branch-indexing branch-regex: ^.*test.*$ ``` while preserving support for currently existing syntax: ``` - suppress-scm-triggering: true ``` Change-Id: I5d0b32042fc07d674bf2f26e5b468f051077fb02 --- jenkins_jobs/modules/project_multibranch.py | 91 +++++++++++++++++-- ...suppress_scm_trigger_invalid_type001.error | 1 + ..._suppress_scm_trigger_invalid_type001.yaml | 17 ++++ ...ess_scm_trigger_missing_branch_regex.error | 1 + ...ress_scm_trigger_missing_branch_regex.yaml | 17 ++++ ...uppress_scm_trigger_missing_strategy.error | 1 + ...suppress_scm_trigger_missing_strategy.yaml | 17 ++++ ..._suppress_scm_trigger_wrong_strategy.error | 2 + ...b_suppress_scm_trigger_wrong_strategy.yaml | 18 ++++ .../multibranch/fixtures/scm_github_full.xml | 5 +- .../multibranch/fixtures/scm_github_full.yaml | 4 +- .../scm_github_suppress_scm_trigger.xml | 81 +++++++++++++++++ .../scm_github_suppress_scm_trigger.yaml | 16 ++++ ...cm_github_suppress_scm_trigger_boolean.xml | 77 ++++++++++++++++ ...m_github_suppress_scm_trigger_boolean.yaml | 14 +++ 15 files changed, 352 insertions(+), 10 deletions(-) create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.error create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.yaml create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.error create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.yaml create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.error create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.yaml create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.error create mode 100644 tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.yaml create mode 100644 tests/multibranch/fixtures/scm_github_suppress_scm_trigger.xml create mode 100644 tests/multibranch/fixtures/scm_github_suppress_scm_trigger.yaml create mode 100644 tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.xml create mode 100644 tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.yaml diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index 73adbf8e7..7a9bb4e0f 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -1460,8 +1460,21 @@ def property_strategies(xml_parent, data): * **all-branches** (list): A list of property strategy definitions for use with all branches. - * **suppress-scm-triggering** (bool): Suppresses automatic SCM - triggering (optional) + * **suppress-scm-triggering** (dict): + Suppresses automatic SCM triggering (optional). + + * **branch-regex** (str): + Regex matching branch names. + Only these branches will be affected by the selected + suppression strategy. Default ``^$``. + * **suppress-strategy** (str): + Select what to suppress for branches matching the regex. + Valid values: ``suppress-nothing`` (default), + ``suppress-branch-indexing``, or ``suppress-webhooks``. + + .. note:: + Using ``suppress-scm-triggering: true`` is deprecated + since Branch API 2.1045.v4ec3ed07b_e4f. * **pipeline-branch-durability-override** (str): Set a custom branch speed/durability level. Valid values: performance-optimized, survivable-nonatomic, or @@ -1509,8 +1522,21 @@ def property_strategies(xml_parent, data): to be applied by default to all branches, unless overridden by an entry in `exceptions` - * **suppress-scm-triggering** (bool): Suppresses automatic SCM - triggering (optional) + * **suppress-scm-triggering** (dict): + Suppresses automatic SCM triggering (optional). + + * **branch-regex** (str): + Regex matching branch names. + Only these branches will be affected by the selected + suppression strategy. Default ``^$``. + * **suppress-strategy** (str): + Select what to suppress for branches matching the regex. + Valid values: ``suppress-nothing`` (default), + ``suppress-branch-indexing``, or ``suppress-webhooks``. + + .. note:: + Using ``suppress-scm-triggering: true`` is deprecated + since Branch API 2.1045.v4ec3ed07b_e4f. * **pipeline-branch-durability-override** (str): Set a custom branch speed/durability level. Valid values: performance-optimized, survivable-nonatomic, or @@ -1562,8 +1588,22 @@ def property_strategies(xml_parent, data): * **properties** (list): A list of properties to apply to this branch. - * **suppress-scm-triggering** (bool): Suppresses - automatic SCM triggering (optional) + * **suppress-scm-triggering** (dict): + Suppresses automatic SCM triggering (optional). + + * **branch-regex** (str): + Regex matching branch names. + Only these branches will be affected by the selected + suppression strategy. Default ``^$``. + * **suppress-strategy** (str): + Select what to suppress for branches matching the regex. + Valid values: ``suppress-nothing`` (default), + ``suppress-branch-indexing``, or ``suppress-webhooks``. + + .. note:: + Using ``suppress-scm-triggering: true`` is deprecated + since Branch API 2.1045.v4ec3ed07b_e4f. + * **pipeline-branch-durability-override** (str): Set a custom branch speed/durability level. Valid values: performance-optimized, survivable-nonatomic, or @@ -1733,14 +1773,49 @@ def apply_property_strategies(props_elem, props_list): ] ) + # valid options for suppress SCM trigger property + # strategy name says what's blocked + sst_map = collections.OrderedDict( + [ + ("suppress-nothing", "NONE"), + ("suppress-webhooks", "EVENTS"), + ("suppress-branch-indexing", "INDEXING"), + ] + ) + for dbs_list in props_list: - if dbs_list.get("suppress-scm-triggering", False): - XML.SubElement( + sst_val = dbs_list.get("suppress-scm-triggering", False) + if sst_val: + sst_elem = XML.SubElement( props_elem, "".join([basic_property_strategies, ".NoTriggerBranchProperty"]), ) + if isinstance(sst_val, dict): + if "branch-regex" not in sst_val: + raise MissingAttributeError("suppress-scm-triggering[branch-regex]") + if "suppression-strategy" not in sst_val: + raise MissingAttributeError( + "suppress-scm-triggering[suppression-strategy]" + ) + + sst_ss_val = sst_val["suppression-strategy"] + if not sst_map.get(sst_ss_val): + raise InvalidAttributeError( + "suppression-strategy", sst_ss_val, sst_map.keys() + ) + XML.SubElement(sst_elem, "triggeredBranchesRegex").text = sst_val[ + "branch-regex" + ] + XML.SubElement(sst_elem, "strategy").text = sst_map[sst_ss_val] + + elif isinstance(sst_val, bool): + # no sub-elements in this case + pass + else: + raise InvalidAttributeError("suppress-scm-triggering", sst_val) + pbdo_val = dbs_list.get("pipeline-branch-durability-override", None) if pbdo_val: if not pbdo_map.get(pbdo_val): diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.error b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.error new file mode 100644 index 000000000..5183273ab --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.error @@ -0,0 +1 @@ +'['test']' is an invalid value for attribute name.suppress-scm-triggering diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.yaml b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.yaml new file mode 100644 index 000000000..10bd4d495 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_invalid_type001.yaml @@ -0,0 +1,17 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: + - test + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: + allow-untrusted-users: false + - trigger-build-on-pr-update: + allow-untrusted-users: false diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.error b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.error new file mode 100644 index 000000000..35fe692e0 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.error @@ -0,0 +1 @@ +Missing suppress-scm-triggering[branch-regex] from an instance of 'name' diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.yaml b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.yaml new file mode 100644 index 000000000..32582f567 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_branch_regex.yaml @@ -0,0 +1,17 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: + suppression-strategy: suppress-webhooks + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: + allow-untrusted-users: false + - trigger-build-on-pr-update: + allow-untrusted-users: false diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.error b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.error new file mode 100644 index 000000000..63f8813bf --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.error @@ -0,0 +1 @@ +Missing suppress-scm-triggering[suppression-strategy] from an instance of 'name' diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.yaml b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.yaml new file mode 100644 index 000000000..de5644ae3 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_missing_strategy.yaml @@ -0,0 +1,17 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: + branch-regex: ^.*test.*$ + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: + allow-untrusted-users: false + - trigger-build-on-pr-update: + allow-untrusted-users: false diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.error b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.error new file mode 100644 index 000000000..787dfbf41 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.error @@ -0,0 +1,2 @@ +'suppress-build' is an invalid value for attribute name.suppression-strategy +Valid values include: 'suppress-nothing', 'suppress-webhooks', 'suppress-branch-indexing' diff --git a/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.yaml b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.yaml new file mode 100644 index 000000000..1bb9f4a36 --- /dev/null +++ b/tests/multibranch/error_fixtures/scm_github_suppress_scm_trigger_wrong_strategy.yaml @@ -0,0 +1,18 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: + suppression-strategy: suppress-build + branch-regex: ^$ + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: + allow-untrusted-users: false + - trigger-build-on-pr-update: + allow-untrusted-users: false diff --git a/tests/multibranch/fixtures/scm_github_full.xml b/tests/multibranch/fixtures/scm_github_full.xml index 3c0410aa8..dd5b46ec1 100644 --- a/tests/multibranch/fixtures/scm_github_full.xml +++ b/tests/multibranch/fixtures/scm_github_full.xml @@ -167,7 +167,10 @@ - + + ^.*test.*$ + INDEXING + MAX_SURVIVABILITY diff --git a/tests/multibranch/fixtures/scm_github_full.yaml b/tests/multibranch/fixtures/scm_github_full.yaml index 6d2e97ab3..ce1c72044 100644 --- a/tests/multibranch/fixtures/scm_github_full.yaml +++ b/tests/multibranch/fixtures/scm_github_full.yaml @@ -45,7 +45,9 @@ scm: verbose-logs: true property-strategies: all-branches: - - suppress-scm-triggering: true + - suppress-scm-triggering: + suppression-strategy: suppress-branch-indexing + branch-regex: ^.*test.*$ - pipeline-branch-durability-override: max-survivability - trigger-build-on-pr-comment: comment: "Ci build!" diff --git a/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.xml b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.xml new file mode 100644 index 000000000..6a2e9718c --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.xml @@ -0,0 +1,81 @@ + + + + + + All + false + false + + + + + + + + + + + false + + + + + + + true + -1 + -1 + false + + + + + + + gh-johndoe-foo + johndoe + foo + + + 1 + + + 1 + + + + 1 + + + + + + + + + + + ^$ + EVENTS + + + MAX_SURVIVABILITY + + + Ci build! + + + + + + + + + + + + + Jenkinsfile + + diff --git a/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.yaml b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.yaml new file mode 100644 index 000000000..5e6dd655d --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger.yaml @@ -0,0 +1,16 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: + suppression-strategy: suppress-webhooks + branch-regex: ^$ + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: true + - trigger-build-on-pr-update: true diff --git a/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.xml b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.xml new file mode 100644 index 000000000..17109c8fc --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.xml @@ -0,0 +1,77 @@ + + + + + + All + false + false + + + + + + + + + + + false + + + + + + + true + -1 + -1 + false + + + + + + + gh-johndoe-foo + johndoe + foo + + + 1 + + + 1 + + + + 1 + + + + + + + + + + + MAX_SURVIVABILITY + + + Ci build! + + + + + + + + + + + + + Jenkinsfile + + diff --git a/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.yaml b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.yaml new file mode 100644 index 000000000..1af40ab4c --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_suppress_scm_trigger_boolean.yaml @@ -0,0 +1,14 @@ +name: 'demo-multibranch-github-min' +project-type: multibranch +scm: + - github: + repo: 'foo' + repo-owner: 'johndoe' + + property-strategies: + all-branches: + - suppress-scm-triggering: false + - pipeline-branch-durability-override: max-survivability + - trigger-build-on-pr-comment: "Ci build!" + - trigger-build-on-pr-review: true + - trigger-build-on-pr-update: true