more tests for url support + try to retrieve current version in unclear situation
authorPavel Sher <pavel.sher@gmail.com>
Sun, 24 Nov 2013 14:25:44 +0000 (15:25 +0100)
committerPavel Sher <pavel.sher@gmail.com>
Sun, 24 Nov 2013 14:25:44 +0000 (15:25 +0100)
git-server/src/jetbrains/buildServer/buildTriggers/vcs/git/GitUrlSupport.java
git-server/src/jetbrains/buildServer/buildTriggers/vcs/git/GitVcsSupport.java
git-tests/src/jetbrains/buildServer/buildTriggers/vcs/git/tests/GitUrlSupportTest.java

index 3c1cb48745f4aa1d6ffe940a58618cf4b147cd7f..a2172aed247b05da78a1dbcf3242123aba31f7b3 100644 (file)
@@ -18,6 +18,8 @@ package jetbrains.buildServer.buildTriggers.vcs.git;
 
 import jetbrains.buildServer.util.StringUtil;
 import jetbrains.buildServer.vcs.*;
+import jetbrains.buildServer.vcs.impl.VcsRootImpl;
+import org.eclipse.jgit.errors.NoRemoteRepositoryException;
 import org.eclipse.jgit.transport.URIish;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -35,41 +37,62 @@ public class GitUrlSupport implements UrlSupport {
 
   private static final String PROVIDER_SCHEMA = "git:ssh";
   private static final Collection<String> PROVIDER_SCHEMA_LIST = StringUtil.split(PROVIDER_SCHEMA, ":");
+  private final GitVcsSupport myGitSupport;
+
+  public GitUrlSupport(@NotNull GitVcsSupport gitSupport) {
+    myGitSupport = gitSupport;
+  }
 
   @Nullable
   public Map<String, String> convertToVcsRootProperties(@NotNull VcsUrl url) throws VcsException {
+    String fetchUrl = url.getUrl();
+    boolean testRequired = !fetchUrl.contains("git");
     MavenVcsUrl vcsUrl = url.asMavenVcsUrl();
-    if (vcsUrl == null) return null;
+    if (vcsUrl != null) {
+      final String providerSchema = vcsUrl.getProviderSchema();
+      if(!PROVIDER_SCHEMA_LIST.contains(providerSchema))
+        return null;
 
-    final String providerSchema = vcsUrl.getProviderSchema();
-    if(!PROVIDER_SCHEMA_LIST.contains(providerSchema))
-      return null;
+      fetchUrl = vcsUrl.getProviderSpecificPart();
+      testRequired = false;
+    }
 
     URIish uri;
     try {
-      uri = new URIish(vcsUrl.getProviderSpecificPart());
+      uri = new URIish(fetchUrl);
     } catch (URISyntaxException e) {
       throw new VcsException(e.getMessage(), e);
     }
 
-    Credentials credentials = vcsUrl.getCredentials();
-    Map<String, String> result = new HashMap<String, String>();
-    result.put(Constants.FETCH_URL, vcsUrl.getProviderSpecificPart());
+    Credentials credentials = url.getCredentials();
+    Map<String, String> props = new HashMap<String, String>();
+    props.put(Constants.FETCH_URL, fetchUrl);
     if (credentials != null) {
-      result.put(Constants.USERNAME, credentials.getUsername());
+      props.put(Constants.USERNAME, credentials.getUsername());
     }
     final boolean scpSyntax = isScpSyntax(uri);
     if (scpSyntax || "ssh".equals(uri.getScheme())) {
       if (scpSyntax && credentials == null) {
-        result.put(Constants.USERNAME, uri.getUser());
+        props.put(Constants.USERNAME, uri.getUser());
       }
-      result.put(Constants.AUTH_METHOD, AuthenticationMethod.PRIVATE_KEY_DEFAULT.toString());
-      result.put(Constants.IGNORE_KNOWN_HOSTS, "true");
-    } else if (credentials != null && !StringUtil.isEmptyOrSpaces(vcsUrl.getProviderSpecificPart())) {
-      result.put(Constants.AUTH_METHOD, AuthenticationMethod.PASSWORD.toString());
-      result.put(Constants.PASSWORD, credentials.getPassword());
+      props.put(Constants.AUTH_METHOD, AuthenticationMethod.PRIVATE_KEY_DEFAULT.toString());
+      props.put(Constants.IGNORE_KNOWN_HOSTS, "true");
+    } else if (credentials != null && !StringUtil.isEmptyOrSpaces(fetchUrl)) {
+      props.put(Constants.AUTH_METHOD, AuthenticationMethod.PASSWORD.toString());
+      props.put(Constants.PASSWORD, credentials.getPassword());
     }
-    return result;
+
+    if (testRequired) {
+      try {
+        myGitSupport.getCurrentVersion(new VcsRootImpl(-1, Constants.VCS_NAME, props));
+      } catch (VcsException e) {
+        if (e.getCause() instanceof NoRemoteRepositoryException) {
+          return null; // definitely not git
+        }
+      }
+    }
+
+    return props;
   }
 
   private boolean isScpSyntax(URIish uriish) {
index 6793b26ed7a82ddadf4c734619ca9a5ba8c5f05f..c156179225e073c7b4ec31b557911bb958e19663 100755 (executable)
@@ -340,7 +340,7 @@ public class GitVcsSupport extends ServerVcsSupport
 
   @Override
   public UrlSupport getUrlSupport() {
-    return new GitUrlSupport();
+    return new GitUrlSupport(this);
   }
 
 
index de2cefea707a4979040e7ba77c5ca80cd97301de..da040d222a983d570ac77acaca229656f5bffad6 100644 (file)
@@ -25,6 +25,7 @@ import jetbrains.buildServer.vcs.VcsException;
 import jetbrains.buildServer.vcs.*;
 import jetbrains.buildServer.vcs.impl.VcsRootImpl;
 import org.eclipse.jgit.transport.URIish;
+import org.jetbrains.annotations.NotNull;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -36,21 +37,25 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import static jetbrains.buildServer.buildTriggers.vcs.git.tests.GitSupportBuilder.gitSupport;
+
 /**
  * @author dmitry.neverov
  */
 public class GitUrlSupportTest extends BaseTestCase {
 
   private TempFiles myTempFiles = new TempFiles();
-  private GitUrlSupport mySupport;
+  private GitUrlSupport myUrlSupport;
   private MirrorManager myMirrorManager;
 
   @BeforeMethod
   public void setUp() throws IOException {
-    mySupport = new GitUrlSupport();
     ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
     PluginConfig config = new PluginConfigBuilder(paths).build();
     myMirrorManager = new MirrorManagerImpl(config, new HashCalculatorImpl());
+
+    GitVcsSupport vcsSupport = gitSupport().withServerPaths(paths).build();
+    myUrlSupport = new GitUrlSupport(vcsSupport);
   }
 
   @AfterMethod
@@ -86,8 +91,8 @@ public class GitUrlSupportTest extends BaseTestCase {
 
   @Test
   public void should_throw_exception_when_url_incorrect() throws MalformedURLException, VcsException {
-    MavenVcsUrl url = new MavenVcsUrl("scm:svn:ssh://svn.repo.com/path_to_repository");
-    assertNull(mySupport.convertToVcsRootProperties(url));
+    VcsUrl url = new VcsUrl("scm:svn:ssh://svn.repo.com/path_to_repository");
+    assertNull(myUrlSupport.convertToVcsRootProperties(url));
   }
 
 
@@ -103,12 +108,29 @@ public class GitUrlSupportTest extends BaseTestCase {
 
   @Test
   public void convert_scp_like_syntax_with_credentials() throws Exception {
-    MavenVcsUrl url = new MavenVcsUrl("scm:git:git@github.com:user/repo.git", new Credentials("user", "pass"));
+    VcsUrl url = new VcsUrl("scm:git:git@github.com:user/repo.git", new Credentials("user", "pass"));
     GitVcsRoot root = toGitRoot(url);
     assertEquals(new URIish("user@github.com:user/repo.git"), root.getRepositoryFetchURL());
     assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
     assertEquals("user", root.getAuthSettings().toMap().get(Constants.USERNAME));
     assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
+
+    assertEquals(root.getProperties(),
+                 myUrlSupport.convertToVcsRootProperties(new VcsUrl("git@github.com:user/repo.git", new Credentials("user", "pass"))));
+  }
+
+  @Test
+  public void http_protocol() throws Exception {
+    VcsUrl url = new VcsUrl("http://git.jetbrains.org/teamcity/git-plugin.git");
+    GitVcsRoot root = toGitRoot(url);
+
+    assertEquals("http://git.jetbrains.org/teamcity/git-plugin.git", root.getRepositoryFetchURL().toString());
+  }
+
+  @Test
+  public void http_protocol_svn_repo() throws Exception {
+    VcsUrl url = new VcsUrl("http://svn.jetbrains.org/teamcity/plugins/xml-tests-reporting/trunk/");
+    assertNull(myUrlSupport.convertToVcsRootProperties(url));
   }
 
 
@@ -135,8 +157,8 @@ public class GitUrlSupportTest extends BaseTestCase {
     }
   }
 
-  private GitVcsRoot toGitRoot(MavenVcsUrl url) throws VcsException {
-    Map<String, String> properties = mySupport.convertToVcsRootProperties(url);
+  private GitVcsRoot toGitRoot(@NotNull VcsUrl url) throws VcsException {
+    Map<String, String> properties = myUrlSupport.convertToVcsRootProperties(url);
     VcsRootImpl myRoot = new VcsRootImpl(1, properties);
     return new GitVcsRoot(myMirrorManager, myRoot);
   }