From 1997249c24e9df959eb2121ef8e73e4b312f45da Mon Sep 17 00:00:00 2001 From: "Rodriguez,Hector (IT EDP)" Date: Wed, 4 Dec 2024 10:49:30 +0100 Subject: [PATCH 1/3] Add support to git submodules in QS commit/push stage --- src/org/ods/quickstarter/Pipeline.groovy | 2 +- .../ods/quickstarter/PushToRemoteStage.groovy | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/org/ods/quickstarter/Pipeline.groovy b/src/org/ods/quickstarter/Pipeline.groovy index b3ce84abd3..f497ed733f 100644 --- a/src/org/ods/quickstarter/Pipeline.groovy +++ b/src/org/ods/quickstarter/Pipeline.groovy @@ -85,7 +85,7 @@ class Pipeline implements Serializable { // Execute user-defined stages. block(context) - new PushToRemoteStage(script, context).execute() + new PushToRemoteStage(script, context, config).execute() } } diff --git a/src/org/ods/quickstarter/PushToRemoteStage.groovy b/src/org/ods/quickstarter/PushToRemoteStage.groovy index 57f120e9b0..ee2b3591b1 100644 --- a/src/org/ods/quickstarter/PushToRemoteStage.groovy +++ b/src/org/ods/quickstarter/PushToRemoteStage.groovy @@ -48,10 +48,24 @@ class PushToRemoteStage extends Stage { rm -rf \$clonedGitFolderName git config user.email "undefined" git config user.name "ODS System User" + """, + label: 'Copy quickstarter files' + ) + config?.gitSubModules?.each { submodule -> + script.sh( + script: """ + echo "Adding ${submodule.name} git submodule" + git submodule add -b ${submodule.branch} ${submodule.url} ${submodule.folder} + """, + label: 'Add submodule to quickstarter files' + ) + } + script.sh( + script: """ git add --all . git commit -m "Initial OpenDevStack commit" """, - label: 'Copy and commit quickstarter files' + label: 'Commit quickstarter files' ) } script.echo("Pushing quickstarter git repo to ${context.gitUrlHttp}") From c727564255449519990d0e426616bfb339e093e7 Mon Sep 17 00:00:00 2001 From: "Rodriguez,Hector (IT EDP)" Date: Wed, 4 Dec 2024 12:00:11 +0100 Subject: [PATCH 2/3] Added unit tests --- test/groovy/org/ods/PipelineScript.groovy | 11 ++++ .../quickstarter/PushToRemoteStageSpec.groovy | 55 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 test/groovy/org/ods/quickstarter/PushToRemoteStageSpec.groovy diff --git a/test/groovy/org/ods/PipelineScript.groovy b/test/groovy/org/ods/PipelineScript.groovy index c3668f1321..087afd07a6 100644 --- a/test/groovy/org/ods/PipelineScript.groovy +++ b/test/groovy/org/ods/PipelineScript.groovy @@ -39,6 +39,9 @@ class PipelineScript { body() } + def usernamePassword(def foo) { + } + def sh(def foo) { return "test" } @@ -96,10 +99,18 @@ class PipelineScript { } + def writeFile (Map args) { + + } + def readJSON (Map args) { } + def dir(def foo, Closure body) { + body() + } + def withSonarQubeEnv(String conf, Closure closure) { closure() } diff --git a/test/groovy/org/ods/quickstarter/PushToRemoteStageSpec.groovy b/test/groovy/org/ods/quickstarter/PushToRemoteStageSpec.groovy new file mode 100644 index 0000000000..de9cc0f164 --- /dev/null +++ b/test/groovy/org/ods/quickstarter/PushToRemoteStageSpec.groovy @@ -0,0 +1,55 @@ +package org.ods.quickstarter + +import org.ods.PipelineScript + +import util.SpecHelper + + +class PushToRemoteStageSpec extends SpecHelper { + PushToRemoteStage pushToRemoteStage + IContext context + PipelineScript script + + def setup() { + script = Spy(new PipelineScript()) + context = new Context([targetDir: 'fake-dir', cdUserCredentialsId: 'credentials-id', bitbucketUrl: 'http://fake-url']) + } + + def "successful execution without git submodules"() { + given: + pushToRemoteStage = Spy(new PushToRemoteStage(script, context, [:])) + + when: + pushToRemoteStage.run() + + then: + 1 * script.fileExists(_) >> false + 1 * script.echo("Initializing quickstarter git repo ${context.targetDir} @${context.gitUrlHttp}") + 1 * script.sh({ it.label == 'Copy quickstarter files' }) + 0 * script.sh({ it.label == 'Add submodule to quickstarter files' }) + 1 * script.sh({ it.label == 'Commit quickstarter files' }) + 1 * script.echo("Pushing quickstarter git repo to ${context.gitUrlHttp}") + 1 * script.sh({ it.label == 'Push to remote' }) + } + + def "successful execution with git submodules"() { + given: + pushToRemoteStage = Spy(new PushToRemoteStage(script, context, [ + gitSubModules: [ + [name: 'submodule', url: 'https://fake-submodule.git', branch: 'master', folder: 'src/code'] + ], + ])) + + when: + pushToRemoteStage.run() + + then: + 1 * script.fileExists(_) >> false + 1 * script.echo("Initializing quickstarter git repo ${context.targetDir} @${context.gitUrlHttp}") + 1 * script.sh({ it.label == 'Copy quickstarter files' }) + 1 * script.sh({ it.label == 'Add submodule to quickstarter files' }) + 1 * script.sh({ it.label == 'Commit quickstarter files' }) + 1 * script.echo("Pushing quickstarter git repo to ${context.gitUrlHttp}") + 1 * script.sh({ it.label == 'Push to remote' }) + } +} From ea3d7c31e347d509b9768d367e60fe5c9d3e1908 Mon Sep 17 00:00:00 2001 From: "Rodriguez,Hector (IT EDP)" Date: Thu, 12 Dec 2024 10:05:12 +0100 Subject: [PATCH 3/3] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95f0dea13f..3aa23c7a33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added * add devcontainer setup ([#1172](https://github.com/opendevstack/ods-jenkins-shared-library/issues/1172)) +* Add support for git submodules in Quickstarters ([#1181](https://github.com/opendevstack/ods-jenkins-shared-library/pull/1181)) ### Changed * Enhance SSDS Document Generation Performance using New Atlassian APIs ([#1084](https://github.com/opendevstack/ods-jenkins-shared-library/issues/1084))