From: Simon Brandhof Date: Fri, 19 Apr 2019 11:20:55 +0000 (+0200) Subject: SONAR-12000 add secret to response of WS api/webhooks/list X-Git-Tag: 7.8~321 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=367a2e7e4c976956f43b11d7b2b6076bba05f3d8;p=sonarqube.git SONAR-12000 add secret to response of WS api/webhooks/list --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java index efce8bb5c6a..5b698ec4101 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java @@ -19,11 +19,11 @@ */ 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.
" + "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 doHandle(DbSession dbSession, @Nullable String organizationKey, @Nullable String projectKey) { - OrganizationDto organizationDto; if (isNotBlank(organizationKey)) { Optional dtoOptional = dbClient.organizationDao().selectByKey(dbSession, organizationKey); @@ -119,8 +116,7 @@ public class ListAction implements WebhooksWsAction { } if (isNotBlank(projectKey)) { - - Optional optional = ofNullable(dbClient.componentDao().selectByKey(dbSession, projectKey).orElse(null)); + Optional 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 webhookDtos, Map 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 = 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 index 00000000000..008bfd0f624 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-list.json @@ -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 index ba3ba250df4..00000000000 --- a/server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-search.json +++ /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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java index e45fa31686e..b713aee9c26 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java @@ -95,7 +95,7 @@ public class ListActionTest { .containsExactlyInAnyOrder( tuple("organization", false), tuple("project", false)); - + assertThat(action.changelog()).hasSize(1); } @Test diff --git a/sonar-ws/src/main/protobuf/ws-webhooks.proto b/sonar-ws/src/main/protobuf/ws-webhooks.proto index f8642e7c5ef..b67f893791f 100644 --- a/sonar-ws/src/main/protobuf/ws-webhooks.proto +++ b/sonar-ws/src/main/protobuf/ws-webhooks.proto @@ -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;