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