org.eclipse.jgit.transport.RemoteConfig Java Examples

The following examples show how to use org.eclipse.jgit.transport.RemoteConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected Transport openTransport (boolean openPush) throws URISyntaxException, NotSupportedException, TransportException {
    URIish uri = getUriWithUsername(openPush);
    // WA for #200693, jgit fails to initialize ftp protocol
    for (TransportProtocol proto : Transport.getTransportProtocols()) {
        if (proto.getSchemes().contains("ftp")) { //NOI18N
            Transport.unregister(proto);
        }
    }
    try {
        Transport transport = Transport.open(getRepository(), uri);
        RemoteConfig config = getRemoteConfig();
        if (config != null) {
            transport.applyConfig(config);
        }
        if (transport.getTimeout() <= 0) {
            transport.setTimeout(45);
        }
        transport.setCredentialsProvider(getCredentialsProvider());
        return transport;
    } catch (IllegalArgumentException ex) {
        throw new TransportException(ex.getLocalizedMessage(), ex);
    }
}
 
Example #2
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void prune(RemoteConfig repository) throws GitException {
    try (Repository gitRepo = getRepository()) {
        String remote = repository.getName();
        String prefix = "refs/remotes/" + remote + "/";

        Set<String> branches = listRemoteBranches(remote);

        for (Ref r : new ArrayList<>(gitRepo.getAllRefs().values())) {
            if (r.getName().startsWith(prefix) && !branches.contains(r.getName())) {
                // delete this ref
                RefUpdate update = gitRepo.updateRef(r.getName());
                update.setRefLogMessage("remote branch pruned", false);
                update.setForceUpdate(true);
                update.delete();
            }
        }
    } catch (URISyntaxException | IOException e) {
        throw new GitException(e);
    }
}
 
Example #3
Source File: FetchTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    workDir = getWorkingDirectory();
    repository = getRepository(getLocalGitRepository());
    
    otherWT = new File(workDir.getParentFile(), "repo2");
    GitClient client = getClient(otherWT);
    client.init(NULL_PROGRESS_MONITOR);
    f = new File(otherWT, "f");
    write(f, "init");
    client.add(new File[] { f }, NULL_PROGRESS_MONITOR);
    masterInfo = client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
    branch = client.createBranch(BRANCH_NAME, Constants.MASTER, NULL_PROGRESS_MONITOR);
    RemoteConfig cfg = new RemoteConfig(repository.getConfig(), "origin");
    cfg.addURI(new URIish(otherWT.toURI().toURL().toString()));
    cfg.update(repository.getConfig());
    repository.getConfig().save();
}
 
Example #4
Source File: GitUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 检查本地的remote是否存在对应的url
 *
 * @param url  要检查的url
 * @param file 本地仓库文件
 * @return true 存在对应url
 * @throws IOException     IO
 * @throws GitAPIException E
 */
private static boolean checkRemoteUrl(String url, File file) throws IOException, GitAPIException {
    try (Git git = Git.open(file)) {
        RemoteListCommand remoteListCommand = git.remoteList();
        boolean urlTrue = false;
        List<RemoteConfig> list = remoteListCommand.call();
        end:
        for (RemoteConfig remoteConfig : list) {
            for (URIish urIish : remoteConfig.getURIs()) {
                if (urIish.toString().equals(url)) {
                    urlTrue = true;
                    break end;
                }
            }
        }
        return urlTrue;
    }
}
 
Example #5
Source File: PullTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    workDir = getWorkingDirectory();
    repository = getRepository(getLocalGitRepository());
    
    otherWT = new File(workDir.getParentFile(), "repo2");
    GitClient client = getClient(otherWT);
    client.init(NULL_PROGRESS_MONITOR);
    f = new File(otherWT, "f");
    write(f, "init");
    f2 = new File(otherWT, "f2");
    write(f2, "init");
    client.add(new File[] { f, f2 }, NULL_PROGRESS_MONITOR);
    masterInfo = client.commit(new File[] { f, f2 }, "init commit", null, null, NULL_PROGRESS_MONITOR);
    branch = client.createBranch(BRANCH_NAME, Constants.MASTER, NULL_PROGRESS_MONITOR);
    RemoteConfig cfg = new RemoteConfig(repository.getConfig(), "origin");
    cfg.addURI(new URIish(otherWT.toURI().toURL().toString()));
    cfg.update(repository.getConfig());
    repository.getConfig().save();
}
 
