From bdedf1460cda8e6db73c2bbe908a1192664a6865 Mon Sep 17 00:00:00 2001 From: Igor Lakhtenkov Date: Wed, 30 Dec 2020 16:30:28 +0300 Subject: [PATCH] Added support of SCM Filter branch PR behavior filters for multibranch projects Change-Id: I29d1050f021d509ca021136cc90a113756cd11cf Task: #41308 --- jenkins_jobs/modules/project_multibranch.py | 131 ++++++++++++++++++ .../fixtures/scm_bitbucket_full.xml | 20 +++ .../fixtures/scm_bitbucket_full.yaml | 17 +++ tests/multibranch/fixtures/scm_git_full.xml | 20 +++ tests/multibranch/fixtures/scm_git_full.yaml | 17 +++ .../multibranch/fixtures/scm_github_full.xml | 20 +++ .../multibranch/fixtures/scm_github_full.yaml | 17 +++ 7 files changed, 242 insertions(+) diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index 9fc8afa2e..0dc873ad1 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -374,6 +374,10 @@ def bitbucket_scm(xml_parent, data): :arg str head-filter-regex: A regular expression for filtering discovered source branches. Requires the :jenkins-plugins:`SCM API Plugin `. + :arg list head-pr-filter-behaviors: Definition of Filter Branch PR behaviors. + Requires the :jenkins-plugins:`SCM Filter Branch PR Plugin + `. + Refer to :func:`~add_filter_branch_pr_behaviors `. :arg str discover-branch: Discovers branches on the repository. Valid options: ex-pr, only-pr, all. Value is not specified by default. @@ -522,6 +526,9 @@ def bitbucket_scm(xml_parent, data): rshf = XML.SubElement(traits, "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait") XML.SubElement(rshf, "regex").text = data.get("head-filter-regex") + if data.get("head-pr-filter-behaviors", None): + add_filter_branch_pr_behaviors(traits, data.get("head-pr-filter-behaviors")) + if data.get("discover-pr-origin", None): dpro = XML.SubElement( traits, @@ -771,6 +778,10 @@ def git_scm(xml_parent, data): :arg str head-filter-regex: A regular expression for filtering discovered source branches. Requires the :jenkins-plugins:`SCM API Plugin `. + :arg list head-pr-filter-behaviors: Definition of Filter Branch PR behaviors. + Requires the :jenkins-plugins:`SCM Filter Branch PR Plugin + `. + Refer to :func:`~add_filter_branch_pr_behaviors `. :arg list build-strategies: Provides control over whether to build a branch (or branch like things such as change requests and tags) whenever it is discovered initially or a change from the previous revision has been @@ -852,6 +863,9 @@ def git_scm(xml_parent, data): rshf = XML.SubElement(traits, "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait") XML.SubElement(rshf, "regex").text = data.get("head-filter-regex") + if data.get("head-pr-filter-behaviors", None): + add_filter_branch_pr_behaviors(traits, data.get("head-pr-filter-behaviors")) + if data.get("property-strategies", None): property_strategies(xml_parent, data) @@ -902,6 +916,10 @@ def github_scm(xml_parent, data): Valid options: merge-current, current, both, false. (default 'merge-current') :arg bool discover-tags: Discovers tags on the repository. (default false) + :arg list head-pr-filter-behaviors: Definition of Filter Branch PR behaviors. + Requires the :jenkins-plugins:`SCM Filter Branch PR Plugin + `. + Refer to :func:`~add_filter_branch_pr_behaviors `. :arg list build-strategies: Provides control over whether to build a branch (or branch like things such as change requests and tags) whenever it is discovered initially or a change from the previous revision has been @@ -1059,6 +1077,9 @@ def github_scm(xml_parent, data): rshf = XML.SubElement(traits, "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait") XML.SubElement(rshf, "regex").text = data.get("head-filter-regex") + if data.get("head-pr-filter-behaviors", None): + add_filter_branch_pr_behaviors(traits, data.get("head-pr-filter-behaviors")) + if data.get("property-strategies", None): property_strategies(xml_parent, data) @@ -1601,3 +1622,113 @@ def apply_property_strategies(props_elem, props_list): "".join([pr_comment_build, pcb_bool_opts.get(opt)]), {"plugin": "github-pr-comment-build"}, ) + + +def add_filter_branch_pr_behaviors(traits, data): + """Configure Filter Branch PR behaviors + + Requires the :jenkins-plugins:`SCM Filter Branch PR Plugin + `. + + :arg list head-pr-filter-behaviors: Definition of filters. + + * **head-pr-destined-regex** (dict): Filter by name incl. PR destined to + this branch with regexp + + * **branch-regexp** (str) Regular expression to filter branches and + PRs (optional, default ".*") + * **tag-regexp** (str) Regular expression to filter tags + (optional, default "(?!.*)") + + * **head-pr-destined-wildcard** (dict): Filter by name incl. PR + destined to this branch with wildcard + + * **branch-includes** (str) Wildcard expression to include branches + and PRs (optional, default "*") + * **tag-includes** (str) Wildcard expression to include tags + (optional, default "") + * **branch-excludes** (str) Wildcard expression to exclude branches + and PRs (optional, default "") + * **tag-excludes** (str) Wildcard expression to exclude tags + (optional, default "*") + + * **head-pr-originated-regex** (dict): Filter by name incl. PR destined + to this branch with regexp + + * **branch-regexp** (str) Regular expression to filter branches + and PRs (optional, default ".*") + * **tag-regexp** (str) Regular expression to filter tags + (optional, default "(?!.*)") + + * **head-pr-originated-wildcard** (dict): Filter by name incl. PR + destined to this branch with wildcard + + * **branch-includes** (str) Wildcard expression to include branches + and PRs (optional, default "*") + * **tag-includes** (str) Wildcard expression to include tags + (optional, default "") + * **branch-excludes** (str) Wildcard expression to exclude branches + and PRs (optional, default "") + * **tag-excludes** (str) Wildcard expression to exclude tags + (optional, default "*") + """ + + regexp_mapping = [ + ("branch-regexp", "regex", ".*"), + ("tag-regexp", "tagRegex", "(?!.*)"), + ] + wildcard_mapping = [ + ("branch-includes", "includes", "*"), + ("branch-excludes", "excludes", ""), + ("tag-includes", "tagIncludes", ""), + ("tag-excludes", "tagExcludes", "*"), + ] + + if data.get("head-pr-destined-regex"): + rshf = XML.SubElement( + traits, + "net.gleske.scmfilter.impl.trait.RegexSCMHeadFilterTrait", + {"plugin": "scm-filter-branch-pr"}, + ) + helpers.convert_mapping_to_xml( + rshf, data.get("head-pr-destined-regex"), regexp_mapping, fail_required=True + ) + + if data.get("head-pr-destined-wildcard"): + wshf = XML.SubElement( + traits, + "net.gleske.scmfilter.impl.trait.WildcardSCMHeadFilterTrait", + {"plugin": "scm-filter-branch-pr"}, + ) + helpers.convert_mapping_to_xml( + wshf, + data.get("head-pr-destined-wildcard"), + wildcard_mapping, + fail_required=True, + ) + + if data.get("head-pr-originated-regex"): + rsof = XML.SubElement( + traits, + "net.gleske.scmfilter.impl.trait.RegexSCMOriginFilterTrait", + {"plugin": "scm-filter-branch-pr"}, + ) + helpers.convert_mapping_to_xml( + rsof, + data.get("head-pr-originated-regex"), + regexp_mapping, + fail_required=True, + ) + + if data.get("head-pr-originated-wildcard"): + wsof = XML.SubElement( + traits, + "net.gleske.scmfilter.impl.trait.WildcardSCMOriginFilterTrait", + {"plugin": "scm-filter-branch-pr"}, + ) + helpers.convert_mapping_to_xml( + wsof, + data.get("head-pr-originated-wildcard"), + wildcard_mapping, + fail_required=True, + ) diff --git a/tests/multibranch/fixtures/scm_bitbucket_full.xml b/tests/multibranch/fixtures/scm_bitbucket_full.xml index 90425c4fc..30c759e32 100644 --- a/tests/multibranch/fixtures/scm_bitbucket_full.xml +++ b/tests/multibranch/fixtures/scm_bitbucket_full.xml @@ -55,6 +55,26 @@ master|\d+\.\d+ + + foo/.* + 20\..* + + + foo* + bar* + qaz* + *baz + + + (foo/.*|bar/.*) + 1\..* + + + qaz* + baz* + bar* + *qaz + 2 diff --git a/tests/multibranch/fixtures/scm_bitbucket_full.yaml b/tests/multibranch/fixtures/scm_bitbucket_full.yaml index 22d0defc5..8b1a02467 100644 --- a/tests/multibranch/fixtures/scm_bitbucket_full.yaml +++ b/tests/multibranch/fixtures/scm_bitbucket_full.yaml @@ -10,6 +10,23 @@ scm: discover-tags: true lfs: true head-filter-regex: 'master|\d+\.\d+' + head-pr-filter-behaviors: + head-pr-destined-regex: + branch-regexp: "foo/.*" + tag-regexp: "20\\..*" + head-pr-destined-wildcard: + branch-includes: "foo*" + tag-includes: "qaz*" + branch-excludes: "bar*" + tag-excludes: "*baz" + head-pr-originated-regex: + branch-regexp: "(foo/.*|bar/.*)" + tag-regexp: "1\\..*" + head-pr-originated-wildcard: + branch-includes: "qaz*" + tag-includes: "bar*" + branch-excludes: "baz*" + tag-excludes: "*qaz" discover-pr-origin: headOnly discover-branch: all discover-pr-forks-strategy: current diff --git a/tests/multibranch/fixtures/scm_git_full.xml b/tests/multibranch/fixtures/scm_git_full.xml index 30aeabeb0..c2c508cd7 100644 --- a/tests/multibranch/fixtures/scm_git_full.xml +++ b/tests/multibranch/fixtures/scm_git_full.xml @@ -41,6 +41,26 @@ master|\d+\.\d+ + + foo/.* + 20\..* + + + foo* + bar* + qaz* + *baz + + + (foo/.*|bar/.*) + 1\..* + + + qaz* + baz* + bar* + *qaz + diff --git a/tests/multibranch/fixtures/scm_git_full.yaml b/tests/multibranch/fixtures/scm_git_full.yaml index aaa0ce6bc..a663ead86 100644 --- a/tests/multibranch/fixtures/scm_git_full.yaml +++ b/tests/multibranch/fixtures/scm_git_full.yaml @@ -9,6 +9,23 @@ scm: ignore-on-push-notifications: true discover-tags: true head-filter-regex: 'master|\d+\.\d+' + head-pr-filter-behaviors: + head-pr-destined-regex: + branch-regexp: "foo/.*" + tag-regexp: "20\\..*" + head-pr-destined-wildcard: + branch-includes: "foo*" + tag-includes: "qaz*" + branch-excludes: "bar*" + tag-excludes: "*baz" + head-pr-originated-regex: + branch-regexp: "(foo/.*|bar/.*)" + tag-regexp: "1\\..*" + head-pr-originated-wildcard: + branch-includes: "qaz*" + tag-includes: "bar*" + branch-excludes: "baz*" + tag-excludes: "*qaz" property-strategies: all-branches: - suppress-scm-triggering: true diff --git a/tests/multibranch/fixtures/scm_github_full.xml b/tests/multibranch/fixtures/scm_github_full.xml index c5b883ed9..d1a568371 100644 --- a/tests/multibranch/fixtures/scm_github_full.xml +++ b/tests/multibranch/fixtures/scm_github_full.xml @@ -55,6 +55,26 @@ (.*/master|.*/release/.*) + + foo/.* + 20\..* + + + foo* + bar* + qaz* + *baz + + + (foo/.*|bar/.*) + 1\..* + + + qaz* + baz* + bar* + *qaz + jenkins.example.com/my_context true diff --git a/tests/multibranch/fixtures/scm_github_full.yaml b/tests/multibranch/fixtures/scm_github_full.yaml index 92417c426..79215d0ff 100644 --- a/tests/multibranch/fixtures/scm_github_full.yaml +++ b/tests/multibranch/fixtures/scm_github_full.yaml @@ -11,6 +11,23 @@ scm: credentials-id: example-credential branch-discovery: all head-filter-regex: "(.*/master|.*/release/.*)" + head-pr-filter-behaviors: + head-pr-destined-regex: + branch-regexp: "foo/.*" + tag-regexp: "20\\..*" + head-pr-destined-wildcard: + branch-includes: "foo*" + tag-includes: "qaz*" + branch-excludes: "bar*" + tag-excludes: "*baz" + head-pr-originated-regex: + branch-regexp: "(foo/.*|bar/.*)" + tag-regexp: "1\\..*" + head-pr-originated-wildcard: + branch-includes: "qaz*" + tag-includes: "bar*" + branch-excludes: "baz*" + tag-excludes: "*qaz" discover-pr-forks-strategy: both discover-pr-forks-trust: everyone discover-pr-origin: both