From bf385683631006bc2f6c6e52dc93e5e3232f9472 Mon Sep 17 00:00:00 2001 From: Kristian Kraljic Date: Fri, 29 Apr 2022 22:52:13 +0200 Subject: [PATCH] add: type-suffix for GitHub notification-context trait Change-Id: Ibadc0de88d87df95d0319da8872c881247cd1ba6 Story: 2009995 --- jenkins_jobs/modules/project_githuborg.py | 15 +- jenkins_jobs/modules/project_multibranch.py | 56 +++- tests/githuborg/fixtures/github-org-full.xml | 2 +- tests/githuborg/fixtures/github-org-full.yaml | 4 +- .../fixtures/github-org-legacy-nc.xml | 229 ++++++++++++++++ .../fixtures/github-org-legacy-nc.yaml | 101 ++++++++ .../multibranch/fixtures/scm_github_full.xml | 2 +- .../multibranch/fixtures/scm_github_full.yaml | 4 +- .../fixtures/scm_github_legacy_nc.xml | 245 ++++++++++++++++++ .../fixtures/scm_github_legacy_nc.yaml | 104 ++++++++ 10 files changed, 737 insertions(+), 25 deletions(-) create mode 100644 tests/githuborg/fixtures/github-org-legacy-nc.xml create mode 100644 tests/githuborg/fixtures/github-org-legacy-nc.yaml create mode 100644 tests/multibranch/fixtures/scm_github_legacy_nc.xml create mode 100644 tests/multibranch/fixtures/scm_github_legacy_nc.yaml diff --git a/jenkins_jobs/modules/project_githuborg.py b/jenkins_jobs/modules/project_githuborg.py index 0e5163e10..45577e990 100644 --- a/jenkins_jobs/modules/project_githuborg.py +++ b/jenkins_jobs/modules/project_githuborg.py @@ -228,10 +228,12 @@ def github_org(xml_parent, data): Requires the :jenkins-plugins:`SCM Filter Branch PR Plugin `. Refer to :func:`~add_filter_branch_pr_behaviors `. - :arg str notification-context: Change the default GitHub check notification - context from "continuous-integration/jenkins/SUFFIX" to a custom text, + :arg dict notification-context: Change the default GitHub check notification + context from "continuous-integration/jenkins/SUFFIX" to a custom label / suffix. + (set a label and suffix to true or false, optional) Requires the :jenkins-plugins:`Github Custom Notification Context SCM Behaviour `. + Refer to :func:`~add_notification_context_trait `. :arg dict property-strategies: Provides control over how to build a branch (like to disable SCM triggering or to override the pipeline durability) (optional) @@ -387,14 +389,7 @@ def github_org(xml_parent, data): if data.get("build-strategies", None): multibranch.build_strategies(xml_parent, data) - if data.get("notification-context", None): - rshf = XML.SubElement( - traits, - "org.jenkinsci.plugins.githubScmTraitNotificationContext." - "NotificationContextTrait", - ) - XML.SubElement(rshf, "contextLabel").text = data.get("notification-context") - XML.SubElement(rshf, "typeSuffix").text = "true" + multibranch.add_notification_context_trait(traits, data) # handle the default git extensions like: # - clean diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index 0ea412221..4ed5414b0 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -983,10 +983,12 @@ 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 str notification-context: Change the default GitHub check notification - context from "continuous-integration/jenkins/SUFFIX" to a custom text, + :arg dict notification-context: Change the default GitHub check notification + context from "continuous-integration/jenkins/SUFFIX" to a custom label / suffix. + (set a label and suffix to true or false, optional) Requires the :jenkins-plugins:`Github Custom Notification Context SCM Behaviour `. + Refer to :func:`~add_notification_context_trait `. :arg dict property-strategies: Provides control over how to build a branch (like to disable SCM triggering or to override the pipeline durability) (optional) @@ -1158,15 +1160,7 @@ def github_scm(xml_parent, data): if data.get("build-strategies", None): build_strategies(xml_parent, data) - if data.get("notification-context", None): - rshf = XML.SubElement( - traits, - "org.jenkinsci.plugins.githubScmTraitNotificationContext." - "NotificationContextTrait", - ) - XML.SubElement(rshf, "contextLabel").text = data.get("notification-context") - XML.SubElement(rshf, "typeSuffix").text = "true" - + add_notification_context_trait(traits, data) add_filter_by_name_wildcard_behaviors(traits, data) # handle the default git extensions like: @@ -1856,3 +1850,43 @@ def add_filter_by_name_wildcard_behaviors(traits, data): wscmf_name_mapping, fail_required=True, ) + +def add_notification_context_trait(traits, data): + """Change the default GitHub check notification context from + "continuous-integration/jenkins/SUFFIX" to a custom label / suffix. + (set a label and suffix to true or false, optional) + + Requires the :jenkins-plugins:`Github Custom Notification Context SCM + Behaviour `. + + :arg dict notification-context: Definition of notification-context. A + `label` must be specified. `suffix` may be specified with true / + false, default being true. + + * **label** (str): The text of the context label for Github status + notifications. + * **suffix** (bool): Appends the relevant suffix to the context label + based on the build type. '/pr-merge', '/pr-head' or '/branch' + (optional, default true) + """ + if data.get("notification-context", None): + nc_trait = XML.SubElement( + traits, + "org.jenkinsci.plugins.githubScmTraitNotificationContext." + "NotificationContextTrait", + ) + nc = data.get("notification-context") + nc_trait_label = XML.SubElement(nc_trait, "contextLabel") + nc_trait_suffix = XML.SubElement(nc_trait, "typeSuffix") + if isinstance(nc, str): + nc_trait_label.text = nc + nc_trait_suffix.text = "true" + else: + nc_trait_label.text = nc.get("label") + nc_suffix = nc.get("suffix", None) + if nc_suffix is None: + nc_trait_suffix.text = "true" + elif type(nc_suffix) == bool: + nc_trait_suffix.text = str(nc_suffix).lower() + else: + nc_trait_suffix.text = nc_suffix \ No newline at end of file diff --git a/tests/githuborg/fixtures/github-org-full.xml b/tests/githuborg/fixtures/github-org-full.xml index 44bc65756..d17c6522f 100644 --- a/tests/githuborg/fixtures/github-org-full.xml +++ b/tests/githuborg/fixtures/github-org-full.xml @@ -63,7 +63,7 @@ jenkins.example.com/my_context - true + false diff --git a/tests/githuborg/fixtures/github-org-full.yaml b/tests/githuborg/fixtures/github-org-full.yaml index 469756529..cf3cb6c85 100644 --- a/tests/githuborg/fixtures/github-org-full.yaml +++ b/tests/githuborg/fixtures/github-org-full.yaml @@ -29,7 +29,9 @@ github-org: discover-pr-forks-trust: everyone discover-pr-origin: both discover-tags: true - notification-context: 'jenkins.example.com/my_context' + notification-context: + label: 'jenkins.example.com/my_context' + suffix: false property-strategies: all-branches: - suppress-scm-triggering: true diff --git a/tests/githuborg/fixtures/github-org-legacy-nc.xml b/tests/githuborg/fixtures/github-org-legacy-nc.xml new file mode 100644 index 000000000..44bc65756 --- /dev/null +++ b/tests/githuborg/fixtures/github-org-legacy-nc.xml @@ -0,0 +1,229 @@ + + + + + + + + + false + + + + + + + true + -1 + -1 + + + + + example-owner + http://example.org/github + example-credential + + + 3 + + + ssh_secret + + + + 3 + + + + 3 + + + (.*/master|.*/release/.*) + + + foo/.* + 20\..* + + + foo* + bar* + qaz* + *baz + + + (foo/.*|bar/.*) + 1\..* + + + qaz* + baz* + bar* + *qaz + + + jenkins.example.com/my_context + true + + + + + + + + + + no-reply@ci.example.com + + + + + true + 3 + true + 100 + + + + + + + + path1 + + + path2 + + + path3 + + + + + + + false + true + false + true + + 100 + 1 + + + + + 100 + + + + + + + + + + + + + + + + + + +refs/heads/*:refs/remotes/@{remote}/* + + + + + + + + + + + MAX_SURVIVABILITY + + + Ci build! + + + + + + + + + + + + + + + + + false + + + -1 + -1 + + + + + 86400000 + 604800000 + + + -1 + -1 + + + true + + + false + + + + + + + test + true + + + test.*$ + true + + + testinclude + testexclude + + + + + + + + false + + + ^.*$ + false + + + * + + + + + + + + + + Jenkinsfile + + + diff --git a/tests/githuborg/fixtures/github-org-legacy-nc.yaml b/tests/githuborg/fixtures/github-org-legacy-nc.yaml new file mode 100644 index 000000000..adfac9dbe --- /dev/null +++ b/tests/githuborg/fixtures/github-org-legacy-nc.yaml @@ -0,0 +1,101 @@ +name: github-org-legacy-nc +project-type: githuborg +github-org: + api-uri: http://example.org/github + ssh-checkout: + credentials: 'ssh_secret' + repo-owner: example-owner + 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 + discover-tags: true + notification-context: 'jenkins.example.com/my_context' + property-strategies: + all-branches: + - suppress-scm-triggering: true + - 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 + build-strategies: + - all-strategies-match: + strategies: + - regular-branches: true + - skip-initial-build: true + - any-strategies-match: + strategies: + - change-request: {} + - tags: {} + - tags: + ignore-tags-newer-than: 1 + ignore-tags-older-than: 7 + - tags: {} + - change-request: + ignore-target-only-changes: true + - change-request: {} + - regular-branches: true + - skip-initial-build: true + - named-branches: + - exact-name: + name: 'test' + case-sensitive: true + - regex-name: + regex: 'test.*$' + case-sensitive: true + - wildcards-name: + excludes: 'testexclude' + includes: 'testinclude' + - named-branches: + - exact-name: {} + - regex-name: {} + - wildcards-name: {} + clean: + after: true + before: true + committer: + user: CI System + email: no-reply@ci.example.com + prune: true + local-branch: true + sparse-checkout: + paths: + - "path1" + - "path2" + - "path3" + shallow-clone: true + depth: 3 + do-not-fetch-tags: true + disable-pr-notifications: true + refspecs: + - '+refs/heads/*:refs/remotes/@{remote}/*' + submodule: + disable: false + recursive: true + parent-credentials: true + timeout: 100 + threads: 1 + timeout: "100" + skip-notifications: true + use-author: true + wipe-workspace: true + lfs-pull: true diff --git a/tests/multibranch/fixtures/scm_github_full.xml b/tests/multibranch/fixtures/scm_github_full.xml index 5dc7ff67f..ff7d5fcb7 100644 --- a/tests/multibranch/fixtures/scm_github_full.xml +++ b/tests/multibranch/fixtures/scm_github_full.xml @@ -77,7 +77,7 @@ jenkins.example.com/my_context - true + false diff --git a/tests/multibranch/fixtures/scm_github_full.yaml b/tests/multibranch/fixtures/scm_github_full.yaml index 06d827029..3b7277ff0 100644 --- a/tests/multibranch/fixtures/scm_github_full.yaml +++ b/tests/multibranch/fixtures/scm_github_full.yaml @@ -32,7 +32,9 @@ scm: discover-pr-forks-trust: everyone discover-pr-origin: both discover-tags: true - notification-context: 'jenkins.example.com/my_context' + notification-context: + label: 'jenkins.example.com/my_context' + suffix: false property-strategies: all-branches: - suppress-scm-triggering: true diff --git a/tests/multibranch/fixtures/scm_github_legacy_nc.xml b/tests/multibranch/fixtures/scm_github_legacy_nc.xml new file mode 100644 index 000000000..5dc7ff67f --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_legacy_nc.xml @@ -0,0 +1,245 @@ + + + + + + All + false + false + + + + + + + + + + + false + + + + + + + true + -1 + -1 + + + + + + + gh-example-owner-example-repo + example-owner + example-repo + http://example.org/github + example-credential + + + 3 + + + ssh_secret + + + + 3 + + + + 3 + + + (.*/master|.*/release/.*) + + + foo/.* + 20\..* + + + foo* + bar* + qaz* + *baz + + + (foo/.*|bar/.*) + 1\..* + + + qaz* + baz* + bar* + *qaz + + + jenkins.example.com/my_context + true + + + + + + + + + + no-reply@ci.example.com + + + + + true + 3 + true + 100 + + + + + + + + path1 + + + path2 + + + path3 + + + + + + + false + true + false + true + + 100 + 1 + + + + + 100 + + + + + + + + + + + + + + + + + + +refs/heads/*:refs/remotes/@{remote}/* + + + + + + + + + + + + MAX_SURVIVABILITY + + + Ci build! + + + + + + + + + + + + + + + + + false + + + -1 + -1 + + + + + 86400000 + 604800000 + + + -1 + -1 + + + true + + + false + + + + + + + test + true + + + test.*$ + true + + + testinclude + testexclude + + + + + + + + false + + + ^.*$ + false + + + * + + + + + + + + + + + + some.Jenkinsfile + + diff --git a/tests/multibranch/fixtures/scm_github_legacy_nc.yaml b/tests/multibranch/fixtures/scm_github_legacy_nc.yaml new file mode 100644 index 000000000..7e770a951 --- /dev/null +++ b/tests/multibranch/fixtures/scm_github_legacy_nc.yaml @@ -0,0 +1,104 @@ +name: scm-github-legacy-nc +project-type: multibranch +script-path: some.Jenkinsfile +scm: + - github: + api-uri: http://example.org/github + ssh-checkout: + credentials: 'ssh_secret' + repo: example-repo + repo-owner: example-owner + 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 + discover-tags: true + notification-context: 'jenkins.example.com/my_context' + property-strategies: + all-branches: + - suppress-scm-triggering: true + - 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 + build-strategies: + - all-strategies-match: + strategies: + - regular-branches: true + - skip-initial-build: true + - any-strategies-match: + strategies: + - change-request: {} + - tags: {} + - tags: + ignore-tags-newer-than: 1 + ignore-tags-older-than: 7 + - tags: {} + - change-request: + ignore-target-only-changes: true + - change-request: {} + - regular-branches: true + - skip-initial-build: true + - named-branches: + - exact-name: + name: 'test' + case-sensitive: true + - regex-name: + regex: 'test.*$' + case-sensitive: true + - wildcards-name: + excludes: 'testexclude' + includes: 'testinclude' + - named-branches: + - exact-name: {} + - regex-name: {} + - wildcards-name: {} + clean: + after: true + before: true + committer: + user: CI System + email: no-reply@ci.example.com + prune: true + local-branch: true + sparse-checkout: + paths: + - "path1" + - "path2" + - "path3" + shallow-clone: true + depth: 3 + do-not-fetch-tags: true + disable-pr-notifications: true + refspecs: + - '+refs/heads/*:refs/remotes/@{remote}/*' + submodule: + disable: false + recursive: true + parent-credentials: true + timeout: 100 + threads: 1 + timeout: "100" + skip-notifications: true + use-author: true + wipe-workspace: true + lfs-pull: true