Skip to content

Commit

Permalink
Merge pull request #331 from uhafner/markdown-description
Browse files Browse the repository at this point in the history
Add markdown details as additional parameter to create source code annotations
  • Loading branch information
uhafner authored Mar 23, 2024
2 parents d65a1b6 + 8d37cb5 commit f8ef0a6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
40 changes: 31 additions & 9 deletions src/main/java/edu/hm/hafner/grading/CommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.registry.ParserRegistry;
import edu.hm.hafner.coverage.FileNode;
import edu.hm.hafner.coverage.Metric;
import edu.hm.hafner.coverage.Mutation;
Expand Down Expand Up @@ -77,22 +78,32 @@ public void createAnnotations(final AggregatedScore score) {
* @param lineEnd
* end line of the comment
* @param message
* message of the comment
* plain text message of the comment
* @param title
* title of the comment
* plain text title of the comment
* @param columnStart
* column of the comment (-1 if not applicable)
* @param columnEnd
* column of the comment (-1 if not applicable)
* @param details
* additional details of the comment (empty if not applicable)
* additional plain text details of the comment (empty if not applicable)
* @param markDownDetails
* additional details of the comment in Markdown (empty if not applicable)
*/
@SuppressWarnings("checkstyle:ParameterNumber")
protected abstract void createComment(CommentType commentType, String relativePath,
int lineStart, int lineEnd,
String message, String title,
int columnStart, int columnEnd,
String details);
String details, String markDownDetails);

private void createComment(final CommentType commentType, final String relativePath,
final int lineStart, final int lineEnd,
final String message, final String title) {
createComment(commentType, relativePath, lineStart, lineEnd, message, title,
NO_COLUMN, NO_COLUMN,
NO_ADDITIONAL_DETAILS, NO_ADDITIONAL_DETAILS);
}

private Set<String> extractAdditionalSourcePaths(final List<? extends Score<?, ?>> scores) {
return scores.stream()
Expand All @@ -111,9 +122,19 @@ private void createAnnotationForIssue(final Issue issue,
final Set<String> sourcePaths) {
var relativePath = cleanPath(createRelativeRepositoryPath(issue.getFileName(), sourcePaths));

var text = getDescription(issue);

createComment(CommentType.WARNING, relativePath, issue.getLineStart(), issue.getLineEnd(),
issue.getMessage(), issue.getOriginName() + ": " + issue.getType(), issue.getColumnStart(),
issue.getColumnEnd(), NO_ADDITIONAL_DETAILS);
issue.getColumnEnd(), NO_ADDITIONAL_DETAILS, text);
}

private String getDescription(final Issue issue) {
var parserRegistry = new ParserRegistry();
if (parserRegistry.contains(issue.getOrigin())) {
return parserRegistry.get(issue.getOrigin()).getDescription(issue);
}
return issue.getDescription();
}

private String cleanPath(final String path) {
Expand Down Expand Up @@ -141,8 +162,7 @@ private void createAnnotationForMissedLineRange(final FileNode file, final LineR
createComment(CommentType.NO_COVERAGE,
relativePath, range.getStart(),
range.getEnd(), getMissedLinesDescription(range),
getMissedLinesMessage(range), NO_COLUMN,
NO_COLUMN, NO_ADDITIONAL_DETAILS);
getMissedLinesMessage(range));
}

private String getMissedLinesMessage(final LineRange range) {
Expand Down Expand Up @@ -177,7 +197,7 @@ private void createAnnotationForMissedBranches(final FileNode file,
createComment(CommentType.PARTIAL_COVERAGE,
createRelativeRepositoryPath(file.getRelativePath(), sourcePaths), branchCoverage.getKey(),
branchCoverage.getKey(), createBranchMessage(branchCoverage.getKey(), branchCoverage.getValue()),
"Partially covered line", NO_COLUMN, NO_COLUMN, NO_ADDITIONAL_DETAILS);
"Partially covered line");
}

