Skip to content

Commit

Permalink
SONAR-10347 Add search-by-webhook to webhook deliveries search ws.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Jambet authored and gjambet committed Mar 1, 2018
1 parent bfb8b46 commit 3ae1835
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 33 deletions.
Expand Up @@ -769,6 +769,7 @@ CREATE INDEX "PROJECT_WEBHOOK" ON "WEBHOOKS" ("PROJECT_UUID");

CREATE TABLE "WEBHOOK_DELIVERIES" (
"UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
"WEBHOOK_UUID" VARCHAR(40),
"COMPONENT_UUID" VARCHAR(40) NOT NULL,
"ANALYSIS_UUID" VARCHAR(40),
"CE_TASK_UUID" VARCHAR(40),
Expand Down
Expand Up @@ -30,6 +30,13 @@ public Optional<WebhookDeliveryDto> selectByUuid(DbSession dbSession, String uui
return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
}

/**
* All the deliveries for the specified webhook. Results are ordered by descending date.
*/
public List<WebhookDeliveryLiteDto> selectByWebhookUuid(DbSession dbSession, String webhookUuid) {
return mapper(dbSession).selectByWebhookUuid(webhookUuid);
}

/**
* All the deliveries for the specified component. Results are ordered by descending date.
*/
Expand Down
Expand Up @@ -26,6 +26,8 @@
public class WebhookDeliveryLiteDto<T extends WebhookDeliveryLiteDto> {
/** Technical unique identifier, can't be null */
protected String uuid;
/** Technical unique identifier, can be null for migration */
protected String webhookUuid;
/** Component UUID, can't be null */
protected String componentUuid;
/** Compute Engine task UUID, can be null */
Expand All @@ -50,7 +52,16 @@ public String getUuid() {

public T setUuid(String s) {
this.uuid = s;
return (T)this;
return (T) this;
}

public String getWebhookUuid() {
return webhookUuid;
}

public T setWebhookUuid(String webhookUuid) {
this.webhookUuid = webhookUuid;
return (T) this;
}

public String getComponentUuid() {
Expand All @@ -59,7 +70,7 @@ public String getComponentUuid() {

public T setComponentUuid(String s) {
this.componentUuid = s;
return (T)this;
return (T) this;
}

@CheckForNull
Expand All @@ -69,7 +80,7 @@ public String getCeTaskUuid() {

public T setCeTaskUuid(@Nullable String s) {
this.ceTaskUuid = s;
return (T)this;
return (T) this;
}

@CheckForNull
Expand All @@ -79,7 +90,7 @@ public String getAnalysisUuid() {

public T setAnalysisUuid(@Nullable String s) {
this.analysisUuid = s;
return (T)this;
return (T) this;
}

public String getName() {
Expand All @@ -88,7 +99,7 @@ public String getName() {

public T setName(String s) {
this.name = s;
return (T)this;
return (T) this;
}

public boolean isSuccess() {
Expand All @@ -97,7 +108,7 @@ public boolean isSuccess() {

public T setSuccess(boolean b) {
this.success = b;
return (T)this;
return (T) this;
}

@CheckForNull
Expand All @@ -107,7 +118,7 @@ public Integer getHttpStatus() {

public T setHttpStatus(@Nullable Integer i) {
this.httpStatus = i;
return (T)this;
return (T) this;
}

@CheckForNull
Expand All @@ -117,7 +128,7 @@ public Integer getDurationMs() {

public T setDurationMs(@Nullable Integer i) {
this.durationMs = i;
return (T)this;
return (T) this;
}

public String getUrl() {
Expand All @@ -126,7 +137,7 @@ public String getUrl() {

public T setUrl(String s) {
this.url = s;
return (T)this;
return (T) this;
}

public long getCreatedAt() {
Expand All @@ -135,7 +146,7 @@ public long getCreatedAt() {

public T setCreatedAt(long l) {
this.createdAt = l;
return (T)this;
return (T) this;
}

@Override
Expand Down
Expand Up @@ -28,6 +28,8 @@ public interface WebhookDeliveryMapper {
@CheckForNull
WebhookDeliveryDto selectByUuid(@Param("uuid") String uuid);

List<WebhookDeliveryLiteDto> selectByWebhookUuid(@Param("webhookUuid") String webhookUuid);

List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid);

List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid);
Expand Down
Expand Up @@ -7,6 +7,7 @@
<sql id="sqlLiteColumns">
uuid,
component_uuid as componentUuid,
webhook_uuid as webhookUuid,
ce_task_uuid as ceTaskUuid,
name,
url,
Expand All @@ -25,6 +26,13 @@
where uuid = #{uuid,jdbcType=VARCHAR}
</select>

<select id="selectByWebhookUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
select <include refid="sqlLiteColumns" />
from webhook_deliveries
where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
order by created_at desc
</select>

<select id="selectOrderedByComponentUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
select <include refid="sqlLiteColumns" />
from webhook_deliveries
Expand All @@ -42,6 +50,7 @@
<insert id="insert" parameterType="org.sonar.db.webhook.WebhookDeliveryDto" useGeneratedKeys="false">
insert into webhook_deliveries (
uuid,
webhook_uuid,
component_uuid,
ce_task_uuid,
analysis_uuid,
Expand All @@ -55,6 +64,7 @@
created_at
) values (
#{uuid,jdbcType=VARCHAR},
#{webhookUuid,jdbcType=VARCHAR},
#{componentUuid,jdbcType=VARCHAR},
#{ceTaskUuid,jdbcType=VARCHAR},
#{analysisUuid,jdbcType=VARCHAR},
Expand Down
@@ -0,0 +1,46 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v71;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddWebhookKeyToWebhookDeliveriesTable extends DdlChange {

public AddWebhookKeyToWebhookDeliveriesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), "webhook_deliveries")
.addColumn(newVarcharColumnDefBuilder()
.setColumnName("webhook_uuid")
.setIsNullable(true)
.setLimit(40)
.build())
.build());
}

}
Expand Up @@ -42,6 +42,7 @@ public void addSteps(MigrationStepRegistry registry) {
.add(2012, "Rename table PROJECT_LINKS2 to PROJECT_LINKS", RenameTableProjectLinks2ToProjectLinks.class)
.add(2013, "Create WEBHOOKS Table", CreateWebhooksTable.class)
.add(2014, "Migrate webhooks from SETTINGS table to WEBHOOKS table", MigrateWebhooksToWebhooksTable.class)
.add(2015, "Add webhook key to WEBHOOK_DELIVERIES table", AddWebhookKeyToWebhookDeliveriesTable.class)
;
}
}
@@ -0,0 +1,55 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v71;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;

import static java.sql.Types.VARCHAR;
import static org.sonar.db.CoreDbTester.createForSchema;

public class AddWebhookKeyToWebhookDeliveriesTableTest {
@Rule
public final CoreDbTester dbTester = createForSchema(AddWebhookKeyToWebhookDeliveriesTableTest.class, "webhook-deliveries.sql");

@Rule
public ExpectedException expectedException = ExpectedException.none();

private AddWebhookKeyToWebhookDeliveriesTable underTest = new AddWebhookKeyToWebhookDeliveriesTable(dbTester.database());

@Test
public void column_is_added_to_table() throws SQLException {
underTest.execute();

dbTester.assertColumnDefinition("webhook_deliveries", "webhook_uuid", VARCHAR, 40, true);
}

@Test
public void migration_is_not_reentrant() throws SQLException {
underTest.execute();

expectedException.expect(IllegalStateException.class);

underTest.execute();
}
}
Expand Up @@ -36,7 +36,7 @@ public void migrationNumber_starts_at_2000() {

@Test
public void verify_migration_count() {
verifyMigrationCount(underTest, 15);
verifyMigrationCount(underTest, 16);
}

}
@@ -0,0 +1,18 @@
CREATE TABLE "WEBHOOK_DELIVERIES" (
"UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
"COMPONENT_UUID" VARCHAR(40) NOT NULL,
"ANALYSIS_UUID" VARCHAR(40),
"CE_TASK_UUID" VARCHAR(40),
"NAME" VARCHAR(100) NOT NULL,
"URL" VARCHAR(2000) NOT NULL,
"SUCCESS" BOOLEAN NOT NULL,
"HTTP_STATUS" INT,
"DURATION_MS" INT,
"PAYLOAD" CLOB NOT NULL,
"ERROR_STACKTRACE" CLOB,
"CREATED_AT" BIGINT NOT NULL
);
CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID");
CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID");
CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID");
CREATE INDEX "ANALYSIS_UUID" ON "WEBHOOK_DELIVERIES" ("ANALYSIS_UUID");
Expand Up @@ -73,7 +73,7 @@ private Stream<WebhookDto> readWebHooksFrom(String projectUuid) {
@Override
public void sendProjectAnalysisUpdate(Analysis analysis, Supplier<WebhookPayload> payloadSupplier) {
List<Webhook> webhooks = readWebHooksFrom(analysis.getProjectUuid())
.map(dto -> new Webhook(analysis.getProjectUuid(), analysis.getCeTaskUuid(), analysis.getAnalysisUuid(), dto.getName(), dto.getUrl()))
.map(dto -> new Webhook(dto.getUuid(), analysis.getProjectUuid(), analysis.getCeTaskUuid(), analysis.getAnalysisUuid(), dto.getName(), dto.getUrl()))
.collect(MoreCollectors.toList());
if (webhooks.isEmpty()) {
return;
Expand Down
Expand Up @@ -29,13 +29,15 @@
@Immutable
public class Webhook {

private final String uuid;
private final String componentUuid;
private final String ceTaskUuid;
private final String analysisUuid;
private final String name;
private final String url;

public Webhook(String componentUuid, @Nullable String ceTaskUuid, @Nullable String analysisUuid, String name, String url) {
public Webhook(String uuid, String componentUuid, @Nullable String ceTaskUuid, @Nullable String analysisUuid, String name, String url) {
this.uuid = uuid;
this.componentUuid = requireNonNull(componentUuid);
this.ceTaskUuid = ceTaskUuid;
this.analysisUuid = analysisUuid;
Expand All @@ -59,6 +61,10 @@ public String getUrl() {
return url;
}

public String getUuid() {
return uuid;
}

public Optional<String> getAnalysisUuid() {
return ofNullable(analysisUuid);
}
Expand Down
Expand Up @@ -65,6 +65,7 @@ public void purge(String componentUuid) {
private WebhookDeliveryDto toDto(WebhookDelivery delivery) {
WebhookDeliveryDto dto = new WebhookDeliveryDto();
dto.setUuid(uuidFactory.create());
dto.setWebhookUuid(delivery.getWebhook().getUuid());
dto.setComponentUuid(delivery.getWebhook().getComponentUuid());
delivery.getWebhook().getCeTaskUuid().ifPresent(dto::setCeTaskUuid);
delivery.getWebhook().getAnalysisUuid().ifPresent(dto::setAnalysisUuid);
Expand Down

0 comments on commit 3ae1835

Please sign in to comment.