mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-22 00:09:47 +01:00
Post receive hook after import (#1544)
Fire post receive repository hook event after pull from remote and after unbundle (Git, HG and SVN) Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
@@ -24,77 +24,101 @@
|
||||
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.io.Files;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.tmatesoft.svn.core.SVNException;
|
||||
import org.tmatesoft.svn.core.io.SVNRepository;
|
||||
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryTestData;
|
||||
import sonia.scm.repository.SvnUtil;
|
||||
import sonia.scm.repository.api.HookChangesetBuilder;
|
||||
import sonia.scm.repository.api.HookContext;
|
||||
import sonia.scm.repository.api.HookContextFactory;
|
||||
import sonia.scm.repository.api.UnbundleResponse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
public class SvnUnbundleCommandTest extends AbstractSvnCommandTestBase {
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SvnUnbundleCommandTest extends AbstractSvnCommandTestBase
|
||||
{
|
||||
private final Repository repository = RepositoryTestData.createHeartOfGold("svn");
|
||||
private HookContextFactory hookContextFactory;
|
||||
private ScmEventBus eventBus;
|
||||
private SvnLogCommand logCommand;
|
||||
private HookChangesetBuilder hookChangesetBuilder;
|
||||
|
||||
@Captor
|
||||
private final ArgumentCaptor<PostReceiveRepositoryHookEvent> eventCaptor =
|
||||
ArgumentCaptor.forClass(PostReceiveRepositoryHookEvent.class);
|
||||
|
||||
@Before
|
||||
public void initMocks() {
|
||||
hookContextFactory = mock(HookContextFactory.class);
|
||||
eventBus = mock(ScmEventBus.class);
|
||||
logCommand = mock(SvnLogCommand.class);
|
||||
HookContext hookContext = mock(HookContext.class);
|
||||
hookChangesetBuilder = mock(HookChangesetBuilder.class);
|
||||
when(hookContextFactory.createContext(any(), eq(repository))).thenReturn(hookContext);
|
||||
when(hookContext.getChangesetProvider()).thenReturn(hookChangesetBuilder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnbundle()
|
||||
throws IOException, SVNException
|
||||
{
|
||||
public void shouldFirePostCommitEventAfterUnbundle() throws IOException, SVNException {
|
||||
Changeset first = new Changeset("1", 0L, new Person("trillian"), "first commit");
|
||||
when(hookChangesetBuilder.getChangesetList()).thenReturn(ImmutableList.of(first));
|
||||
|
||||
File bundle = bundle();
|
||||
SvnContext ctx = createEmptyContext();
|
||||
//J-
|
||||
UnbundleResponse res = new SvnUnbundleCommand(ctx)
|
||||
UnbundleResponse res = new SvnUnbundleCommand(ctx, hookContextFactory, eventBus, logCommand)
|
||||
.unbundle(new UnbundleCommandRequest(Files.asByteSource(bundle))
|
||||
);
|
||||
);
|
||||
//J+
|
||||
|
||||
assertThat(res, notNullValue());
|
||||
assertThat(res.getChangesetCount(), is(5l));
|
||||
|
||||
assertThat(res).isNotNull();
|
||||
assertThat(res.getChangesetCount()).isEqualTo(5);
|
||||
SVNRepository repo = ctx.open();
|
||||
assertThat(repo.getLatestRevision()).isEqualTo(5);
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
PostReceiveRepositoryHookEvent event = eventCaptor.getValue();
|
||||
List<Changeset> changesets = event.getContext().getChangesetProvider().getChangesetList();
|
||||
assertThat(changesets).hasSize(1);
|
||||
assertThat(changesets).contains(first);
|
||||
|
||||
assertThat(repo.getLatestRevision(), is(5l));
|
||||
SvnUtil.closeSession(repo);
|
||||
}
|
||||
|
||||
private File bundle() throws IOException
|
||||
{
|
||||
private File bundle() throws IOException {
|
||||
File file = tempFolder.newFile();
|
||||
|
||||
//J-
|
||||
new SvnBundleCommand(createContext())
|
||||
.bundle(new BundleCommandRequest(Files.asByteSink(file))
|
||||
);
|
||||
);
|
||||
//J+
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws SVNException
|
||||
*/
|
||||
private SvnContext createEmptyContext() throws IOException, SVNException
|
||||
{
|
||||
private SvnContext createEmptyContext() throws IOException, SVNException {
|
||||
File folder = tempFolder.newFolder();
|
||||
|
||||
SVNRepositoryFactory.createLocalRepository(folder, true, true);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user