Use namespace and name instead of id in repository hooks

This commit is contained in:
René Pfeuffer
2018-08-06 11:11:44 +02:00
parent e60bea5f08
commit 83005bebf4
6 changed files with 49 additions and 164 deletions

View File

@@ -34,13 +34,14 @@ import com.github.legman.ReferenceType;
import com.github.legman.Subscribe; import com.github.legman.Subscribe;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import javax.inject.Inject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.EagerSingleton; import sonia.scm.EagerSingleton;
import sonia.scm.repository.Changeset; import sonia.scm.repository.Changeset;
import sonia.scm.repository.PostReceiveRepositoryHookEvent; import sonia.scm.repository.PostReceiveRepositoryHookEvent;
import javax.inject.Inject;
/** /**
* {@link PostReceiveRepositoryHookEvent} which stores receives data and passes it to the {@link DebugService}. * {@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()); LOG.trace("store changeset ids from repository", event.getRepository().getId());
debugService.put( debugService.put(
event.getRepository().getId(), event.getRepository().getNamespaceAndName(),
new DebugHookData(Collections2.transform( new DebugHookData(Collections2.transform(
event.getContext().getChangesetProvider().getChangesetList(), IDEXTRACTOR) event.getContext().getChangesetProvider().getChangesetList(), IDEXTRACTOR)
)); ));

View File

@@ -30,20 +30,22 @@
*/ */
package sonia.scm.debug; package sonia.scm.debug;
import java.util.Collection; import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.Collection;
/** /**
* Rest api resource for the {@link DebugService}. * Rest api resource for the {@link DebugService}.
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@Path("debug/{repository}/post-receive") @Path("debug/{namespace}/{name}/post-receive")
public final class DebugResource public final class DebugResource
{ {
private final DebugService debugService; private final DebugService debugService;
@@ -62,28 +64,30 @@ public final class DebugResource
/** /**
* Returns all received hook data for the given repository. * 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 * @return all received hook data for the given repository
*/ */
@GET @GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Collection<DebugHookData> getAll(@PathParam("repository") String repository){ public Collection<DebugHookData> getAll(@PathParam("namespace") String namespace, @PathParam("name") String name){
return debugService.getAll(repository); return debugService.getAll(new NamespaceAndName(namespace, name));
} }
/** /**
* Returns the last received hook data for the given repository. * 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 * @return the last received hook data for the given repository
*/ */
@GET @GET
@Path("last") @Path("last")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DebugHookData getLast(@PathParam("repository") String repository){ public DebugHookData getLast(@PathParam("namespace") String namespace, @PathParam("name") String name){
return debugService.getLast(repository); return debugService.getLast(new NamespaceAndName(namespace, name));
} }
} }

View File

@@ -34,10 +34,12 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.Collection;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.security.Role; import sonia.scm.security.Role;
import java.util.Collection;
/** /**
* The DebugService stores and returns received data from repository hook events. * The DebugService stores and returns received data from repository hook events.
* *
@@ -47,30 +49,23 @@ import sonia.scm.security.Role;
public final class DebugService public final class DebugService
{ {
private final Multimap<String,DebugHookData> receivedHooks = LinkedListMultimap.create(); private final Multimap<NamespaceAndName,DebugHookData> receivedHooks = LinkedListMultimap.create();
/** /**
* Stores {@link DebugHookData} for the given repository. * 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. * 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); SecurityUtils.getSubject().checkRole(Role.ADMIN);
DebugHookData hookData = null; DebugHookData hookData = null;
Collection<DebugHookData> receivedHookData = receivedHooks.get(repository); Collection<DebugHookData> receivedHookData = receivedHooks.get(namespaceAndName);
if (receivedHookData != null && ! receivedHookData.isEmpty()){ if (receivedHookData != null && ! receivedHookData.isEmpty()){
hookData = Iterables.getLast(receivedHookData); hookData = Iterables.getLast(receivedHookData);
} }
@@ -79,14 +74,9 @@ public final class DebugService
/** /**
* Returns all received hook data for the given repository. * Returns all received hook data for the given repository.
*
* @param repository repository id
*
* @return all received hook data for the given repository
*/ */
public Collection<DebugHookData> getAll(String repository){ public Collection<DebugHookData> getAll(NamespaceAndName namespaceAndName){
SecurityUtils.getSubject().checkRole(Role.ADMIN); SecurityUtils.getSubject().checkRole(Role.ADMIN);
return receivedHooks.get(repository); return receivedHooks.get(namespaceAndName);
} }
} }