private String createBranchMessage(final int line, final int missed) {
Expand Down Expand Up @@ -216,11 +236,13 @@ private void createAnnotationsForSurvivedMutations(final FileNode file,
private void createAnnotationForSurvivedMutation(final FileNode file,
final Entry<Integer, List<Mutation>> mutationsPerLine,
final Set<String> sourcePaths) {
var mutationDetails = createMutationDetails(mutationsPerLine.getValue());
createComment(CommentType.MUTATION_SURVIVED,
createRelativeRepositoryPath(file.getRelativePath(), sourcePaths), mutationsPerLine.getKey(),
mutationsPerLine.getKey(),
createMutationMessage(mutationsPerLine.getKey(), mutationsPerLine.getValue()),
"Mutation survived", NO_COLUMN, NO_COLUMN, createMutationDetails(mutationsPerLine.getValue()));
"Mutation survived", NO_COLUMN, NO_COLUMN,
mutationDetails, mutationDetails);
}

private String createMutationMessage(final int line, final List<Mutation> survived) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/GradingReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public String getMarkdownDetails(final AggregatedScore score, final String title

private String createMarkdownTotal(final AggregatedScore score, final String title, final int size) {
if (score.getMaxScore() == 0) {
return "#".repeat(size) + " :sunny: " + title;
return "#".repeat(size) + " :sunny: &nbsp; " + title;
}
return "#".repeat(size) + " :mortar_board: " + createTotal(score, title);
return "#".repeat(size) + " :mortar_board: &nbsp; " + createTotal(score, title);
}

private String createTotal(final AggregatedScore score, final String title) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/ScoreMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public String createSummary(final AggregatedScore aggregation) {

protected String getTitle(final S score, final int size) {
return "#".repeat(size)
+ String.format(" :%s: %s", getIcon(score), score.getName())
+ String.format(" :%s: &nbsp; %s", getIcon(score), score.getName())
+ createScoreTitle(score);
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/edu/hm/hafner/grading/AnalysisMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void shouldShowScoreWithOneResult() {
var analysisMarkdown = new AnalysisMarkdown();

assertThat(analysisMarkdown.createSummary(score)).startsWith(
"- :warning: CheckStyle - 70 of 100: 10 warnings found (1 error, 2 high, 3 normal, 4 low)");
"- :warning: &nbsp; CheckStyle - 70 of 100: 10 warnings found (1 error, 2 high, 3 normal, 4 low)");
assertThat(analysisMarkdown.createDetails(score))
.contains("CheckStyle - 70 of 100")
.contains("|CheckStyle 1|1|1|2|3|4|10|-30")
Expand Down Expand Up @@ -124,7 +124,7 @@ void shouldShowScoreWithTwoSubResults() {
var analysisMarkdown = new AnalysisMarkdown();

assertThat(analysisMarkdown.createSummary(score))
.startsWith("- :warning: CheckStyle - 50 of 100: 20 warnings found (5 errors, 5 high, 5 normal, 5 low)");
.startsWith("- :warning: &nbsp; CheckStyle - 50 of 100: 20 warnings found (5 errors, 5 high, 5 normal, 5 low)");
assertThat(analysisMarkdown.createDetails(score))
.contains("CheckStyle - 50 of 100",
"|CheckStyle|1|1|2|3|4|10|-30",
Expand Down Expand Up @@ -159,7 +159,7 @@ void shouldShowNoImpactsWithTwoSubResults() {
var analysisMarkdown = new AnalysisMarkdown();

assertThat(analysisMarkdown.createSummary(score))
.startsWith("- :warning: CheckStyle: 20 warnings found (5 errors, 5 high, 5 normal, 5 low)");
.startsWith("- :warning: &nbsp; CheckStyle: 20 warnings found (5 errors, 5 high, 5 normal, 5 low)");
assertThat(analysisMarkdown.createDetails(score))
.contains("CheckStyle",
"|CheckStyle|1|1|2|3|4|10",
Expand Down Expand Up @@ -213,8 +213,8 @@ void shouldShowScoreWithTwoResults() {
":moneybag:|:heavy_minus_sign:|*1*|*2*|*3*|*4*|:heavy_minus_sign:|:heavy_minus_sign:",
":moneybag:|:heavy_minus_sign:|*-11*|*-12*|*-13*|*-14*|:heavy_minus_sign:|:heavy_minus_sign:");
assertThat(analysisMarkdown.createSummary(score))
.contains("- :warning: Style - 60 of 100: 20 warnings found (2 errors, 4 high, 6 normal, 8 low)",
"- :warning: Bugs - 0 of 100: 20 warnings found (8 errors, 6 high, 4 normal, 2 low)")
.contains("- :warning: &nbsp; Style - 60 of 100: 20 warnings found (2 errors, 4 high, 6 normal, 8 low)",
"- :warning: &nbsp; Bugs - 0 of 100: 20 warnings found (8 errors, 6 high, 4 normal, 2 low)")
.doesNotContain("Total");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public List<String> getComments() {
@Override @SuppressWarnings("checkstyle:ParameterNumber")
protected void createComment(final CommentType commentType, final String relativePath, final int lineStart, final int lineEnd,
final String message, final String title,
final int columnStart, final int columnEnd, final String details) {
final int columnStart, final int columnEnd, final String details, final String markDownDetails) {
comments.add(String.format("[%s] %s:%d-%d: %s (%s)", commentType.name(), relativePath, lineStart, lineEnd, message, title));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/edu/hm/hafner/grading/CoverageMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void shouldShowMaximumScore() {
.contains("Code Coverage - 100 of 100", "|JaCoCo|100|0|100", IMPACT_CONFIGURATION)
.doesNotContain("Total");
assertThat(codeCoverageMarkdown.createSummary(score))
.startsWith("- :footprints: Code Coverage - 100 of 100: 100% coverage achieved");
.startsWith("- :footprints: &nbsp; Code Coverage - 100 of 100: 100% coverage achieved");

assertThat(new MutationCoverageMarkdown().createDetails(score)).contains(
"Mutation Coverage Score: not enabled");
Expand Down Expand Up @@ -97,7 +97,7 @@ void shouldShowScoreWithOneResult() {
.contains("Code Coverage - 20 of 100", "|JaCoCo|60|40|20", IMPACT_CONFIGURATION)
.doesNotContain("Total");
assertThat(codeCoverageMarkdown.createSummary(score))
.startsWith("- :footprints: Code Coverage - 20 of 100: 60% coverage achieved");
.startsWith("- :footprints: &nbsp; Code Coverage - 20 of 100: 60% coverage achieved");
assertThat(new MutationCoverageMarkdown().createDetails(score)).contains(
"Mutation Coverage Score: not enabled");
}
Expand Down Expand Up @@ -146,7 +146,7 @@ void shouldShowScoreWithTwoSubResults() {
"|**Total Ø**|**70**|**30**|**40**",
IMPACT_CONFIGURATION);
assertThat(codeCoverageMarkdown.createSummary(score)).startsWith(
"- :footprints: Code Coverage - 40 of 100: 70% coverage achieved");
"- :footprints: &nbsp; Code Coverage - 40 of 100: 70% coverage achieved");
assertThat(new MutationCoverageMarkdown().createDetails(score)).contains(
"Mutation Coverage Score: not enabled");
}
Expand Down Expand Up @@ -186,7 +186,7 @@ void shouldShowNoImpactsWithTwoSubResults() {
.doesNotContain(IMPACT_CONFIGURATION)
.doesNotContain("Impact");
assertThat(codeCoverageMarkdown.createSummary(score)).startsWith(
"- :footprints: Code Coverage: 70% coverage achieved");
"- :footprints: &nbsp; Code Coverage: 70% coverage achieved");
assertThat(new MutationCoverageMarkdown().createDetails(score)).contains(
"Mutation Coverage Score: not enabled");
}
Expand Down Expand Up @@ -246,14 +246,14 @@ void shouldShowScoreWithTwoResults() {
IMPACT_CONFIGURATION)
.doesNotContain("Mutation Coverage", "PIT");
assertThat(codeCoverageMarkdown.createSummary(score)).startsWith(
"- :footprints: JaCoCo - 40 of 100: 70% coverage achieved");
"- :footprints: &nbsp; JaCoCo - 40 of 100: 70% coverage achieved");

var mutationCoverageMarkdown = new MutationCoverageMarkdown();
assertThat(mutationCoverageMarkdown.createDetails(score)).contains(
"PIT - 20 of 100", IMPACT_CONFIGURATION)
.doesNotContain("JaCoCo", "Line Coverage", "Branch Coverage", "Total");
assertThat(mutationCoverageMarkdown.createSummary(score)).contains(
"- :microscope: PIT - 20 of 100: 60% mutations killed");
"- :microscope: &nbsp; PIT - 20 of 100: 60% mutations killed");
}

static ModuleNode createTwoReports(final ToolConfiguration tool) {
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/edu/hm/hafner/grading/GradingReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void shouldCreateAllResults() {
assertThat(results.getMarkdownSummary(score, "Summary")).contains(
"img title=\"Score percentage: 33%\"",
"percentages/033.svg",
"# :mortar_board: Summary - 167 of 500",
"# :mortar_board: &nbsp; Summary - 167 of 500",
"JUnit - 77 of 100",
"14 tests failed, 5 passed, 3 skipped",
"JaCoCo - 40 of 100",
Expand All @@ -130,10 +130,10 @@ void shouldCreateAllResults() {
"title=\"PIT: 20%\"",
"Style - 30 of 100",
"title=\"Style: 30%\"",
":warning: Style",
":warning: &nbsp; Style",
"Bugs - 0 of 100",
"title=\"Bugs: 0%\"",
":bug: Bugs");
":bug: &nbsp; Bugs");
}

@Test
Expand Down Expand Up @@ -203,18 +203,18 @@ void shouldSkipScores() {
entry("spotbugs", 10));

assertThat(results.getMarkdownSummary(aggregation, "Summary")).contains(
"### :sunny: Summary",
"- :vertical_traffic_light: JUnit: 14 tests failed, 5 passed, 3 skipped",
"- :footprints: JaCoCo: 70% coverage achieved",
"- :microscope: PIT: 60% mutations killed",
"- :warning: Style: 10 warnings found (1 error, 2 high, 3 normal, 4 low)",
"- :bug: Bugs: 10 warnings found (4 errors, 3 high, 2 normal, 1 low)");
"### :sunny: &nbsp; Summary",
"- :vertical_traffic_light: &nbsp; JUnit: 14 tests failed, 5 passed, 3 skipped",
"- :footprints: &nbsp; JaCoCo: 70% coverage achieved",
"- :microscope: &nbsp; PIT: 60% mutations killed",
"- :warning: &nbsp; Style: 10 warnings found (1 error, 2 high, 3 normal, 4 low)",
"- :bug: &nbsp; Bugs: 10 warnings found (4 errors, 3 high, 2 normal, 1 low)");
assertThat(results.getTextSummary(aggregation)).isEqualTo(
"Autograding score");
assertThat(results.getTextSummary(aggregation, "Quality Summary")).isEqualTo(
"Quality Summary");
assertThat(results.getMarkdownDetails(aggregation, "Quality Summary")).contains(
"# :sunny: Quality Summary",
"# :sunny: &nbsp; Quality Summary",
"JUnit",
"JaCoCo",
"title=\"JaCoCo: 70%\"",
Expand Down

0 comments on commit f8ef0a6

Please sign in to comment.