Browse Source

Merge pull request #20 from xaviershay/issue-19

Don't mirror empty repositories.
pull/40/head
Adrian 11 years ago
parent
commit
387d936793
  1. 10
      src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java
  2. 53
      src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java

10
src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java

@ -7,6 +7,7 @@ import com.atlassian.stash.hook.repository.RepositoryHookContext;
import com.atlassian.stash.i18n.I18nService; import com.atlassian.stash.i18n.I18nService;
import com.atlassian.stash.repository.RefChange; import com.atlassian.stash.repository.RefChange;
import com.atlassian.stash.repository.Repository; import com.atlassian.stash.repository.Repository;
import com.atlassian.stash.repository.RepositoryMetadataService;
import com.atlassian.stash.scm.CommandExitHandler; import com.atlassian.stash.scm.CommandExitHandler;
import com.atlassian.stash.scm.DefaultCommandExitHandler; import com.atlassian.stash.scm.DefaultCommandExitHandler;
import com.atlassian.stash.scm.git.GitScm; import com.atlassian.stash.scm.git.GitScm;
@ -46,6 +47,8 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
private final ScheduledExecutorService executor; private final ScheduledExecutorService executor;
private final PasswordEncryptor passwordEncryptor; private final PasswordEncryptor passwordEncryptor;
private final SettingsReflectionHelper settingsReflectionHelper; private final SettingsReflectionHelper settingsReflectionHelper;
private final RepositoryMetadataService repositoryMetadataService;
private static final Logger logger = LoggerFactory.getLogger(MirrorRepositoryHook.class); private static final Logger logger = LoggerFactory.getLogger(MirrorRepositoryHook.class);
public MirrorRepositoryHook( public MirrorRepositoryHook(
@ -54,7 +57,8 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
ScheduledExecutorService executor, ScheduledExecutorService executor,
PasswordEncryptor passwordEncryptor, PasswordEncryptor passwordEncryptor,
SettingsReflectionHelper settingsReflectionHelper, SettingsReflectionHelper settingsReflectionHelper,
PluginSettingsFactory pluginSettingsFactory PluginSettingsFactory pluginSettingsFactory,
RepositoryMetadataService repositoryMetadataService
) { ) {
logger.debug("MirrorRepositoryHook: init started"); logger.debug("MirrorRepositoryHook: init started");
@ -64,6 +68,7 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
this.executor = executor; this.executor = executor;
this.passwordEncryptor = passwordEncryptor; this.passwordEncryptor = passwordEncryptor;
this.settingsReflectionHelper = settingsReflectionHelper; this.settingsReflectionHelper = settingsReflectionHelper;
this.repositoryMetadataService = repositoryMetadataService;
// Init password encryptor // Init password encryptor
PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(PLUGIN_SETTINGS_KEY); PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(PLUGIN_SETTINGS_KEY);
@ -99,6 +104,9 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
} }
void runMirrorCommand(MirrorSettings settings, final Repository repository) { void runMirrorCommand(MirrorSettings settings, final Repository repository) {
if (repositoryMetadataService.isEmpty(repository)) {
return;
}
try { try {
final String password = passwordEncryptor.decrypt(settings.password); final String password = passwordEncryptor.decrypt(settings.password);

53
src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java

@ -6,6 +6,7 @@ import com.atlassian.stash.hook.repository.RepositoryHookContext;
import com.atlassian.stash.i18n.I18nService; import com.atlassian.stash.i18n.I18nService;
import com.atlassian.stash.repository.RefChange; import com.atlassian.stash.repository.RefChange;
import com.atlassian.stash.repository.Repository; import com.atlassian.stash.repository.Repository;
import com.atlassian.stash.repository.RepositoryMetadataService;
import com.atlassian.stash.scm.CommandErrorHandler; import com.atlassian.stash.scm.CommandErrorHandler;
import com.atlassian.stash.scm.CommandExitHandler; import com.atlassian.stash.scm.CommandExitHandler;
import com.atlassian.stash.scm.CommandOutputHandler; import com.atlassian.stash.scm.CommandOutputHandler;
@ -20,6 +21,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Captor; import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
@ -55,6 +57,8 @@ public class MirrorRepositoryHookTest {
private PluginSettingsFactory pluginSettingsFactory; private PluginSettingsFactory pluginSettingsFactory;
@Mock @Mock
private PluginSettings pluginSettings; private PluginSettings pluginSettings;
@Mock
private RepositoryMetadataService repositoryMetadataService;
private final String mirrorRepoUrlHttp = "https://stash-mirror.englishtown.com/scm/test/test.git"; private final String mirrorRepoUrlHttp = "https://stash-mirror.englishtown.com/scm/test/test.git";
private final String mirrorRepoUrlSsh = "ssh://git@stash-mirror.englishtown.com/scm/test/test.git"; private final String mirrorRepoUrlSsh = "ssh://git@stash-mirror.englishtown.com/scm/test/test.git";
@ -85,35 +89,29 @@ public class MirrorRepositoryHookTest {
when(pluginSettingsFactory.createSettingsForKey(anyString())).thenReturn(pluginSettings); when(pluginSettingsFactory.createSettingsForKey(anyString())).thenReturn(pluginSettings);
hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, passwordEncryptor hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, passwordEncryptor
, settingsReflectionHelper, pluginSettingsFactory); , settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService);
} }
@Test @Test
public void testPostReceive() throws Exception { public void testPostReceive() throws Exception {
when(passwordEncryptor.decrypt(anyString())).thenReturn(password); when(passwordEncryptor.decrypt(anyString())).thenReturn(password);
Map<String, Object> map = new HashMap<String, Object>();
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, "");
Settings settings = mock(Settings.class);
when(settings.asMap()).thenReturn(map);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_USERNAME), eq(""))).thenReturn(username);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password);
Repository repo = mock(Repository.class); Repository repo = mock(Repository.class);
when(repo.getName()).thenReturn("test"); when(repo.getName()).thenReturn("test");
RepositoryHookContext context = mock(RepositoryHookContext.class); hook.postReceive(buildContext(repo), new ArrayList<RefChange>());
when(context.getSettings()).thenReturn(settings); verifyExecutor();
when(context.getRepository()).thenReturn(repo); }
@Test
public void testEmptyRepositoriesNotMirrored() {
Repository repo = mock(Repository.class);
when(repositoryMetadataService.isEmpty(repo)).thenReturn(true);
Collection<RefChange> refChanges = new ArrayList<RefChange>(); hook.postReceive(buildContext(repo), new ArrayList<RefChange>());
hook.postReceive(context, refChanges); verify(executor, never()).submit(Matchers.<Callable<Object>>any());
verifyExecutor();
} }
@Test @Test
@ -122,7 +120,7 @@ public class MirrorRepositoryHookTest {
GitScm gitScm = mock(GitScm.class); GitScm gitScm = mock(GitScm.class);
when(gitScm.getCommandBuilderFactory()).thenThrow(new RuntimeException("Intentional unit test exception")); when(gitScm.getCommandBuilderFactory()).thenThrow(new RuntimeException("Intentional unit test exception"));
MirrorRepositoryHook hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, MirrorRepositoryHook hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor,
passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory); passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService);
MirrorRepositoryHook.MirrorSettings ms = new MirrorRepositoryHook.MirrorSettings(); MirrorRepositoryHook.MirrorSettings ms = new MirrorRepositoryHook.MirrorSettings();
ms.mirrorRepoUrl = mirrorRepoUrlHttp; ms.mirrorRepoUrl = mirrorRepoUrlHttp;
ms.username = username; ms.username = username;
@ -267,4 +265,23 @@ public class MirrorRepositoryHookTest {
} }
private RepositoryHookContext buildContext(Repository repo) {
RepositoryHookContext context = mock(RepositoryHookContext.class);
Settings settings = defaultSettings();
when(context.getSettings()).thenReturn(settings);
when(context.getRepository()).thenReturn(repo);
return context;
}
private Settings defaultSettings() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, "");
Settings settings = mock(Settings.class);
when(settings.asMap()).thenReturn(map);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_USERNAME), eq(""))).thenReturn(username);
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password);
return settings;
}
} }

Loading…
Cancel
Save