diff --git a/pom.xml b/pom.xml index 05bf582..bf2a686 100644 --- a/pom.xml +++ b/pom.xml @@ -5,14 +5,14 @@ com.github.kronenthaler.sonarqube.plugins.coveragevariation sonar-coveragevariation-plugin sonar-plugin - 3.0.4 + 3.1.0 Coverage Variation Plugin for SonarQube 9.x Plugin calculates the variation of the coverage between the current scan and the main branch's coverage UTF-8 - 9.5.0.56709 + 10.1.0.73491 1.8 kronenthaler diff --git a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/PreviousCoverageSensor.java b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/PreviousCoverageSensor.java index 701d02a..fe06391 100644 --- a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/PreviousCoverageSensor.java +++ b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/PreviousCoverageSensor.java @@ -50,7 +50,15 @@ public class PreviousCoverageSensor implements ProjectSensor { private static final Logger log = Loggers.get(PreviousCoverageSensor.class); private static String getAuthorizationHeader(Configuration configs) { - String payload = configs.get(CoreProperties.LOGIN).orElseThrow() + ":" + configs.get(CoreProperties.PASSWORD).orElse(""); + String payload; + if (configs.get(CoreProperties.LOGIN).isPresent()){ + payload = configs.get(CoreProperties.LOGIN).orElseThrow() + ":" + configs.get(CoreProperties.PASSWORD).orElse(""); + log.info("Authenticating with sonar.login + sonar.password (deprecated)"); + } else { + payload = configs.get("sonar.token").orElseThrow() + ":"; + log.info("Authenticating with sonar.token"); + } + return "Basic " + Base64.getEncoder().encodeToString(payload.getBytes()); } diff --git a/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/PreviousCoverageSensorTests.java b/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/PreviousCoverageSensorTests.java index 08e71b3..e83b21c 100644 --- a/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/PreviousCoverageSensorTests.java +++ b/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/PreviousCoverageSensorTests.java @@ -36,8 +36,7 @@ public void testExecuteWithErrorOnBranchRetrieval() { 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")); - when(configs.get(CoreProperties.LOGIN)).thenReturn(Optional.of("user")); - when(configs.get(CoreProperties.PASSWORD)).thenReturn(Optional.of("123456")); + when(configs.get("sonar.token")).thenReturn(Optional.of("squ_123456")); when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(true)); SensorContext context = mock(SensorContext.class); @@ -67,8 +66,7 @@ public void testExecuteWithErrorOnCoverageRetrieval() { .thenReturn(Optional.of(baseUrl + "success/")) // pass first branch check .thenReturn(Optional.of(baseUrl + "failed/")); // fail on the coverage check 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.get("sonar.token")).thenReturn(Optional.of("squ_123456")); when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(true)); SensorContext context = mock(SensorContext.class); @@ -95,6 +93,53 @@ public void testExecuteWithErrorOnCoverageRetrieval() { @Test public void testExecuteSuccessCreateMetric() throws Exception { + Configuration configs = mock(Configuration.class); + when(configs.get("sonar.host.url")).thenReturn(Optional.of(baseUrl + "success/")); + when(configs.get(CoreProperties.PROJECT_KEY_PROPERTY)).thenReturn(Optional.of("list-project")); + when(configs.get("sonar.token")).thenReturn(Optional.of("squ_123456")); + when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(true)); + + InputProject project = mock(InputProject.class); + + ArgumentCaptor metricCapture = ArgumentCaptor.forClass(Metric.class); + ArgumentCaptor valueCapture = ArgumentCaptor.forClass(Double.class); + ArgumentCaptor 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 server = Mockito.mockConstruction(SonarServerApi.class, (mock, mockingContext) -> { + Gson gson = new Gson(); + + String branches = new String(new BufferedInputStream(new FileInputStream(basePath + "success/" + 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 + "success/" + 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) 38.5, valueCapture.getValue()); + } + + @Test + public void testExecuteSuccessCreateMetricWithLoginPassword() throws Exception { Configuration configs = mock(Configuration.class); when(configs.get("sonar.host.url")).thenReturn(Optional.of(baseUrl + "success/")); when(configs.get(CoreProperties.PROJECT_KEY_PROPERTY)).thenReturn(Optional.of("list-project")); @@ -146,8 +191,7 @@ 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.get("sonar.token")).thenReturn(Optional.of("squ_123456")); when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(true)); InputProject project = mock(InputProject.class); @@ -196,8 +240,7 @@ public void testExecuteWithSensorDisabled() { 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")); - when(configs.get(CoreProperties.LOGIN)).thenReturn(Optional.of("user")); - when(configs.get(CoreProperties.PASSWORD)).thenReturn(Optional.of("123456")); + when(configs.get("sonar.token")).thenReturn(Optional.of("squ_123456")); when(configs.getBoolean(CoverageVariationPlugin.COVERAGE_VARIATION_ENABLED_KEY)).thenReturn(Optional.of(false)); SensorContext context = mock(SensorContext.class);