add repository import event

This commit is contained in:
Eduard Heimbuch
2020-11-26 13:58:34 +01:00
parent a7c4d41e4e
commit 9a06bc7d8a
3 changed files with 57 additions and 3 deletions

View File

@@ -0,0 +1,43 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import sonia.scm.HandlerEventType;
import sonia.scm.event.Event;
@Event
@Getter
@EqualsAndHashCode(callSuper = true)
public class RepositoryImportEvent extends RepositoryEvent {
private final boolean failed;
public RepositoryImportEvent(HandlerEventType eventType, Repository repository, boolean failed) {
super(eventType, repository);
this.failed = failed;
}
}

View File

@@ -37,11 +37,14 @@ import lombok.NoArgsConstructor;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.HandlerEventType;
import sonia.scm.NotFoundException;
import sonia.scm.Type;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryImportEvent;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType;
@@ -73,13 +76,17 @@ public class RepositoryImportResource {
private final RepositoryManager manager;
private final RepositoryServiceFactory serviceFactory;
private final ResourceLinks resourceLinks;
private final ScmEventBus eventBus;
@Inject
public RepositoryImportResource(RepositoryManager manager,
RepositoryServiceFactory serviceFactory, ResourceLinks resourceLinks) {
RepositoryServiceFactory serviceFactory,
ResourceLinks resourceLinks,
ScmEventBus eventBus) {
this.manager = manager;
this.serviceFactory = serviceFactory;
this.resourceLinks = resourceLinks;
this.eventBus = eventBus;
}
// /**
@@ -217,6 +224,7 @@ public class RepositoryImportResource {
}
pullCommand.pull(request.getUrl());
eventBus.post(new RepositoryImportEvent(HandlerEventType.CREATE, repository, false));
} catch (ImportFailedException ex) {
handleImportFailure(ex, repository);
throw ex;
@@ -504,6 +512,7 @@ public class RepositoryImportResource {
logger.error("import for repository failed, delete repository", ex);
try {
eventBus.post(new RepositoryImportEvent(HandlerEventType.BEFORE_DELETE, repository, true));
manager.delete(repository);
} catch (InternalRepositoryException | NotFoundException e) {
logger.error("can not delete repository after import failure", e);

View File

@@ -28,7 +28,6 @@ import com.github.sdorra.shiro.ShiroRule;
import com.github.sdorra.shiro.SubjectAware;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.jboss.resteasy.mock.MockHttpRequest;
@@ -42,6 +41,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import sonia.scm.PageResult;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.CustomNamespaceStrategy;
import sonia.scm.repository.ImportHandler;
import sonia.scm.repository.NamespaceAndName;
@@ -127,6 +127,8 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
private ScmConfiguration configuration;
@Mock
private Set<NamespaceStrategy> strategies;
@Mock
private ScmEventBus eventBus;
@Captor
private ArgumentCaptor<Predicate<Repository>> filterCaptor;
@@ -147,7 +149,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
super.manager = repositoryManager;
RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper = new RepositoryCollectionToDtoMapper(repositoryToDtoMapper, resourceLinks);
super.repositoryCollectionResource = new RepositoryCollectionResource(repositoryManager, repositoryCollectionToDtoMapper, dtoToRepositoryMapper, resourceLinks, repositoryInitializer);
super.repositoryImportResource = new RepositoryImportResource(repositoryManager, serviceFactory, resourceLinks);
super.repositoryImportResource = new RepositoryImportResource(repositoryManager, serviceFactory, resourceLinks, eventBus);
dispatcher.addSingletonResource(getRepositoryRootResource());
when(serviceFactory.create(any(Repository.class))).thenReturn(service);
when(scmPathInfoStore.get()).thenReturn(uriInfo);