From 60f0316389f2e0a0441db10b838cb88ef9770c7a Mon Sep 17 00:00:00 2001 From: Dan Mick Date: Tue, 11 Feb 2025 20:56:54 -0800 Subject: [PATCH] scm: add support for hudson.plugins.git.util.AncestryBuildChooser Adds a new scm.git choosing-strategy 'ancestry', corresponding to AncestryBuildChooser, and two new options if that strategy is selected, maximum-age and ancestor-sha1. Includes doc and test updates. Change-Id: Ib94f6100d4891b4196a6326c0678f638b4edbeab Signed-off-by: Dan Mick --- jenkins_jobs/modules/scm.py | 20 +++++++++-- .../fixtures/git-extensions-build-chooser.xml | 36 +++++++++++++++++++ .../git-extensions-build-chooser.yaml | 6 ++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/scm/fixtures/git-extensions-build-chooser.xml create mode 100644 tests/scm/fixtures/git-extensions-build-chooser.yaml diff --git a/jenkins_jobs/modules/scm.py b/jenkins_jobs/modules/scm.py index 5b427c240..4d56964e5 100644 --- a/jenkins_jobs/modules/scm.py +++ b/jenkins_jobs/modules/scm.py @@ -260,8 +260,14 @@ def git(registry, xml_parent, data): * **branch** (`string`) - name of the branch to create changelog against (default 'master') * **choosing-strategy**: (`string`) - Jenkins class for selecting what - to build. Can be one of `default`,`inverse`, or `gerrit` + to build. Can be one of `default`,`inverse`, `gerrit`, or `ancestry` (default 'default') + * **maximum-age** (`int`) - Maximum age, in days, of commits to examine + for build (when choosing-strategy=ancestry) + (default: none) + * **ancestor-sha1** (`string`) - If set, only branches including this + commit will be built (when choosing-strategy=ancestry) + (default: none) * **clean** (`dict`) * **after** (`dict`) - Clean the workspace after checkout * **remove-stale-nested-repos** (`bool`) - Deletes untracked @@ -489,6 +495,7 @@ def git_extensions(xml_parent, data): "gerrit.trigger.hudsontrigger.GerritTriggerBuildChooser" ), "inverse": "hudson.plugins.git.util.InverseBuildChooser", + "ancestry": "hudson.plugins.git.util.AncestryBuildChooser", } if not trait and "basedir" in data: @@ -509,8 +516,17 @@ def git_extensions(xml_parent, data): raise ValueError( "Invalid choosing-strategy %r" % data.get("choosing-strategy") ) + ext = XML.SubElement(xml_parent, impl_prefix + "BuildChooserSetting") - XML.SubElement(ext, "buildChooser", {"class": choosing_strategy}) + chooser = XML.SubElement(ext, "buildChooser", {"class": choosing_strategy}) + if choosing_strategy == choosing_strategies["ancestry"]: + maximum_age = data.get("maximum-age", None) + ancestor_sha1 = data.get("ancestor-sha1", None) + if maximum_age: + XML.SubElement(chooser, "maximumAgeInDays").text = str(maximum_age) + if ancestor_sha1: + XML.SubElement(chooser, "ancestorCommitSha1").text = ancestor_sha1 + if "clean" in data: # Keep support for old format 'clean' configuration by checking # if 'clean' is boolean. Else we're using the new extensions style. diff --git a/tests/scm/fixtures/git-extensions-build-chooser.xml b/tests/scm/fixtures/git-extensions-build-chooser.xml new file mode 100644 index 000000000..9fd98bc9d --- /dev/null +++ b/tests/scm/fixtures/git-extensions-build-chooser.xml @@ -0,0 +1,36 @@ + + + + 2 + + + origin + +refs/heads/*:refs/remotes/origin/* + https://example.com/git/repo + + + + + ** + + + false + false + false + false + Default + + + + + + + + 7 + ancestor-sha1 + + + + + + diff --git a/tests/scm/fixtures/git-extensions-build-chooser.yaml b/tests/scm/fixtures/git-extensions-build-chooser.yaml new file mode 100644 index 000000000..e7c7076cc --- /dev/null +++ b/tests/scm/fixtures/git-extensions-build-chooser.yaml @@ -0,0 +1,6 @@ +scm: + - git: + url: https://example.com/git/repo + choosing-strategy: ancestry + maximum-age: 7 + ancestor-sha1: ancestor-sha1