Example #6
Source File: RemotesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddRemote () throws Exception {
    StoredConfig config = repository.getConfig();
    assertEquals(0, config.getSubsections("remote").size());
    
    GitClient client = getClient(workDir);
    GitRemoteConfig remoteConfig = new GitRemoteConfig("origin",
            Arrays.asList(new File(workDir.getParentFile(), "repo2").toURI().toString()),
            Arrays.asList(new File(workDir.getParentFile(), "repo2").toURI().toString()),
            Arrays.asList("+refs/heads/*:refs/remotes/origin/*"),
            Arrays.asList("refs/remotes/origin/*:+refs/heads/*"));
    client.setRemote(remoteConfig, NULL_PROGRESS_MONITOR);
    
    config.load();
    RemoteConfig cfg = new RemoteConfig(config, "origin");
    assertEquals(Arrays.asList(new URIish(new File(workDir.getParentFile(), "repo2").toURI().toString())), cfg.getURIs());
    assertEquals(Arrays.asList(new URIish(new File(workDir.getParentFile(), "repo2").toURI().toString())), cfg.getPushURIs());
    assertEquals(Arrays.asList(new RefSpec("+refs/heads/*:refs/remotes/origin/*")), cfg.getFetchRefSpecs());
    assertEquals(Arrays.asList(new RefSpec("refs/remotes/origin/*:+refs/heads/*")), cfg.getPushRefSpecs());
}
 
Example #7
Source File: GitScmAdapter.java    From bitbucket-build-status-notifier-plugin with MIT License 6 votes vote down vote up
public Map<String, URIish> getCommitRepoMap() throws Exception {
    List<RemoteConfig> repoList = this.gitScm.getRepositories();
    if (repoList.size() < 1) {
        throw new Exception("No repos found");
    }

    HashMap<String, URIish> commitRepoMap = new HashMap<String, URIish>();
    BuildData buildData = build.getAction(BuildData.class);
    if (buildData == null || buildData.getLastBuiltRevision() == null) {
        logger.warning("Build data could not be found");
    } else {
        commitRepoMap.put(buildData.getLastBuiltRevision().getSha1String(), repoList.get(0).getURIs().get(0));
    }

    return commitRepoMap;
}
 
Example #8
Source File: GitUtils.java    From jphp with Apache License 2.0 6 votes vote down vote up
public static ArrayMemory valueOf(RemoteConfig value) {
    ArrayMemory memory = new ArrayMemory();
    memory.refOfIndex("name").assign(value.getName());
    memory.refOfIndex("receivePack").assign(value.getReceivePack());
    memory.refOfIndex("uploadPack").assign(value.getUploadPack());
    memory.refOfIndex("mirror").assign(value.isMirror());
    memory.refOfIndex("timeout").assign(value.getTimeout());
    memory.refOfIndex("tagOpt").assign(value.getTagOpt().name());
    ArrayMemory uris = ArrayMemory.createListed(value.getURIs().size());

    for (URIish urIish : value.getURIs()) {
        uris.add(urIish.toPrivateString());
    }

    memory.refOfIndex("uris").assign(uris);

    return memory;
}
 
Example #9
Source File: LocalRepoMock.java    From gitflow-incremental-builder with MIT License 6 votes vote down vote up
private void configureRemote(Git git, String repoUrl) throws URISyntaxException, IOException, GitAPIException {
    StoredConfig config = git.getRepository().getConfig();
    config.clear();
    config.setString("remote", "origin" ,"fetch", "+refs/heads/*:refs/remotes/origin/*");
    config.setString("remote", "origin" ,"push", "+refs/heads/*:refs/remotes/origin/*");
    config.setString("branch", "master", "remote", "origin");
    config.setString("baseBranch", "master", "merge", "refs/heads/master");
    config.setString("push", null, "default", "current");

    // disable all gc
    // http://download.eclipse.org/jgit/site/5.2.1.201812262042-r/apidocs/org/eclipse/jgit/internal/storage/file/GC.html#setAuto-boolean-
    config.setString("gc", null, "auto", "0");
    config.setString("gc", null, "autoPackLimit", "0");
    config.setBoolean("receive", null, "autogc", false);

    RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
    URIish uri = new URIish(repoUrl);
    remoteConfig.addURI(uri);
    remoteConfig.addFetchRefSpec(new RefSpec("refs/heads/master:refs/heads/master"));
    remoteConfig.addPushRefSpec(new RefSpec("refs/heads/master:refs/heads/master"));
    remoteConfig.update(config);

    config.save();
}
 
