diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2020-01-30 17:51:09 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2020-02-11 20:46:10 +0100 |
commit | 49c4ddbfef30767bdf9d6c145cdf972a03ac8c5b (patch) | |
tree | 96cfea759617a6a56ca4be1aa5ed12edaf465e66 /server/sonar-server-common | |
parent | 1d360ec35242f8f7b4a678881ca58a39a98a2fa1 (diff) | |
download | sonarqube-49c4ddbfef30767bdf9d6c145cdf972a03ac8c5b.tar.gz sonarqube-49c4ddbfef30767bdf9d6c145cdf972a03ac8c5b.zip |
SONAR-12962 Compute new Security Review measures on Projects
Diffstat (limited to 'server/sonar-server-common')
2 files changed, 39 insertions, 8 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java b/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java index d0877931b41..6a5d2673eb3 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java @@ -21,27 +21,58 @@ package org.sonar.server.security; import org.sonar.server.measure.Rating; +import static org.sonar.server.measure.Rating.A; +import static org.sonar.server.measure.Rating.B; +import static org.sonar.server.measure.Rating.C; +import static org.sonar.server.measure.Rating.D; +import static org.sonar.server.measure.Rating.E; + public class SecurityReviewRating { private SecurityReviewRating() { // Only static method } - public static Rating compute(int ncloc, int securityHotspots) { + /** + * This code will be removed when updating computation of Security Review Rating for portfolios + */ + @Deprecated + public static Rating computeForPortfolios(int ncloc, int securityHotspots) { if (ncloc == 0) { - return Rating.A; + return A; } double ratio = (double) securityHotspots * 1000d / (double) ncloc; if (ratio <= 3d) { - return Rating.A; + return A; } else if (ratio <= 10) { - return Rating.B; + return B; } else if (ratio <= 15) { - return Rating.C; + return C; } else if (ratio <= 25) { - return Rating.D; + return D; } else { - return Rating.E; + return E; + } + } + + public static Double computePercent(long hotspotsToReview, long hotspotsReviewed) { + long total = hotspotsToReview + hotspotsReviewed; + if (total == 0) { + return 100.0; + } + return hotspotsReviewed * 100.0 / total; + } + + public static Rating computeRating(Double percent) { + if (percent >= 80.0) { + return A; + } else if (percent >= 70.0) { + return B; + } else if (percent >= 50.0) { + return C; + } else if (percent >= 30.0) { + return D; } + return E; } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java index 0a0b8d64c8d..cdb276aba16 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java @@ -62,7 +62,7 @@ public class SecurityReviewRatingTest { @Test @UseDataProvider("values") public void compute_security_review_rating_on_project(int ncloc, int securityHotspots, Rating expectedRating) { - assertThat(SecurityReviewRating.compute(ncloc, securityHotspots)).isEqualTo(expectedRating); + assertThat(SecurityReviewRating.computeForPortfolios(ncloc, securityHotspots)).isEqualTo(expectedRating); } } |