Skip to content

Commit

Permalink
fix: resolve issue on first scan when the main branch values are not …
Browse files Browse the repository at this point in the history
…existing
  • Loading branch information
kronenthaler committed Mar 22, 2023
1 parent 0e683dd commit 9702e2e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.kronenthaler.sonarqube.plugins.coveragevariation</groupId>
<artifactId>sonar-coveragevariation-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>3.0.3</version>
<version>3.0.4</version>

<name>Coverage Variation Plugin for SonarQube 9.x</name>
<description>Plugin calculates the variation of the coverage between the current scan and the main branch's coverage</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,29 @@ public void execute(SensorContext context) {
}

private Double currentCoverage(SensorContext context) throws NoSuchElementException, IOException {
String branch = defaultBranch(context);
log.info("Main Branch: " + branch);

Configuration configs = context.config();
String componentKey = configs.get(org.sonar.api.CoreProperties.PROJECT_KEY_PROPERTY).orElseThrow();
String branch = defaultBranch(context);
log.info("Main Branch: " + branch);

HashMap<String, String> params = new HashMap<>();
params.put("component", componentKey);
params.put("metricKeys", "coverage");
params.put("branch", branch);
Configuration configs = context.config();
String componentKey = configs.get(org.sonar.api.CoreProperties.PROJECT_KEY_PROPERTY).orElseThrow();

SonarServerApi api = new SonarServerApi(context.config().get("sonar.host.url").orElseThrow(), SonarServerApi.Endpoint.MEASURES, params);
HashMap<String, String> params = new HashMap<>();
params.put("component", componentKey);
params.put("metricKeys", "coverage");
params.put("branch", branch);

HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader(configs));
SonarServerApi api = new SonarServerApi(context.config().get("sonar.host.url").orElseThrow(), SonarServerApi.Endpoint.MEASURES, params);

SonarMeasure measure = api.connect(headers, SonarMeasure.class);
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader(configs));

return Double.parseDouble(measure.getComponent().getMeasures()[0].getValue());
SonarMeasure measure = api.connect(headers, SonarMeasure.class);
try {
return Double.parseDouble(measure.getComponent().getMeasures()[0].getValue());
} catch (ArrayIndexOutOfBoundsException e){
return 0.0;
}
}

private String defaultBranch(SensorContext context) throws NoSuchElementException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class PreviousCoverageSensorTests {

@Test
public void testExecuteWithErrorOnBranchRetrieval() {
System.err.println(basePath);

Configuration configs = mock(Configuration.class);
when(configs.get("sonar.host.url")).thenReturn(Optional.of(baseUrl + "failed/"));
when(configs.get(CoreProperties.PROJECT_KEY_PROPERTY)).thenReturn(Optional.of("list-project"));
Expand Down Expand Up @@ -91,7 +89,8 @@ public void testExecuteWithErrorOnCoverageRetrieval() {
PreviousCoverageSensor sensor = new PreviousCoverageSensor();
sensor.execute(context);
}
verify(context, never()).newMeasure();

verify(context, times(1)).newMeasure();
}

@Test
Expand Down Expand Up @@ -142,6 +141,54 @@ public void testExecuteSuccessCreateMetric() throws Exception {
assertEquals((Double) 38.5, valueCapture.getValue());
}

@Test
public void testExecuteSuccessCreateMetricFirstScan() throws Exception {
Configuration configs = mock(Configuration.class);
when(configs.get("sonar.host.url")).thenReturn(Optional.of(baseUrl + "first_scan/"));
when(configs.get(CoreProperties.PROJECT_KEY_PROPERTY)).thenReturn(Optional.of("list-project"));
when(configs.get(CoreProperties.LOGIN)).thenReturn(Optional.of("user"));
when(configs.get(CoreProperties.PASSWORD)).thenReturn(Optional.of("123456"));
when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(true));

InputProject project = mock(InputProject.class);

ArgumentCaptor<Metric> metricCapture = ArgumentCaptor.forClass(Metric.class);
ArgumentCaptor<Double> valueCapture = ArgumentCaptor.forClass(Double.class);
ArgumentCaptor<InputComponent> componentCapture = ArgumentCaptor.forClass(InputComponent.class);

NewMeasure newMeasure = mock(NewMeasure.class);

SensorContext context = mock(SensorContext.class, RETURNS_DEEP_STUBS);
when(context.config()).thenReturn(configs);
when(context.project()).thenReturn(project);
when(context.newMeasure()
.forMetric(metricCapture.capture())
.on(componentCapture.capture())
.withValue(valueCapture.capture())).thenReturn(newMeasure);
doNothing().when(newMeasure).save();

try (MockedConstruction<SonarServerApi> server = Mockito.mockConstruction(SonarServerApi.class, (mock, mockingContext) -> {
Gson gson = new Gson();

String branches = new String(new BufferedInputStream(new FileInputStream(basePath + "first_scan/" + SonarServerApi.Endpoint.PROJECT_BRANCHES.path)).readAllBytes());
when(mock.connect(any(), eq(SonarProjectBranches.class)))
.thenReturn(gson.fromJson(branches, SonarProjectBranches.class));

String measure = new String(new BufferedInputStream(new FileInputStream(basePath + "first_scan/" + SonarServerApi.Endpoint.MEASURES.path)).readAllBytes());
when(mock.connect(any(), eq(SonarMeasure.class)))
.thenReturn(gson.fromJson(measure, SonarMeasure.class));

})) {
PreviousCoverageSensor sensor = new PreviousCoverageSensor();
sensor.execute(context);
}

verify(newMeasure, times(1)).save();
assertEquals(CoverageVariationMetrics.PREVIOUS_COVERAGE, metricCapture.getValue());
assertEquals(project, componentCapture.getValue());
assertEquals((Double) 0.0, valueCapture.getValue());
}

@Test
public void testExecuteWithSensorDisabled() {
System.err.println(basePath);
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/first_scan/api/measures/component
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"component": {
"key": "test-project",
"name": "test-project",
"qualifier": "TRK",
"measures": []
}
}
14 changes: 14 additions & 0 deletions src/test/resources/first_scan/api/project_branches/list
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"branches": [
{
"name": "master",
"isMain": true,
"type": "BRANCH",
"status": {
"qualityGateStatus": "OK"
},
"analysisDate": "2021-12-29T15:03:31+0000",
"excludedFromPurge": true
}
]
}

0 comments on commit 9702e2e

Please sign in to comment.