Search in sources :

Example 1 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class CollectChangesTest method fetch_should_not_fail_if_remote_repository_does_not_have_some_branches.

@Test
@TestFor(issues = "http://youtrack.jetbrains.com/issue/TW-29798#comment=27-537697")
public void fetch_should_not_fail_if_remote_repository_does_not_have_some_branches() throws Exception {
    VcsRoot root = vcsRoot().withFetchUrl(myRepo).withBranch("master").withReportTags(true).build();
    // setup fetcher with a counter
    ServerPluginConfig config = myConfig.build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    FetchCommand fetchCommand = new FetchCommandImpl(config, new TransportFactoryImpl(config, manager), new FetcherProperties(config), manager);
    FetchCommandCountDecorator fetchCounter = new FetchCommandCountDecorator(fetchCommand);
    GitVcsSupport git = gitSupport().withPluginConfig(myConfig).withFetchCommand(fetchCounter).build();
    RepositoryStateData state = git.getCurrentState(root);
    // has a single branch
    RepositoryStateData s1 = createVersionState("refs/heads/master", map("refs/heads/master", state.getBranchRevisions().get("refs/heads/master")));
    Map<String, String> branches = new HashMap<String, String>(state.getBranchRevisions());
    // unknown branch that points to a commit that exists in remote repo
    branches.put("refs/heads/unknown.branch", branches.get(state.getDefaultBranchName()));
    // has many branches, some of them don't exist in remote repository
    RepositoryStateData s2 = createVersionState("refs/heads/master", branches);
    // no failure if 'fromState' contains non-existing branch
    git.getCollectChangesPolicy().collectChanges(root, s2, s1, CheckoutRules.DEFAULT);
    assertEquals(fetchCounter.getFetchCount(), 1);
    FileUtil.delete(config.getCachesDir());
    fetchCounter.resetFetchCounter();
    try {
        git.getCollectChangesPolicy().collectChanges(root, s1, s2, CheckoutRules.DEFAULT);
        fail("Changes collection was expected to fail because 'toState' contains non-existing branch");
    } catch (VcsException e) {
    // expected
    }
}
Also used : VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) Test(org.testng.annotations.Test) TestFor(jetbrains.buildServer.util.TestFor)

Example 2 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class CollectChangesTest method ignore_missing_branch.

@TestFor(issues = "TW-38899")
@Test(dataProvider = "doFetchInSeparateProcess", dataProviderClass = FetchOptionsDataProvider.class)
public void ignore_missing_branch(boolean fetchInSeparateProcess) throws Exception {
    myConfig.setSeparateProcessForFetch(fetchInSeparateProcess);
    myConfig.setIgnoreMissingRemoteRef(true);
    myConfig.withFetcherProperties(PluginConfigImpl.IGNORE_MISSING_REMOTE_REF, "true");
    File repo = copyRepository(myTempFiles, dataFile("repo_for_fetch.2.personal"), "repo.git");
    ServerPluginConfig config = myConfig.build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    AtomicBoolean updateRepo = new AtomicBoolean(false);
    // wrapper for fetch command which will remove ref in remote repository just before fetch
    FetchCommand fetchCommand = new FetchCommandImpl(config, new TransportFactoryImpl(config, manager), new FetcherProperties(config), manager) {

        @Override
        public void fetch(@NotNull Repository db, @NotNull URIish fetchURI, @NotNull FetchSettings settings) throws IOException, VcsException {
            if (updateRepo.get()) {
                FileUtil.delete(repo);
                copyRepository(dataFile("repo_for_fetch.3"), repo);
            }
            super.fetch(db, fetchURI, settings);
        }
    };
    GitVcsSupport git = gitSupport().withPluginConfig(myConfig).withFetchCommand(fetchCommand).build();
    VcsRoot root = vcsRoot().withFetchUrl(repo).build();
    RepositoryStateData s0 = createVersionState("refs/heads/master", map("refs/heads/master", "add81050184d3c818560bdd8839f50024c188586", "refs/heads/personal", "add81050184d3c818560bdd8839f50024c188586"));
    RepositoryStateData s1 = createVersionState("refs/heads/master", map("refs/heads/master", "add81050184d3c818560bdd8839f50024c188586", "refs/heads/personal", "d47dda159b27b9a8c4cee4ce98e4435eb5b17168"));
    // init repo on server:
    git.getCollectChangesPolicy().collectChanges(root, s0, s1, CheckoutRules.DEFAULT);
    RepositoryStateData s2 = createVersionState("refs/heads/master", map("refs/heads/master", "bba7fbcc200b4968e6abd2f7d475dc15306cafc6", "refs/heads/personal", "d47dda159b27b9a8c4cee4ce98e4435eb5b17168"));
    // refs/heads/personal ref disappears from remote repo:
    updateRepo.set(true);
    // changes collecting doesn't fail
    git.getCollectChangesPolicy().collectChanges(root, s1, s2, CheckoutRules.DEFAULT);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) NotNull(org.jetbrains.annotations.NotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GitTestUtil.copyRepository(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.copyRepository) Repository(org.eclipse.jgit.lib.Repository) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) File(java.io.File) Test(org.testng.annotations.Test) TestFor(jetbrains.buildServer.util.TestFor)

