mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 23:15:43 +01:00
Use clone and push to create branches
Generalize workdir creation for git and hg and create branches in clones instead of the scm repository, so that hooks will be fired correctly once the changes are pushed back. Missing: - Evaluation of the git response from the push command - configuration of the hg environment and the authentication, so that the scmhooks.py script can be triggeret correctly and can callback the scm manager
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import sonia.scm.repository.util.CloseableWrapper;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
@@ -34,11 +34,19 @@ package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.transport.ScmTransportProtocol;
|
||||
import org.eclipse.jgit.transport.Transport;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.PreProcessorUtil;
|
||||
import sonia.scm.repository.api.HookContextFactory;
|
||||
import sonia.scm.store.InMemoryConfigurationStoreFactory;
|
||||
|
||||
import static com.google.inject.util.Providers.of;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -105,4 +113,5 @@ public class AbstractGitCommandTestBase extends ZippedRepositoryTestBase
|
||||
|
||||
/** Field description */
|
||||
private GitContext context;
|
||||
private ScmTransportProtocol scmTransportProtocol;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.eclipse.jgit.transport.ScmTransportProtocol;
|
||||
import org.eclipse.jgit.transport.Transport;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.PreProcessorUtil;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.api.HookContextFactory;
|
||||
|
||||
import static com.google.inject.util.Providers.of;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class BindTransportProtocolRule extends ExternalResource {
|
||||
|
||||
private ScmTransportProtocol scmTransportProtocol;
|
||||
|
||||
@Override
|
||||
protected void before() throws Throwable {
|
||||
HookContextFactory hookContextFactory = new HookContextFactory(mock(PreProcessorUtil.class));
|
||||
RepositoryManager repositoryManager = mock(RepositoryManager.class);
|
||||
HookEventFacade hookEventFacade = new HookEventFacade(of(repositoryManager), hookContextFactory);
|
||||
GitRepositoryHandler gitRepositoryHandler = mock(GitRepositoryHandler.class);
|
||||
scmTransportProtocol = new ScmTransportProtocol(of(hookEventFacade), of(gitRepositoryHandler));
|
||||
|
||||
Transport.register(scmTransportProtocol);
|
||||
|
||||
when(gitRepositoryHandler.getRepositoryId(any())).thenReturn("1");
|
||||
when(repositoryManager.get("1")).thenReturn(new sonia.scm.repository.Repository());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() {
|
||||
Transport.unregister(scmTransportProtocol);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import sonia.scm.repository.Branch;
|
||||
|
||||
@@ -10,13 +11,16 @@ import java.util.List;
|
||||
|
||||
public class GitBranchCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@Rule
|
||||
public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule();
|
||||
|
||||
@Test
|
||||
public void shouldCreateBranch() throws IOException {
|
||||
GitContext context = createContext();
|
||||
|
||||
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals("new_branch")).isEmpty();
|
||||
|
||||
new GitBranchCommand(context, repository).branch("new_branch");
|
||||
new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory()).branch("new_branch");
|
||||
|
||||
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals("new_branch")).isNotEmpty();
|
||||
}
|
||||
|
||||
@@ -41,27 +41,8 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private ScmTransportProtocol scmTransportProtocol;
|
||||
|
||||
@Before
|
||||
public void bindScmProtocol() {
|
||||
HookContextFactory hookContextFactory = new HookContextFactory(mock(PreProcessorUtil.class));
|
||||
RepositoryManager repositoryManager = mock(RepositoryManager.class);
|
||||
HookEventFacade hookEventFacade = new HookEventFacade(of(repositoryManager), hookContextFactory);
|
||||
GitRepositoryHandler gitRepositoryHandler = mock(GitRepositoryHandler.class);
|
||||
scmTransportProtocol = new ScmTransportProtocol(of(hookEventFacade), of(gitRepositoryHandler));
|
||||
|
||||
Transport.register(scmTransportProtocol);
|
||||
|
||||
when(gitRepositoryHandler.getRepositoryId(any())).thenReturn("1");
|
||||
when(repositoryManager.get("1")).thenReturn(new sonia.scm.repository.Repository());
|
||||
}
|
||||
|
||||
@After
|
||||
public void unregisterScmProtocol() {
|
||||
Transport.unregister(scmTransportProtocol);
|
||||
}
|
||||
@Rule
|
||||
public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule();
|
||||
|
||||
@Test
|
||||
public void shouldDetectMergeableBranches() {
|
||||
|
||||
@@ -12,6 +12,7 @@ import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.PreProcessorUtil;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.api.HookContextFactory;
|
||||
import sonia.scm.repository.util.WorkingCopy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -44,7 +45,7 @@ public class SimpleGitWorkdirFactoryTest extends AbstractGitCommandTestBase {
|
||||
SimpleGitWorkdirFactory factory = new SimpleGitWorkdirFactory(temporaryFolder.newFolder());
|
||||
File masterRepo = createRepositoryDirectory();
|
||||
|
||||
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
try (WorkingCopy<Repository> workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
|
||||
assertThat(workingCopy.get().getDirectory())
|
||||
.exists()
|
||||
@@ -57,25 +58,15 @@ public class SimpleGitWorkdirFactoryTest extends AbstractGitCommandTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloneFromPoolShouldBeClosed() throws IOException {
|
||||
PoolWithSpy factory = new PoolWithSpy(temporaryFolder.newFolder());
|
||||
|
||||
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
assertThat(workingCopy).isNotNull();
|
||||
}
|
||||
verify(factory.createdClone).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloneFromPoolShouldNotBeReused() throws IOException {
|
||||
SimpleGitWorkdirFactory factory = new SimpleGitWorkdirFactory(temporaryFolder.newFolder());
|
||||
|
||||
File firstDirectory;
|
||||
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
try (WorkingCopy<Repository> workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
firstDirectory = workingCopy.get().getDirectory();
|
||||
}
|
||||
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
try (WorkingCopy<Repository> workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
File secondDirectory = workingCopy.get().getDirectory();
|
||||
assertThat(secondDirectory).isNotEqualTo(firstDirectory);
|
||||
}
|
||||
@@ -86,23 +77,9 @@ public class SimpleGitWorkdirFactoryTest extends AbstractGitCommandTestBase {
|
||||
SimpleGitWorkdirFactory factory = new SimpleGitWorkdirFactory(temporaryFolder.newFolder());
|
||||
|
||||
File directory;
|
||||
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
try (WorkingCopy<Repository> workingCopy = factory.createWorkingCopy(createContext())) {
|
||||
directory = workingCopy.get().getWorkTree();
|
||||
}
|
||||
assertThat(directory).doesNotExist();
|
||||
}
|
||||
|
||||
private static class PoolWithSpy extends SimpleGitWorkdirFactory {
|
||||
PoolWithSpy(File poolDirectory) {
|
||||
super(poolDirectory);
|
||||
}
|
||||
|
||||
Repository createdClone;
|
||||
|
||||
@Override
|
||||
protected Repository cloneRepository(File bareRepository, File destination) throws GitAPIException {
|
||||
createdClone = spy(super.cloneRepository(bareRepository, destination));
|
||||
return createdClone;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user