Skip to content

Commit

Permalink
More logging added to report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhukovAN committed Oct 6, 2021
1 parent 0c9af0e commit cbf2c3a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 104 deletions.
166 changes: 94 additions & 72 deletions .idea/compiler.xml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,6 @@
### 20210927
+ [Fix] "Fail-if-failed" and "fail-if-unstable" settings for Jenkins plugin are changed from boolean to enum. This allows us to mark build step as unstable and thus allow AST results save in pipeline jobs
+ [Fix] Broken symlinks processing fixed. Test task "advancedTest" added as Windows requires "Run as administrator" privilege to create symlinks
+ [Fix] CLI plugin project delete error fixed
+ [Fix] CLI plugin project delete error fixed
### 20211006
+ [Feature] More logging added to report generation
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void check(@NonNull final Reports reports) {
@Override
public void generate(@NonNull final UUID projectId, @NonNull final UUID scanResultId, @NonNull final Reports reports, @NonNull final FileOperations fileOps) throws GenericException {

log.trace("Validate and check reports to be generated");
final Reports checkedReports = reports.validate();
check(checkedReports);

Expand All @@ -131,6 +132,7 @@ public void generate(@NonNull final UUID projectId, @NonNull final UUID scanResu
dummyTemplate, data.getLocale(),
data.getFormat(), data.getFilters());
} else return;
log.trace("Report generated to temp file {}", reportFile.toPath());
byte[] data = call(
() -> Files.readAllBytes(reportFile.toPath()),
"Report data read failed");
Expand All @@ -150,14 +152,14 @@ public void generate(@NonNull final UUID projectId, @NonNull final UUID scanResu
File json = call(
() -> {
Path temp = Files.createTempFile("ptai-", "-scanresult");
log.debug("Created file {} for temporal scan result store", temp);
log.debug("Created file {} for temporal raw scan result store", temp);
mapper.writeValue(temp.toFile(), scanResult);
log.debug("Scan result data saved to {}", temp);
log.debug("Raw scan result data saved to {}", temp);
return temp.toFile();
}, "Scan result save failed");
}, "Raw scan result save failed");
for (Reports.RawData raw : checkedReports.getRaw())
call(() -> fileOps.saveArtifact(raw.getFileName(), json), "Raw JSON result save failed");
log.debug("Deleting temporal scan results file {}", json.getAbsolutePath());
log.debug("Deleting temporal raw scan results file {}", json.getAbsolutePath());
call(json::delete, "Temporal file " + json.getAbsolutePath() + " delete failed", true);
}
}
Expand All @@ -179,6 +181,7 @@ public File generateReport(
@NonNull UUID projectId, @NonNull UUID scanResultId, @NonNull UUID templateId,
@NonNull Locale locale, @NonNull Reports.Report.Format type,
Reports.IssuesFilter filters) throws GenericException {
log.trace("Create report generation model");
ReportGenerateModel model = new ReportGenerateModel()
.parameters(new UserReportParameters()
.includeDFD(true)
Expand Down Expand Up @@ -247,6 +250,7 @@ protected File generateReport(
@NonNull Locale locale, @NonNull Reports.Report.Format type,
Reports.IssuesFilter filters) throws GenericException {
// Get all report templates for given locale
log.trace("Load all report templates to find one with {} name", template);
List<ReportTemplateModel> templates = call(
() -> client.getReportsApi().apiReportsTemplatesGet(locale.getValue(), false),
"PT AI report templates list read failed");
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ slf4jVersion=1.7.30
# jenkinsVersion=2.277.4
# jenkinsVersion=2.289.3
jenkinsVersion=2.303.1
teamcityVersion=2020.1
teamcityVersion=2019.1
# teamcityVersion=2020.1
localizerVersion=1.26
jacksonVersion=2.12.3
commonsIoVersion=2.6
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Files;

@Slf4j
@Builder
@RequiredArgsConstructor
public class JenkinsFileOperations implements FileOperations {
Expand All @@ -23,12 +25,15 @@ public class JenkinsFileOperations implements FileOperations {

@SneakyThrows
public void saveArtifact(@NonNull String name, @NonNull File file) {
log.trace("Saving artifact {} from {} file", name, file.getAbsolutePath());
byte[] data = Files.readAllBytes(file.toPath());
log.trace("Data load from {} file is completed", file.getAbsolutePath());
saveArtifact(name, data);
}

@SneakyThrows
public void saveArtifact(@NonNull String name, @NonNull byte[] data) {
log.trace("Save in-memory data as {} artifact. Data is {} bytes long", name, data.length);
RemoteFileUtils.saveReport(owner.getLauncher(), owner.getListener(), owner.getWorkspace().getRemote(), name, data, owner.isVerbose());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jenkins.security.MasterToSlaveCallable;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
Expand Down Expand Up @@ -48,8 +49,10 @@ public static FilePath collect(
@SuppressWarnings("UnusedReturnValue")
public static FilePath saveReport(Launcher launcher, TaskListener listener, String dir, String artifact, final byte[] data, boolean verbose) throws GenericException {
BinaryReportSaver saver = new BinaryReportSaver(dir, artifact, data);
log.trace("Binary report saver created");
saver.setConsole(listener.getLogger());
saver.setVerbose(verbose);
log.trace("Save report using BinaryReportSaver instance: {}", saver);
return CallHelper.call(
() -> Objects.requireNonNull(launcher.getChannel()).call(new RemoteFileUtils(saver)),
"Remote save report call failed");
Expand All @@ -74,44 +77,26 @@ public File execute() throws GenericException {
}
}

@RequiredArgsConstructor
protected static class ReportSaver extends AbstractTool implements Executor, Serializable {
protected final String dir;
protected final String artifact;
protected final String data;

public File execute() throws GenericException {
try {
Path destination = Paths.get(dir).resolve(AbstractJob.DEFAULT_OUTPUT_FOLDER).resolve(artifact);
if (!destination.toFile().getParentFile().exists()) {
if (!destination.toFile().getParentFile().mkdirs())
throw new IOException("Failed to create folder structure for " + destination.toFile());
}
return Files.write(
destination,
data.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING).toFile();
} catch (IOException e) {
throw GenericException.raise("Report file save failed", e);
}
}
}

@ToString(callSuper = true)
protected static class BinaryReportSaver extends AbstractTool implements Executor, Serializable {

public BinaryReportSaver(@NonNull final String dir, @NonNull final String artifact, final byte[] data) {
log.trace("Create binary report saver for {} artifact in {} folder", artifact, dir);
this.dir = dir;
this.artifact = artifact;
this.data = Arrays.copyOf(data, data.length);
}

protected final String dir;
protected final String artifact;

@ToString.Exclude
private final byte[] data;

public File execute() throws GenericException {
try {
Path destination = Paths.get(dir).resolve(AbstractJob.DEFAULT_OUTPUT_FOLDER).resolve(artifact);
log.trace("Destination file path: {}", destination);
String fileName = CallHelper.call(
() -> destination.getFileName().toString(),
"Empty destination file name");
Expand All @@ -125,6 +110,7 @@ public File execute() throws GenericException {
log.error("Report " + fileName + " delete failed");
}

log.trace("In-memory data will be saved to {} file", destination);
return Files.write(
destination,
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def st = namespace("jelly:stapler")
f.entry(
title: _('onAstFailed'),
field: 'onAstFailed') {
f.select(style: 'width: 350px; ')
f.select(style: 'width: 420px; ')
}

f.entry(
title: _('onAstUnstable'),
field: 'onAstUnstable') {
f.select(style: 'width: 350px; ')
f.select(style: 'width: 420px; ')
}

f.entry(
Expand All @@ -25,4 +25,3 @@ f.entry(
field: 'reports',
addCaption: _('reportAdd'))
}

0 comments on commit cbf2c3a

Please sign in to comment.