Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Sep 12, 2024
1 parent 6d5121a commit 0bc0ff0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
38 changes: 11 additions & 27 deletions src/main/java/io/jenkins/plugins/jfrog/JfStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -322,8 +325,8 @@ public boolean isApplicable(Class<? extends AbstractProject> 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();
Expand All @@ -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);
}
}
23 changes: 3 additions & 20 deletions src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<Arguments> jfrogCLIPathProvider() {
Expand Down

0 comments on commit 0bc0ff0

Please sign in to comment.