Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-18109] Show all violations highlighted on a single page
- New URL ".../cppcheckResult/source.all" points to a dynamic page rendered using a newly implemented class CppcheckSourceAll. Optional parameters are before=NUM and after=NUM to specify number of displayed lines, their default value is 5. A link was added above the table on the details page. - Variable cppcheckfile refactored to cppcheckFile (f/F) to prevent copy-paste errors. - Null check for return value of diffCurrentAndPrevious() removed, the method never returns null (found by FindBugs). - Source details show only part of the path relative to the workspace in the header instead of the very long absolute path, the method getFileNameHtmlWrap() removed. - Cppcheck message must be HTML escaped before passing to the page (e.g. "Comparison of a variable having boolean value using relational (<, >, <= or >=) operator."). - Missing space added to the tooltip on the page with source code of a whole file. - Annotations added to deprecated members present for backward compatibility.
- Loading branch information
1 parent
5b3b675
commit 1f0faed
Showing
8 changed files
with
296 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/org/jenkinsci/plugins/cppcheck/CppcheckSourceAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package org.jenkinsci.plugins.cppcheck; | ||
|
||
import hudson.model.AbstractBuild; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang.StringEscapeUtils; | ||
|
||
import com.thalesgroup.hudson.plugins.cppcheck.model.CppcheckWorkspaceFile; | ||
|
||
/** | ||
* Show all violations highlighted on a single page. | ||
* | ||
* @author Michal Turek | ||
* @since 1.16 | ||
*/ | ||
public class CppcheckSourceAll { | ||
/** The related build. */ | ||
private final AbstractBuild<?, ?> owner; | ||
|
||
/** The files to show. */ | ||
private final Collection<CppcheckWorkspaceFile> files; | ||
|
||
/** Number of lines to show before the highlighted line. */ | ||
private final int linesBefore; | ||
|
||
/** Number of lines to show after the highlighted line. */ | ||
private final int linesAfter; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param owner | ||
* the related build | ||
* @param files | ||
* the files to show | ||
* @param linesBefore | ||
* number of lines to show before the highlighted line | ||
* @param linesAfter | ||
* number of lines to show after the highlighted line | ||
*/ | ||
public CppcheckSourceAll(AbstractBuild<?, ?> owner, | ||
Collection<CppcheckWorkspaceFile> files, int linesBefore, | ||
int linesAfter) { | ||
this.owner = owner; | ||
this.files = files; | ||
this.linesBefore = linesBefore; | ||
this.linesAfter = linesAfter; | ||
} | ||
|
||
public AbstractBuild<?, ?> getOwner() { | ||
return owner; | ||
} | ||
|
||
public Collection<CppcheckWorkspaceFile> getFiles() { | ||
return files; | ||
} | ||
|
||
public int getLinesBefore() { | ||
return linesBefore; | ||
} | ||
|
||
public int getLinesAfter() { | ||
return linesAfter; | ||
} | ||
|
||
/** | ||
* Get specified lines of source code from the file. | ||
* | ||
* @param file | ||
* the input file | ||
* @return the related lines of code with HTML formatting | ||
*/ | ||
public String getSourceCode(CppcheckWorkspaceFile file) { | ||
File tempFile = new File(file.getTempName(owner)); | ||
|
||
if (!tempFile.exists()) { | ||
return "Can't read file: " + tempFile.getAbsolutePath(); | ||
} | ||
|
||
BufferedReader reader = null; | ||
|
||
try { | ||
reader = new BufferedReader(new FileReader(tempFile)); | ||
return getRelatedLines(reader, file.getCppcheckFile() | ||
.getLineNumber()); | ||
} catch (FileNotFoundException e) { | ||
return "Can't read file: " + e; | ||
} catch (IOException e) { | ||
return "Reading file failed: " + e; | ||
} finally { | ||
IOUtils.closeQuietly(reader); | ||
} | ||
} | ||
|
||
/** | ||
* Get specified lines from a stream. | ||
* | ||
* @param reader | ||
* the input stream | ||
* @param lineNumber | ||
* the base line | ||
* @return the lines with HTML formatting | ||
* @throws IOException | ||
* if something fails | ||
*/ | ||
private String getRelatedLines(BufferedReader reader, int lineNumber) | ||
throws IOException { | ||
final int start = (lineNumber > linesBefore) ? lineNumber - linesBefore : 1; | ||
final int end = lineNumber + linesAfter; | ||
final String numberFormat = "%0" + String.valueOf(end).length() + "d"; | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
int current = 1; | ||
String line = ""; | ||
|
||
while ((line = reader.readLine()) != null && current <= end) { | ||
if (current >= start) { | ||
if (current == lineNumber) { | ||
builder.append("<div class=\"line highlighted\">"); | ||
} else { | ||
builder.append("<div class=\"line\">"); | ||
} | ||
|
||
builder.append("<span class=\"lineNumber\">"); | ||
builder.append(String.format(numberFormat, current)); | ||
builder.append("</span> ");// The space separates line number and code | ||
builder.append(StringEscapeUtils.escapeHtml(line)); | ||
builder.append("</div>\n"); | ||
} | ||
|
||
++current; | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1f0faed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is such a useful view. However, is it possible to have parameters that define what to show? For example:
Which would just show the delta of new errors introduced since the last build. Also, allowing a slice into the data to be performed:
Which would just show 20 errors from offset 100. We have some 11000+ errors/warnings we are dealing with and displaying them all can hang the browser or just be really slow to load.
1f0faed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this make sense, please create a new Jira feature request for this...