Example #10
Source File: Branch.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@PropertyDescription(name = GitConstants.KEY_REMOTE)
private JSONArray getRemotes() throws URISyntaxException, JSONException, IOException, CoreException {
	String branchName = Repository.shortenRefName(ref.getName());
	String remoteName = getConfig().getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_REMOTE);
	List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(getConfig());
	ArrayList<JSONObject> remotes = new ArrayList<JSONObject>();
	for (RemoteConfig remoteConfig : remoteConfigs) {
		if (!remoteConfig.getFetchRefSpecs().isEmpty()) {
			Remote r = new Remote(cloneLocation, db, remoteConfig.getName());
			r.setNewBranch(branchName);
			// Ensure that the remote tracking branch is the first in the list.
			// Insert at the beginning of the list as well when the remote tracking branch is not set but the branch has been pushed to the remote
			if (remoteConfig.getName().equals(remoteName) || (remoteName == null && db.resolve(Constants.R_REMOTES + remoteConfig.getName() + "/" + branchName) != null)) {
				remotes.add(0, r.toJSON());
			} else {
				remotes.add(r.toJSON());
			}
		}
	}
	JSONArray result = new JSONArray();
	for (JSONObject remote : remotes) {
		result.put(remote);
	}
	return result;
}
 
Example #11
Source File: AbstractGitTest.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
void setOriginOnProjectToTmp(File origin, File project, boolean push)
		throws GitAPIException, IOException, URISyntaxException {
	try (Git git = openGitProject(project)) {
		RemoteRemoveCommand remove = git.remoteRemove();
		remove.setName("origin");
		remove.call();
		RemoteSetUrlCommand command = git.remoteSetUrl();
		command.setUri(new URIish(origin.toURI().toURL()));
		command.setName("origin");
		command.setPush(push);
		command.call();
		StoredConfig config = git.getRepository().getConfig();
		RemoteConfig originConfig = new RemoteConfig(config, "origin");
		originConfig
				.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
		originConfig.update(config);
		config.save();
	}
}
 
Example #12
Source File: TransportCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected final URIish getUri (boolean pushUri) throws URISyntaxException {
    RemoteConfig config = getRemoteConfig();
    List<URIish> uris;
    if (config == null) {
        uris = Collections.emptyList();
    } else {
        if (pushUri) {
            uris = config.getPushURIs();
            if (uris.isEmpty()) {
                uris = config.getURIs();
            }
        } else {
            uris = config.getURIs();
        }
    }
    if (uris.isEmpty()) {
        return new URIish(remote);
    } else {
        return uris.get(0);
    }
}
 
Example #13
Source File: LegacyCompatibleGitAPIImplTest.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Test
@Deprecated
public void testCloneRemoteConfig() throws URISyntaxException, InterruptedException, IOException, ConfigInvalidException {
    if (gitImpl.equals("jgit")) {
        return;
    }
    Config config = new Config();
    /* Use local git-client-plugin repository as source for clone test */
    String remoteName = "localCopy";
    String localRepoPath = (new File(".")).getCanonicalPath().replace("\\", "/");
    String configText = "[remote \"" + remoteName + "\"]\n"
            + "url = " + localRepoPath + "\n"
            + "fetch = +refs/heads/*:refs/remotes/" + remoteName + "/*\n";
    config.fromText(configText);
    RemoteConfig remoteConfig = new RemoteConfig(config, remoteName);
    List<URIish> list = remoteConfig.getURIs();
    git.clone(remoteConfig);
    File[] files = git.workspace.listFiles();
    assertEquals(files.length + "files in " + Arrays.toString(files), 1, files.length);
    assertEquals("Wrong file name", ".git", files[0].getName());
}
 
Example #14
Source File: UIGitTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddRemoveRemote() throws Exception {
  URIish uri = new URIish( db2.getDirectory().toURI().toURL().toString() );
  uiGit.addRemote( uri.toString() );
  assertEquals( uri.toString(), uiGit.getRemote() );

  uiGit.removeRemote();
  // assert that there are no remotes left
  assertTrue( RemoteConfig.getAllRemoteConfigs( db.getConfig() ).isEmpty() );
}
 
