From 8f82ee4239b476ae2c5d9c0526d679f71cb15239 Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Thu, 12 Oct 2023 17:27:16 +0200 Subject: [PATCH] SONAR-20665 add response field qualityProfile to projectanalysis/search response --- .../projectanalysis/ws/SearchActionIT.java | 4 ++ .../projectanalysis/ws/SearchAction.java | 3 +- .../ws/SearchResponseBuilder.java | 42 +++++++++++++------ .../projectanalysis/ws/search-example.json | 12 +++++- .../main/protobuf/ws-projectanalyses.proto | 7 ++++ 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java index 4dda87318fe..6bbc61b3c25 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java @@ -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") diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java index ce344a847aa..609c96c9e3e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java @@ -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); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchResponseBuilder.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchResponseBuilder.java index 83d2da299ec..1afd3f3f0b8 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchResponseBuilder.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SearchResponseBuilder.java @@ -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 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 eventComponentChangeDtos = searchData.componentChangesByEventUuid.get(event.getUuid()); diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/projectanalysis/ws/search-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/projectanalysis/ws/search-example.json index 27fa8cfadf9..c7edc4357a6 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/projectanalysis/ws/search-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/projectanalysis/ws/search-example.json @@ -16,12 +16,22 @@ { "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)" } ] }, diff --git a/sonar-ws/src/main/protobuf/ws-projectanalyses.proto b/sonar-ws/src/main/protobuf/ws-projectanalyses.proto index c435380d59a..bbcffc82c91 100644 --- a/sonar-ws/src/main/protobuf/ws-projectanalyses.proto +++ b/sonar-ws/src/main/protobuf/ws-projectanalyses.proto @@ -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; -- 2.39.5