Browse Source

Merge branch 'release/1.8.0'

pull/21/head 1.8.0
Adrian Gonzalez 11 years ago
parent
commit
6265a01b47
  1. 2
      pom.xml
  2. 10
      src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java
  3. 53
      src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java

2
pom.xml

@ -6,7 +6,7 @@
<groupId>com.englishtown</groupId>
<artifactId>stash-hook-mirror</artifactId>
<version>1.7.0</version>
<version>1.8.0</version>
<organization>
<name>Englishtown</name>

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.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);

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.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<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);
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<RefChange>());
verifyExecutor();
}
@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);
verifyExecutor();
verify(executor, never()).submit(Matchers.<Callable<Object>>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<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