Example #15
Source File: RepoInfo.java    From aws-codecommit-trigger-plugin with Apache License 2.0 5 votes vote down vote up
public static RepoInfo fromSqsJob(SQSJob sqsJob) {
    if (sqsJob == null) {
        return null;
    }

    RepoInfo repoInfo = new RepoInfo();

    List<SCM> scms = sqsJob.getScmList();
    List<String> codeCommitUrls = new ArrayList<>();
    List<String> nonCodeCommitUrls = new ArrayList<>();
    List<String> branches = new ArrayList<>();

    for (SCM scm : scms) {
        if (scm instanceof GitSCM) {//TODO refactor to visitor
            GitSCM git = (GitSCM) scm;
            List<RemoteConfig> repos = git.getRepositories();
            for (RemoteConfig repo : repos) {
                for (URIish urIish : repo.getURIs()) {
                    String url = urIish.toString();
                    if (StringUtils.isCodeCommitRepo(url)) {
                        codeCommitUrls.add(url);
                    }
                    else {
                        nonCodeCommitUrls.add(url);
                    }
                }
            }

            for (BranchSpec branchSpec : git.getBranches()) {
                branches.add(branchSpec.getName());
            }
        }
    }

    repoInfo.nonCodeCommitUrls = nonCodeCommitUrls;
    repoInfo.codeCommitUrls = codeCommitUrls;
    repoInfo.branches = branches;
    return repoInfo;
}
 
Example #16
Source File: ScmJobEventTriggerMatcher.java    From aws-codecommit-trigger-plugin with Apache License 2.0 5 votes vote down vote up
private URIish getMatchesConfig(final Event event, final RemoteConfig config) {
    List<URIish> uris = config.getURIs();
    for (final URIish uri : uris) {
        log.debug("Event-arn= %s ; uri= %s", event.getArn(), uri);
        if (event.isMatch(uri)) {//TODO use here matchBranch(event, branchSpec)
            log.info("Event matched uri %s", uri);
            return uri;
        }
    }

    log.info("Found no event matched config: ", event.getArn(), config.getName());
    return null;
}
 
Example #17
Source File: LegacyCompatibleGitAPIImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public void clone(RemoteConfig rc, boolean useShallowClone) throws GitException, InterruptedException {
    // Assume only 1 URL for this repository
    final String source = rc.getURIs().get(0).toPrivateString();
    clone(source, rc.getName(), useShallowClone, null);
}
 
Example #18
Source File: GitRemoteConfig.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static GitRemoteConfig fromRemoteConfig (RemoteConfig config) {
    return new GitRemoteConfig(config.getName(),
            getAsStrings(config.getURIs()),
            getAsStrings(config.getPushURIs()),
            getAsStrings(config.getFetchRefSpecs()),
            getAsStrings(config.getPushRefSpecs()));
}
 
Example #19
Source File: TransportCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected final RemoteConfig getRemoteConfig () throws URISyntaxException {
    RemoteConfig config = new RemoteConfig(getRepository().getConfig(), remote);
    if (config.getURIs().isEmpty() && config.getPushURIs().isEmpty()) {
        return null;
    } else {
        return config;
    }
}
 
Example #20
Source File: ProjectBranchesProvider.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private URIish getFirstRepoURL(List<RemoteConfig> repositories) {
    if (!repositories.isEmpty()) {
        List<URIish> uris = repositories.get(repositories.size() - 1).getURIs();
        if (!uris.isEmpty()) {
            return uris.get(uris.size() - 1);
        }
    }
    throw new IllegalStateException(Messages.GitLabPushTrigger_NoSourceRepository());
}
 
