Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[D|d]ockerfile
*.[D|d]ockerfile
.dockerignore
*.git
*.md
target
*.log
*.jar
*.war
*.iml
.idea
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# syntax=docker/dockerfile:1

ARG DOCKERHUB_REGISTRY=docker.io

FROM $DOCKERHUB_REGISTRY/maven:3.9.11-eclipse-temurin-17 AS build

WORKDIR /app

COPY . .

RUN mvn clean package

FROM $DOCKERHUB_REGISTRY/openjdk:17-jdk-slim

WORKDIR /app

COPY --from=build /app/target/sonar-cnes-report-*.jar ./sonar-cnes-report.jar

ENTRYPOINT ["java", "-jar", "sonar-cnes-report.jar"]
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.cnes.sonar</groupId>
<artifactId>cnesreport</artifactId>
<version>5.0.2</version>
<version>5.0.3</version>
<packaging>sonar-plugin</packaging>

<name>SonarQube CNES Report</name>
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/requests.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#Number max of results per page
MAX_PER_PAGE_SONARQUBE = 500
# Request to get the list of components and their metrics
GET_COMPONENTS_REQUEST = %s/api/measures/component_tree?component=%s&metricKeys=%s&p=%s&ps=%s&branch=%s
# The qualifiers=FIL parameter ensures we only get file-level components,
# preventing aggregated metrics that would show sum values instead of per-file values
GET_COMPONENTS_REQUEST = %s/api/measures/component_tree?component=%s&metricKeys=%s&p=%s&ps=%s&branch=%s&qualifiers=FIL
# Request to get the list of metrics
GET_MEASURES_REQUEST = %s/api/measures/component?component=%s&metricKeys=%s&branch=%s
# Request for getting a specific project
Expand Down
84 changes: 84 additions & 0 deletions src/test/ut/java/fr/cnes/sonar/report/model/MaxMetricsFixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package fr.cnes.sonar.report.model;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* Test to verify that max metrics are calculated correctly and not showing sum values.
*
* This test addresses the issue where complexity, cognitive_complexity, and ncloc
* metrics were showing the sum of all files instead of the maximum value per file.
* The fix ensures the SonarQube API request includes qualifiers=FIL to get
* file-level metrics rather than aggregated project-level metrics.
*/
public class MaxMetricsFixTest {

@Test
public void testMaxComplexityNotSum() {
// Simulate multiple files with different complexity values
ArrayList<Map<String,String>> componentsTest = new ArrayList<>();
Components components = new Components();

// File 1:
Map<String,String> file1 = new HashMap<>();
file1.put("complexity", "10");
file1.put("cognitive_complexity", "15");
file1.put("ncloc", "100");

// File 2:
// complexity 32 (this should be the max)
// cognitive_complexity 45 (this should be the max)
// ncloc 200 (this should be the max)
Map<String,String> file2 = new HashMap<>();
file2.put("complexity", "32");
file2.put("cognitive_complexity", "45");
file2.put("ncloc", "200");

// File 3:
Map<String,String> file3 = new HashMap<>();
file3.put("complexity", "8");
file3.put("cognitive_complexity", "12");
file3.put("ncloc", "50");

componentsTest.add(file1);
componentsTest.add(file2);
componentsTest.add(file3);
components.setComponentsList(componentsTest);

Map<String, Double> metricStats = components.getMetricStats();

// Test that max values are NOT equal to sum values
double sumComplexity = 10 + 32 + 8; // = 50
double sumCognitive = 15 + 45 + 12; // = 72
double sumNcloc = 100 + 200 + 50; // = 350

// Verify max values are correct (not sums)
assertTrue(metricStats.containsKey("maxcomplexity"), "maxcomplexity should be present");
assertTrue(metricStats.containsKey("maxcognitive_complexity"), "maxcognitive_complexity should be present");
assertTrue(metricStats.containsKey("maxncloc"), "maxncloc should be present");

double maxComplexity = metricStats.get("maxcomplexity");
double maxCognitive = metricStats.get("maxcognitive_complexity");
double maxNcloc = metricStats.get("maxncloc");

// These should be max values, not sums
assertNotEquals(sumComplexity, maxComplexity, "maxcomplexity should not equal sum (would indicate bug)");
assertNotEquals(sumCognitive, maxCognitive, "maxcognitive_complexity should not equal sum (would indicate bug)");
assertNotEquals(sumNcloc, maxNcloc, "maxncloc should not equal sum (would indicate bug)");

// Verify they are actually the correct max values
assertTrue(maxComplexity == 32.0, "maxcomplexity should be 32 (the highest individual file complexity)");
assertTrue(maxCognitive == 45.0, "maxcognitive_complexity should be 45 (the highest individual file cognitive complexity)");
assertTrue(maxNcloc == 200.0, "maxncloc should be 200 (the highest individual file ncloc)");

System.out.println("✓ Fix verified: Max metrics are calculated correctly");
System.out.println(" maxcomplexity: " + maxComplexity + " (expected: 32, sum would be: " + sumComplexity + ")");
System.out.println(" maxcognitive_complexity: " + maxCognitive + " (expected: 45, sum would be: " + sumCognitive + ")");
System.out.println(" maxncloc: " + maxNcloc + " (expected: 200, sum would be: " + sumNcloc + ")");
}
}