Browse Source

Merge branch 'release/1.7.0'

pull/21/head 1.7.0
Adrian Gonzalez 11 years ago
parent
commit
59c169b944
  1. 2
      pom.xml
  2. 45
      src/main/java/com/englishtown/stash/hook/MirrorRepositoryHook.java
  3. 2
      src/main/resources/static/mirror-repository-hook.soy
  4. 12
      src/test/java/com/englishtown/stash/hook/MirrorRepositoryHookTest.java

2
pom.xml

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

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

@ -102,7 +102,7 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
try { try {
final String password = passwordEncryptor.decrypt(settings.password); final String password = passwordEncryptor.decrypt(settings.password);
final URI authenticatedUrl = getAuthenticatedUrl(settings.mirrorRepoUrl, settings.username, password); final String authenticatedUrl = getAuthenticatedUrl(settings.mirrorRepoUrl, settings.username, password);
executor.submit(new Callable<Void>() { executor.submit(new Callable<Void>() {
@ -119,7 +119,7 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
String result = builder String result = builder
.command("push") .command("push")
.argument("--prune") // this deletes locally deleted branches .argument("--prune") // this deletes locally deleted branches
.argument(authenticatedUrl.toString()) .argument(authenticatedUrl)
.argument("+refs/heads/*:refs/heads/*") // Only mirror heads .argument("+refs/heads/*:refs/heads/*") // Only mirror heads
.argument("+refs/tags/*:refs/tags/*") // and tags .argument("+refs/tags/*:refs/tags/*") // and tags
.errorHandler(passwordHandler) .errorHandler(passwordHandler)
@ -149,19 +149,18 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
} }
} }
protected URI getAuthenticatedUrl(String mirrorRepoUrl, String username, String password) throws URISyntaxException { protected String getAuthenticatedUrl(String mirrorRepoUrl, String username, String password) throws URISyntaxException {
URI uri = URI.create(mirrorRepoUrl); // Only http(s) has username/password
if (!mirrorRepoUrl.toLowerCase().startsWith("http")) {
// ssh doesn't have username/password return mirrorRepoUrl;
if (uri.getScheme().equals("ssh")) {
return uri;
} }
URI uri = URI.create(mirrorRepoUrl);
String userInfo = username + ":" + password; String userInfo = username + ":" + password;
return new URI(uri.getScheme(), userInfo, uri.getHost(), uri.getPort(), return new URI(uri.getScheme(), userInfo, uri.getHost(), uri.getPort(),
uri.getPath(), uri.getQuery(), uri.getFragment()); uri.getPath(), uri.getQuery(), uri.getFragment()).toString();
} }
@ -232,41 +231,26 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
boolean result = true; boolean result = true;
boolean isHttp = false; boolean isHttp = false;
boolean isSsh = false;
if (ms.mirrorRepoUrl.isEmpty()) { if (ms.mirrorRepoUrl.isEmpty()) {
result = false; result = false;
errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix, "The mirror repo url is required."); errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix, "The mirror repo url is required.");
} else { } else {
URI uri;
try { try {
uri = URI.create(ms.mirrorRepoUrl); URI uri = URI.create(ms.mirrorRepoUrl);
String scheme = uri.getScheme().toLowerCase(); String scheme = uri.getScheme().toLowerCase();
if (scheme.equals("ssh")) { if (scheme.startsWith("http")) {
isSsh = true;
if (!ms.mirrorRepoUrl.startsWith("ssh://git@")) {
result = false;
errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix,
"An ssh URL should start with ssh://git@");
}
} else if (scheme.startsWith("http")) {
isHttp = true; isHttp = true;
if (ms.mirrorRepoUrl.contains("@")) { if (ms.mirrorRepoUrl.contains("@")) {
result = false; result = false;
errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix, errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix,
"The username and password should not be included."); "The username and password should not be included.");
} }
} else {
result = false;
errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix,
"The mirror repo url must be a ssh or http(s) URL.");
} }
} catch (Exception ex) { } catch (Exception ex) {
result = false; // Not a valid url, assume it is something git can read
errors.addFieldError(SETTING_MIRROR_REPO_URL + ms.suffix,
"The mirror repo url must be a valid ssh or http(s) URL.");
} }
} }
@ -281,9 +265,8 @@ public class MirrorRepositoryHook implements AsyncPostReceiveRepositoryHook, Rep
result = false; result = false;
errors.addFieldError(SETTING_PASSWORD + ms.suffix, "The password is required when using http(s)."); errors.addFieldError(SETTING_PASSWORD + ms.suffix, "The password is required when using http(s).");
} }
} } else {
// SSH should not have username or password // Only http should have username or password
if (isSsh) {
ms.password = ms.username = ""; ms.password = ms.username = "";
} }

2
src/main/resources/static/mirror-repository-hook.soy

@ -48,7 +48,7 @@
{/param} {/param}
{param isRequired: true /} {param isRequired: true /}
{param descriptionText: stash_i18n('com.englishtown.stash.hook.mirror.strings.mirrorRepoUrl.description', {param descriptionText: stash_i18n('com.englishtown.stash.hook.mirror.strings.mirrorRepoUrl.description',
'The ssh or http(s) URL to the remote mirrored repo') /} 'The GIT URL (ssh, git, http(s), file) to the remote mirrored repo') /}
{param extraClasses: 'long et-mirror-repo' /} {param extraClasses: 'long et-mirror-repo' /}
{param errorTexts: $errors ? $errors['mirrorRepoUrl' + $index] : null /} {param errorTexts: $errors ? $errors['mirrorRepoUrl' + $index] : null /}
{/call} {/call}

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

@ -175,10 +175,8 @@ public class MirrorRepositoryHookTest {
@Test @Test
public void testGetAuthenticatedUrl() throws Exception { public void testGetAuthenticatedUrl() throws Exception {
URI result; String result = hook.getAuthenticatedUrl(mirrorRepoUrlHttp, username, password);
assertEquals(repository, result);
result = hook.getAuthenticatedUrl(mirrorRepoUrlHttp, username, password);
assertEquals(repository, result.toString());
} }
@ -239,8 +237,8 @@ public class MirrorRepositoryHookTest {
errors = mock(SettingsValidationErrors.class); errors = mock(SettingsValidationErrors.class);
hook.validate(settings, errors, repo); hook.validate(settings, errors, repo);
verify(errors, never()).addFormError(anyString()); verify(errors, never()).addFormError(anyString());
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString()); verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString());
verify(errors).addFieldError(anyString(), anyString()); verify(errors, never()).addFieldError(anyString(), anyString());
errors = mock(SettingsValidationErrors.class); errors = mock(SettingsValidationErrors.class);
hook.validate(settings, errors, repo); hook.validate(settings, errors, repo);
@ -252,7 +250,7 @@ public class MirrorRepositoryHookTest {
errors = mock(SettingsValidationErrors.class); errors = mock(SettingsValidationErrors.class);
hook.validate(settings, errors, repo); hook.validate(settings, errors, repo);
verify(errors, never()).addFormError(anyString()); verify(errors, never()).addFormError(anyString());
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString()); verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString());
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_USERNAME + "0"), anyString()); verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_USERNAME + "0"), anyString());
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_PASSWORD + "0"), anyString()); verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_PASSWORD + "0"), anyString());

Loading…
Cancel
Save