Example #21
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__cloud_branch_norev_anon__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(false, null);
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId(null);
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is(nullValue()));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.com/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("https://github.com/tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("https://github.com/tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is(nullValue()));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("https://github.com/tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #22
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__cloud_branch_norev_userpass__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(false, null);
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId("user-pass");
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is("user-pass"));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.com/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("https://github.com/tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("https://github.com/tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is("user-pass"));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("https://github.com/tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #23
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__cloud_branch_norev_userkey__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(false, null);
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId("user-key");
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is("user-key"));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.com/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("[email protected]:tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("[email protected]:tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is("user-key"));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("[email protected]:tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #24
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__server_branch_norev_anon__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git");
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId(null);
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is(nullValue()));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.test/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("https://github.test/tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("https://github.test/tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is(nullValue()));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("https://github.test/tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #25
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__server_branch_norev_userpass__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git");
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId("user-pass");
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is("user-pass"));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.test/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("https://github.test/tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("https://github.test/tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is("user-pass"));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("https://github.test/tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #26
Source File: GitHubSCMBuilderTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__server_branch_norev_userkey__when__build__then__scmBuilt() throws Exception {
    createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git");
    BranchSCMHead head = new BranchSCMHead("test-branch");
    source.setCredentialsId("user-key");
    GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, null);
    assertThat(instance.credentialsId(), is("user-key"));
    assertThat(instance.head(), is(head));
    assertThat(instance.revision(), is(nullValue()));
    assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"));
    assertThat("expecting guess value until withGitHubRemote called",
            instance.remote(), is("https://github.test/tester/test-repo.git"));
    assertThat(instance.browser(), instanceOf(GithubWeb.class));
    assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo"));

    instance.withGitHubRemote();
    assertThat(instance.remote(), is("[email protected]:tester/test-repo.git"));

    GitSCM actual = instance.build();
    assertThat(actual.getBrowser(), instanceOf(GithubWeb.class));
    assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo"));
    assertThat(actual.getGitTool(), nullValue());
    assertThat(actual.getUserRemoteConfigs(), hasSize(1));
    UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
    assertThat(config.getName(), is("origin"));
    assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch"));
    assertThat(config.getUrl(), is("[email protected]:tester/test-repo.git"));
    assertThat(config.getCredentialsId(), is("user-key"));
    RemoteConfig origin = actual.getRepositoryByName("origin");
    assertThat(origin, notNullValue());
    assertThat(origin.getURIs(), hasSize(1));
    assertThat(origin.getURIs().get(0).toString(), is("[email protected]:tester/test-repo.git"));
    assertThat(origin.getFetchRefSpecs(), hasSize(1));
    assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch"));
    assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
    assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false));
    assertThat(actual.getExtensions(), contains(instanceOf(GitSCMSourceDefaults.class)));
}
 
Example #27
Source File: PushHookTriggerHandlerGitlabServerTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Theory
public void createRevisionParameterAction_pushCommitRequestWith2Remotes(GitLabPushRequestSamples samples) throws Exception {
    PushHook hook = samples.pushCommitRequest();

    GitSCM gitSCM = new GitSCM(Arrays.asList(new UserRemoteConfig("[email protected]:test.git", null, null, null),
                                             new UserRemoteConfig("[email protected]:fork.git", "fork", null, null)),
                               Collections.singletonList(new BranchSpec("")),
                               false, Collections.<SubmoduleConfig>emptyList(),
                               null, null, null);
    RevisionParameterAction revisionParameterAction = new PushHookTriggerHandlerImpl(false).createRevisionParameter(hook, gitSCM);

    assertThat(revisionParameterAction, is(notNullValue()));
    assertThat(revisionParameterAction.commit, is(hook.getAfter()));
    assertFalse(revisionParameterAction.canOriginateFrom(new ArrayList<RemoteConfig>()));
}
 
Example #28
Source File: GitUtils.java    From jphp with Apache License 2.0 5 votes vote down vote up
public static ArrayMemory valueOfRemoteConfigs(Iterable<RemoteConfig> values) {
    ArrayMemory memory = new ArrayMemory();

    for (RemoteConfig value : values) {
        memory.add(valueOf(value));
    }

    return memory;
}
 
Example #29
Source File: RepositoryManagementServiceInternalImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
private void fetchRemote(String siteId, Git git, RemoteConfig conf)
        throws CryptoException, IOException, ServiceLayerException, GitAPIException {
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, conf.getName());
    if (remoteRepository != null) {
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        FetchCommand fetchCommand = git.fetch().setRemote(conf.getName());
        fetchCommand = helper.setAuthenticationForCommand(fetchCommand,
                remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(),
                remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(),
                remoteRepository.getRemotePrivateKey(), tempKey, true);
        fetchCommand.call();
    }
}
 
Example #30
Source File: ProjectLabelsProvider.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private URIish getFirstRepoURL(List<RemoteConfig> repositories) {
    if (!repositories.isEmpty()) {
        List<URIish> uris = repositories.get(repositories.size() - 1).getURIs();
        if (!uris.isEmpty()) {
            return uris.get(uris.size() - 1);
        }
    }
    throw new IllegalStateException(Messages.GitLabPushTrigger_NoSourceRepository());
}