From 83005bebf4ff0a6b7e2483c9e45062ebbfc12614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Mon, 6 Aug 2018 11:11:44 +0200 Subject: [PATCH] Use namespace and name instead of id in repository hooks --- .../main/java/sonia/scm/debug/DebugHook.java | 5 +- .../java/sonia/scm/debug/DebugResource.java | 24 ++-- .../java/sonia/scm/debug/DebugService.java | 30 ++--- .../sonia/scm/it/RepositoryHookITCase.java | 34 +++-- .../scm/it/RepositoryHttpCacheITCase.java | 119 ------------------ .../java/sonia/scm/it/RepositoryITUtil.java | 1 + 6 files changed, 49 insertions(+), 164 deletions(-) delete mode 100644 scm-webapp/src/test/java/sonia/scm/it/RepositoryHttpCacheITCase.java diff --git a/scm-webapp/src/main/java/sonia/scm/debug/DebugHook.java b/scm-webapp/src/main/java/sonia/scm/debug/DebugHook.java index d75b455a95..7fe813159a 100644 --- a/scm-webapp/src/main/java/sonia/scm/debug/DebugHook.java +++ b/scm-webapp/src/main/java/sonia/scm/debug/DebugHook.java @@ -34,13 +34,14 @@ import com.github.legman.ReferenceType; import com.github.legman.Subscribe; import com.google.common.base.Function; import com.google.common.collect.Collections2; -import javax.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.EagerSingleton; import sonia.scm.repository.Changeset; import sonia.scm.repository.PostReceiveRepositoryHookEvent; +import javax.inject.Inject; + /** * {@link PostReceiveRepositoryHookEvent} which stores receives data and passes it to the {@link DebugService}. * @@ -78,7 +79,7 @@ public final class DebugHook LOG.trace("store changeset ids from repository", event.getRepository().getId()); debugService.put( - event.getRepository().getId(), + event.getRepository().getNamespaceAndName(), new DebugHookData(Collections2.transform( event.getContext().getChangesetProvider().getChangesetList(), IDEXTRACTOR) )); diff --git a/scm-webapp/src/main/java/sonia/scm/debug/DebugResource.java b/scm-webapp/src/main/java/sonia/scm/debug/DebugResource.java index 0933242b49..6ee035bf01 100644 --- a/scm-webapp/src/main/java/sonia/scm/debug/DebugResource.java +++ b/scm-webapp/src/main/java/sonia/scm/debug/DebugResource.java @@ -30,20 +30,22 @@ */ package sonia.scm.debug; -import java.util.Collection; +import sonia.scm.repository.NamespaceAndName; + import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import java.util.Collection; /** * Rest api resource for the {@link DebugService}. * * @author Sebastian Sdorra */ -@Path("debug/{repository}/post-receive") +@Path("debug/{namespace}/{name}/post-receive") public final class DebugResource { private final DebugService debugService; @@ -62,28 +64,30 @@ public final class DebugResource /** * Returns all received hook data for the given repository. * - * @param repository repository id - * + * @param namespace repository namespace + * @param name repository name + * * @return all received hook data for the given repository */ @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - public Collection getAll(@PathParam("repository") String repository){ - return debugService.getAll(repository); + public Collection getAll(@PathParam("namespace") String namespace, @PathParam("name") String name){ + return debugService.getAll(new NamespaceAndName(namespace, name)); } /** * Returns the last received hook data for the given repository. - * - * @param repository repository id + * + * @param namespace repository namespace + * @param name repository name * * @return the last received hook data for the given repository */ @GET @Path("last") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - public DebugHookData getLast(@PathParam("repository") String repository){ - return debugService.getLast(repository); + public DebugHookData getLast(@PathParam("namespace") String namespace, @PathParam("name") String name){ + return debugService.getLast(new NamespaceAndName(namespace, name)); } } diff --git a/scm-webapp/src/main/java/sonia/scm/debug/DebugService.java b/scm-webapp/src/main/java/sonia/scm/debug/DebugService.java index 31282b6b08..8e2475d802 100644 --- a/scm-webapp/src/main/java/sonia/scm/debug/DebugService.java +++ b/scm-webapp/src/main/java/sonia/scm/debug/DebugService.java @@ -34,10 +34,12 @@ import com.google.common.collect.Iterables; import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Multimap; import com.google.inject.Singleton; -import java.util.Collection; import org.apache.shiro.SecurityUtils; +import sonia.scm.repository.NamespaceAndName; import sonia.scm.security.Role; +import java.util.Collection; + /** * The DebugService stores and returns received data from repository hook events. * @@ -47,30 +49,23 @@ import sonia.scm.security.Role; public final class DebugService { - private final Multimap receivedHooks = LinkedListMultimap.create(); + private final Multimap receivedHooks = LinkedListMultimap.create(); /** * Stores {@link DebugHookData} for the given repository. - * - * @param repository repository id - * @param hookData received hook data */ - void put(String repository, DebugHookData hookData) + void put(NamespaceAndName namespaceAndName, DebugHookData hookData) { - receivedHooks.put(repository, hookData); + receivedHooks.put(namespaceAndName, hookData); } /** * Returns the last received hook data for the given repository. - * - * @param repository repository id - * - * @return the last received hook data for the given repository */ - public DebugHookData getLast(String repository){ + public DebugHookData getLast(NamespaceAndName namespaceAndName){ SecurityUtils.getSubject().checkRole(Role.ADMIN); DebugHookData hookData = null; - Collection receivedHookData = receivedHooks.get(repository); + Collection receivedHookData = receivedHooks.get(namespaceAndName); if (receivedHookData != null && ! receivedHookData.isEmpty()){ hookData = Iterables.getLast(receivedHookData); } @@ -79,14 +74,9 @@ public final class DebugService /** * Returns all received hook data for the given repository. - * - * @param repository repository id - * - * @return all received hook data for the given repository */ - public Collection getAll(String repository){ + public Collection getAll(NamespaceAndName namespaceAndName){ SecurityUtils.getSubject().checkRole(Role.ADMIN); - return receivedHooks.get(repository); + return receivedHooks.get(namespaceAndName); } - } diff --git a/scm-webapp/src/test/java/sonia/scm/it/RepositoryHookITCase.java b/scm-webapp/src/test/java/sonia/scm/it/RepositoryHookITCase.java index d6e8a0d2fe..2be3bfd81b 100644 --- a/scm-webapp/src/test/java/sonia/scm/it/RepositoryHookITCase.java +++ b/scm-webapp/src/test/java/sonia/scm/it/RepositoryHookITCase.java @@ -32,6 +32,7 @@ package sonia.scm.it; import com.google.common.base.Charsets; import com.google.common.io.Files; +import com.sun.jersey.api.client.WebResource; import org.junit.After; import org.junit.Assume; import org.junit.Before; @@ -42,6 +43,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import sonia.scm.api.v2.resources.RepositoryDto; +import sonia.scm.debug.DebugHookData; import sonia.scm.repository.Changeset; import sonia.scm.repository.Person; import sonia.scm.repository.client.api.ClientCommand; @@ -52,6 +54,12 @@ import java.io.File; import java.io.IOException; import java.util.Collection; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.core.AllOf.allOf; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static sonia.scm.it.IntegrationTestUtil.createResource; import static sonia.scm.it.IntegrationTestUtil.readJson; import static sonia.scm.it.RepositoryITUtil.createRepository; import static sonia.scm.it.RepositoryITUtil.deleteRepository; @@ -129,10 +137,10 @@ public class RepositoryHookITCase extends AbstractAdminITCaseBase Thread.sleep(WAIT_TIME); // check debug servlet for pushed commit -// WebResource wr = createResource(client, "debug/" + repository.getId() + "/post-receive/last"); -// DebugHookData data = wr.get(DebugHookData.class); -// assertNotNull(data); -// assertThat(data.getChangesets(), contains(changeset.getId())); + WebResource.Builder wr = createResource(client, "../debug/" + repository.getNamespace() + "/" + repository.getName() + "/post-receive/last"); + DebugHookData data = wr.get(DebugHookData.class); + assertNotNull(data); + assertThat(data.getChangesets(), contains(changeset.getId())); } /** @@ -164,15 +172,15 @@ public class RepositoryHookITCase extends AbstractAdminITCaseBase Thread.sleep(WAIT_TIME); // check debug servlet that only one commit is present -// WebResource wr = createResource(client, "debug/" + repository.getId() + "/post-receive/last"); -// DebugHookData data = wr.get(DebugHookData.class); -// assertNotNull(data); -// assertThat(data.getChangesets(), allOf( -// contains(b.getId()), -// not( -// contains(a.getId()) -// ) -// )); + WebResource.Builder wr = createResource(client, "../debug/" + repository.getNamespace() + "/" + repository.getName() + "/post-receive/last"); + DebugHookData data = wr.get(DebugHookData.class); + assertNotNull(data); + assertThat(data.getChangesets(), allOf( + contains(b.getId()), + not( + contains(a.getId()) + ) + )); } private Changeset commit(String message) throws IOException { diff --git a/scm-webapp/src/test/java/sonia/scm/it/RepositoryHttpCacheITCase.java b/scm-webapp/src/test/java/sonia/scm/it/RepositoryHttpCacheITCase.java deleted file mode 100644 index 7f22366ecb..0000000000 --- a/scm-webapp/src/test/java/sonia/scm/it/RepositoryHttpCacheITCase.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright (c) 2010, Sebastian Sdorra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of SCM-Manager; nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * http://bitbucket.org/sdorra/scm-manager - * - */ - - -package sonia.scm.it; - -//~--- non-JDK imports -------------------------------------------------------- - -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import org.junit.Ignore; -import sonia.scm.repository.Repository; -import sonia.scm.repository.RepositoryTestData; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static sonia.scm.it.IntegrationTestUtil.createAdminClient; -import static sonia.scm.it.IntegrationTestUtil.createResource; - -//~--- JDK imports ------------------------------------------------------------ - -/** - * - * @author Sebastian Sdorra - */ -@Ignore() -public class RepositoryHttpCacheITCase extends HttpCacheITCaseBase -{ - - /** - * Method description - * - * - * @return - */ - @Override - protected Repository createSampleItem() - { - Repository repository = RepositoryTestData.createHeartOfGold("git"); - ScmClient client = createAdminClient(); - WebResource.Builder resource = createResource(client, "repositories"); - ClientResponse response = resource.post(ClientResponse.class, repository); - - assertNotNull(response); - assertEquals(201, response.getStatus()); - - String location = response.getHeaders().get("Location").get(0); - - assertNotNull(location); - response = client.resource(location).get(ClientResponse.class); - assertNotNull(response); - assertEquals(200, response.getStatus()); - repository = response.getEntity(Repository.class); - assertNotNull(repository); - assertNotNull(repository.getId()); - - return repository; - } - - /** - * Method description - * - * - * @param item - */ - @Override - protected void destroy(Repository item) - { - ScmClient client = createAdminClient(); - WebResource.Builder resource = createResource(client, - "repositories/".concat(item.getId())); - ClientResponse response = resource.delete(ClientResponse.class); - - assertNotNull(response); - assertEquals(204, response.getStatus()); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - @Override - protected String getCollectionUrlPart() - { - return "repositories"; - } -} diff --git a/scm-webapp/src/test/java/sonia/scm/it/RepositoryITUtil.java b/scm-webapp/src/test/java/sonia/scm/it/RepositoryITUtil.java index 05915f3b67..9284beebd0 100644 --- a/scm-webapp/src/test/java/sonia/scm/it/RepositoryITUtil.java +++ b/scm-webapp/src/test/java/sonia/scm/it/RepositoryITUtil.java @@ -76,6 +76,7 @@ public final class RepositoryITUtil assertNotNull(other); assertNotNull(other.getType()); + assertNotNull(other.getNamespace()); assertNotNull(other.getCreationDate()); return other;