]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12000 add secret to response of WS api/webhooks/list
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 19 Apr 2019 11:20:55 +0000 (13:20 +0200)
committerSonarTech <sonartech@sonarsource.com>
Mon, 29 Apr 2019 18:21:07 +0000 (20:21 +0200)
server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java
server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-list.json [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-search.json [deleted file]
server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java
sonar-ws/src/main/protobuf/ws-webhooks.proto

index efce8bb5c6a3b1fe84e48879e63770c8645cbc7c..5b698ec410160849d26448e482944db8f31c3ac6 100644 (file)
  */
 package org.sonar.server.webhook.ws;
 
-import com.google.common.io.Resources;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import javax.annotation.Nullable;
+import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -39,7 +39,6 @@ import org.sonarqube.ws.Webhooks;
 import org.sonarqube.ws.Webhooks.ListResponse;
 import org.sonarqube.ws.Webhooks.ListResponseElement;
 
-import static java.util.Optional.ofNullable;
 import static org.apache.commons.lang.StringUtils.isNotBlank;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
 import static org.sonar.server.webhook.HttpUrlHelper.obfuscateCredentials;
@@ -68,12 +67,11 @@ public class ListAction implements WebhooksWsAction {
 
   @Override
   public void define(WebService.NewController controller) {
-
     WebService.NewAction action = controller.createAction(LIST_ACTION)
       .setDescription("Search for global webhooks or project webhooks. Webhooks are ordered by name.<br>" +
         "Requires 'Administer' permission on the specified project, or global 'Administer' permission.")
       .setSince("7.1")
-      .setResponseExample(Resources.getResource(this.getClass(), "example-webhooks-search.json"))
+      .setResponseExample(getClass().getResource("example-webhooks-list.json"))
       .setHandler(this);
 
     action.createParam(ORGANIZATION_KEY_PARAM)
@@ -87,11 +85,11 @@ public class ListAction implements WebhooksWsAction {
       .setRequired(false)
       .setExampleValue(KEY_PROJECT_EXAMPLE_001);
 
+    action.setChangelog(new Change("7.8", "Field 'secret' added to response"));
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-
     String projectKey = request.param(PROJECT_KEY_PARAM);
     String organizationKey = request.param(ORGANIZATION_KEY_PARAM);
 
@@ -109,7 +107,6 @@ public class ListAction implements WebhooksWsAction {
   }
 
   private List<WebhookDto> doHandle(DbSession dbSession, @Nullable String organizationKey, @Nullable String projectKey) {
-
     OrganizationDto organizationDto;
     if (isNotBlank(organizationKey)) {
       Optional<OrganizationDto> dtoOptional = dbClient.organizationDao().selectByKey(dbSession, organizationKey);
@@ -119,8 +116,7 @@ public class ListAction implements WebhooksWsAction {
     }
 
     if (isNotBlank(projectKey)) {
-
-      Optional<ComponentDto> optional = ofNullable(dbClient.componentDao().selectByKey(dbSession, projectKey).orElse(null));
+      Optional<ComponentDto> optional = dbClient.componentDao().selectByKey(dbSession, projectKey);
       ComponentDto componentDto = checkFoundWithOptional(optional, "project %s does not exist", projectKey);
       webhookSupport.checkPermission(componentDto);
       webhookSupport.checkThatProjectBelongsToOrganization(componentDto, organizationDto, "Project '%s' does not belong to organisation '%s'", projectKey, organizationKey);
@@ -128,12 +124,9 @@ public class ListAction implements WebhooksWsAction {
       return dbClient.webhookDao().selectByProject(dbSession, componentDto);
 
     } else {
-
       webhookSupport.checkPermission(organizationDto);
       return dbClient.webhookDao().selectByOrganization(dbSession, organizationDto);
-
     }
-
   }
 
   private static void writeResponse(Request request, Response response, List<WebhookDto> webhookDtos, Map<String, WebhookDeliveryLiteDto> lastDeliveries) {
@@ -145,6 +138,9 @@ public class ListAction implements WebhooksWsAction {
           .setKey(webhook.getUuid())
           .setName(webhook.getName())
           .setUrl(obfuscateCredentials(webhook.getUrl()));
+        if (webhook.getSecret() != null) {
+          responseElementBuilder.setSecret(webhook.getSecret());
+        }
         addLastDelivery(responseElementBuilder, webhook, lastDeliveries);
       });
     writeProtobuf(responseBuilder.build(), request, response);
@@ -172,5 +168,4 @@ public class ListAction implements WebhooksWsAction {
     Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, uuid);
     return checkStateWithOptional(organizationDto, "the default organization '%s' was not found", uuid);
   }
-
 }
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-list.json b/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-list.json
new file mode 100644 (file)
index 0000000..008bfd0
--- /dev/null
@@ -0,0 +1,15 @@
+{
+  "webhooks": [
+    {
+      "key": "UUID-1",
+      "name": "my first webhook",
+      "url": "http://www.my-webhook-listener.com/sonarqube"
+    },
+    {
+      "key": "UUID-2",
+      "name": "my 2nd webhook",
+      "url": "https://www.my-other-webhook-listener.com/fancy-listner",
+      "secret": "your_secret"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-search.json b/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-search.json
deleted file mode 100644 (file)
index ba3ba25..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-  "webhooks": [
-    {
-      "key": "UUID-1",
-      "name": "my first webhook",
-      "url": "http://www.my-webhook-listener.com/sonarqube"
-    },
-    {
-      "key": "UUID-2",
-      "name": "my 2nd webhook",
-      "url": "https://www.my-other-webhook-listener.com/fancy-listner"
-    }
-  ]
-}
\ No newline at end of file
index e45fa31686e7abd33c751ab59f3ab9a6da95f572..b713aee9c266d5d77367d2f16c518559e8fb3620 100644 (file)
@@ -95,7 +95,7 @@ public class ListActionTest {
       .containsExactlyInAnyOrder(
         tuple("organization", false),
         tuple("project", false));
-
+    assertThat(action.changelog()).hasSize(1);
   }
 
   @Test
index f8642e7c5efb1c8e4cd6ddb57400972f89feac08..b67f893791fd9c5505e1353c2f55b0411c758511 100644 (file)
@@ -39,6 +39,7 @@ message ListResponseElement {
   optional string name = 2;
   optional string url = 3;
   optional LatestDelivery latestDelivery = 4;
+  optional string secret = 5;
 }
 
 // GET api/webhooks/list
@@ -60,7 +61,6 @@ message CreateWsResponse {
 
 // WS api/webhooks/deliveries
 message DeliveriesWsResponse {
-
   optional sonarqube.ws.commons.Paging paging = 1;
 
   repeated Delivery deliveries = 2;