Skip to content

Commit

Permalink
Merge pull request #1010 from jenkinsci/part-of-modified-code-not-equals
Browse files Browse the repository at this point in the history
Part of modified code not equals
  • Loading branch information
uhafner authored Jan 25, 2024
2 parents b30177e + 7118e7a commit 9184d74
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/main/java/edu/hm/hafner/analysis/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static Predicate<Issue> byType(final String type) {
private String description; // fixed

private String fingerprint; // mutable, not part of equals
private boolean partOfModifiedCode; // mutable
private boolean partOfModifiedCode; // mutable, not part of equals

/**
* Creates a new instance of {@link Issue} using the properties of the other issue instance. The new issue has the
Expand Down Expand Up @@ -901,9 +901,6 @@ public boolean equals(@CheckForNull final Object o) {

Issue issue = (Issue) o;

if (partOfModifiedCode != issue.partOfModifiedCode) {
return false;
}
if (lineStart != issue.lineStart) {
return false;
}
Expand Down Expand Up @@ -971,7 +968,6 @@ public int hashCode() {
result = 31 * result + moduleName.hashCode();
result = 31 * result + packageName.hashCode();
result = 31 * result + fileName.hashCode();
result = 31 * result + (partOfModifiedCode ? 1 : 0);
return result;
}

Expand Down
36 changes: 32 additions & 4 deletions src/main/java/edu/hm/hafner/analysis/IssueBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.apache.commons.lang3.StringUtils;

import com.google.errorprone.annotations.CanIgnoreReturnValue;

import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.TreeString;
import edu.hm.hafner.util.TreeStringBuilder;
Expand Down Expand Up @@ -92,6 +94,7 @@ public class IssueBuilder implements AutoCloseable {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setId(final UUID id) {
this.id = id;
return this;
Expand All @@ -106,21 +109,23 @@ public IssueBuilder setId(final UUID id) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setAdditionalProperties(@CheckForNull final Serializable additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}

/**
* Sets the fingerprint for this issue. Used to decide if two issues are equal even if the equals method returns
* {@code false} since some of the properties differ due to code refactorings. The fingerprint is created by
* {@code false} since some properties differ due to code refactorings. The fingerprint is created by
* analyzing the content of the affected file.
*
* @param fingerprint
* the fingerprint to set
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setFingerprint(@CheckForNull final String fingerprint) {
this.fingerprint = fingerprint;
return this;
Expand All @@ -135,6 +140,7 @@ public IssueBuilder setFingerprint(@CheckForNull final String fingerprint) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setFileName(@CheckForNull final String fileName) {
this.fileName = internFileName(fileName);

Expand Down Expand Up @@ -166,14 +172,15 @@ TreeString internFileName(@CheckForNull final String unsafeFileName) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setDirectory(@CheckForNull final String directory) {
this.directory = directory;
return this;
}

/**
* Sets the path of the affected file. Note that this path is not the parent folder of the affected file. This path
* is the folder that contains all of the affected files of a {@link Report}. The path of an affected file is stored
* is the folder that contains all the affected files of a {@link Report}. The path of an affected file is stored
* in the {@code path} and {@code fileName} properties so that issues can be tracked even if the root folder changes
* (due to different build environments).
*
Expand All @@ -182,6 +189,7 @@ public IssueBuilder setDirectory(@CheckForNull final String directory) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setPathName(@CheckForNull final String pathName) {
this.pathName = pathName;
return this;
Expand All @@ -195,6 +203,7 @@ public IssueBuilder setPathName(@CheckForNull final String pathName) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setLineStart(final int lineStart) {
this.lineStart = lineStart;
return this;
Expand All @@ -208,6 +217,7 @@ public IssueBuilder setLineStart(final int lineStart) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setLineStart(@CheckForNull final String lineStart) {
this.lineStart = parseInt(lineStart);
return this;
Expand All @@ -221,6 +231,7 @@ public IssueBuilder setLineStart(@CheckForNull final String lineStart) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setLineEnd(final int lineEnd) {
this.lineEnd = lineEnd;
return this;
Expand All @@ -234,6 +245,7 @@ public IssueBuilder setLineEnd(final int lineEnd) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setLineEnd(@CheckForNull final String lineEnd) {
this.lineEnd = parseInt(lineEnd);
return this;
Expand All @@ -247,6 +259,7 @@ public IssueBuilder setLineEnd(@CheckForNull final String lineEnd) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setColumnStart(final int columnStart) {
this.columnStart = columnStart;
return this;
Expand All @@ -260,32 +273,35 @@ public IssueBuilder setColumnStart(final int columnStart) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setColumnStart(@CheckForNull final String columnStart) {
this.columnStart = parseInt(columnStart);
return this;
}

/**
* Sets the the last column of this issue (columns start at 1).
* Sets the last column of this issue (columns start at 1).
*
* @param columnEnd
* the last column
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setColumnEnd(final int columnEnd) {
this.columnEnd = columnEnd;
return this;
}

/**
* Sets the the last column of this issue (columns start at 1).
* Sets the last column of this issue (columns start at 1).
*
* @param columnEnd
* the last column
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setColumnEnd(@CheckForNull final String columnEnd) {
this.columnEnd = parseInt(columnEnd);
return this;
Expand All @@ -300,6 +316,7 @@ public IssueBuilder setColumnEnd(@CheckForNull final String columnEnd) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setCategory(@CheckForNull final String category) {
this.category = category;
return this;
Expand All @@ -314,6 +331,7 @@ public IssueBuilder setCategory(@CheckForNull final String category) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setType(@CheckForNull final String type) {
this.type = type;
return this;
Expand All @@ -327,6 +345,7 @@ public IssueBuilder setType(@CheckForNull final String type) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setPackageName(@CheckForNull final String packageName) {
this.packageName = internPackageName(packageName);

Expand All @@ -350,6 +369,7 @@ TreeString internPackageName(@CheckForNull final String unsafePackageName) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setModuleName(@CheckForNull final String moduleName) {
this.moduleName = moduleName;
return this;
Expand All @@ -363,6 +383,7 @@ public IssueBuilder setModuleName(@CheckForNull final String moduleName) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setOrigin(@CheckForNull final String origin) {
this.origin = origin;
return this;
Expand All @@ -376,6 +397,7 @@ public IssueBuilder setOrigin(@CheckForNull final String origin) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setOriginName(@CheckForNull final String originName) {
this.originName = originName;
return this;
Expand All @@ -389,6 +411,7 @@ public IssueBuilder setOriginName(@CheckForNull final String originName) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setReference(@CheckForNull final String reference) {
this.reference = reference;
return this;
Expand All @@ -402,6 +425,7 @@ public IssueBuilder setReference(@CheckForNull final String reference) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setSeverity(@CheckForNull final Severity severity) {
this.severity = severity;
return this;
Expand All @@ -416,6 +440,7 @@ public IssueBuilder setSeverity(@CheckForNull final Severity severity) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder guessSeverity(@CheckForNull final String severityString) {
severity = Severity.guessFromString(severityString);
return this;
Expand All @@ -429,6 +454,7 @@ public IssueBuilder guessSeverity(@CheckForNull final String severityString) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setMessage(@CheckForNull final String message) {
if (StringUtils.isBlank(message)) {
this.message = EMPTY_TREE_STRING;
Expand All @@ -448,6 +474,7 @@ public IssueBuilder setMessage(@CheckForNull final String message) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setDescription(@CheckForNull final String description) {
this.description = StringUtils.stripToEmpty(description);
return this;
Expand All @@ -462,6 +489,7 @@ public IssueBuilder setDescription(@CheckForNull final String description) {
*
* @return this
*/
@CanIgnoreReturnValue
public IssueBuilder setLineRanges(final LineRangeList lineRanges) {
this.lineRanges = new LineRangeList(lineRanges);
return this;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ public Issue get(final int index) {
return all.next();
}

/**
* Returns the issues in this report that are part of modified code. This will include the issues of any sub-reports.
*
* @return all issues in this report
*/
public Collection<Issue> getInModifiedCode() {
return stream().filter(Issue::isPartOfModifiedCode).collect(Collectors.toList());
}

/**
* Returns the number of issues in this container.
*
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/edu/hm/hafner/analysis/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void shouldObeyEqualsContract() {
TREE_STRING_BUILDER.intern("Two"))
.withPrefabValues(LineRangeList.class, new LineRangeList(10), filled)
.forClass(Issue.class)
.withIgnoredFields("id", "reference", "pathName", "fingerprint").verify();
.withIgnoredFields("id", "reference", "pathName", "fingerprint", "partOfModifiedCode").verify();
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/edu/hm/hafner/analysis/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ static void beforeAll() {
SLF4JBridgeHandler.install();
}

@Test
void shouldFindIssuesInModifiedCode() {
Report report = new Report();
report.addAll(allIssuesAsList());

assertThat(report).hasNoInModifiedCode();

report.get(0).markAsPartOfModifiedCode();
assertThat(report).hasInModifiedCode(report.get(0));
report.get(2).markAsPartOfModifiedCode();
assertThat(report).hasInModifiedCode(report.get(0), report.get(2));
}

@Test
void shouldMergeLog() {
var issues = new Report();
Expand Down

0 comments on commit 9184d74

Please sign in to comment.