From 5ecf2432457d5ece7322512220bf3cc655e9b0fb Mon Sep 17 00:00:00 2001 From: Nimbusprogrammer Date: Sun, 7 Dec 2025 07:22:28 +0000 Subject: [PATCH 1/3] Warn when 'Check out to a sub-directory' is used with Pipeline jobs (fixes #3068) --- .../plugins/git/extensions/impl/RelativeTargetDirectory.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/hudson/plugins/git/extensions/impl/RelativeTargetDirectory.java b/src/main/java/hudson/plugins/git/extensions/impl/RelativeTargetDirectory.java index 2f93739a34..f5e9cd998c 100644 --- a/src/main/java/hudson/plugins/git/extensions/impl/RelativeTargetDirectory.java +++ b/src/main/java/hudson/plugins/git/extensions/impl/RelativeTargetDirectory.java @@ -38,6 +38,10 @@ public FilePath getWorkingDirectory(GitSCM scm, Job context, FilePath work if (relativeTargetDir == null || relativeTargetDir.length() == 0 || relativeTargetDir.equals(".")) { return workspace; } + //fixing issue : #3068 : Warn if used in Pipeline + if ( context != null && context.getClass().getName().equals("org.jenkinsci.plugins.workflow.job.WorkflowJob")){ + listener.getLogger().println("[Warning] 'Check out to a sub-directory' is not intended for use with Pipeline jobs. Please use the 'dir' step instead. "); + } return workspace.child(environment.expand(relativeTargetDir)); } From 73df5a6bd14e21c633b6c5c7fac868a7c11e32cb Mon Sep 17 00:00:00 2001 From: Nimbusprogrammer Date: Sun, 7 Dec 2025 13:47:44 +0000 Subject: [PATCH 2/3] test: verify warning when RelativeTargetDirectory used in Pipeline --- .../RelativeTargetDirectoryPipelineTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java diff --git a/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java b/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java new file mode 100644 index 0000000000..992be1a0af --- /dev/null +++ b/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java @@ -0,0 +1,55 @@ +package hudson.plugins.git; + +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterEach; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; +import jenkins.plugins.git.GitSampleRepoRule; + +@WithJenkins +public class RelativeTargetDirectoryPipelineTest { + + public GitSampleRepoRule sampleRepo = new GitSampleRepoRule(); + + @BeforeEach + public void setupRepo() throws Throwable { + sampleRepo.before(); + } + + @AfterEach + public void tearDownRepo() { + sampleRepo.after(); + } + + @Test + public void warningIsLoggedWhenUsedInPipeline(JenkinsRule j) throws Exception { + // 1. Init a git repo + sampleRepo.init(); + sampleRepo.write("Jenkinsfile", "echo 'hello'"); + sampleRepo.git("add", "Jenkinsfile"); + sampleRepo.git("commit", "--all", "-m", "init"); + + // 2. Create Pipeline Job + WorkflowJob job = j.createProject(WorkflowJob.class, "test-job"); + String repoUrl = sampleRepo.toString(); + + // 3. Script that uses the bad feature + String pipelineScript = + "node {\n" + + " checkout([$class: 'GitSCM',\n" + + " branches: [[name: '*/master']],\n" + + " userRemoteConfigs: [[url: '" + repoUrl + "']],\n" + + " extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdir']]\n" + + " ])\n" + + "}\n"; + job.setDefinition(new CpsFlowDefinition(pipelineScript, true)); + + // 4. Run and Assert + WorkflowRun run = j.buildAndAssertSuccess(job); + j.assertLogContains("'Check out to a sub-directory' is not intended for use with Pipeline jobs", run); + } +} \ No newline at end of file From 1e249b7005dfa00a247b27efd4a7db95cf9f6cce Mon Sep 17 00:00:00 2001 From: Nimbusprogrammer Date: Tue, 9 Dec 2025 17:21:12 +0000 Subject: [PATCH 3/3] tests: migrate to @WithGitSampleRepo and assert Pipeline warnings --- .../RelativeTargetDirectoryPipelineTest.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java b/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java index 992be1a0af..26059e4050 100644 --- a/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java +++ b/src/test/java/hudson/plugins/git/RelativeTargetDirectoryPipelineTest.java @@ -1,43 +1,38 @@ package hudson.plugins.git; +import jenkins.plugins.git.GitSampleRepoRule; +import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.AfterEach; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import jenkins.plugins.git.GitSampleRepoRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; @WithJenkins +@WithGitSampleRepo public class RelativeTargetDirectoryPipelineTest { - public GitSampleRepoRule sampleRepo = new GitSampleRepoRule(); + private JenkinsRule r; + private GitSampleRepoRule sampleRepo; @BeforeEach - public void setupRepo() throws Throwable { - sampleRepo.before(); - } - - @AfterEach - public void tearDownRepo() { - sampleRepo.after(); - } + void beforeEach(JenkinsRule rule, GitSampleRepoRule repo) throws Exception { + this.r = rule; + this.sampleRepo = repo; - @Test - public void warningIsLoggedWhenUsedInPipeline(JenkinsRule j) throws Exception { - // 1. Init a git repo sampleRepo.init(); sampleRepo.write("Jenkinsfile", "echo 'hello'"); sampleRepo.git("add", "Jenkinsfile"); sampleRepo.git("commit", "--all", "-m", "init"); + } - // 2. Create Pipeline Job - WorkflowJob job = j.createProject(WorkflowJob.class, "test-job"); + @Test + void warningIsLoggedWhenUsedInPipeline() throws Exception { + WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "test-job"); String repoUrl = sampleRepo.toString(); - // 3. Script that uses the bad feature String pipelineScript = "node {\n" + " checkout([$class: 'GitSCM',\n" + @@ -48,8 +43,8 @@ public void warningIsLoggedWhenUsedInPipeline(JenkinsRule j) throws Exception { "}\n"; job.setDefinition(new CpsFlowDefinition(pipelineScript, true)); - // 4. Run and Assert - WorkflowRun run = j.buildAndAssertSuccess(job); - j.assertLogContains("'Check out to a sub-directory' is not intended for use with Pipeline jobs", run); + WorkflowRun run = r.buildAndAssertSuccess(job); + r.assertLogContains("'Check out to a sub-directory' is not intended for use with Pipeline jobs", run); + r.assertLogContains("Please use the 'dir' step instead", run); } } \ No newline at end of file