]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20665 add response field qualityProfile to projectanalysis/search response
authorMatteo Mara <matteo.mara@sonarsource.com>
Thu, 12 Oct 2023 15:27:16 +0000 (17:27 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 18 Oct 2023 20:03:04 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchResponseBuilder.java
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/projectanalysis/ws/search-example.json
sonar-ws/src/main/protobuf/ws-projectanalyses.proto

index 4dda87318fef013bdb20cb1b62adbcd31d1da67c..6bbc61b3c25642c02fd7aa8eefedda87d373797f 100644 (file)
@@ -160,9 +160,13 @@ public class SearchActionIT {
       .setName("6.3").setCategory(VERSION.getLabel()));
     db.events().insertEvent(newEvent(a2).setUuid("E21")
       .setName("Quality Profile changed to Sonar Way")
+        .setData("key=Profile Key;languageKey=The Key of the Language;name=Profile Name")
       .setCategory(EventCategory.QUALITY_PROFILE.getLabel()));
     db.events().insertEvent(newEvent(a2).setUuid("E22")
       .setName("6.3").setCategory(OTHER.getLabel()));
+    db.events().insertEvent(newEvent(a2).setUuid("E23")
+      .setName("Use 'Sonar Way update profile' (JavaScript)")
+      .setCategory(EventCategory.QUALITY_PROFILE.getLabel()));
 
     EventDto eventDto = db.events().insertEvent(newEvent(a3)
       .setUuid("E31")
index ce344a847aa5571ae21bae53df2573175ebd17a5..609c96c9e3e8ec3486e42e01500534d434587935 100644 (file)
@@ -83,7 +83,8 @@ public class SearchAction implements ProjectAnalysesWsAction {
       .setSince("6.3")
       .setResponseExample(getClass().getResource("search-example.json"))
       .setChangelog(
-        new Change("9.0", "Add field response 'detectedCI'"),
+        new Change("10.3", "Add response field 'qualityProfile' for events related to quality profile changes"),
+        new Change("9.0", "Add response field 'detectedCI'"),
         new Change("7.5", "Add QualityGate information on Applications"))
       .setHandler(this);
 
index 83d2da299ece615eae6f0bb6a10eb118af71a1a7..1afd3f3f0b8410bed094b9a8b89b6c065d9ee12b 100644 (file)
@@ -25,11 +25,13 @@ import com.google.gson.JsonSyntaxException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.event.EventComponentChangeDto;
 import org.sonar.db.event.EventDto;
@@ -37,6 +39,7 @@ import org.sonarqube.ws.ProjectAnalyses;
 import org.sonarqube.ws.ProjectAnalyses.Analysis;
 import org.sonarqube.ws.ProjectAnalyses.Event;
 import org.sonarqube.ws.ProjectAnalyses.QualityGate;
+import org.sonarqube.ws.ProjectAnalyses.QualityProfile;
 import org.sonarqube.ws.ProjectAnalyses.SearchResponse;
 
 import static java.lang.String.format;
@@ -52,12 +55,14 @@ class SearchResponseBuilder {
   private final Event.Builder wsEvent;
   private final SearchData searchData;
   private final QualityGate.Builder wsQualityGate;
+  private final QualityProfile.Builder wsQualityProfile;
   private final ProjectAnalyses.DefinitionChange.Builder wsDefinitionChange;
 
   SearchResponseBuilder(SearchData searchData) {
     this.wsAnalysis = Analysis.newBuilder();
     this.wsEvent = Event.newBuilder();
     this.wsQualityGate = QualityGate.newBuilder();
+    this.wsQualityProfile = QualityProfile.newBuilder();
     this.wsDefinitionChange = ProjectAnalyses.DefinitionChange.newBuilder();
     this.searchData = searchData;
   }
@@ -102,22 +107,33 @@ class SearchResponseBuilder {
     wsEvent.clear().setKey(dbEvent.getUuid());
     ofNullable(dbEvent.getName()).ifPresent(wsEvent::setName);
     ofNullable(dbEvent.getDescription()).ifPresent(wsEvent::setDescription);
-    ofNullable(dbEvent.getCategory()).ifPresent(cat -> wsEvent.setCategory(fromLabel(cat).name()));
-    if (dbEvent.getCategory() != null) {
-      switch (EventCategory.fromLabel(dbEvent.getCategory())) {
-        case DEFINITION_CHANGE:
-          addDefinitionChange(dbEvent);
-          break;
-        case QUALITY_GATE:
-          addQualityGateInformation(dbEvent);
-          break;
-        default:
-          break;
-      }
-    }
+    ofNullable(dbEvent.getCategory())
+      .ifPresent(cat -> {
+        wsEvent.setCategory(fromLabel(cat).name());
+        switch (fromLabel(cat)) {
+          case DEFINITION_CHANGE -> addDefinitionChange(dbEvent);
+          case QUALITY_GATE -> addQualityGateInformation(dbEvent);
+          case QUALITY_PROFILE -> addQualityProfileInformation(dbEvent);
+          default -> {
+            //Nothing to do if not one of the previous cases
+          }
+        }
+      });
     return wsEvent;
   }
 
+  private void addQualityProfileInformation(EventDto event) {
+    wsQualityProfile.clear();
+
+    Map<String, String> data = KeyValueFormat.parse(event.getData());
+
+    Optional.ofNullable(data.get("key")).ifPresent(wsQualityProfile::setKey);
+    Optional.ofNullable(data.get("name")).ifPresent(wsQualityProfile::setName);
+    Optional.ofNullable(data.get("languageKey")).ifPresent(wsQualityProfile::setLanguageKey);
+
+    wsEvent.setQualityProfile(wsQualityProfile.build());
+  }
+
   private void addQualityGateInformation(EventDto event) {
     wsQualityGate.clear();
     List<EventComponentChangeDto> eventComponentChangeDtos = searchData.componentChangesByEventUuid.get(event.getUuid());
index 27fa8cfadf9dbe7b0ee38503e5f7860e522c0c94..c7edc4357a657e8075d664222e1e56c03c2a885e 100644 (file)
         {
           "key": "E21",
           "category": "QUALITY_PROFILE",
-          "name": "Quality Profile changed to Sonar Way"
+          "name": "Quality Profile changed to Sonar Way",
+          "qualityProfile": {
+            "key": "Profile Key",
+            "name": "Profile Name",
+            "languageKey": "The Key of the Language"
+          }
         },
         {
           "key": "E22",
           "category": "OTHER",
           "name": "6.3"
+        },
+        {
+          "key": "E23",
+          "category": "QUALITY_PROFILE",
+          "name": "Use 'Sonar Way update profile' (JavaScript)"
         }
       ]
     },
index c435380d59ab782702210b902cb39c0e325a78e0..bbcffc82c91096d3e5f7f9ce3caf1e6e2d404a95 100644 (file)
@@ -50,6 +50,7 @@ message Event {
   optional string description = 5;
   optional QualityGate qualityGate = 6;
   optional DefinitionChange definitionChange = 7;
+  optional QualityProfile qualityProfile = 8;
 }
 
 message Analysis {
@@ -69,6 +70,12 @@ message QualityGate {
   repeated Failing failing = 3;
 }
 
+message QualityProfile {
+  optional string key = 1;
+  optional string name = 2;
+  optional string languageKey = 3;
+}
+
 message Failing {
   optional string key = 1;
   optional string name = 2;