Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.jenkins.plugins.analysis.warnings;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.ParsingCanceledException;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;

import java.io.File;
import java.io.Serial;
import java.nio.charset.Charset;

import hudson.remoting.VirtualChannel;
import jenkins.MasterToSlaveFileCallable;

import io.jenkins.plugins.analysis.core.util.FileFinder;
import io.jenkins.plugins.util.ValidationUtilities;

/**
* Searches in the workspace for files matching the given include and exclude patterns, then scans each file
* line-by-line for occurrences of a configurable regular expression. Runs on the agent side so that files
* residing only on remote nodes can be read without copying them to the master.
*
* @author Akash Manna
*/
class AgentGrepScanner extends MasterToSlaveFileCallable<Report> {
@Serial
private static final long serialVersionUID = -4023490687124215210L;

private final String regexp;
private final String severity;
private final String messageTemplate;
private final String includePattern;
private final String excludePattern;
private final String sourceCodeEncoding;

/**
* Creates a new {@link AgentGrepScanner}.
*
* @param regexp
* the regular expression to match against each line
* @param severity
* the name of the {@link Severity} to assign to matched issues
* @param messageTemplate
* optional message template for matched issues; blank means use the matched line
* @param includePattern
* Ant file-set pattern specifying files to scan
* @param excludePattern
* Ant file-set pattern specifying files to exclude
* @param sourceCodeEncoding
* the encoding used to read files
*/
@SuppressWarnings("ParameterNumber")
AgentGrepScanner(final String regexp, final String severity, final String messageTemplate,
final String includePattern, final String excludePattern, final String sourceCodeEncoding) {
super();

this.regexp = regexp;
this.severity = severity;
this.messageTemplate = messageTemplate;
this.includePattern = StringUtils.defaultString(includePattern);
this.excludePattern = StringUtils.defaultString(excludePattern);
this.sourceCodeEncoding = sourceCodeEncoding;
}

@Override
@SuppressWarnings("PMD.DoNotUseThreads")
public Report invoke(final File workspace, final VirtualChannel channel) {
var report = new Report();
report.logInfo(
"Searching for files in workspace '%s' that match the include pattern '%s' and exclude pattern '%s'",
workspace, includePattern, excludePattern);

var fileFinder = new FileFinder(includePattern, excludePattern);
var fileNames = fileFinder.find(workspace);
report.logInfo("-> found %d files that will be scanned", fileNames.length);

var scanner = new GrepScanner(regexp, Severity.valueOf(severity, Severity.WARNING_NORMAL), messageTemplate);

Check warning on line 78 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/AgentGrepScanner.java

View check run for this annotation

ci.jenkins.io / CPD

CPD

LOW: Found duplicated code.
Raw output
<pre><code>this.includePattern &#61; StringUtils.defaultString(includePattern); this.excludePattern &#61; StringUtils.defaultString(excludePattern); this.sourceCodeEncoding &#61; sourceCodeEncoding; } &#64;Override &#64;SuppressWarnings(&#34;PMD.DoNotUseThreads&#34;) public Report invoke(final File workspace, final VirtualChannel channel) { var report &#61; new Report(); report.logInfo( &#34;Searching for files in workspace &#39;%s&#39; that match the include pattern &#39;%s&#39; and exclude pattern &#39;%s&#39;&#34;, workspace, includePattern, excludePattern); var fileFinder &#61; new FileFinder(includePattern, excludePattern); var fileNames &#61; fileFinder.find(workspace); report.logInfo(&#34;-&gt; found %d files that will be scanned&#34;, fileNames.length); var scanner &#61; new GrepScanner(regexp, Severity.valueOf(severity, Severity.WARNING_NORMAL), messageTemplate);</code></pre>
report.logInfo("Scanning all %d file(s) for pattern '%s'", fileNames.length, regexp);

var root = workspace.toPath();
var charset = getCharset();
for (String fileName : fileNames) {
report.addAll(scanner.scan(root.resolve(fileName), charset).get());

if (Thread.interrupted()) {

Check warning on line 86 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/AgentGrepScanner.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Partially covered line

Line 86 is only partially covered, one branch is missing

Check warning on line 86 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/AgentGrepScanner.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Partially covered line

Line 86 is only partially covered, one branch is missing
throw new ParsingCanceledException();

Check warning on line 87 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/AgentGrepScanner.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Not covered line

Line 87 is not covered by tests

Check warning on line 87 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/AgentGrepScanner.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Not covered line

Line 87 is not covered by tests
}
}
report.logInfo("Found a total of %d grep matches", report.size());
return report;
}

private Charset getCharset() {
return new ValidationUtilities().getCharset(sourceCodeEncoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public String getLinkName() {
}

/** Descriptor for this static analysis tool. */
abstract static class DuplicateCodeDescriptor extends AnalysisModelParserDescriptor {
public abstract static class DuplicateCodeDescriptor extends AnalysisModelParserDescriptor {
private static final JenkinsFacade JENKINS = new JenkinsFacade();
private static final ThresholdValidation VALIDATION = new ThresholdValidation();

Expand Down
Loading
Loading