View File

@@ -32,6 +32,7 @@ package sonia.scm.it;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.sun.jersey.api.client.WebResource;
import org.junit.After; import org.junit.After;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
@@ -42,6 +43,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; import org.junit.runners.Parameterized.Parameters;
import sonia.scm.api.v2.resources.RepositoryDto; import sonia.scm.api.v2.resources.RepositoryDto;
import sonia.scm.debug.DebugHookData;
import sonia.scm.repository.Changeset; import sonia.scm.repository.Changeset;
import sonia.scm.repository.Person; import sonia.scm.repository.Person;
import sonia.scm.repository.client.api.ClientCommand; import sonia.scm.repository.client.api.ClientCommand;
@@ -52,6 +54,12 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; 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.IntegrationTestUtil.readJson;
import static sonia.scm.it.RepositoryITUtil.createRepository; import static sonia.scm.it.RepositoryITUtil.createRepository;
import static sonia.scm.it.RepositoryITUtil.deleteRepository; import static sonia.scm.it.RepositoryITUtil.deleteRepository;
@@ -129,10 +137,10 @@ public class RepositoryHookITCase extends AbstractAdminITCaseBase
Thread.sleep(WAIT_TIME); Thread.sleep(WAIT_TIME);
// check debug servlet for pushed commit // check debug servlet for pushed commit
// WebResource wr = createResource(client, "debug/" + repository.getId() + "/post-receive/last"); WebResource.Builder wr = createResource(client, "../debug/" + repository.getNamespace() + "/" + repository.getName() + "/post-receive/last");
// DebugHookData data = wr.get(DebugHookData.class); DebugHookData data = wr.get(DebugHookData.class);
// assertNotNull(data); assertNotNull(data);
// assertThat(data.getChangesets(), contains(changeset.getId())); assertThat(data.getChangesets(), contains(changeset.getId()));
} }
/** /**
@@ -164,15 +172,15 @@ public class RepositoryHookITCase extends AbstractAdminITCaseBase
Thread.sleep(WAIT_TIME); Thread.sleep(WAIT_TIME);
// check debug servlet that only one commit is present // check debug servlet that only one commit is present
// WebResource wr = createResource(client, "debug/" + repository.getId() + "/post-receive/last"); WebResource.Builder wr = createResource(client, "../debug/" + repository.getNamespace() + "/" + repository.getName() + "/post-receive/last");
// DebugHookData data = wr.get(DebugHookData.class); DebugHookData data = wr.get(DebugHookData.class);
// assertNotNull(data); assertNotNull(data);
// assertThat(data.getChangesets(), allOf( assertThat(data.getChangesets(), allOf(
// contains(b.getId()), contains(b.getId()),
// not( not(
// contains(a.getId()) contains(a.getId())
// ) )
// )); ));
} }
private Changeset commit(String message) throws IOException { private Changeset commit(String message) throws IOException {

View File

@@ -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<Repository>
{
/**
* 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";
}
}

View File

@@ -76,6 +76,7 @@ public final class RepositoryITUtil
assertNotNull(other); assertNotNull(other);
assertNotNull(other.getType()); assertNotNull(other.getType());
assertNotNull(other.getNamespace());
assertNotNull(other.getCreationDate()); assertNotNull(other.getCreationDate());
return other; return other;