Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire post receive hook after import #1754

Merged
merged 1 commit into from Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/changelog/plugin_logo.yaml
@@ -1,2 +1,2 @@
- type: Added
description: Add bounding box for plugin avatar ([#1749](https://github.com/scm-manager/scm-manager/pull/1749))
description: Bounding box for plugin avatar ([#1749](https://github.com/scm-manager/scm-manager/pull/1749))
2 changes: 2 additions & 0 deletions gradle/changelog/post_receive_hook_event_after_import.yaml
@@ -0,0 +1,2 @@
- type: Fixed
description: Post 'post receive repository hook event' after import ([#1754](https://github.com/scm-manager/scm-manager/pull/1754))
2 changes: 1 addition & 1 deletion gradle/changelog/query_with_hypen.yaml
@@ -1,2 +1,2 @@
- type: Fixed
description: Fixed search queries containing hypens ([#1743](https://github.com/scm-manager/scm-manager/issues/1743) and [#1753](https://github.com/scm-manager/scm-manager/pull/1753))
description: Search queries containing hypens ([#1743](https://github.com/scm-manager/scm-manager/issues/1743) and [#1753](https://github.com/scm-manager/scm-manager/pull/1753))
Expand Up @@ -29,7 +29,6 @@
import sonia.scm.ContextEntry;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
import sonia.scm.repository.RepositoryHookEvent;
import sonia.scm.repository.Tag;
import sonia.scm.repository.WrappedRepositoryHookEvent;
import sonia.scm.repository.api.ImportFailedException;
Expand Down
Expand Up @@ -31,6 +31,7 @@
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.ImportRepositoryHookEvent;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryHookEvent;
import sonia.scm.repository.RepositoryImportEvent;
Expand Down Expand Up @@ -132,6 +133,7 @@ private void runUnbundleCommand(boolean compressed, RepositoryService service, F
.unbundle(file);
RepositoryHookEvent repositoryHookEvent = eventSink.get();
if (repositoryHookEvent != null) {
eventBus.post(new PostReceiveRepositoryHookEvent(repositoryHookEvent));
eventBus.post(new ImportRepositoryHookEvent(repositoryHookEvent));
}
}
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand All @@ -40,8 +39,11 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.ImportRepositoryHookEvent;
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryHookEvent;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryTestData;
Expand All @@ -65,6 +67,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -136,8 +139,7 @@ void initMocks(@TempDir Path temp) throws IOException {

@Test
void shouldImportCompressedBundle() throws IOException {
URL dumpUrl = Resources.getResource("sonia/scm/api/v2/svn.dump.gz");
InputStream in = new ByteArrayInputStream(Resources.toByteArray(dumpUrl));
InputStream in = buildInput("sonia/scm/api/v2/svn.dump.gz");

importer.importFromBundle(true, in, REPOSITORY);

Expand All @@ -147,8 +149,7 @@ void shouldImportCompressedBundle() throws IOException {

@Test
void shouldImportNonCompressedBundle() throws IOException {
URL dumpUrl = Resources.getResource("sonia/scm/api/v2/svn.dump");
InputStream in = new ByteArrayInputStream(Resources.toByteArray(dumpUrl));
InputStream in = buildInput("sonia/scm/api/v2/svn.dump");

importer.importFromBundle(false, in, REPOSITORY);

Expand All @@ -158,8 +159,7 @@ void shouldImportNonCompressedBundle() throws IOException {

@Test
void shouldSetPermissionForCurrentUser() throws IOException {
URL dumpUrl = Resources.getResource("sonia/scm/api/v2/svn.dump");
InputStream in = new ByteArrayInputStream(Resources.toByteArray(dumpUrl));
InputStream in = buildInput("sonia/scm/api/v2/svn.dump");

Repository createdRepository = importer.importFromBundle(false, in, REPOSITORY);

Expand All @@ -170,17 +170,38 @@ void shouldSetPermissionForCurrentUser() throws IOException {
assertThat(permission.isGroupPermission()).isFalse();
assertThat(permission.getRole()).isEqualTo("OWNER");
}

@Test
void shouldPostEvents() throws IOException {
InputStream in = buildInput("sonia/scm/api/v2/svn.dump");

RepositoryHookEvent event = mock(RepositoryHookEvent.class);
when(unbundleCommandBuilder.setPostEventSink(any()))
.thenAnswer(invocationOnMock -> {
invocationOnMock.getArgument(0, Consumer.class).accept(event);
return invocationOnMock.getMock();
});

importer.importFromBundle(false, in, REPOSITORY);

verify(eventBus).post(argThat(o -> o instanceof PostReceiveRepositoryHookEvent));
verify(eventBus).post(argThat(o -> o instanceof ImportRepositoryHookEvent));
}
}

@Test
void shouldFailWithoutPermission() throws IOException {
URL dumpUrl = Resources.getResource("sonia/scm/api/v2/svn.dump");
InputStream in = new ByteArrayInputStream(Resources.toByteArray(dumpUrl));
InputStream in = buildInput("sonia/scm/api/v2/svn.dump");

doThrow(new AuthorizationException()).when(subject).checkPermission("repository:create");

assertThrows(AuthorizationException.class, () -> importer.importFromBundle(false, in, REPOSITORY));

verify(manager, never()).create(any(), any());
}

private InputStream buildInput(String s) throws IOException {
URL dumpUrl = Resources.getResource(s);
return new ByteArrayInputStream(Resources.toByteArray(dumpUrl));
}
}