Skip to content

Commit a472353

Browse files
authored
Merge pull request #386 from jenkinsci/fix/benalvo/version-check
Fix CheckmarxInstaller to update version from file for 'latest' or empty input
2 parents 3e165b5 + e992e54 commit a472353

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/main/java/com/checkmarx/jenkins/tools/CheckmarxInstaller.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,43 @@ public class CheckmarxInstaller extends ToolInstaller {
5656
public static final String cliDefaultVersion = "2.3.18";
5757
private static final String cliVersionFileName = "cli.version";
5858
@Getter
59-
private final String version;
59+
private String version;
6060
@Getter
6161
private final Long updatePolicyIntervalHours;
6262
private CxLoggerAdapter log;
6363

6464
@DataBoundConstructor
6565
public CheckmarxInstaller(String label, String version, Long updatePolicyIntervalHours) {
6666
super(label);
67-
this.version = "latest".equalsIgnoreCase(version.trim()) || version.isEmpty() ? readCLILatestVersionFromVersionFile() : version;
67+
this.version = version;
6868
this.updatePolicyIntervalHours = updatePolicyIntervalHours;
6969
}
7070

7171
@Override
7272
public FilePath performInstallation(ToolInstallation toolInstallation, Node node, TaskListener taskListener) throws IOException, InterruptedException {
7373
log = new CxLoggerAdapter(taskListener.getLogger());
74-
74+
String versionToInstall = getVersionNumber();
7575
FilePath expected = preferredLocation(toolInstallation, node);
7676

7777
if (isUpToDate(expected, log)) {
7878
log.info("Checkmarx installation is UP-TO-DATE");
7979
return expected;
8080
}
81-
log.info("Installing Checkmarx AST CLI tool (version '" + fixEmptyAndTrim(version) + "')");
8281

83-
return installCheckmarxCliAsSingleBinary(expected, node, taskListener);
82+
log.info("Installing Checkmarx AST CLI tool (version '{}')", fixEmptyAndTrim(versionToInstall));
83+
84+
return installCheckmarxCliAsSingleBinary(versionToInstall, expected, node, taskListener);
85+
}
86+
87+
public String getVersionNumber() {
88+
if ("latest".equalsIgnoreCase(version.trim()) || version.isEmpty()) {
89+
return readCLILatestVersionFromVersionFile();
90+
} else {
91+
return version;
92+
}
8493
}
8594

86-
private String readCLILatestVersionFromVersionFile() {
95+
public String readCLILatestVersionFromVersionFile() {
8796
try {
8897
Path versionFilePath = findVersionFilePath().orElseThrow(() -> new ToolDetectionException("Could not find version file"));
8998
String fileVersion = Files.readString(versionFilePath.resolve(cliVersionFileName)).trim();
@@ -130,7 +139,7 @@ private boolean isUpToDate(FilePath expectedLocation, CxLoggerAdapter log) throw
130139
return timestampDifference < updateInterval;
131140
}
132141

133-
private FilePath installCheckmarxCliAsSingleBinary(FilePath expected, Node node, TaskListener log) throws IOException, InterruptedException {
142+
private FilePath installCheckmarxCliAsSingleBinary(String version, FilePath expected, Node node, TaskListener log) throws IOException, InterruptedException {
134143
final VirtualChannel nodeChannel = node.getChannel();
135144
if (nodeChannel == null) {
136145
throw new IOException(format("Node '%s' is offline", node.getDisplayName()));

src/test/java/com/checkmarx/jenkins/unit/tools/CheckmarxInstallerTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testInstallerDescriptorDisplayName() {
9797
@Test
9898
public void testConstructorWithNullValues() {
9999
CheckmarxInstaller installer = new CheckmarxInstaller(null, "", null);
100-
assertNotEquals("", installer.getVersion());
100+
assertNotEquals("", installer.getVersionNumber());
101101
assertNull(installer.getUpdatePolicyIntervalHours());
102102
}
103103

@@ -111,14 +111,13 @@ public void testConstructorWithEmptyLabel() {
111111
@Test
112112
public void testConstructorWithLatestVersion() {
113113
CheckmarxInstaller installer = new CheckmarxInstaller("", "latest ", 24L);
114-
assertNotEquals("latest ", installer.getVersion());
114+
assertEquals("latest ", installer.getVersion());
115115
assertEquals(Long.valueOf(24L), installer.getUpdatePolicyIntervalHours());
116116
}
117117

118118
@Test
119119
public void testValidateEqualsCLIDefaultVersionAndCLIVersionFile() {
120-
CheckmarxInstaller installer = new CheckmarxInstaller("", "latest", 24L);
121-
assertEquals(CheckmarxInstaller.cliDefaultVersion, installer.getVersion());
120+
assertEquals(CheckmarxInstaller.cliDefaultVersion, installer.readCLILatestVersionFromVersionFile());
122121
}
123122

124123
@Test(expected = Exception.class)

src/test/java/com/checkmarx/jenkins/unit/tools/internal/DownloadServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testGetDownloadUrlForCli_withLatestVersion() throws IOException {
7878
CheckmarxInstaller installer = new CheckmarxInstaller("test", "latest", 24L);
7979
Platform platform = Platform.LINUX;
8080

81-
URL actualUrl = DownloadService.getDownloadUrlForCli(installer.getVersion(), platform);
81+
URL actualUrl = DownloadService.getDownloadUrlForCli(installer.getVersionNumber(), platform);
8282
assertNotNull(actualUrl);
8383
}
8484

0 commit comments

Comments
 (0)