Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 336d619

Browse files
committed
Handle cases where measures are missing
1 parent b8e3a8c commit 336d619

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/BitbucketPullRequestReport.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
1717
import org.sonar.api.ce.posttask.QualityGate;
1818

19+
import java.math.BigDecimal;
1920
import java.time.Instant;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
@@ -98,42 +99,55 @@ BitbucketPullRequestReport setReportData(MeasureResponse response) {
9899
reportData.addAll(Arrays.asList(
99100
ReportData.builder()
100101
.title("Bugs")
101-
.value(new DataValue.Text(measures.get("new_bugs").firstValue().orElse("0")))
102+
.value(new DataValue.Text(Optional.ofNullable(measures.get("new_bugs"))
103+
.flatMap(Measure::firstValue)
104+
.orElse("-")))
102105
.build(),
103106
ReportData.builder()
104107
.title("Code Coverage")
105-
.value(new DataValue.Percentage(measures.get("new_coverage").firstValue()
106-
.map(Double::parseDouble)
107-
.orElse(0d)))
108+
.value(Optional.ofNullable(measures.get("new_coverage"))
109+
.flatMap(Measure::firstValue)
110+
.map(BigDecimal::new)
111+
.map(DataValue.Percentage::new)
112+
.map(DataValue.class::cast)
113+
.orElseGet(() -> new DataValue.Text("-"))
114+
)
108115
.build(),
109116
ReportData.builder()
110117
.title("Vulnerabilities")
111-
.value(new DataValue.Text(measures.get("new_vulnerabilities").firstValue().orElse("0")))
118+
.value(new DataValue.Text(Optional.ofNullable(measures.get("new_vulnerabilities"))
119+
.flatMap(Measure::firstValue)
120+
.orElse("-")))
112121
.build(),
113122
ReportData.builder()
114123
.title("Duplication")
115-
.value(new DataValue.Percentage(measures.get("new_duplicated_lines_density").firstValue()
116-
.map(Double::parseDouble)
117-
.orElse(0d)))
124+
.value(Optional.ofNullable(measures.get("new_duplicated_lines_density"))
125+
.flatMap(Measure::firstValue)
126+
.map(BigDecimal::new)
127+
.map(DataValue.Percentage::new)
128+
.map(DataValue.class::cast)
129+
.orElseGet(() -> new DataValue.Text("-")))
118130
.build(),
119131
ReportData.builder()
120132
.title("Code Smells")
121-
.value(new DataValue.Text(measures.get("new_code_smells").firstValue().orElse("0")))
133+
.value(new DataValue.Text(Optional.ofNullable(measures.get("new_code_smells"))
134+
.flatMap(Measure::firstValue)
135+
.orElse("-")))
122136
.build()
123137
));
124138
}
125139
reportData.add(ReportData.builder()
126-
.title("Details")
127-
.value(DataValue.Link.builder().linktext("Go to SonarQube")
128-
.href(response.getDashboardUrl())
129-
.build())
130-
.build());
140+
.title("Details")
141+
.value(DataValue.Link.builder()
142+
.linktext("Go to SonarQube")
143+
.href(response.getDashboardUrl())
144+
.build())
145+
.build());
131146
this.data = reportData;
132147
return this;
133148
}
134149

135-
136-
public CreateReportRequest toCreateReportRequest() {
150+
CreateReportRequest toCreateReportRequest() {
137151
return CreateReportRequest.builder()
138152
.title("SonarQube")
139153
.vendor("SonarQube")
@@ -180,7 +194,6 @@ private String toBitbucketSeverity(Issue issue) {
180194
}
181195

182196
private String toBitbucketType(String sonarqubeType) {
183-
System.out.println(sonarqubeType);
184197
switch (sonarqubeType) {
185198
case "SECURITY_HOTSPOT":
186199
case "VULNERABILITY":

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/model/DataValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import lombok.EqualsAndHashCode;
77
import lombok.Value;
88

9+
import java.math.BigDecimal;
10+
911
public abstract class DataValue {
1012

1113
@Value
@@ -29,6 +31,6 @@ public static class Text extends DataValue {
2931
@EqualsAndHashCode(callSuper = false)
3032
public static class Percentage extends DataValue {
3133
@JsonValue
32-
Double value; //TODO use a more proper type
34+
BigDecimal value;
3335
}
3436
}

0 commit comments

Comments
 (0)