Example 3 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class CollectChangesTest method do_not_do_fetch_per_branch.

@TestFor(issues = "TW-29798")
public void do_not_do_fetch_per_branch() throws Exception {
    VcsRoot root = vcsRoot().withFetchUrl(myRepo).withBranch("master").withReportTags(true).build();
    // setup fetcher with counter
    ServerPluginConfig config = myConfig.build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    FetchCommand fetchCommand = new FetchCommandImpl(config, new TransportFactoryImpl(config, manager), new FetcherProperties(config), manager);
    FetchCommandCountDecorator fetchCounter = new FetchCommandCountDecorator(fetchCommand);
    GitVcsSupport git = gitSupport().withPluginConfig(myConfig).withFetchCommand(fetchCounter).build();
    RepositoryStateData state = git.getCurrentState(root);
    // has a single branch
    RepositoryStateData s1 = createVersionState("refs/heads/master", map("refs/heads/master", state.getBranchRevisions().get("refs/heads/master")));
    // has many branches
    RepositoryStateData s2 = createVersionState("refs/heads/master", state.getBranchRevisions());
    git.getCollectChangesPolicy().collectChanges(root, s1, s2, CheckoutRules.DEFAULT);
    assertEquals(fetchCounter.getFetchCount(), 1);
    FileUtil.delete(config.getCachesDir());
    fetchCounter.resetFetchCounter();
    git.getCollectChangesPolicy().collectChanges(root, s2, s1, CheckoutRules.DEFAULT);
    assertEquals(fetchCounter.getFetchCount(), 1);
}
Also used : VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) TestFor(jetbrains.buildServer.util.TestFor)

Example 4 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class GitPatchProcess method main.

public static void main(String... args) throws Exception {
    Map<String, String> properties = VcsUtil.stringToProperties(GitServerUtil.readInput());
    GitPatchProcessSettings settings = new GitPatchProcessSettings(properties);
    GitServerUtil.configureInternalProperties(settings.getInternalProperties());
    GitServerUtil.configureStreamFileThreshold(Integer.MAX_VALUE);
    GitServerUtil.configureExternalProcessLogger(settings.isDebugEnabled());
    GitServerUtil.setupMemoryMappedIndexReading();
    JSchConfigInitializer.initJSchConfig(JSch.class);
    PluginConfigImpl config = new PluginConfigImpl(new ConstantCachePaths(settings.getGitCachesDir()));
    RepositoryManager repositoryManager = new RepositoryManagerImpl(config, new MirrorManagerImpl(config, new HashCalculatorImpl(), new RemoteRepositoryUrlInvestigatorImpl()));
    GitMapFullPath mapFullPath = new GitMapFullPath(config, new RevisionsCache(config));
    VcsRootSshKeyManager sshKeyManager = new ConstantSshKeyManager(settings.getKeyBytes());
    TransportFactory transportFactory = new TransportFactoryImpl(config, sshKeyManager, settings.getGitTrustStoreProvider());
    FetcherProperties fetcherProperties = new FetcherProperties(config);
    FetchCommand fetchCommand = new FetchCommandImpl(config, transportFactory, fetcherProperties, sshKeyManager, settings.getGitTrustStoreProvider());
    GitRepoOperations repoOperations = new GitRepoOperationsImpl(config, transportFactory, sshKeyManager, fetchCommand);
    CommitLoader commitLoader = new CommitLoaderImpl(repositoryManager, repoOperations, mapFullPath, config);
    OperationContext context = new OperationContext(commitLoader, repositoryManager, settings.getRoot(), "build patch", GitProgress.NO_OP, config, null);
    OutputStream fos = new BufferedOutputStream(new FileOutputStream(settings.getPatchFile()));
    try {
        PatchBuilderImpl patchBuilder = new PatchBuilderImpl(fos);
        new GitPatchBuilder(context, patchBuilder, settings.getFromRevision(), settings.getToRevision(), settings.getCheckoutRules(), settings.isVerboseTreeWalkLog(), new PrintFile(), transportFactory).buildPatch();
        patchBuilder.close();
    } catch (Throwable t) {
        if (settings.isDebugEnabled() || isImportant(t)) {
            System.err.println(t.getMessage());
            t.printStackTrace(System.err);
        } else {
            String msg = t.getMessage();
            boolean printStackTrace = false;
            if (t instanceof SubmoduleFetchException) {
                Throwable cause = t.getCause();
                printStackTrace = cause != null && isImportant(cause);
            }
            System.err.println(msg);
            if (printStackTrace)
                t.printStackTrace(System.err);
        }
        System.exit(1);
    } finally {
        fos.close();
    }
}
Also used : SubmoduleFetchException(jetbrains.buildServer.buildTriggers.vcs.git.submodules.SubmoduleFetchException) PatchBuilderImpl(jetbrains.buildServer.vcs.patches.PatchBuilderImpl) GitRepoOperationsImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitRepoOperationsImpl) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager)

