Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/source-code-retention' into ever…
Browse files Browse the repository at this point in the history
…ything
  • Loading branch information
uhafner committed Jan 31, 2024
2 parents c38875f + 7422834 commit df76e4f
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PropertyStatistics {
* @param property
* the property to show the details for
* @param propertyFormatter
* the formatter that show the property
* the formatter that shows the property
*/
PropertyStatistics(final Report report, final Report newIssues,
final String property, final Function<String, String> propertyFormatter) {
Expand All @@ -57,9 +57,9 @@ public int getTotal() {
}

/**
* Returns the amount of issues introduced since the last build.
* Returns the number of issues introduced since the last build.
*
* @return the amount of new issues
* @return the number of new issues
*/
public int getTotalNewIssues() {
return totalNewIssues;
Expand Down Expand Up @@ -151,7 +151,7 @@ public long getNewCount(final String key) {
* @param key
* the property instance
*
* @return the number of high severity issues
* @return the number of high-severity issues
*/
public long getErrorsCount(final String key) {
return getReportFor(key).getSizeOf(Severity.ERROR);
Expand All @@ -163,7 +163,7 @@ public long getErrorsCount(final String key) {
* @param key
* the property instance
*
* @return the number of high severity issues
* @return the number of high-severity issues
*/
public long getHighCount(final String key) {
return getReportFor(key).getSizeOf(Severity.WARNING_HIGH);
Expand All @@ -175,7 +175,7 @@ public long getHighCount(final String key) {
* @param key
* the property instance
*
* @return the number of normal severity issues
* @return the number of normal-severity issues
*/
public long getNormalCount(final String key) {
return getReportFor(key).getSizeOf(Severity.WARNING_NORMAL);
Expand All @@ -187,7 +187,7 @@ public long getNormalCount(final String key) {
* @param key
* the property instance
*
* @return the number of low severity issues
* @return the number of low-severity issues
*/
public long getLowCount(final String key) {
return getReportFor(key).getSizeOf(Severity.WARNING_LOW);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jenkins.model.Jenkins;

import io.jenkins.plugins.analysis.core.util.ModelValidation;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.JenkinsFacade;
import io.jenkins.plugins.util.ValidationUtilities;

Expand Down Expand Up @@ -85,6 +86,20 @@ public FormValidation doCheckSourceCodeEncoding(@AncestorInPath final BuildableI
return VALIDATION_UTILITIES.validateCharset(sourceCodeEncoding);
}

/**
* Returns a model with all {@link SourceCodeRetention} strategies.
*
* @return a model with all {@link SourceCodeRetention} strategies.
*/
@POST
@SuppressWarnings("unused") // used by Stapler view data binding
public ListBoxModel doFillSourceCodeRetentionItems() {
if (JENKINS.hasPermission(Jenkins.READ)) {
return SourceCodeRetention.fillItems();
}
return new ListBoxModel();
}

/**
* Returns a model with all available severity filters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.jenkins.plugins.analysis.core.util.WarningsQualityGate;
import io.jenkins.plugins.checks.steps.ChecksInfo;
import io.jenkins.plugins.prism.SourceCodeDirectory;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.JenkinsFacade;
import io.jenkins.plugins.util.LogHandler;
import io.jenkins.plugins.util.ResultHandler;
Expand Down Expand Up @@ -92,8 +93,8 @@ public class IssuesRecorder extends Recorder {
private List<Tool> analysisTools = new ArrayList<>();

private String sourceCodeEncoding = StringUtils.EMPTY;
private String sourceDirectory = StringUtils.EMPTY;
private Set<SourceCodeDirectory> sourceDirectories = new HashSet<>(); // @since 9.11.0
private SourceCodeRetention sourceCodeRetention = SourceCodeRetention.EVERY_BUILD;

private boolean ignoreQualityGate = false; // by default, a successful quality gate is mandatory;
private boolean ignoreFailedBuilds = true; // by default, failed builds are ignored;
Expand All @@ -114,14 +115,6 @@ public class IssuesRecorder extends Recorder {
private boolean quiet = false;

private boolean isBlameDisabled;
/**
* Not used anymore.
*
* @deprecated since 8.5.0
*/
@Deprecated
private transient boolean isForensicsDisabled;

private boolean skipPublishingChecks; // by default, checks will be published
private boolean publishAllIssues; // by default, only new issues will be published

Expand Down Expand Up @@ -156,14 +149,8 @@ public IssuesRecorder() {
* @return this
*/
protected Object readResolve() {
if (sourceDirectory == null) {
sourceDirectory = StringUtils.EMPTY;
}
if (sourceDirectories == null) {
sourceDirectories = new HashSet<>();
if (StringUtils.isNotBlank(sourceDirectory)) {
sourceDirectories.add(new SourceCodeDirectory(sourceDirectory));
}
}
if (trendChartType == null) {
trendChartType = TrendChartType.AGGREGATION_TOOLS;
Expand All @@ -177,6 +164,9 @@ protected Object readResolve() {
if (scm == null) {
scm = StringUtils.EMPTY;
}
if (sourceCodeRetention == null) {
sourceCodeRetention = SourceCodeRetention.EVERY_BUILD;
}
return this;
}

Expand Down Expand Up @@ -335,22 +325,6 @@ public void setSourceCodeEncoding(final String sourceCodeEncoding) {
this.sourceCodeEncoding = sourceCodeEncoding;
}

public String getSourceDirectory() {
return sourceDirectory;
}

/**
* Sets the path to the directory that contains the source code. If not relative and thus not part of the workspace,
* then this directory needs to be added in Jenkins global configuration to prevent accessing of forbidden resources.
*
* @param sourceDirectory
* directory containing the source code
*/
@DataBoundSetter
public void setSourceDirectory(final String sourceDirectory) {
this.sourceDirectory = sourceDirectory;
}

/**
* Sets the paths to the directories that contain the source code. If not relative and thus not part of the workspace,
* then these directories need to be added in Jenkins global configuration to prevent accessing of forbidden resources.
Expand All @@ -367,6 +341,21 @@ public List<SourceCodeDirectory> getSourceDirectories() {
return new ArrayList<>(sourceDirectories);
}

/**
* Defines the retention strategy for source code files.
*
* @param sourceCodeRetention
* the retention strategy for source code files
*/
@DataBoundSetter
public void setSourceCodeRetention(final SourceCodeRetention sourceCodeRetention) {
this.sourceCodeRetention = sourceCodeRetention;
}

public SourceCodeRetention getSourceCodeRetention() {
return sourceCodeRetention;
}

/**
* Returns whether the results for each configured static analysis result should be aggregated into a single result
* or if every tool should get an individual result.
Expand Down Expand Up @@ -764,7 +753,8 @@ private String getReportName(final Tool tool) {
private AnnotatedReport scanWithTool(final Run<?, ?> run, final FilePath workspace, final TaskListener listener,
final Tool tool) throws IOException, InterruptedException {
IssuesScanner issuesScanner = new IssuesScanner(tool, getFilters(), getSourceCodeCharset(),
workspace, getSourceCodePaths(), run, new FilePath(run.getRootDir()), listener,
workspace, getSourceCodePaths(), getSourceCodeRetention(),
run, new FilePath(run.getRootDir()), listener,
scm, isBlameDisabled ? BlameMode.DISABLED : BlameMode.ENABLED,
skipPostProcessing ? PostProcessingMode.DISABLED : PostProcessingMode.ENABLED, quiet);

Expand Down Expand Up @@ -891,6 +881,20 @@ public ComboBoxModel doFillSourceCodeEncodingItems(@AncestorInPath final Buildab
return new ComboBoxModel();
}

/**
* Returns a model with all {@link SourceCodeRetention} strategies.
*
* @return a model with all {@link SourceCodeRetention} strategies.
*/
@POST
@SuppressWarnings("unused") // used by Stapler view data binding
public ListBoxModel doFillSourceCodeRetentionItems() {
if (JENKINS.hasPermission(Jenkins.READ)) {
return SourceCodeRetention.fillItems();
}
return new ListBoxModel();
}

/**
* Returns a model with all available severity filters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.jenkins.plugins.forensics.miner.RepositoryStatistics;
import io.jenkins.plugins.prism.PermittedSourceCodeDirectory;
import io.jenkins.plugins.prism.PrismConfiguration;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.prism.SourceDirectoryFilter;
import io.jenkins.plugins.util.LogHandler;

Expand All @@ -64,6 +65,7 @@
class IssuesScanner {
private final FilePath workspace;
private final Set<String> sourceDirectories;
private final SourceCodeRetention sourceCodeRetention;
private final Run<?, ?> run;
private final FilePath jenkinsRootDir;
private final Charset sourceCodeEncoding;
Expand All @@ -85,7 +87,8 @@ enum PostProcessingMode {

@SuppressWarnings("checkstyle:ParameterNumber")
IssuesScanner(final Tool tool, final List<RegexpFilter> filters, final Charset sourceCodeEncoding,
final FilePath workspace, final Set<String> sourceDirectories, final Run<?, ?> run,
final FilePath workspace, final Set<String> sourceDirectories,
final SourceCodeRetention sourceCodeRetention, final Run<?, ?> run,
final FilePath jenkinsRootDir, final TaskListener listener,
final String scm, final BlameMode blameMode, final PostProcessingMode postProcessingMode,
final boolean quiet) {
Expand All @@ -94,6 +97,7 @@ enum PostProcessingMode {
this.tool = tool;
this.workspace = workspace;
this.sourceDirectories = sourceDirectories;
this.sourceCodeRetention = sourceCodeRetention;
this.run = run;
this.jenkinsRootDir = jenkinsRootDir;
this.listener = listener;
Expand Down Expand Up @@ -177,12 +181,18 @@ private Blamer createBlamer(final Report report) {

private void copyAffectedFiles(final Report report, final FilePath buildFolder)
throws InterruptedException {
report.logInfo("Copying affected files to Jenkins' build folder '%s'", buildFolder);
var log = new FilteredLog("Errors while processing affected files");
if (sourceCodeRetention != SourceCodeRetention.NEVER) {
report.logInfo("Copying affected files to Jenkins' build folder '%s'", buildFolder);

Set<String> permittedSourceDirectories = getPermittedSourceDirectories();
permittedSourceDirectories.add(workspace.getRemote());
new AffectedFilesResolver().copyAffectedFilesToBuildFolder(
report, workspace, permittedSourceDirectories, buildFolder);
}
sourceCodeRetention.cleanup(run, AFFECTED_FILES_FOLDER_NAME, log);

Set<String> permittedSourceDirectories = getPermittedSourceDirectories();
permittedSourceDirectories.add(workspace.getRemote());
new AffectedFilesResolver().copyAffectedFilesToBuildFolder(
report, workspace, permittedSourceDirectories, buildFolder);
report.mergeLogMessages(log);
}

private FilePath createAffectedFilesFolder(final Report report) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.jenkins.plugins.analysis.core.util.WarningsQualityGate;
import io.jenkins.plugins.checks.steps.ChecksInfo;
import io.jenkins.plugins.prism.SourceCodeDirectory;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.PipelineResultHandler;
import io.jenkins.plugins.util.QualityGateEvaluator;
import io.jenkins.plugins.util.ResultHandler;
Expand Down Expand Up @@ -69,8 +70,8 @@ public class RecordIssuesStep extends Step implements Serializable {
private List<Tool> analysisTools = new ArrayList<>();

private String sourceCodeEncoding = StringUtils.EMPTY;
private String sourceDirectory = StringUtils.EMPTY;
private Set<SourceCodeDirectory> sourceDirectories = new HashSet<>(); // @since 9.11.0
private SourceCodeRetention sourceCodeRetention = SourceCodeRetention.EVERY_BUILD;

private boolean ignoreQualityGate = false; // by default, a successful quality gate is mandatory;
private boolean ignoreFailedBuilds = true; // by default, failed builds are ignored;
Expand Down Expand Up @@ -310,22 +311,6 @@ public void setSourceCodeEncoding(final String sourceCodeEncoding) {
this.sourceCodeEncoding = sourceCodeEncoding;
}

public String getSourceDirectory() {
return sourceDirectory;
}

/**
* Sets the path to the folder that contains the source code. If not relative and thus not part of the workspace
* then this folder needs to be added in Jenkins global configuration.
*
* @param sourceDirectory
* a folder containing the source code
*/
@DataBoundSetter
public void setSourceDirectory(final String sourceDirectory) {
this.sourceDirectory = sourceDirectory;
}

/**
* Sets the paths to the directories that contain the source code. If not relative and thus not part of the
* workspace, then these directories need to be added in Jenkins global configuration to prevent accessing of
Expand All @@ -344,11 +329,22 @@ public List<SourceCodeDirectory> getSourceDirectories() {
}

private List<SourceCodeDirectory> getAllSourceDirectories() {
Set<SourceCodeDirectory> directories = new HashSet<>(getSourceDirectories());
if (StringUtils.isNotBlank(getSourceDirectory())) {
directories.add(new SourceCodeDirectory(getSourceDirectory()));
}
return new ArrayList<>(directories);
return new ArrayList<>(new HashSet<>(getSourceDirectories()));
}

/**
* Defines the retention strategy for source code files.
*
* @param sourceCodeRetention
* the retention strategy for source code files
*/
@DataBoundSetter
public void setSourceCodeRetention(final SourceCodeRetention sourceCodeRetention) {
this.sourceCodeRetention = sourceCodeRetention;
}

public SourceCodeRetention getSourceCodeRetention() {
return sourceCodeRetention;
}

/**
Expand Down
Loading

0 comments on commit df76e4f

Please sign in to comment.