Skip to content

Commit

Permalink
Merge pull request #353 from jenkinsci/bug/miryamFoifer/archiveInTmpDir
Browse files Browse the repository at this point in the history
Support archive in tmp dir (AST-64900)
  • Loading branch information
miryamfoiferCX authored Jan 7, 2025
2 parents 8d2a7bb + e2337fe commit bb8047d
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions src/main/java/com/checkmarx/jenkins/CheckmarxScanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void close() throws IOException {
ArtifactArchiver artifactArchiverJson = new ArtifactArchiver(workspace.toURI().relativize(jsonReportFilePath.toURI()).toString());
artifactArchiverJson.perform(run, workspace, envVars, launcher, listener);

saveInArtifactAdditionalReports(scanConfig, workspace, envVars, launcher, listener, run);
saveInArtifactAdditionalReports(scanConfig, workspace, envVars, launcher, listener, run, tempDir);

} finally {
//Deleting temporary directory to clean up the workspace env
Expand All @@ -376,38 +376,46 @@ public void close() throws IOException {
run.setResult(Result.SUCCESS);
}

private void saveInArtifactAdditionalReports(ScanConfig scanConfig, FilePath workspace, EnvVars envVars, Launcher launcher, TaskListener listener, Run<?, ?> run) throws IOException, InterruptedException {
if (scanConfig.getAdditionalOptions() != null && scanConfig.getAdditionalOptions().contains("--report-format")) {
try {
String additionalOptions = scanConfig.getAdditionalOptions();

String formatTypes = extractOptionValue(additionalOptions, "--report-format");
String[] formats = formatTypes.split(",");

for (String formatType : formats) {
String fileName = (additionalOptions.contains("--output-name")
? extractOptionValue(additionalOptions, "--output-name")
: PluginUtils.defaultOutputName) + "." + formatType;
String outputPath = extractOptionValue(additionalOptions, "--output-path");

String fullFilePath = new File(outputPath, fileName).getPath();
FilePath destinationPath = workspace.child(fileName);

File fileToCopy = new File(fullFilePath);
private void saveInArtifactAdditionalReports(ScanConfig scanConfig, FilePath workspace, EnvVars envVars, Launcher launcher, TaskListener listener, Run<?, ?> run, FilePath tempDir) {
if(scanConfig.getAdditionalOptions() == null || !scanConfig.getAdditionalOptions().contains("--report-format")){
return;
}
String additionalOptions = scanConfig.getAdditionalOptions();
String formatTypes = extractOptionValue(additionalOptions, "--report-format");
String[] formats = formatTypes.split(",");

if (fileToCopy.exists()) {
new FilePath(fileToCopy).copyTo(destinationPath);
for (String formatType : formats) {
try {
String fileName = (additionalOptions.contains("--output-name")
? extractOptionValue(additionalOptions, "--output-name")
: PluginUtils.defaultOutputName) + "." + formatType;

String outputPath = extractOptionValue(additionalOptions, "--output-path");
log.info("Output path: " + outputPath);
if(outputPath == null || outputPath.isEmpty()) {
outputPath = workspace.getRemote();
}
else {
workspace = new FilePath(new File(outputPath));
}
File fileToCopy = new File(outputPath, fileName);

ArtifactArchiver artifactArchiver = new ArtifactArchiver(fileName);
artifactArchiver.perform(run, workspace, envVars, launcher, listener);
}
if (fileToCopy.exists()) {
createArchiveFile(tempDir, fileName, fileToCopy, run, workspace, envVars, launcher, listener);
}
} catch (Exception e) {
log.error("Error saving additional reports: " + e.getMessage());
log.error(String.format("Error saving additional reports (%s format): %s", formatType, e.getMessage()));
}

}
}

private void createArchiveFile(FilePath tempDir, String fileName, File fileToCopy, Run<?, ?> run, FilePath workspace, EnvVars envVars, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
log.info("Copying file to workspace: " + tempDir.getRemote());
FilePath tempDirPath = tempDir.child(fileName);
new FilePath(fileToCopy).copyTo(tempDirPath);
ArtifactArchiver artifactArchiver = new ArtifactArchiver(tempDirPath.getName());
artifactArchiver.perform(run, workspace, envVars, launcher, listener);
}

private String extractOptionValue(String options, String optionKey) {
if (options.contains(optionKey)) {
Expand Down Expand Up @@ -537,8 +545,9 @@ private ScanConfig resolveConfiguration(Run<?, ?> run, FilePath workspace, Check
String additionalOptions = getUseOwnAdditionalOptions() ? getAdditionalOptions() : descriptor.getAdditionalOptions();
if (fixEmptyAndTrim(additionalOptions) != null) {
String prefixPath = workspace.getRemote();
String separator = File.separator;
if (additionalOptions.contains("--output-path")) {
additionalOptions = additionalOptions.replaceAll("(--output-path\\s+)(\\S+)", "$1" + prefixPath + "/$2");
additionalOptions = additionalOptions.replaceAll("(--output-path\\s+)(\\S+)", "$1" + prefixPath + separator + "$2");
} else {
additionalOptions += String.format(" --output-path \"%s\"", prefixPath);
}
Expand Down

0 comments on commit bb8047d

Please sign in to comment.