From f0aeddf53a3d5ff573c130b51c8df52ef5ff0a50 Mon Sep 17 00:00:00 2001 From: sp-ricard-valverde Date: Thu, 24 Nov 2022 19:34:15 +0100 Subject: [PATCH] fix(scm): add clean options to after and before git-scm clean after and before extensions take an optional parameter to remove untracked nested git repositories Added support to specify such options while maintaining the previous specification for after and before clean extensions, although a deprecation message will be shown Signed-off-by: sp-ricard-valverde Change-Id: I0ff101bbc68fc81f390b27fe5690f8821fdea1d0 --- jenkins_jobs/modules/project_githuborg.py | 10 ++++-- jenkins_jobs/modules/project_multibranch.py | 40 ++++++++++++++++----- jenkins_jobs/modules/scm.py | 40 ++++++++++++++++++--- tests/scm/fixtures/git-clean02.xml | 36 +++++++++++++++++++ tests/scm/fixtures/git-clean02.yaml | 8 +++++ tests/scm/fixtures/git-clean03.xml | 32 +++++++++++++++++ tests/scm/fixtures/git-clean03.yaml | 6 ++++ 7 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 tests/scm/fixtures/git-clean02.xml create mode 100644 tests/scm/fixtures/git-clean02.yaml create mode 100644 tests/scm/fixtures/git-clean03.xml create mode 100644 tests/scm/fixtures/git-clean03.yaml diff --git a/jenkins_jobs/modules/project_githuborg.py b/jenkins_jobs/modules/project_githuborg.py index 45577e990..75a8fa6b7 100644 --- a/jenkins_jobs/modules/project_githuborg.py +++ b/jenkins_jobs/modules/project_githuborg.py @@ -246,8 +246,14 @@ def github_org(xml_parent, data): :extensions: * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **depth** (`int`) - Set shallow clone depth (default 1) * **disable-pr-notifications** (`bool`) - Disable default github status notifications on pull requests (default false) (Requires the diff --git a/jenkins_jobs/modules/project_multibranch.py b/jenkins_jobs/modules/project_multibranch.py index ac85974fb..44d87a3bb 100644 --- a/jenkins_jobs/modules/project_multibranch.py +++ b/jenkins_jobs/modules/project_multibranch.py @@ -427,8 +427,14 @@ def bitbucket_scm(xml_parent, data): :extensions: * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **prune** (`bool`) - Prune remote branches (default false) * **shallow-clone** (`bool`) - Perform shallow clone (default false) * **sparse-checkout** (dict) @@ -678,8 +684,14 @@ def gerrit_scm(xml_parent, data): :extensions: * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **prune** (`bool`) - Prune remote branches (default false) * **shallow-clone** (`bool`) - Perform shallow clone (default false) * **sparse-checkout** (dict) @@ -848,8 +860,14 @@ def git_scm(xml_parent, data): :extensions: * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **prune** (`bool`) - Prune remote branches (default false) * **shallow-clone** (`bool`) - Perform shallow clone (default false) * **sparse-checkout** (dict) @@ -1006,8 +1024,14 @@ def github_scm(xml_parent, data): :extensions: * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **prune** (`bool`) - Prune remote branches (default false) * **shallow-clone** (`bool`) - Perform shallow clone (default false) * **sparse-checkout** (dict) diff --git a/jenkins_jobs/modules/scm.py b/jenkins_jobs/modules/scm.py index d5681e718..53716a481 100644 --- a/jenkins_jobs/modules/scm.py +++ b/jenkins_jobs/modules/scm.py @@ -262,8 +262,14 @@ def git(registry, xml_parent, data): to build. Can be one of `default`,`inverse`, or `gerrit` (default 'default') * **clean** (`dict`) - * **after** (`bool`) - Clean the workspace after checkout - * **before** (`bool`) - Clean the workspace before checkout + * **after** (`dict`) - Clean the workspace after checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) + * **before** (`dict`) - Clean the workspace before checkout + * **remove-stale-nested-repos** (`bool`) - Deletes untracked + submodules and any other subdirectories which contain .git directories + (default false) * **committer** (`dict`) * **name** (`str`) - Name to use as author of new commits * **email** (`str`) - E-mail address to use for new commits @@ -518,7 +524,17 @@ def git_extensions(xml_parent, data): else: clean_after = data["clean"].get("after", False) clean_before = data["clean"].get("before", False) - if clean_after: + if clean_after is not False: + if isinstance(clean_after, bool): + clean_after_opts = {} + logger.warning( + "'clean: after: bool' configuration format is deprecated, " + "after should be an empty dict or filled with accepted " + "options." + ) + else: + clean_after_opts = clean_after + ext_name = impl_prefix + "CleanCheckout" if trait: trait_name = "CleanAfterCheckoutTrait" @@ -526,7 +542,20 @@ def git_extensions(xml_parent, data): ext = XML.SubElement(tr, "extension", {"class": ext_name}) else: ext = XML.SubElement(xml_parent, ext_name) - if clean_before: + if "remove-stale-nested-repos" in clean_after_opts: + elm = XML.SubElement(ext, "deleteUntrackedNestedRepositories") + elm.text = "true" + if clean_before is not False: + if isinstance(clean_before, bool): + clean_before_opts = {} + logger.warning( + "'clean: before: bool' configuration format is deprecated, " + "before should be an empty dict or filled with accepted " + "options." + ) + else: + clean_before_opts = clean_before + ext_name = impl_prefix + "CleanBeforeCheckout" if trait: trait_name = "CleanBeforeCheckoutTrait" @@ -534,6 +563,9 @@ def git_extensions(xml_parent, data): ext = XML.SubElement(tr, "extension", {"class": ext_name}) else: ext = XML.SubElement(xml_parent, ext_name) + if "remove-stale-nested-repos" in clean_before_opts: + elm = XML.SubElement(ext, "deleteUntrackedNestedRepositories") + elm.text = "true" committer = data.get("committer", {}) if committer: ext_name = impl_prefix + "UserIdentity" diff --git a/tests/scm/fixtures/git-clean02.xml b/tests/scm/fixtures/git-clean02.xml new file mode 100644 index 000000000..11c9223d9 --- /dev/null +++ b/tests/scm/fixtures/git-clean02.xml @@ -0,0 +1,36 @@ + + + + 2 + + + origin + +refs/heads/*:refs/remotes/origin/* + https://github.com/openstack-infra/jenkins-job-builder.git + + + + + ** + + + false + false + false + false + Default + + + + + + + true + + + true + + + + + diff --git a/tests/scm/fixtures/git-clean02.yaml b/tests/scm/fixtures/git-clean02.yaml new file mode 100644 index 000000000..8e2ae767b --- /dev/null +++ b/tests/scm/fixtures/git-clean02.yaml @@ -0,0 +1,8 @@ +scm: + - git: + url: https://github.com/openstack-infra/jenkins-job-builder.git + clean: + after: + remove-stale-nested-repos: true + before: + remove-stale-nested-repos: true diff --git a/tests/scm/fixtures/git-clean03.xml b/tests/scm/fixtures/git-clean03.xml new file mode 100644 index 000000000..f742cf4fb --- /dev/null +++ b/tests/scm/fixtures/git-clean03.xml @@ -0,0 +1,32 @@ + + + + 2 + + + origin + +refs/heads/*:refs/remotes/origin/* + https://github.com/openstack-infra/jenkins-job-builder.git + + + + + ** + + + false + false + false + false + Default + + + + + + + + + + + diff --git a/tests/scm/fixtures/git-clean03.yaml b/tests/scm/fixtures/git-clean03.yaml new file mode 100644 index 000000000..49a3e25f1 --- /dev/null +++ b/tests/scm/fixtures/git-clean03.yaml @@ -0,0 +1,6 @@ +scm: + - git: + url: https://github.com/openstack-infra/jenkins-job-builder.git + clean: + after: {} + before: {}