*/
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;
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;
@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)
.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);
}
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);
}
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);
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) {
.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);
Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, uuid);
return checkStateWithOptional(organizationDto, "the default organization '%s' was not found", uuid);
}
-
}