Skip to content

Commit

Permalink
feat: add support for authentication with new sonar.token property
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions (on behalf of ignacio-calderon) committed Sep 28, 2023
1 parent eb5662c commit 7b715ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<groupId>com.github.kronenthaler.sonarqube.plugins.coveragevariation</groupId>
<artifactId>sonar-coveragevariation-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>3.0.4</version>
<version>3.1.0</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>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.apiVersion>9.5.0.56709</sonar.apiVersion>
<sonar.apiVersion>10.1.0.73491</sonar.apiVersion>
<jdk.min.version>1.8</jdk.min.version>
<!-- Sonar cloud properties -->
<sonar.organization>kronenthaler</sonar.organization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<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 + "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"));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7b715ae

Please sign in to comment.