diff --git a/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java b/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java index e29d5de..4db4529 100644 --- a/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java +++ b/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.repository.RefChange; import com.atlassian.stash.repository.Repository; +import com.atlassian.stash.repository.RepositoryMetadataService; import com.atlassian.stash.scm.CommandExitHandler; import com.atlassian.stash.scm.DefaultCommandExitHandler; import com.atlassian.stash.scm.git.GitScm; @@ -46,6 +47,8 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep private final ScheduledExecutorService executor; private final PasswordEncryptor passwordEncryptor; private final SettingsReflectionHelper settingsReflectionHelper; + private final RepositoryMetadataService repositoryMetadataService; + private static final Logger logger = LoggerFactory.getLogger(MirrorRepositoryHook.class); public MirrorRepositoryHook( @@ -54,7 +57,8 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep ScheduledExecutorService executor, PasswordEncryptor passwordEncryptor, SettingsReflectionHelper settingsReflectionHelper, - PluginSettingsFactory pluginSettingsFactory + PluginSettingsFactory pluginSettingsFactory, + RepositoryMetadataService repositoryMetadataService ) { logger.debug("MirrorRepositoryHook: init started"); @@ -64,6 +68,7 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep this.executor = executor; this.passwordEncryptor = passwordEncryptor; this.settingsReflectionHelper = settingsReflectionHelper; + this.repositoryMetadataService = repositoryMetadataService; // Init password encryptor PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(PLUGIN_SETTINGS_KEY); @@ -99,6 +104,9 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep } void runMirrorCommand(MirrorSettings settings, final Repository repository) { + if (repositoryMetadataService.isEmpty(repository)) { + return; + } try { final String password = passwordEncryptor.decrypt(settings.password); diff --git a/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java b/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java index 4e88203..daef955 100644 --- a/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java +++ b/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.repository.RefChange; import com.atlassian.stash.repository.Repository; +import com.atlassian.stash.repository.RepositoryMetadataService; import com.atlassian.stash.scm.CommandErrorHandler; import com.atlassian.stash.scm.CommandExitHandler; import com.atlassian.stash.scm.CommandOutputHandler; @@ -20,6 +21,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; +import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @@ -55,6 +57,8 @@ public class MirrorRepositoryHookTest { private PluginSettingsFactory pluginSettingsFactory; @Mock private PluginSettings pluginSettings; + @Mock + private RepositoryMetadataService repositoryMetadataService; 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"; @@ -85,35 +89,29 @@ public class MirrorRepositoryHookTest { when(pluginSettingsFactory.createSettingsForKey(anyString())).thenReturn(pluginSettings); hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, passwordEncryptor - , settingsReflectionHelper, pluginSettingsFactory); + , settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService); } @Test public void testPostReceive() throws Exception { - when(passwordEncryptor.decrypt(anyString())).thenReturn(password); - Map map = new HashMap(); - 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); when(repo.getName()).thenReturn("test"); - RepositoryHookContext context = mock(RepositoryHookContext.class); - when(context.getSettings()).thenReturn(settings); - when(context.getRepository()).thenReturn(repo); + hook.postReceive(buildContext(repo), new ArrayList()); + verifyExecutor(); + } + + @Test + public void testEmptyRepositoriesNotMirrored() { + Repository repo = mock(Repository.class); + when(repositoryMetadataService.isEmpty(repo)).thenReturn(true); - Collection refChanges = new ArrayList(); + hook.postReceive(buildContext(repo), new ArrayList()); - hook.postReceive(context, refChanges); - verifyExecutor(); + verify(executor, never()).submit(Matchers.>any()); } @Test @@ -122,7 +120,7 @@ public class MirrorRepositoryHookTest { GitScm gitScm = mock(GitScm.class); when(gitScm.getCommandBuilderFactory()).thenThrow(new RuntimeException("Intentional unit test exception")); MirrorRepositoryHook hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, - passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory); + passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService); MirrorRepositoryHook.MirrorSettings ms = new MirrorRepositoryHook.MirrorSettings(); ms.mirrorRepoUrl = mirrorRepoUrlHttp; 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 map = new HashMap(); + 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; + } }