From 9c99073419d0f54109ca7279e44bf542976ce1f1 Mon Sep 17 00:00:00 2001 From: Adrian Date: Thu, 18 Jul 2013 22:12:50 -0400 Subject: [PATCH 1/3] Added marketplace link --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 44bc969..e50125c 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,8 @@ The following is a plugin for Atlassian Stash to provide repository mirroring to Full documentation is always available at: https://developer.atlassian.com/display/DOCS/Introduction+to+the+Atlassian+Plugin+SDK + + + +The plugin can be found on the Atlassian Marketplace here: +https://marketplace.atlassian.com/plugins/com.englishtown.stash-hook-mirror From 3ccc1073b2676c2f85177b3e6889c97276c8db07 Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Fri, 26 Jul 2013 12:10:38 -0400 Subject: [PATCH 2/3] Removed dependency stash-scm-git, use reflection to get at GitCommandExitHandler (fixes #12) --- pom.xml | 5 +--- .../stash/hook/MirrorRepositoryHook.java | 28 +++++++++++++++---- .../stash/hook/MirrorRepositoryHookTest.java | 9 +++--- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 4ed6f71..651d39e 100644 --- a/pom.xml +++ b/pom.xml @@ -65,10 +65,6 @@ ${slf4j.version} provided - - com.atlassian.stash - stash-scm-git - com.atlassian.stash stash-api @@ -100,6 +96,7 @@ commons-lang ${common-lang.version} + com.atlassian.stash diff --git a/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java b/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java index 460f908..c8eccf7 100644 --- a/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java +++ b/src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java @@ -5,10 +5,10 @@ import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; import com.atlassian.stash.hook.repository.AsyncPostReceiveRepositoryHook; import com.atlassian.stash.hook.repository.RepositoryHookContext; import com.atlassian.stash.i18n.I18nService; -import com.atlassian.stash.internal.scm.git.GitCommandExitHandler; import com.atlassian.stash.repository.RefChange; import com.atlassian.stash.repository.Repository; import com.atlassian.stash.scm.CommandExitHandler; +import com.atlassian.stash.scm.DefaultCommandExitHandler; import com.atlassian.stash.scm.git.GitScm; import com.atlassian.stash.scm.git.GitScmCommandBuilder; import com.atlassian.stash.setting.RepositorySettingsValidator; @@ -18,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; +import java.lang.reflect.Method; import java.net.URI; import java.net.URISyntaxException; import java.util.*; @@ -111,8 +112,7 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep public Void call() throws Exception { try { GitScmCommandBuilder builder = gitScm.getCommandBuilderFactory().builder(repository); - CommandExitHandler exitHandler = new GitCommandExitHandler(i18nService, repository); - PasswordHandler passwordHandler = new PasswordHandler(password, exitHandler); + PasswordHandler passwordHandler = getPasswordHandler(builder, password); // Call push command with the prune flag and refspecs for heads and tags // Do not use the mirror flag as pull-request refs are included @@ -130,12 +130,12 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep logger.debug("MirrorRepositoryHook: postReceive completed with result '{}'.", result); } catch (Exception e) { - if (++attempts > MAX_ATTEMPTS) { - logger.error("Failed to mirror repository " + repository.getName() + " after " + --attempts + if (++attempts >= MAX_ATTEMPTS) { + logger.error("Failed to mirror repository " + repository.getName() + " after " + attempts + " attempts.", e); } else { logger.warn("Failed to mirror repository " + repository.getName() + ", " + - "retrying in 1 minute."); + "retrying in 1 minute (attempt {} of {}).", attempts, MAX_ATTEMPTS); executor.schedule(this, 1, TimeUnit.MINUTES); } } @@ -306,4 +306,20 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep } + protected PasswordHandler getPasswordHandler(GitScmCommandBuilder builder, String password) { + + try { + Method method = builder.getClass().getDeclaredMethod("createExitHandler"); + method.setAccessible(true); + CommandExitHandler exitHandler = (CommandExitHandler) method.invoke(builder); + + return new PasswordHandler(password, exitHandler); + + } catch (Throwable t) { + logger.warn("Unable to create exit handler", t); + } + + return new PasswordHandler(password, new DefaultCommandExitHandler(i18nService)); + } + } diff --git a/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java b/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java index b59db33..a60ea6a 100644 --- a/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java +++ b/src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java @@ -149,12 +149,11 @@ public class MirrorRepositoryHookTest { callable = argumentCaptor.getValue(); callable.call(); - verify(executor, times(5)).schedule(argumentCaptor.capture(), anyInt(), any(TimeUnit.class)); - callable = argumentCaptor.getValue(); - callable.call(); - // Make sure it is only called 5 times - verify(executor, times(5)).schedule(argumentCaptor.capture(), anyInt(), any(TimeUnit.class)); + callable.call(); + callable.call(); + callable.call(); + verify(executor, times(4)).schedule(argumentCaptor.capture(), anyInt(), any(TimeUnit.class)); } From 234c1faf06dce39d9303f7fc6c5d6c9a0474f3b4 Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Fri, 26 Jul 2013 14:35:12 -0400 Subject: [PATCH 3/3] Bumped version to 1.5.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 651d39e..1f0dbb8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.englishtown stash-hook-mirror - 1.4.0 + 1.5.0 Englishtown