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() {