Browse Source

SONAR-12000 add secret to response of WS api/webhooks/list

tags/7.8
Simon Brandhof 5 years ago
parent
commit
367a2e7e4c

+ 7
- 12
server/sonar-server/src/main/java/org/sonar/server/webhook/ws/ListAction.java View File

@@ -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.<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);
}

}

server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-search.json → server/sonar-server/src/main/resources/org/sonar/server/webhook/ws/example-webhooks-list.json View File

@@ -8,7 +8,8 @@
{
"key": "UUID-2",
"name": "my 2nd webhook",
"url": "https://www.my-other-webhook-listener.com/fancy-listner"
"url": "https://www.my-other-webhook-listener.com/fancy-listner",
"secret": "your_secret"
}
]
}
}

+ 1
- 1
server/sonar-server/src/test/java/org/sonar/server/webhook/ws/ListActionTest.java View File

@@ -95,7 +95,7 @@ public class ListActionTest {
.containsExactlyInAnyOrder(
tuple("organization", false),
tuple("project", false));
assertThat(action.changelog()).hasSize(1);
}

@Test

+ 1
- 1
sonar-ws/src/main/protobuf/ws-webhooks.proto View 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;

Loading…
Cancel
Save