summaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-02-25 22:16:51 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-02-25 22:18:59 +0100
commit8bc87d944472275389238e2b5d5beefa4214e746 (patch)
treec2c61c8ba5a6aeb13367776d9ff5c17d49386d67 /sonar-server
parent567b62a799271d1479417c5358fe52b75fd015ae (diff)
downloadsonarqube-8bc87d944472275389238e2b5d5beefa4214e746.tar.gz
sonarqube-8bc87d944472275389238e2b5d5beefa4214e746.zip
SONAR-4366 Add naive implementation of WS to list "alertable" metrics
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java19
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java25
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java2
3 files changed, 44 insertions, 2 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
index d58cd56b528..2c3a70de396 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
@@ -19,6 +19,10 @@
*/
package org.sonar.server.qualitygate;
+import com.google.common.base.Predicate;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.collect.Lists;
@@ -180,6 +184,15 @@ public class QualityGates {
propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, projectId);
}
+ public Collection<Metric> gateMetrics() {
+ return Collections2.filter(metricFinder.findAll(), new Predicate<Metric>() {
+ @Override
+ public boolean apply(Metric metric) {
+ return isAlertable(metric);
+ }
+ });
+ }
+
private void validateCondition(Metric metric, String operator, String warningThreshold, String errorThreshold, Integer period) {
List<Message> validationMessages = Lists.newArrayList();
validateMetric(metric, validationMessages);
@@ -214,11 +227,15 @@ public class QualityGates {
}
private void validateMetric(Metric metric, List<Message> validationMessages) {
- if (metric.isDataType() || metric.isHidden() || CoreMetrics.ALERT_STATUS.equals(metric) || ValueType.RATING == metric.getType()) {
+ if (!isAlertable(metric)) {
validationMessages.add(Message.of(String.format("Metric '%s' cannot be used to define a condition.", metric.getKey())));
}
}
+ private boolean isAlertable(Metric metric) {
+ return !metric.isDataType() && !metric.isHidden() && !CoreMetrics.ALERT_STATUS.equals(metric) && ValueType.RATING != metric.getType();
+ }
+
private boolean isDefault(QualityGateDto qGate) {
return qGate.getId().equals(getDefaultId());
}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
index 68d8885276a..87069137dde 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
@@ -19,6 +19,8 @@
*/
package org.sonar.server.qualitygate.ws;
+import org.sonar.api.measures.Metric;
+
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
@@ -174,6 +176,15 @@ public class QualityGatesWs implements WebService {
}
}).newParam(PARAM_ID).setDescription("The ID of the quality gate.");
+ controller.newAction("metrics")
+ .setDescription("List metrics available for definition of conditions.")
+ .setHandler(new RequestHandler() {
+ @Override
+ public void handle(Request request, Response response) {
+ listMetrics(request, response);
+ }
+ });
+
controller.newAction("destroy")
.setDescription("Destroy a quality gate, given its id.")
.setPost(true)
@@ -221,6 +232,20 @@ public class QualityGatesWs implements WebService {
deselect.newParam(PARAM_PROJECT_ID);
}
+ protected void listMetrics(Request request, Response response) {
+ JsonWriter writer = response.newJsonWriter().beginObject().name("metrics").beginArray();
+ for (Metric metric: qualityGates.gateMetrics()) {
+ writer.beginObject()
+ .prop("id", metric.getId())
+ .prop("key", metric.getKey())
+ .prop("name", metric.getName())
+ .prop("type", metric.getType().toString())
+ .prop("domain", metric.getDomain())
+ .endObject();
+ }
+ writer.endArray().endObject().close();
+ }
+
protected void select(Request request, Response response) {
qualityGates.associateProject(parseId(request, PARAM_GATE_ID), parseId(request, PARAM_PROJECT_ID));
response.noContent();
diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java
index c9ce73bfcd5..0fc8b3d047d 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java
@@ -60,7 +60,7 @@ public class QualityGatesWsTest {
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo("api/qualitygates");
assertThat(controller.description()).isNotEmpty();
- assertThat(controller.actions()).hasSize(13);
+ assertThat(controller.actions()).hasSize(14);
WebService.Action list = controller.action("list");
assertThat(list).isNotNull();