-
Notifications
You must be signed in to change notification settings - Fork 476
feat: Implement conversion of diff content to ReviewDog format #2478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
YongGoose
wants to merge
21
commits into
diffplug:main
Choose a base branch
from
YongGoose:feature/655
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e44435b
feat: Implement conversion of diff content to ReviewDog format
YongGoose 31ab0b9
Add changes
YongGoose 10c23f4
Add license
YongGoose 6d8b7e8
Revert changes
YongGoose 782bd94
Apply comment
YongGoose dc52b26
Add ReviewDogGenerator
YongGoose 25a4c27
Apply spotless
YongGoose 17ef58e
Apply comment
YongGoose b63253d
Update license header
YongGoose dfd422f
Remove unused code
YongGoose fb5aa2d
Refactor some codes
YongGoose 745810b
Refactor test codes
YongGoose 5724b73
Refactor test codes
YongGoose 62eb863
Apply spotless
YongGoose e7015df
Update README.md
YongGoose a94b357
Update README.md
YongGoose 6688b33
Add `reviewDog property` and pipe it to `spotlessCheck`
YongGoose 5ae21df
Implement configurable ReviewDog output directory
YongGoose a7d515c
Improve Gradle caching by using String lists instead of FormatterStep…
YongGoose 376eafb
Merge branch 'main' into feature/655
YongGoose 0cac676
Merge branch 'diffplug:main' into feature/655
YongGoose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
lib-extra/src/main/java/com/diffplug/spotless/extra/middleware/ReviewDogGenerator.java
This file contains hidden or 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,165 @@ | ||
/* | ||
* Copyright 2022-2025 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.middleware; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.Lint; | ||
|
||
/** | ||
* Utility class for generating ReviewDog compatible output in the rdjsonl format. | ||
* This class provides methods to create diff and lint reports that can be used by ReviewDog. | ||
*/ | ||
public final class ReviewDogGenerator { | ||
|
||
private static final String SOURCE = "spotless"; | ||
|
||
private ReviewDogGenerator() { | ||
// Prevent instantiation | ||
} | ||
|
||
/** | ||
* Generates a ReviewDog compatible JSON line (rdjsonl) for a diff between | ||
* the actual content and the formatted content of a file. | ||
* | ||
* @param path The file path | ||
* @param actualContent The content as it currently exists in the file | ||
* @param formattedContent The content after formatting is applied | ||
* @return A string in rdjsonl format representing the diff | ||
*/ | ||
public static String rdjsonlDiff(String path, String actualContent, String formattedContent) { | ||
if (actualContent.equals(formattedContent)) { | ||
return ""; | ||
} | ||
|
||
String diff = createUnifiedDiff(path, actualContent, formattedContent); | ||
|
||
return String.format( | ||
"{\"message\":{\"path\":\"%s\",\"message\":\"File requires formatting\",\"diff\":\"%s\"}}", | ||
escapeJson(path), | ||
escapeJson(diff)); | ||
} | ||
|
||
/** | ||
* Generates ReviewDog compatible JSON lines (rdjsonl) for lint issues | ||
* identified by formatting steps. | ||
* | ||
* @param path The file path | ||
* @param steps The list of formatter steps applied | ||
* @param lintsPerStep The list of lints produced by each step | ||
* @return A string in rdjsonl format representing the lints | ||
*/ | ||
public static String rdjsonlLintsFromSteps(String path, List<FormatterStep> steps, List<List<Lint>> lintsPerStep) { | ||
if (steps == null || steps.isEmpty()) { | ||
return rdjsonlLintsFromStrings(path, Collections.emptyList(), lintsPerStep); | ||
} | ||
List<String> stepNames = steps.stream() | ||
.map(FormatterStep::getName) | ||
.collect(Collectors.toList()); | ||
|
||
if (lintsPerStep == null || lintsPerStep.isEmpty()) { | ||
return rdjsonlLintsFromStrings(path, stepNames, Collections.emptyList()); | ||
} | ||
return rdjsonlLintsFromStrings(path, stepNames, lintsPerStep); | ||
} | ||
|
||
private static String rdjsonlLintsFromStrings(String path, List<String> stepNames, List<List<Lint>> lintsPerStep) { | ||
if (lintsPerStep == null || lintsPerStep.isEmpty()) { | ||
return ""; | ||
} | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
|
||
for (int i = 0; i < lintsPerStep.size(); i++) { | ||
List<Lint> lints = lintsPerStep.get(i); | ||
if (lints == null || lints.isEmpty()) { | ||
continue; | ||
} | ||
|
||
String stepName = (i < stepNames.size()) ? stepNames.get(i) : "unknown"; | ||
for (Lint lint : lints) { | ||
builder.append(formatLintAsJson(path, lint, stepName)).append('\n'); | ||
} | ||
} | ||
|
||
return builder.toString().trim(); | ||
} | ||
|
||
/** | ||
* Creates a unified diff between two text contents. | ||
*/ | ||
private static String createUnifiedDiff(String path, String actualContent, String formattedContent) { | ||
String[] actualLines = actualContent.split("\\r?\\n", -1); | ||
String[] formattedLines = formattedContent.split("\\r?\\n", -1); | ||
|
||
StringBuilder diff = new StringBuilder(); | ||
diff.append("--- a/").append(path).append('\n'); | ||
diff.append("+++ b/").append(path).append('\n'); | ||
diff.append("@@ -1,").append(actualLines.length).append(" +1,").append(formattedLines.length).append(" @@\n"); | ||
|
||
for (String line : actualLines) { | ||
diff.append('-').append(line).append('\n'); | ||
} | ||
|
||
for (String line : formattedLines) { | ||
diff.append('+').append(line).append('\n'); | ||
} | ||
|
||
return diff.toString(); | ||
} | ||
|
||
/** | ||
* Formats a single lint issue as a JSON line. | ||
*/ | ||
private static String formatLintAsJson(String path, Lint lint, String ruleCode) { | ||
return String.format( | ||
"{" | ||
+ "\"source\":\"%s\"," | ||
+ "\"code\":\"%s\"," | ||
+ "\"level\":\"warning\"," | ||
+ "\"message\":\"%s\"," | ||
+ "\"path\":\"%s\"," | ||
+ "\"line\":%d," | ||
+ "\"column\":%d" | ||
+ "}", | ||
escapeJson(SOURCE), | ||
escapeJson(ruleCode), | ||
escapeJson(lint.getDetail()), | ||
escapeJson(path), | ||
lint.getLineStart(), | ||
1); | ||
} | ||
|
||
/** | ||
* Escapes special characters in a string for JSON compatibility. | ||
*/ | ||
private static String escapeJson(String str) { | ||
if (str == null) { | ||
return ""; | ||
} | ||
return str | ||
.replace("\\", "\\\\") | ||
.replace("\"", "\\\"") | ||
.replace("\n", "\\n") | ||
.replace("\r", "\\r") | ||
.replace("\t", "\\t") | ||
.replace("\b", "\\b") | ||
.replace("\f", "\\f"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
lib-extra/src/main/java/com/diffplug/spotless/extra/middleware/package-info.java
This file contains hidden or 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,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@ReturnValuesAreNonnullByDefault | ||
package com.diffplug.spotless.extra.middleware; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault; |
112 changes: 112 additions & 0 deletions
112
lib-extra/src/test/java/com/diffplug/spotless/extra/middleware/ReviewDogGeneratorTest.java
This file contains hidden or 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,112 @@ | ||
/* | ||
* Copyright 2022-2025 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.middleware; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.diffplug.selfie.Selfie; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.Lint; | ||
|
||
public class ReviewDogGeneratorTest { | ||
|
||
@Test | ||
public void diffSingleLine() { | ||
String result = ReviewDogGenerator.rdjsonlDiff("test.txt", "dirty", "clean"); | ||
Selfie.expectSelfie(result).toBe("{\"message\":{\"path\":\"test.txt\",\"message\":\"File requires formatting\",\"diff\":\"--- a/test.txt\\n+++ b/test.txt\\n@@ -1,1 +1,1 @@\\n-dirty\\n+clean\\n\"}}"); | ||
} | ||
|
||
@Test | ||
public void diffNoChange() { | ||
String result = ReviewDogGenerator.rdjsonlDiff("test.txt", "same", "same"); | ||
Selfie.expectSelfie(result).toBe(""); | ||
} | ||
|
||
@Test | ||
public void diffMultipleLines() { | ||
String actual = "Line 1\nLine 2\nDirty line\nLine 4"; | ||
String formatted = "Line 1\nLine 2\nClean line\nLine 4"; | ||
|
||
String result = ReviewDogGenerator.rdjsonlDiff("src/main.java", actual, formatted); | ||
Selfie.expectSelfie(result).toBe("{\"message\":{\"path\":\"src/main.java\",\"message\":\"File requires formatting\",\"diff\":\"--- a/src/main.java\\n+++ b/src/main.java\\n@@ -1,4 +1,4 @@\\n-Line 1\\n-Line 2\\n-Dirty line\\n-Line 4\\n+Line 1\\n+Line 2\\n+Clean line\\n+Line 4\\n\"}}"); | ||
} | ||
|
||
@Test | ||
public void lintsEmpty() { | ||
List<FormatterStep> steps = new ArrayList<>(); | ||
List<List<Lint>> lintsPerStep = new ArrayList<>(); | ||
|
||
String result = ReviewDogGenerator.rdjsonlLintsFromSteps("test.txt", steps, lintsPerStep); | ||
Selfie.expectSelfie(result).toBe(""); | ||
} | ||
|
||
@Test | ||
public void lintsSingleIssue() { | ||
FormatterStep step = FormatterStep.create( | ||
"testStep", | ||
"formatter-state", | ||
state -> rawUnix -> rawUnix); | ||
List<FormatterStep> steps = Collections.singletonList(step); | ||
|
||
Lint lint = Lint.atLine(1, "TEST001", "Test lint message"); | ||
List<List<Lint>> lintsPerStep = Collections.singletonList(Collections.singletonList(lint)); | ||
|
||
String result = ReviewDogGenerator.rdjsonlLintsFromSteps("src/main.java", steps, lintsPerStep); | ||
Selfie.expectSelfie(result).toBe("{\"source\":\"spotless\",\"code\":\"testStep\",\"level\":\"warning\",\"message\":\"Test lint message\",\"path\":\"src/main.java\",\"line\":1,\"column\":1}"); | ||
} | ||
|
||
@Test | ||
public void lintsMultipleIssues() { | ||
FormatterStep step1 = new FormatterStep() { | ||
@Override | ||
public String getName() { | ||
return "step1"; | ||
} | ||
|
||
@Override | ||
public String format(String rawUnix, File file) { | ||
return rawUnix; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
}; | ||
|
||
FormatterStep step2 = FormatterStep.create( | ||
"step2", | ||
"formatter-state", | ||
state -> rawUnix -> rawUnix); | ||
|
||
List<FormatterStep> steps = Arrays.asList(step1, step2); | ||
|
||
Lint lint1 = Lint.atLine(1, "RULE1", "First issue"); | ||
Lint lint2 = Lint.atLine(5, "RULE2", "Second issue"); | ||
|
||
List<List<Lint>> lintsPerStep = Arrays.asList( | ||
Collections.singletonList(lint1), | ||
Collections.singletonList(lint2)); | ||
|
||
String result = ReviewDogGenerator.rdjsonlLintsFromSteps("src/main.java", steps, lintsPerStep); | ||
Selfie.expectSelfie(result).toBe("{\"source\":\"spotless\",\"code\":\"step1\",\"level\":\"warning\",\"message\":\"First issue\",\"path\":\"src/main.java\",\"line\":1,\"column\":1}", | ||
"{\"source\":\"spotless\",\"code\":\"step2\",\"level\":\"warning\",\"message\":\"Second issue\",\"path\":\"src/main.java\",\"line\":5,\"column\":1}"); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.