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

View File

@@ -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<DebugHookData> getAll(@PathParam("repository") String repository){
return debugService.getAll(repository);
public Collection<DebugHookData> 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));
}
}

View File

@@ -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<String,DebugHookData> receivedHooks = LinkedListMultimap.create();
private final Multimap<NamespaceAndName,DebugHookData> 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<DebugHookData> receivedHookData = receivedHooks.get(repository);
Collection<DebugHookData> 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<DebugHookData> getAll(String repository){
public Collection<DebugHookData> getAll(NamespaceAndName namespaceAndName){
SecurityUtils.getSubject().checkRole(Role.ADMIN);
return receivedHooks.get(repository);
return receivedHooks.get(namespaceAndName);
}
}