From f57a1925855e51ba0dcf1ec06580de39877a66e1 Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 8 Sep 2024 16:53:33 +0300 Subject: [PATCH 01/21] Use stdin if available --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 9a721ee1..2f5afe4e 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -30,6 +30,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import javax.annotation.Nonnull; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -47,6 +48,7 @@ public class JfStep extends Builder implements SimpleBuildStep { private final ObjectMapper mapper = createMapper(); static final String STEP_NAME = "jf"; + private static final String MIN_CLI_VERSION_PASSWORD_STDIN ="2.31.0"; protected String[] args; @DataBoundConstructor @@ -197,7 +199,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP for (JFrogPlatformInstance jfrogPlatformInstance : jfrogInstances) { // Build 'jf' command ArgumentListBuilder builder = new ArgumentListBuilder(); - addConfigArguments(builder, jfrogPlatformInstance, jfrogBinaryPath, job); + addConfigArguments(builder, jfrogPlatformInstance, jfrogBinaryPath, job, launcher); if (isWindows) { builder = builder.toWindowsCommand(); } @@ -210,7 +212,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP } } - private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job) { + private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job, Launcher.ProcStarter launcher) throws IOException, InterruptedException { String credentialsId = jfrogPlatformInstance.getCredentialsConfig().getCredentialsId(); builder.add(jfrogBinaryPath).add("c").add("add").add(jfrogPlatformInstance.getId()); // Add credentials @@ -220,7 +222,15 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan } else { Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job); builder.add("--user=" + credentials.getUsername()); - builder.addMasked("--password=" + credentials.getPassword()); + String cliVersion = getJfrogCliVersion(launcher, launcher.pwd()); + // Use password-stdin if available + if (isCliVersionGreaterThan(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { + builder.add("--password-stdin="); + ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes()); + launcher.stdin(inputStream); + } else { + builder.addMasked("--password=" + credentials.getPassword()); + } } // Add URLs builder.add("--url=" + jfrogPlatformInstance.getUrl()); @@ -294,4 +304,32 @@ public boolean isApplicable(Class jobType) { return true; } } + + private String getJfrogCliVersion(Launcher.ProcStarter launcher, FilePath workspace) throws IOException, InterruptedException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + int exitCode = launcher.cmds("jf", "--version") + .pwd(workspace) + .stdout(outputStream) + .join(); + if (exitCode != 0) { + throw new IOException("Failed to get JFrog CLI version"); + } + String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); + return versionOutput.split(" ")[2]; // Assuming the version is the third word in the output + } + + private boolean isCliVersionGreaterThan(String currentVersion, String targetVersion) { + String[] currentParts = currentVersion.split("\\."); + String[] targetParts = targetVersion.split("\\."); + for (int i = 0; i < Math.min(currentParts.length, targetParts.length); i++) { + int currentPart = Integer.parseInt(currentParts[i]); + int targetPart = Integer.parseInt(targetParts[i]); + if (currentPart > targetPart) { + return true; + } else if (currentPart < targetPart) { + return false; + } + } + return currentParts.length > targetParts.length; + } } From b4cd09fa1aeb7410bc3e8198493dc2fa3885d85b Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 9 Sep 2024 13:54:42 +0300 Subject: [PATCH 02/21] Add tests and test on local machine --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 24 +++++---- .../io/jenkins/plugins/jfrog/JfStepTest.java | 52 +++++++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 2f5afe4e..24af95a2 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -195,7 +195,7 @@ private boolean shouldConfig(FilePath jfrogHomeTempDir) throws IOException, Inte private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryPath, boolean isWindows, Job job) throws IOException, InterruptedException { // Config all servers using the 'jf c add' command. List jfrogInstances = JFrogPlatformBuilder.getJFrogPlatformInstances(); - if (jfrogInstances != null && jfrogInstances.size() > 0) { + if (jfrogInstances != null && !jfrogInstances.isEmpty()) { for (JFrogPlatformInstance jfrogPlatformInstance : jfrogInstances) { // Build 'jf' command ArgumentListBuilder builder = new ArgumentListBuilder(); @@ -222,10 +222,11 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan } else { Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job); builder.add("--user=" + credentials.getUsername()); - String cliVersion = getJfrogCliVersion(launcher, launcher.pwd()); + String cliVersion = getJfrogCliVersion(launcher); + // Use password-stdin if available - if (isCliVersionGreaterThan(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { - builder.add("--password-stdin="); + if (isCliVersionGreaterThanOrEqual(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { + builder.add("--password-stdin"); ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes()); launcher.stdin(inputStream); } else { @@ -305,20 +306,25 @@ public boolean isApplicable(Class jobType) { } } - private String getJfrogCliVersion(Launcher.ProcStarter launcher, FilePath workspace) throws IOException, InterruptedException { + String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int exitCode = launcher.cmds("jf", "--version") - .pwd(workspace) + .pwd(launcher.pwd()) .stdout(outputStream) .join(); if (exitCode != 0) { throw new IOException("Failed to get JFrog CLI version"); } String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); - return versionOutput.split(" ")[2]; // Assuming the version is the third word in the output + String[] versionParts = versionOutput.split(" "); + if (versionOutput.isEmpty() ||(versionParts.length < 3) ) { + throw new IOException(String.format("Failed to parse JFrog CLI version. CLI Output: %s", versionOutput)); + } + // Based on the output, the version should be located at the second index + return versionParts[2]; } - private boolean isCliVersionGreaterThan(String currentVersion, String targetVersion) { + boolean isCliVersionGreaterThanOrEqual(String currentVersion, String targetVersion) { String[] currentParts = currentVersion.split("\\."); String[] targetParts = targetVersion.split("\\."); for (int i = 0; i < Math.min(currentParts.length, targetParts.length); i++) { @@ -330,6 +336,6 @@ private boolean isCliVersionGreaterThan(String currentVersion, String targetVers return false; } } - return currentParts.length > targetParts.length; + return currentParts.length >= targetParts.length; } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index afa4025f..e0d9bdd2 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -1,15 +1,24 @@ package io.jenkins.plugins.jfrog; import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.stream.Stream; import static io.jenkins.plugins.jfrog.JfStep.getJFrogCLIPath; import static io.jenkins.plugins.jfrog.JfrogInstallation.JFROG_BINARY_PATH; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author yahavi @@ -22,6 +31,49 @@ void getJFrogCLIPathTest(EnvVars inputEnvVars, boolean isWindows, String expecte Assertions.assertEquals(expectedOutput, getJFrogCLIPath(inputEnvVars, isWindows)); } + @Test + void isCliVersionGreaterThanTest() { + JfStep jfStep = new JfStep("--version"); + + // Test cases where the current version is greater + assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.32.0", "2.31.0")); + assertTrue(jfStep.isCliVersionGreaterThanOrEqual("3.0.0", "2.31.0")); + assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.1", "2.31.0")); + + // Test cases where the current version is equal + assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.0")); + + // Test cases where the current version is less + assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.30.0", "2.31.0")); + assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.1")); + assertFalse(jfStep.isCliVersionGreaterThanOrEqual("1.31.0", "2.31.0")); + } + + @Test + void getJfrogCliVersionTest() throws IOException, InterruptedException { + // Mock the Launcher.ProcStarter + Launcher.ProcStarter procStarter = mock(Launcher.ProcStarter.class); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + // Mocks the return value of --version command + outputStream.write("jf version 2.31.0 ".getBytes()); + // Mock the behavior of the ProcStarter + when(procStarter.cmds("jf", "--version")).thenReturn(procStarter); + when(procStarter.pwd((FilePath) any())).thenReturn(procStarter); + when(procStarter.stdout(any(ByteArrayOutputStream.class))).thenAnswer(invocation -> { + ByteArrayOutputStream out = invocation.getArgument(0); + out.write(outputStream.toByteArray()); + return procStarter; + }); + when(procStarter.join()).thenReturn(0); + + // Create an instance of JfStep and call the method + JfStep jfStep = new JfStep("--version"); + String version = jfStep.getJfrogCliVersion(procStarter); + + // Verify the result + assertEquals("2.31.0", version); + } + private static Stream jfrogCLIPathProvider() { return Stream.of( // Unix agent From 6c265b33ca87dadef2c6ecae851e90589c5fc9f3 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 9 Sep 2024 14:00:04 +0300 Subject: [PATCH 03/21] Fix static analysis --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 24af95a2..17befe85 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -227,7 +227,7 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan // Use password-stdin if available if (isCliVersionGreaterThanOrEqual(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { builder.add("--password-stdin"); - ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes()); + ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes(StandardCharsets.UTF_8)); launcher.stdin(inputStream); } else { builder.addMasked("--password=" + credentials.getPassword()); From 3c41f76c9529b841cb43731f6dc6e3791956b18f Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 10 Sep 2024 11:33:35 +0300 Subject: [PATCH 04/21] Test --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 17befe85..c77d2ce2 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -36,6 +36,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.List; +import java.util.Objects; import static io.jenkins.plugins.jfrog.JfrogInstallation.JFROG_BINARY_PATH; import static org.apache.commons.lang3.StringUtils.*; @@ -50,6 +51,7 @@ public class JfStep extends Builder implements SimpleBuildStep { static final String STEP_NAME = "jf"; private static final String MIN_CLI_VERSION_PASSWORD_STDIN ="2.31.0"; protected String[] args; + protected String currentCliVersion; @DataBoundConstructor public JfStep(Object args) { @@ -83,7 +85,6 @@ public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNul ArgumentListBuilder builder = new ArgumentListBuilder(); boolean isWindows = !launcher.isUnix(); String jfrogBinaryPath = getJFrogCLIPath(env, isWindows); - builder.add(jfrogBinaryPath).add(args); if (isWindows) { builder = builder.toWindowsCommand(); @@ -92,6 +93,7 @@ public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNul try (ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream()) { JfTaskListener jfTaskListener = new JfTaskListener(listener, taskOutputStream); Launcher.ProcStarter jfLauncher = setupJFrogEnvironment(run, env, launcher, jfTaskListener, workspace, jfrogBinaryPath, isWindows); + this.currentCliVersion = getJfrogCliVersion(jfLauncher); // Running the 'jf' command int exitValue = jfLauncher.cmds(builder).join(); if (exitValue != 0) { @@ -212,7 +214,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP } } - private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job, Launcher.ProcStarter launcher) throws IOException, InterruptedException { + private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job, Launcher.ProcStarter launcher) { String credentialsId = jfrogPlatformInstance.getCredentialsConfig().getCredentialsId(); builder.add(jfrogBinaryPath).add("c").add("add").add(jfrogPlatformInstance.getId()); // Add credentials @@ -222,10 +224,10 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan } else { Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job); builder.add("--user=" + credentials.getUsername()); - String cliVersion = getJfrogCliVersion(launcher); + // Use password-stdin if available - if (isCliVersionGreaterThanOrEqual(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { + if (isCliVersionGreaterThanOrEqual(this.currentCliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { builder.add("--password-stdin"); ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes(StandardCharsets.UTF_8)); launcher.stdin(inputStream); @@ -307,6 +309,9 @@ public boolean isApplicable(Class jobType) { } String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { + if (!Objects.equals(this.currentCliVersion, "")){ + return this.currentCliVersion; + } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int exitCode = launcher.cmds("jf", "--version") .pwd(launcher.pwd()) From e4ce2d5565eb41898e028eab4ce4734520158af2 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 10 Sep 2024 11:41:20 +0300 Subject: [PATCH 05/21] Fix tests --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index c77d2ce2..fac903ee 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -36,7 +36,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.List; -import java.util.Objects; import static io.jenkins.plugins.jfrog.JfrogInstallation.JFROG_BINARY_PATH; import static org.apache.commons.lang3.StringUtils.*; @@ -309,7 +308,7 @@ public boolean isApplicable(Class jobType) { } String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { - if (!Objects.equals(this.currentCliVersion, "")){ + if (this.currentCliVersion != null && !this.currentCliVersion.isEmpty()) { return this.currentCliVersion; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); From d8d23b086038499baef985c0303ad03213fc8a23 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 10 Sep 2024 12:11:42 +0300 Subject: [PATCH 06/21] Move set version --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index fac903ee..8e373d74 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -92,7 +92,6 @@ public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNul try (ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream()) { JfTaskListener jfTaskListener = new JfTaskListener(listener, taskOutputStream); Launcher.ProcStarter jfLauncher = setupJFrogEnvironment(run, env, launcher, jfTaskListener, workspace, jfrogBinaryPath, isWindows); - this.currentCliVersion = getJfrogCliVersion(jfLauncher); // Running the 'jf' command int exitValue = jfLauncher.cmds(builder).join(); if (exitValue != 0) { @@ -171,6 +170,7 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La logIfNoToolProvided(env, listener); configAllServers(jfLauncher, jfrogBinaryPath, isWindows, run.getParent()); } + this.currentCliVersion= getJfrogCliVersion(jfLauncher); return jfLauncher; } From 33ccd7d9c0a8ed396ece74c561e3bcafeff75a70 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 09:26:33 +0300 Subject: [PATCH 07/21] Fix test --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 2 +- .../jenkins/plugins/jfrog/integration/PipelineTestBase.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 8e373d74..32593603 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -81,6 +81,7 @@ public String[] getArgs() { public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException { workspace.mkdirs(); // Build the 'jf' command + this.currentCliVersion = getJfrogCliVersion(launcher.launch()); ArgumentListBuilder builder = new ArgumentListBuilder(); boolean isWindows = !launcher.isUnix(); String jfrogBinaryPath = getJFrogCLIPath(env, isWindows); @@ -170,7 +171,6 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La logIfNoToolProvided(env, listener); configAllServers(jfLauncher, jfrogBinaryPath, isWindows, run.getParent()); } - this.currentCliVersion= getJfrogCliVersion(jfLauncher); return jfLauncher; } diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 9cba91a9..f6e0b145 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -69,6 +69,7 @@ public class PipelineTestBase { static final String JFROG_CLI_TOOL_NAME_1 = "jfrog-cli"; static final String JFROG_CLI_TOOL_NAME_2 = "jfrog-cli-2"; static final String TEST_CONFIGURED_SERVER_ID = "serverId"; + static final String TEST_CONFIGURED_DUMMY_SERVER_ID = "dummyId"; // Set up jenkins and configure latest JFrog CLI. public void initPipelineTest(JenkinsRule jenkins) throws Exception { @@ -161,12 +162,12 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); - CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, Credentials.EMPTY_CREDENTIALS); + CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, new Credentials(Secret.fromString("username"), Secret.fromString("123"), null)); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); List artifactoryServers = new ArrayList() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). - add(new JFrogPlatformInstance("dummyServerId", "", emptyCred, "", "", "")); + add(new JFrogPlatformInstance(TEST_CONFIGURED_DUMMY_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From 19196fd888aa76cee9d8707e36728c8fffdd337a Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 17:08:30 +0300 Subject: [PATCH 08/21] Test one server --- .../io/jenkins/plugins/jfrog/integration/PipelineTestBase.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index f6e0b145..9397aae1 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -69,7 +69,6 @@ public class PipelineTestBase { static final String JFROG_CLI_TOOL_NAME_1 = "jfrog-cli"; static final String JFROG_CLI_TOOL_NAME_2 = "jfrog-cli-2"; static final String TEST_CONFIGURED_SERVER_ID = "serverId"; - static final String TEST_CONFIGURED_DUMMY_SERVER_ID = "dummyId"; // Set up jenkins and configure latest JFrog CLI. public void initPipelineTest(JenkinsRule jenkins) throws Exception { @@ -162,12 +161,10 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); - CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, new Credentials(Secret.fromString("username"), Secret.fromString("123"), null)); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); List artifactoryServers = new ArrayList() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). - add(new JFrogPlatformInstance(TEST_CONFIGURED_DUMMY_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From eda7a0f4a0751a2886737911ca194f055133cd58 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 17:22:58 +0300 Subject: [PATCH 09/21] Revert --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 2 +- .../io/jenkins/plugins/jfrog/integration/PipelineTestBase.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 32593603..26147029 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -81,7 +81,6 @@ public String[] getArgs() { public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException { workspace.mkdirs(); // Build the 'jf' command - this.currentCliVersion = getJfrogCliVersion(launcher.launch()); ArgumentListBuilder builder = new ArgumentListBuilder(); boolean isWindows = !launcher.isUnix(); String jfrogBinaryPath = getJFrogCLIPath(env, isWindows); @@ -166,6 +165,7 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber())); CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption); Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener); + this.currentCliVersion = getJfrogCliVersion(jfLauncher); // Configure all servers, skip if all server ids have already been configured. if (shouldConfig(jfrogHomeTempDir)) { logIfNoToolProvided(env, listener); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 9397aae1..9cba91a9 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -161,10 +161,12 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); + CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, Credentials.EMPTY_CREDENTIALS); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); List artifactoryServers = new ArrayList() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). + add(new JFrogPlatformInstance("dummyServerId", "", emptyCred, "", "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From 7bc52c40e7082fc276ad573e3512528c3185cb8b Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 17:53:03 +0300 Subject: [PATCH 10/21] Test --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 15 +++++++++------ .../java/io/jenkins/plugins/jfrog/JfStepTest.java | 2 +- .../jfrog/integration/PipelineTestBase.java | 5 +---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 26147029..ee81c73a 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -48,7 +48,7 @@ public class JfStep extends Builder implements SimpleBuildStep { private final ObjectMapper mapper = createMapper(); static final String STEP_NAME = "jf"; - private static final String MIN_CLI_VERSION_PASSWORD_STDIN ="2.31.0"; + private static final String MIN_CLI_VERSION_PASSWORD_STDIN = "2.31.0"; protected String[] args; protected String currentCliVersion; @@ -165,7 +165,7 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber())); CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption); Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener); - this.currentCliVersion = getJfrogCliVersion(jfLauncher); + this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace), jfrogBinaryPath); // Configure all servers, skip if all server ids have already been configured. if (shouldConfig(jfrogHomeTempDir)) { logIfNoToolProvided(env, listener); @@ -307,21 +307,24 @@ public boolean isApplicable(Class jobType) { } } - String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { + String getJfrogCliVersion(Launcher.ProcStarter launcher, String jfrogBinaryPath) throws IOException, InterruptedException { if (this.currentCliVersion != null && !this.currentCliVersion.isEmpty()) { return this.currentCliVersion; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - int exitCode = launcher.cmds("jf", "--version") + ArgumentListBuilder builder = new ArgumentListBuilder(); + builder.add(jfrogBinaryPath).add("--version"); + int exitCode = launcher + .cmds(builder) .pwd(launcher.pwd()) .stdout(outputStream) .join(); if (exitCode != 0) { - throw new IOException("Failed to get JFrog CLI version"); + throw new IOException("Failed to get JFrog CLI version: "+outputStream.toString(StandardCharsets.UTF_8)); } String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); String[] versionParts = versionOutput.split(" "); - if (versionOutput.isEmpty() ||(versionParts.length < 3) ) { + if (versionOutput.isEmpty() || (versionParts.length < 3)) { throw new IOException(String.format("Failed to parse JFrog CLI version. CLI Output: %s", versionOutput)); } // Based on the output, the version should be located at the second index diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index e0d9bdd2..d10fb669 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -68,7 +68,7 @@ void getJfrogCliVersionTest() throws IOException, InterruptedException { // Create an instance of JfStep and call the method JfStep jfStep = new JfStep("--version"); - String version = jfStep.getJfrogCliVersion(procStarter); + String version = jfStep.getJfrogCliVersion(procStarter, getJFrogCLIPath(new EnvVars(),false)); // Verify the result assertEquals("2.31.0", version); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 9cba91a9..381c229b 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -19,7 +19,6 @@ import io.jenkins.plugins.jfrog.BinaryInstaller; import io.jenkins.plugins.jfrog.JfrogInstallation; import io.jenkins.plugins.jfrog.ReleasesInstaller; -import io.jenkins.plugins.jfrog.configuration.Credentials; import io.jenkins.plugins.jfrog.configuration.CredentialsConfig; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformBuilder; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformInstance; @@ -161,12 +160,10 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); - CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, Credentials.EMPTY_CREDENTIALS); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); - List artifactoryServers = new ArrayList() {{ + List artifactoryServers = new ArrayList<>() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). - add(new JFrogPlatformInstance("dummyServerId", "", emptyCred, "", "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From 22afcb3fc0ad9fb52d9a28e590df697970f4dc0d Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 18:04:28 +0300 Subject: [PATCH 11/21] Fix tests --- .../java/io/jenkins/plugins/jfrog/JfStepTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index d10fb669..629be8e5 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -3,6 +3,7 @@ import hudson.EnvVars; import hudson.FilePath; import hudson.Launcher; +import hudson.util.ArgumentListBuilder; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -51,13 +52,17 @@ void isCliVersionGreaterThanTest() { @Test void getJfrogCliVersionTest() throws IOException, InterruptedException { + boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win"); + // Mock the Launcher + Launcher launcher = mock(Launcher.class); // Mock the Launcher.ProcStarter Launcher.ProcStarter procStarter = mock(Launcher.ProcStarter.class); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // Mocks the return value of --version command outputStream.write("jf version 2.31.0 ".getBytes()); - // Mock the behavior of the ProcStarter - when(procStarter.cmds("jf", "--version")).thenReturn(procStarter); + // Mock the behavior of the Launcher and ProcStarter + when(launcher.launch()).thenReturn(procStarter); + when(procStarter.cmds(any(ArgumentListBuilder.class))).thenReturn(procStarter); when(procStarter.pwd((FilePath) any())).thenReturn(procStarter); when(procStarter.stdout(any(ByteArrayOutputStream.class))).thenAnswer(invocation -> { ByteArrayOutputStream out = invocation.getArgument(0); @@ -68,7 +73,7 @@ void getJfrogCliVersionTest() throws IOException, InterruptedException { // Create an instance of JfStep and call the method JfStep jfStep = new JfStep("--version"); - String version = jfStep.getJfrogCliVersion(procStarter, getJFrogCLIPath(new EnvVars(),false)); + String version = jfStep.getJfrogCliVersion(procStarter, JfStep.getJFrogCLIPath(new EnvVars(), isWindows)); // Verify the result assertEquals("2.31.0", version); From 364831bf9fb0119d85dd1d5e69a5b0f2854c978e Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 18:19:57 +0300 Subject: [PATCH 12/21] Diff --- .../jenkins/plugins/jfrog/integration/PipelineTestBase.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 381c229b..9cba91a9 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -19,6 +19,7 @@ import io.jenkins.plugins.jfrog.BinaryInstaller; import io.jenkins.plugins.jfrog.JfrogInstallation; import io.jenkins.plugins.jfrog.ReleasesInstaller; +import io.jenkins.plugins.jfrog.configuration.Credentials; import io.jenkins.plugins.jfrog.configuration.CredentialsConfig; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformBuilder; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformInstance; @@ -160,10 +161,12 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); + CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, Credentials.EMPTY_CREDENTIALS); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); - List artifactoryServers = new ArrayList<>() {{ + List artifactoryServers = new ArrayList() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). + add(new JFrogPlatformInstance("dummyServerId", "", emptyCred, "", "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From 5b9d566253944591c56b49fe5113ea818d5e39e6 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 11 Sep 2024 18:30:37 +0300 Subject: [PATCH 13/21] Add password for dummy test server --- .../plugins/jfrog/integration/PipelineTestBase.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 9cba91a9..6265d98f 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -19,7 +19,6 @@ import io.jenkins.plugins.jfrog.BinaryInstaller; import io.jenkins.plugins.jfrog.JfrogInstallation; import io.jenkins.plugins.jfrog.ReleasesInstaller; -import io.jenkins.plugins.jfrog.configuration.Credentials; import io.jenkins.plugins.jfrog.configuration.CredentialsConfig; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformBuilder; import io.jenkins.plugins.jfrog.configuration.JFrogPlatformInstance; @@ -161,12 +160,12 @@ private static void verifyEnvironment() { private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); - CredentialsConfig emptyCred = new CredentialsConfig(StringUtils.EMPTY, Credentials.EMPTY_CREDENTIALS); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); - List artifactoryServers = new ArrayList() {{ + CredentialsConfig secondPlatformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); + List artifactoryServers = new ArrayList<>() {{ // Dummy server to test multiple configured servers. // The dummy server should be configured first to ensure the right server is being used (and not the first one). - add(new JFrogPlatformInstance("dummyServerId", "", emptyCred, "", "", "")); + add(new JFrogPlatformInstance("dummyServerId", "", secondPlatformCred, "", "", "")); add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); From be500540a722a199083cb5defb2badcb23b15612 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 08:10:49 +0300 Subject: [PATCH 14/21] Set new server, use class variables --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 32 ++++++++++--------- .../io/jenkins/plugins/jfrog/JfStepTest.java | 4 +-- .../jfrog/integration/PipelineTestBase.java | 7 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index ee81c73a..a2ac1ba9 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -22,6 +22,7 @@ import io.jenkins.plugins.jfrog.models.BuildInfoOutputModel; import io.jenkins.plugins.jfrog.plugins.PluginsUtils; import jenkins.tasks.SimpleBuildStep; +import lombok.Getter; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jenkinsci.Symbol; @@ -49,8 +50,14 @@ public class JfStep extends Builder implements SimpleBuildStep { private final ObjectMapper mapper = createMapper(); static final String STEP_NAME = "jf"; private static final String MIN_CLI_VERSION_PASSWORD_STDIN = "2.31.0"; + @Getter protected String[] args; + // The current JFrog CLI version in the agent protected String currentCliVersion; + // The JFrog CLI binary path in the agent + protected String jfrogBinaryPath; + // True if the agent's OS is windows + protected boolean isWindows; @DataBoundConstructor public JfStep(Object args) { @@ -62,10 +69,6 @@ public JfStep(Object args) { this.args = split(args.toString()); } - public String[] getArgs() { - return args; - } - /** * Build and run a 'jf' command. * @@ -80,10 +83,11 @@ public String[] getArgs() { @Override public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException { workspace.mkdirs(); + this.isWindows = !launcher.isUnix(); + this.jfrogBinaryPath = getJFrogCLIPath(env, isWindows); + // Build the 'jf' command ArgumentListBuilder builder = new ArgumentListBuilder(); - boolean isWindows = !launcher.isUnix(); - String jfrogBinaryPath = getJFrogCLIPath(env, isWindows); builder.add(jfrogBinaryPath).add(args); if (isWindows) { builder = builder.toWindowsCommand(); @@ -91,7 +95,7 @@ public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNul try (ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream()) { JfTaskListener jfTaskListener = new JfTaskListener(listener, taskOutputStream); - Launcher.ProcStarter jfLauncher = setupJFrogEnvironment(run, env, launcher, jfTaskListener, workspace, jfrogBinaryPath, isWindows); + Launcher.ProcStarter jfLauncher = setupJFrogEnvironment(run, env, launcher, jfTaskListener, workspace); // Running the 'jf' command int exitValue = jfLauncher.cmds(builder).join(); if (exitValue != 0) { @@ -149,13 +153,11 @@ private void logIfNoToolProvided(EnvVars env, TaskListener listener) { * @param launcher a way to start processes * @param listener a place to send output * @param workspace a workspace to use for any file operations - * @param jfrogBinaryPath path to jfrog cli binary on the filesystem - * @param isWindows is Windows the applicable OS * @return launcher applicable to this step. * @throws InterruptedException if the step is interrupted * @throws IOException in case of any I/O error, or we failed to run the 'jf' command */ - public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, Launcher launcher, TaskListener listener, FilePath workspace, String jfrogBinaryPath, boolean isWindows) throws IOException, InterruptedException { + public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, Launcher launcher, TaskListener listener, FilePath workspace) throws IOException, InterruptedException { JFrogCliConfigEncryption jfrogCliConfigEncryption = run.getAction(JFrogCliConfigEncryption.class); if (jfrogCliConfigEncryption == null) { // Set up the config encryption action to allow encrypting the JFrog CLI configuration and make sure we only create one key @@ -165,11 +167,11 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber())); CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption); Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener); - this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace), jfrogBinaryPath); + this.currentCliVersion = getJfrogCliVersion(jfLauncher); // Configure all servers, skip if all server ids have already been configured. if (shouldConfig(jfrogHomeTempDir)) { logIfNoToolProvided(env, listener); - configAllServers(jfLauncher, jfrogBinaryPath, isWindows, run.getParent()); + configAllServers(jfLauncher, run.getParent()); } return jfLauncher; } @@ -193,7 +195,7 @@ private boolean shouldConfig(FilePath jfrogHomeTempDir) throws IOException, Inte /** * Locally configure all servers that was configured in the Jenkins UI. */ - private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryPath, boolean isWindows, Job job) throws IOException, InterruptedException { + private void configAllServers(Launcher.ProcStarter launcher, Job job) throws IOException, InterruptedException { // Config all servers using the 'jf c add' command. List jfrogInstances = JFrogPlatformBuilder.getJFrogPlatformInstances(); if (jfrogInstances != null && !jfrogInstances.isEmpty()) { @@ -307,7 +309,7 @@ public boolean isApplicable(Class jobType) { } } - String getJfrogCliVersion(Launcher.ProcStarter launcher, String jfrogBinaryPath) throws IOException, InterruptedException { + String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { if (this.currentCliVersion != null && !this.currentCliVersion.isEmpty()) { return this.currentCliVersion; } @@ -320,7 +322,7 @@ String getJfrogCliVersion(Launcher.ProcStarter launcher, String jfrogBinaryPath) .stdout(outputStream) .join(); if (exitCode != 0) { - throw new IOException("Failed to get JFrog CLI version: "+outputStream.toString(StandardCharsets.UTF_8)); + throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); } String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); String[] versionParts = versionOutput.split(" "); diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index 629be8e5..cfc22acb 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -52,7 +52,6 @@ void isCliVersionGreaterThanTest() { @Test void getJfrogCliVersionTest() throws IOException, InterruptedException { - boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win"); // Mock the Launcher Launcher launcher = mock(Launcher.class); // Mock the Launcher.ProcStarter @@ -73,7 +72,8 @@ void getJfrogCliVersionTest() throws IOException, InterruptedException { // Create an instance of JfStep and call the method JfStep jfStep = new JfStep("--version"); - String version = jfStep.getJfrogCliVersion(procStarter, JfStep.getJFrogCLIPath(new EnvVars(), isWindows)); + jfStep.isWindows = System.getProperty("os.name").toLowerCase().contains("win"); + String version = jfStep.getJfrogCliVersion(procStarter); // Verify the result assertEquals("2.31.0", version); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java index 6265d98f..64746fc6 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/PipelineTestBase.java @@ -68,6 +68,7 @@ public class PipelineTestBase { static final String JFROG_CLI_TOOL_NAME_1 = "jfrog-cli"; static final String JFROG_CLI_TOOL_NAME_2 = "jfrog-cli-2"; static final String TEST_CONFIGURED_SERVER_ID = "serverId"; + static final String TEST_CONFIGURED_SERVER_ID_2 = "serverId2"; // Set up jenkins and configure latest JFrog CLI. public void initPipelineTest(JenkinsRule jenkins) throws Exception { @@ -161,12 +162,10 @@ private void setGlobalConfiguration() throws IOException { JFrogPlatformBuilder.DescriptorImpl jfrogBuilder = (JFrogPlatformBuilder.DescriptorImpl) jenkins.getInstance().getDescriptor(JFrogPlatformBuilder.class); Assert.assertNotNull(jfrogBuilder); CredentialsConfig platformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); - CredentialsConfig secondPlatformCred = new CredentialsConfig(Secret.fromString(ARTIFACTORY_USERNAME), Secret.fromString(ARTIFACTORY_PASSWORD), Secret.fromString(ACCESS_TOKEN), "credentials"); List artifactoryServers = new ArrayList<>() {{ - // Dummy server to test multiple configured servers. - // The dummy server should be configured first to ensure the right server is being used (and not the first one). - add(new JFrogPlatformInstance("dummyServerId", "", secondPlatformCred, "", "", "")); + // Configure multiple servers to test multiple servers. add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); + add(new JFrogPlatformInstance(TEST_CONFIGURED_SERVER_ID_2, PLATFORM_URL, platformCred, ARTIFACTORY_URL, "", "")); }}; jfrogBuilder.setJfrogInstances(artifactoryServers); Jenkins.get().getDescriptorByType(JFrogPlatformBuilder.DescriptorImpl.class).setJfrogInstances(artifactoryServers); From 4f65153f472c896e0470d639c569a19a98390ccd Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 08:58:05 +0300 Subject: [PATCH 15/21] fix --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 15 +++++++-------- .../jfrog/integration/JFrogInstallationITest.java | 3 +-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index a2ac1ba9..abfc30fe 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -148,11 +148,11 @@ private void logIfNoToolProvided(EnvVars env, TaskListener listener) { /** * Configure all JFrog relevant environment variables and all servers (if they haven't been configured yet). * - * @param run running as part of a specific build - * @param env environment variables applicable to this step - * @param launcher a way to start processes - * @param listener a place to send output - * @param workspace a workspace to use for any file operations + * @param run running as part of a specific build + * @param env environment variables applicable to this step + * @param launcher a way to start processes + * @param listener a place to send output + * @param workspace a workspace to use for any file operations * @return launcher applicable to this step. * @throws InterruptedException if the step is interrupted * @throws IOException in case of any I/O error, or we failed to run the 'jf' command @@ -220,13 +220,12 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan builder.add(jfrogBinaryPath).add("c").add("add").add(jfrogPlatformInstance.getId()); // Add credentials StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(credentialsId, job); + // Access Token if (accessTokenCredentials != null) { builder.addMasked("--access-token=" + accessTokenCredentials.getSecret().getPlainText()); } else { Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job); builder.add("--user=" + credentials.getUsername()); - - // Use password-stdin if available if (isCliVersionGreaterThanOrEqual(this.currentCliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { builder.add("--password-stdin"); @@ -315,7 +314,7 @@ String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, Int } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ArgumentListBuilder builder = new ArgumentListBuilder(); - builder.add(jfrogBinaryPath).add("--version"); + builder.add(jfrogBinaryPath).add("-v"); int exitCode = launcher .cmds(builder) .pwd(launcher.pwd()) diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java index 79138eac..a73b7dd4 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java @@ -54,8 +54,7 @@ public void testJFrogCliInstallationFromArtifactory(JenkinsRule jenkins) throws // Download the latest CLI version. // Using remote repository to 'releases.jfrog.io' in the client's Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_1, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), true); - WorkflowRun job = runPipeline(jenkins, "basic_version_command"); - assertTrue(job.getLog().contains("jf version ")); + runPipeline(jenkins, "basic_version_command"); } // Gets JfrogInstallation directory in Jenkins work root. From 1e4ec20a50b6710e80b4252038b31dd0e5937998 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 10:29:08 +0300 Subject: [PATCH 16/21] Dont change stdout --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index abfc30fe..99d56ef9 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -167,7 +167,7 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber())); CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption); Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener); - this.currentCliVersion = getJfrogCliVersion(jfLauncher); + this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace)); // Configure all servers, skip if all server ids have already been configured. if (shouldConfig(jfrogHomeTempDir)) { logIfNoToolProvided(env, listener); From 53e149a7ea89190ae32497ffa275d152100d9fcf Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 10:42:01 +0300 Subject: [PATCH 17/21] Move the get version to start --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 3 ++- .../plugins/jfrog/integration/JFrogInstallationITest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 99d56ef9..3a2d6591 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -83,8 +83,10 @@ public JfStep(Object args) { @Override public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException { workspace.mkdirs(); + this.isWindows = !launcher.isUnix(); this.jfrogBinaryPath = getJFrogCLIPath(env, isWindows); + this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace)); // Build the 'jf' command ArgumentListBuilder builder = new ArgumentListBuilder(); @@ -167,7 +169,6 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run run, EnvVars env, La FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber())); CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption); Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener); - this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace)); // Configure all servers, skip if all server ids have already been configured. if (shouldConfig(jfrogHomeTempDir)) { logIfNoToolProvided(env, listener); diff --git a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java index a73b7dd4..79138eac 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/integration/JFrogInstallationITest.java @@ -54,7 +54,8 @@ public void testJFrogCliInstallationFromArtifactory(JenkinsRule jenkins) throws // Download the latest CLI version. // Using remote repository to 'releases.jfrog.io' in the client's Artifactory. configureJfrogCliFromArtifactory(JFROG_CLI_TOOL_NAME_1, TEST_CONFIGURED_SERVER_ID, getRepoKey(TestRepository.CLI_REMOTE_REPO), true); - runPipeline(jenkins, "basic_version_command"); + WorkflowRun job = runPipeline(jenkins, "basic_version_command"); + assertTrue(job.getLog().contains("jf version ")); } // Gets JfrogInstallation directory in Jenkins work root. From 6d5121af441051b298f5e83729b9ab1fbb565d4e Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 10:55:54 +0300 Subject: [PATCH 18/21] Refactor --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 3a2d6591..0028a061 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -83,10 +83,8 @@ public JfStep(Object args) { @Override public void perform(@NonNull Run run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException { workspace.mkdirs(); - - this.isWindows = !launcher.isUnix(); - this.jfrogBinaryPath = getJFrogCLIPath(env, isWindows); - this.currentCliVersion = getJfrogCliVersion(launcher.launch().envs(env).pwd(workspace)); + // Initialize values to be used across the class + initClassValues(workspace, env, launcher); // Build the 'jf' command ArgumentListBuilder builder = new ArgumentListBuilder(); @@ -294,6 +292,21 @@ private void logIllegalBuildPublishOutput(Log log, ByteArrayOutputStream taskOut log.warn("Illegal build-publish output: " + taskOutputStream.toString(StandardCharsets.UTF_8)); } + /** + * initialize values to be used across the class. + * @param env environment variables applicable to this step + * @param launcher a way to start processes + * @param workspace a workspace to use for any file operations + * @throws IOException in case of any I/O error, or we failed to run the 'jf' + * @throws InterruptedException if the step is interrupted + */ + private void initClassValues(FilePath workspace, EnvVars env, Launcher launcher) throws IOException, InterruptedException { + this.isWindows = !launcher.isUnix(); + this.jfrogBinaryPath = getJFrogCLIPath(env, isWindows); + Launcher.ProcStarter procStarter = launcher.launch().envs(env).pwd(workspace); + this.currentCliVersion = getJfrogCliVersion(procStarter); + } + @Symbol("jf") @Extension public static final class DescriptorImpl extends BuildStepDescriptor { From 0bc0ff0403c2be5f0e5d5447416e0f7d671ac5cd Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 13:01:11 +0300 Subject: [PATCH 19/21] CR --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 38 ++++++------------- .../io/jenkins/plugins/jfrog/JfStepTest.java | 23 ++--------- 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 0028a061..da7df388 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -24,10 +24,12 @@ import jenkins.tasks.SimpleBuildStep; import lombok.Getter; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jenkinsci.Symbol; import org.jenkinsci.plugins.plaincredentials.StringCredentials; import org.jfrog.build.api.util.Log; +import org.jfrog.build.client.Version; import org.kohsuke.stapler.DataBoundConstructor; import javax.annotation.Nonnull; @@ -49,11 +51,11 @@ public class JfStep extends Builder implements SimpleBuildStep { private final ObjectMapper mapper = createMapper(); static final String STEP_NAME = "jf"; - private static final String MIN_CLI_VERSION_PASSWORD_STDIN = "2.31.0"; + private static final Version MIN_CLI_VERSION_PASSWORD_STDIN = new Version("2.31.3"); @Getter protected String[] args; // The current JFrog CLI version in the agent - protected String currentCliVersion; + protected Version currentCliVersion; // The JFrog CLI binary path in the agent protected String jfrogBinaryPath; // True if the agent's OS is windows @@ -226,7 +228,7 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job); builder.add("--user=" + credentials.getUsername()); // Use password-stdin if available - if (isCliVersionGreaterThanOrEqual(this.currentCliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) { + if (this.currentCliVersion.isAtLeast(MIN_CLI_VERSION_PASSWORD_STDIN)) { builder.add("--password-stdin"); ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes(StandardCharsets.UTF_8)); launcher.stdin(inputStream); @@ -294,10 +296,11 @@ private void logIllegalBuildPublishOutput(Log log, ByteArrayOutputStream taskOut /** * initialize values to be used across the class. + * * @param env environment variables applicable to this step * @param launcher a way to start processes * @param workspace a workspace to use for any file operations - * @throws IOException in case of any I/O error, or we failed to run the 'jf' + * @throws IOException in case of any I/O error, or we failed to run the 'jf' * @throws InterruptedException if the step is interrupted */ private void initClassValues(FilePath workspace, EnvVars env, Launcher launcher) throws IOException, InterruptedException { @@ -322,8 +325,8 @@ public boolean isApplicable(Class jobType) { } } - String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { - if (this.currentCliVersion != null && !this.currentCliVersion.isEmpty()) { + Version getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException { + if (this.currentCliVersion != null) { return this.currentCliVersion; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -338,26 +341,7 @@ String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, Int throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); } String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); - String[] versionParts = versionOutput.split(" "); - if (versionOutput.isEmpty() || (versionParts.length < 3)) { - throw new IOException(String.format("Failed to parse JFrog CLI version. CLI Output: %s", versionOutput)); - } - // Based on the output, the version should be located at the second index - return versionParts[2]; - } - - boolean isCliVersionGreaterThanOrEqual(String currentVersion, String targetVersion) { - String[] currentParts = currentVersion.split("\\."); - String[] targetParts = targetVersion.split("\\."); - for (int i = 0; i < Math.min(currentParts.length, targetParts.length); i++) { - int currentPart = Integer.parseInt(currentParts[i]); - int targetPart = Integer.parseInt(targetParts[i]); - if (currentPart > targetPart) { - return true; - } else if (currentPart < targetPart) { - return false; - } - } - return currentParts.length >= targetParts.length; + String version = StringUtils.substringAfterLast(versionOutput, " "); + return new Version(version); } } diff --git a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java index cfc22acb..d2b4a995 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java @@ -4,6 +4,7 @@ import hudson.FilePath; import hudson.Launcher; import hudson.util.ArgumentListBuilder; +import org.jfrog.build.client.Version; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -32,24 +33,6 @@ void getJFrogCLIPathTest(EnvVars inputEnvVars, boolean isWindows, String expecte Assertions.assertEquals(expectedOutput, getJFrogCLIPath(inputEnvVars, isWindows)); } - @Test - void isCliVersionGreaterThanTest() { - JfStep jfStep = new JfStep("--version"); - - // Test cases where the current version is greater - assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.32.0", "2.31.0")); - assertTrue(jfStep.isCliVersionGreaterThanOrEqual("3.0.0", "2.31.0")); - assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.1", "2.31.0")); - - // Test cases where the current version is equal - assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.0")); - - // Test cases where the current version is less - assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.30.0", "2.31.0")); - assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.1")); - assertFalse(jfStep.isCliVersionGreaterThanOrEqual("1.31.0", "2.31.0")); - } - @Test void getJfrogCliVersionTest() throws IOException, InterruptedException { // Mock the Launcher @@ -73,10 +56,10 @@ void getJfrogCliVersionTest() throws IOException, InterruptedException { // Create an instance of JfStep and call the method JfStep jfStep = new JfStep("--version"); jfStep.isWindows = System.getProperty("os.name").toLowerCase().contains("win"); - String version = jfStep.getJfrogCliVersion(procStarter); + Version version = jfStep.getJfrogCliVersion(procStarter); // Verify the result - assertEquals("2.31.0", version); + assertEquals("2.31.0", version.toString()); } private static Stream jfrogCLIPathProvider() { From cea9c459d85dc8de58beda28c36dd57bcb2083e4 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 13:03:08 +0300 Subject: [PATCH 20/21] try with closed resources --- .../java/io/jenkins/plugins/jfrog/JfStep.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index da7df388..37b1873d 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -329,19 +329,20 @@ Version getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, In if (this.currentCliVersion != null) { return this.currentCliVersion; } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ArgumentListBuilder builder = new ArgumentListBuilder(); - builder.add(jfrogBinaryPath).add("-v"); - int exitCode = launcher - .cmds(builder) - .pwd(launcher.pwd()) - .stdout(outputStream) - .join(); - if (exitCode != 0) { - throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ + ArgumentListBuilder builder = new ArgumentListBuilder(); + builder.add(jfrogBinaryPath).add("-v"); + int exitCode = launcher + .cmds(builder) + .pwd(launcher.pwd()) + .stdout(outputStream) + .join(); + if (exitCode != 0) { + throw new IOException("Failed to get JFrog CLI version: " + outputStream.toString(StandardCharsets.UTF_8)); + } + String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); + String version = StringUtils.substringAfterLast(versionOutput, " "); + return new Version(version); } - String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim(); - String version = StringUtils.substringAfterLast(versionOutput, " "); - return new Version(version); } } From 9ec102aa3eb2c5c67bc8db95c76a7e7478087d75 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 12 Sep 2024 14:20:37 +0300 Subject: [PATCH 21/21] try with closed resources --- src/main/java/io/jenkins/plugins/jfrog/JfStep.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java index 37b1873d..b79e9ebf 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/JfStep.java +++ b/src/main/java/io/jenkins/plugins/jfrog/JfStep.java @@ -216,7 +216,7 @@ private void configAllServers(Launcher.ProcStarter launcher, Job job) thro } } - private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job, Launcher.ProcStarter launcher) { + private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job job, Launcher.ProcStarter launcher) throws IOException { String credentialsId = jfrogPlatformInstance.getCredentialsConfig().getCredentialsId(); builder.add(jfrogBinaryPath).add("c").add("add").add(jfrogPlatformInstance.getId()); // Add credentials @@ -230,8 +230,9 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan // Use password-stdin if available if (this.currentCliVersion.isAtLeast(MIN_CLI_VERSION_PASSWORD_STDIN)) { builder.add("--password-stdin"); - ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes(StandardCharsets.UTF_8)); - launcher.stdin(inputStream); + try(ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes(StandardCharsets.UTF_8))) { + launcher.stdin(inputStream); + } } else { builder.addMasked("--password=" + credentials.getPassword()); }