Example 5 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class SshAuthenticationTest method do_ssh_test.

private void do_ssh_test(boolean nativeOperationsEnabled, boolean useSshAskPass, @NotNull String urlFormat, @NotNull String sshdConfig, @Nullable TeamCitySshKey tcKey, @Nullable String publicKey, @NotNull VcsRootConfigurator builder, boolean ignoreKnownHosts) throws Exception {
    if (!nativeOperationsEnabled) {
        JSchConfigInitializer.initJSchConfig(JSch.class);
    }
    final File pub_key = publicKey == null ? null : dataFile(publicKey);
    ssh_test(pub_key, sshdConfig, container -> {
        setInternalProperty("teamcity.git.nativeOperationsEnabled", String.valueOf(nativeOperationsEnabled));
        setInternalProperty("teamcity.git.useSshAskPas", String.valueOf(useSshAskPass));
        setInternalProperty("teamcity.git.debugNativeGit", "true");
        final ServerPaths serverPaths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
        final ServerPluginConfig config = new PluginConfigBuilder(serverPaths).build();
        final VcsRootSshKeyManager keyManager = r -> tcKey;
        final TransportFactoryImpl transportFactory = new TransportFactoryImpl(config, keyManager);
        final GitRepoOperationsImpl repoOperations = new GitRepoOperationsImpl(config, transportFactory, keyManager, new FetchCommandImpl(config, transportFactory, new FetcherProperties(config), keyManager));
        final MirrorManagerImpl mirrorManager = new MirrorManagerImpl(config, new HashCalculatorImpl(), new RemoteRepositoryUrlInvestigatorImpl());
        final RepositoryManagerImpl repositoryManager = new RepositoryManagerImpl(config, mirrorManager);
        final String repoUrl = String.format(urlFormat, container.getContainerIpAddress(), container.getMappedPort(22));
        final GitVcsRoot gitRoot = new GitVcsRoot(mirrorManager, builder.config(VcsRootBuilder.vcsRoot().withFetchUrl(repoUrl).withIgnoreKnownHosts(ignoreKnownHosts)).build(), new URIishHelperImpl());
        JSch.setLogger(JSchLoggers.STD_DEBUG_JSCH_LOGGER);
        // here we test some git operations
        final URIish fetchUrl = new URIish(repoUrl);
        final Repository db = repositoryManager.openRepository(fetchUrl);
        final Map<String, Ref> refs = repoOperations.lsRemoteCommand(repoUrl).lsRemote(db, gitRoot, new FetchSettings(gitRoot.getAuthSettings()));
        assertContains(refs.keySet(), "refs/pull/1");
        final StringBuilder progress = new StringBuilder();
        final List<RefSpec> refSpecs = refs.keySet().stream().map(r -> new RefSpec().setSourceDestination(r, r).setForceUpdate(true)).collect(Collectors.toList());
        repoOperations.fetchCommand(repoUrl).fetch(db, fetchUrl, new FetchSettings(gitRoot.getAuthSettings(), new GitProgress() {

            @Override
            public void reportProgress(@NotNull final String p) {
                progress.append(p).append("\n");
            }

            @Override
            public void reportProgress(final float p, @NotNull final String stage) {
                if (p < 0) {
                    progress.append(stage).append("\n");
                } else {
                    int percents = (int) Math.floor(p * 100);
                    progress.append(stage).append(" ").append(percents).append("%");
                }
            }
        }, refSpecs));
        if (nativeOperationsEnabled) {
            assertContains(progress.toString(), "* [new ref]         refs/pull/1               -> refs/pull/1");
        } else {
            assertContains(progress.toString(), "update ref remote name: refs/pull/1, local name: refs/pull/1, old object id: 0000000000000000000000000000000000000000, new object id: b896070465af79121c9a4eb5300ecff29453c164, result: NEW");
        }
        final GitVcsSupport vcsSupport = GitSupportBuilder.gitSupport().withServerPaths(serverPaths).withPluginConfig(config).withTransportFactory(transportFactory).build();
        repoOperations.tagCommand(vcsSupport, repoUrl).tag(vcsSupport.createContext(gitRoot.getOriginalRoot(), "tag"), "test_tag", "b896070465af79121c9a4eb5300ecff29453c164");
        assertContains(repoOperations.lsRemoteCommand(repoUrl).lsRemote(db, gitRoot, new FetchSettings(gitRoot.getAuthSettings())).keySet(), "refs/tags/test_tag");
    });
}
Also used : SkipException(org.testng.SkipException) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) BindMode(org.testcontainers.containers.BindMode) ImageFromDockerfile(org.testcontainers.images.builder.ImageFromDockerfile) JSch(com.jcraft.jsch.JSch) jetbrains.buildServer.buildTriggers.vcs.git(jetbrains.buildServer.buildTriggers.vcs.git) TestFor(jetbrains.buildServer.util.TestFor) TempFiles(jetbrains.buildServer.TempFiles) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) GitRepoOperationsImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitRepoOperationsImpl) FileUtil(jetbrains.buildServer.util.FileUtil) BaseTestCase(jetbrains.buildServer.BaseTestCase) Map(java.util.Map) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) URIish(org.eclipse.jgit.transport.URIish) GenericContainer(org.testcontainers.containers.GenericContainer) JSchConfigInitializer(jetbrains.buildServer.util.jsch.JSchConfigInitializer) Files(java.nio.file.Files) RefSpec(org.eclipse.jgit.transport.RefSpec) BeforeMethod(org.testng.annotations.BeforeMethod) IOException(java.io.IOException) ServerPaths(jetbrains.buildServer.serverSide.ServerPaths) Collectors(java.util.stream.Collectors) SystemInfo(com.intellij.openapi.util.SystemInfo) File(java.io.File) TeamCitySshKey(jetbrains.buildServer.ssh.TeamCitySshKey) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ExecResult(jetbrains.buildServer.ExecResult) SimpleCommandLineProcessRunner(jetbrains.buildServer.SimpleCommandLineProcessRunner) Ref(org.eclipse.jgit.lib.Ref) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) NotNull(org.jetbrains.annotations.NotNull) Repository(org.eclipse.jgit.lib.Repository) URIish(org.eclipse.jgit.transport.URIish) GitRepoOperationsImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitRepoOperationsImpl) NotNull(org.jetbrains.annotations.NotNull) RefSpec(org.eclipse.jgit.transport.RefSpec) ServerPaths(jetbrains.buildServer.serverSide.ServerPaths) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) File(java.io.File)

Aggregations

VcsRootSshKeyManager (jetbrains.buildServer.ssh.VcsRootSshKeyManager)10 TestFor (jetbrains.buildServer.util.TestFor)7 Test (org.testng.annotations.Test)6 NotNull (org.jetbrains.annotations.NotNull)5 File (java.io.File)4 GitTestUtil.dataFile (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile)4 Repository (org.eclipse.jgit.lib.Repository)4 URIish (org.eclipse.jgit.transport.URIish)4 GitTestUtil.copyRepository (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.copyRepository)3 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GitRepoOperationsImpl (jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitRepoOperationsImpl)2 ServerPaths (jetbrains.buildServer.serverSide.ServerPaths)2 Ref (org.eclipse.jgit.lib.Ref)2 RefSpec (org.eclipse.jgit.transport.RefSpec)2 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 SystemInfo (com.intellij.openapi.util.SystemInfo)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 IOException (java.io.IOException)1