Search in sources :

Example 1 with VcsRootImpl

use of jetbrains.buildServer.vcs.impl.VcsRootImpl in project teamcity-git by JetBrains.

the class GitCommitsInfoBuilderTest method test_fetch_service_cache.

public void test_fetch_service_cache() throws Exception {
    VcsRootImpl root = vcsRoot().withFetchUrl(GitUtils.toURL(myRepositoryDir)).withBranch("master").build();
    root.addProperty("INCLUDE_COMMIT_INFO_SUBMODULES", "true");
    GitVcsSupport vcs = gitSupport().withServerPaths(myServerPaths).build();
    final GitFetchService svc = new GitFetchService(vcs);
    // init cache
    svc.fetchRepository(root, CheckoutRules.DEFAULT, new FetchService.FetchRepositoryCallback() {

        public void update(final float progress, @NotNull final String message) {
        }
    });
    final OperationContext oc = vcs.createContext(root, "test");
    try {
        assertTime(2, "cache should be used", 2, new Runnable() {

            public void run() {
                try {
                    for (int i = 0; i < 1000; i++) {
                        svc.getOrCreateRepositoryState(oc);
                    }
                } catch (Throwable t) {
                    throw new Error(t);
                }
            }
        });
    } finally {
        oc.close();
    }
}
Also used : OperationContext(jetbrains.buildServer.buildTriggers.vcs.git.OperationContext) GitVcsSupport(jetbrains.buildServer.buildTriggers.vcs.git.GitVcsSupport) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) GitFetchService(jetbrains.buildServer.buildTriggers.vcs.git.GitFetchService) GitFetchService(jetbrains.buildServer.buildTriggers.vcs.git.GitFetchService)

Example 2 with VcsRootImpl

use of jetbrains.buildServer.vcs.impl.VcsRootImpl in project teamcity-git by JetBrains.

the class GitCommitsInfoBuilderTest method test.

public void test() throws VcsException {
    VcsRootImpl root = vcsRoot().withFetchUrl(GitUtils.toURL(myRepositoryDir)).withBranch("master").build();
    root.addProperty("INCLUDE_COMMIT_INFO_SUBMODULES", "true");
    GitVcsSupport vcs = gitSupport().withServerPaths(myServerPaths).build();
    final List<CommitInfo> commits = new ArrayList<CommitInfo>();
    new GitCommitsInfoBuilder(vcs, new GitFetchService(vcs)).collectCommits(root, CheckoutRules.DEFAULT, new CommitsInfoBuilder.CommitsConsumer() {

        public void consumeCommit(@NotNull CommitInfo commit) {
            commits.add(commit);
        }
    });
    List<String> allCommits = asList("ea5e05051fbfaa7d8da97586807b009cbfebae9d", "27de3d118ca320d3a8a08320ff05aa0567996590", "39679cc440c83671fbf6ad8083d92517f9602300", "9395143dd6c3e73abf9281be7a772c4d286e72a5", "039e7395725ad8d2a143fd44645a3fb72b001217", "777f79b3e89e63ac954fe0881470be3c72b8b0d4", "f61e30ce576e76bff877ddf1d00acf22c5c1b07a", "3df61e6f11a5a9b919cb3f786a83fdd09f058617", "465ad9f630e451b9f2b782ffb09804c6a98c4bb9", "5711cbfe566b6c92e331f95d4b236483f4532eed", "6cf3cb6a87091d17466607858c699c35edf30d3b", "2c7e90053e0f7a5dd25ea2a16ef8909ba71826f6", "2494559261ab85e92b1780860b34f876b5e6bce6", "e7f491963fbc5c96a27e4169b97746a5a7f83870", "1391281d33a83a7205f2f05d3eb64c349c636e87", "3b9fbfbb43e7edfad018b482e15e7f93cca4e69f", "e6b15b1f4741199857e2fa744eaadfe5a9d9aede", "feac610f381e697acf4c1c8ad82b7d76c7643b04", "92112555d9eb3e433eaa91fe32ec001ae8fe3c52", "778cc3d0105ca1b6b2587804ebfe89c2557a7e46", "f5bdd3819df0358a43d9a8f94eaf96bb306e19fe", "78cbbed3561de3417467ee819b1795ba14c03dfb", "233daeefb335b60c7b5700afde97f745d86cb40d", "6eae9acd29db2dba146929634a4bb1e6e72a31fd", "ce6044093939bb47283439d97a1c80f759669ff5", "2eed4ae6732536f76a65136a606f635e8ada63b9", "cbf1073bd3f938e7d7d85718dbc6c3bee10360d9", "d1a88fd33c516c1b607db75eb62244b2ea495c42", "37c371a6db0acefc77e3be99d16a44413e746591", "b5d65401a4e8a09b80b8d73ca4392f1913e99ff5", "592c5bcee6d906482177a62a6a44efa0cff9bbc7", "1837cf38309496165054af8bf7d62a9fe8997202", "049a98762a29677da352405b27b3d910cb94eb3b", "7e916b0edd394d0fca76456af89f4ff7f7f65049", "0dd03338d20d2e8068fbac9f24899d45d443df38", "70dbcf426232f7a33c7e5ebdfbfb26fc8c467a46", "a894d7d58ffde625019a9ecf8267f5f1d1e5c341", "f3f826ce85d6dad25156b2d7550cedeb1a422f4c", "ee886e4adb70fbe3bdc6f3f6393598b3f02e8009", "6fce8fe45550eb72796704a919dad68dc44be44a", "ad4528ed5c84092fdbe9e0502163cf8d6e6141e7", "97442a720324a0bd092fb9235f72246dc8b345bc", "2276eaf76a658f96b5cf3eb25f3e1fda90f6b653");
    List<String> reported = new ArrayList<String>();
    for (CommitInfo c : commits) {
        System.out.println(c.getVersion() + " " + c.getBranches() + " " + c.getTags() + " " + c.getHeadNames() + "  " + c.getMountPoints());
        Assert.assertEquals(c.getBranches(), c.getHeadNames());
        reported.add(c.getVersion());
    }
    assertTrue(reported.containsAll(allCommits));
    System.out.println("actual submodules:");
    Set<String> submodules = new TreeSet<String>();
    for (CommitInfo commit : commits) {
        for (CommitMountPointInfo info : commit.getMountPoints()) {
            final String line = commit.getVersion() + ": " + info.getMountPath() + " => " + info.getMountRevision();
            submodules.add(line);
            System.out.println(line);
        }
    }
    List<String> expectedSubmodules = new ArrayList<String>(Arrays.asList("7253d358a2490321a1808a1c20561b4027d69f77: submodule-wihtout-entry => 293a6e0d110fdbeed9729d87945876a0c52da182", "7253d358a2490321a1808a1c20561b4027d69f77: submodule-with-dirs => ded023a236d184753f826e62ac16b1612060e9d0", "27de3d118ca320d3a8a08320ff05aa0567996590: submodule => 6d944ac86dd5a45265873ddaa60e7ec343c1c4bb", "39679cc440c83671fbf6ad8083d92517f9602300: submodule-wihtout-entry => 293a6e0d110fdbeed9729d87945876a0c52da182", "39679cc440c83671fbf6ad8083d92517f9602300: submodule-with-dirs => 42fd2819d05e5b2b733f0a20e9b8b918e6e62141", "9395143dd6c3e73abf9281be7a772c4d286e72a5: submodule-with-dirs => 42fd2819d05e5b2b733f0a20e9b8b918e6e62141", "039e7395725ad8d2a143fd44645a3fb72b001217: submodule-with-dirs => 42fd2819d05e5b2b733f0a20e9b8b918e6e62141", "777f79b3e89e63ac954fe0881470be3c72b8b0d4: submodule-with-dirs => 42fd2819d05e5b2b733f0a20e9b8b918e6e62141", "f61e30ce576e76bff877ddf1d00acf22c5c1b07a: submodule-on-tag => 09f4eb4218bcbea3e3fa07ed552e1d4e9a6ffda7", "6cf3cb6a87091d17466607858c699c35edf30d3b: submodule-wihtout-entry => 293a6e0d110fdbeed9729d87945876a0c52da182", "6cf3cb6a87091d17466607858c699c35edf30d3b: submodule-with-dirs => 42fd2819d05e5b2b733f0a20e9b8b918e6e62141", "e6b15b1f4741199857e2fa744eaadfe5a9d9aede: submodule-wihtout-entry => 293a6e0d110fdbeed9729d87945876a0c52da182", "92112555d9eb3e433eaa91fe32ec001ae8fe3c52: submodule-wihtout-entry => 6d944ac86dd5a45265873ddaa60e7ec343c1c4bb", "f5bdd3819df0358a43d9a8f94eaf96bb306e19fe: submodule-wihtout-entry => 6d944ac86dd5a45265873ddaa60e7ec343c1c4bb", "ce6044093939bb47283439d97a1c80f759669ff5: first-level-submodule => 2ffafea06c7a385a78092dbc5e8a5a6225574397", "d1a88fd33c516c1b607db75eb62244b2ea495c42: submodule => 6d944ac86dd5a45265873ddaa60e7ec343c1c4bb", "37c371a6db0acefc77e3be99d16a44413e746591: submodule => 293a6e0d110fdbeed9729d87945876a0c52da182", "b5d65401a4e8a09b80b8d73ca4392f1913e99ff5: submodule => 6d944ac86dd5a45265873ddaa60e7ec343c1c4bb"));
    Assert.assertTrue(submodules.containsAll(expectedSubmodules));
}
Also used : CommitMountPointInfo(jetbrains.vcs.api.CommitMountPointInfo) GitCommitsInfoBuilder(jetbrains.buildServer.buildTriggers.vcs.git.commitInfo.GitCommitsInfoBuilder) GitVcsSupport(jetbrains.buildServer.buildTriggers.vcs.git.GitVcsSupport) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) GitCommitsInfoBuilder(jetbrains.buildServer.buildTriggers.vcs.git.commitInfo.GitCommitsInfoBuilder) CommitInfo(jetbrains.vcs.api.CommitInfo) GitFetchService(jetbrains.buildServer.buildTriggers.vcs.git.GitFetchService)

Example 3 with VcsRootImpl

use of jetbrains.buildServer.vcs.impl.VcsRootImpl in project teamcity-git by JetBrains.

the class GitHubPasswordAuthRootRegistryImplTest method test.

@Test
public void test() {
    final Map<String, Long> events = new HashMap<>();
    final MockTimeService timeService = new MockTimeService();
    final Map<String, String> vcsProperties = CollectionsUtil.asMap(VcsUtil.VCS_NAME_PROP, Constants.VCS_NAME, Constants.AUTH_METHOD, AuthenticationMethod.PASSWORD.name(), Constants.FETCH_URL, "https://github.com/name/repo.git", Constants.PASSWORD, "%some.param%");
    final Mockery mockery = new Mockery();
    final ProjectManager projectManager = mockery.mock(ProjectManager.class);
    final ServerResponsibility serverResponsibility = mockery.mock(ServerResponsibility.class);
    mockery.checking(new Expectations() {

        {
            allowing(projectManager).findVcsRootById(1);
            final MockSVcsRoot vcsRoot = new MockSVcsRoot(1, Constants.VCS_NAME);
            vcsRoot.setProperties(vcsProperties);
            will(returnValue(vcsRoot));
            allowing(serverResponsibility).canCheckForChanges();
            will(returnValue(true));
        }
    });
    final GitHubPasswordAuthRootRegistryImpl registry = new GitHubPasswordAuthRootRegistryImpl(new EventDispatcher<BuildServerListener>(BuildServerListener.class) {
    }, new EventDispatcher<RepositoryStateListener>(RepositoryStateListener.class) {
    }, projectManager, serverResponsibility, createMultiNodesEvents(events), timeService);
    vcsProperties.put(Constants.PASSWORD, "12345");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    events.clear();
    timeService.inc();
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    assertTrue(events.isEmpty());
    timeService.inc();
    final VcsRootInstanceContext context = mockery.mock(VcsRootInstanceContext.class);
    mockery.checking(new Expectations() {

        {
            allowing(context).getTimeService();
            will(returnValue(timeService));
            allowing(context).getServerMetrics();
            will(returnValue(null));
        }
    });
    registry.update(new VcsRootInstanceImpl(100, Constants.VCS_NAME, 1, "My Name", vcsProperties, context));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    assertTrue(events.isEmpty());
    timeService.inc();
    vcsProperties.put(Constants.PASSWORD, "1234567890abcdef1234567890abcdef12345678");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime - 1);
    assertTrue(events.isEmpty());
    setInternalProperty("teamcity.git.gitHubPasswordAuthHealthReport.updateIntervalSec", "300");
    timeService.inc(350, TimeUnit.SECONDS);
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertFalse(registry.containsVcsRoot(1));
    assertTrue(registry.getRegistry().isEmpty());
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    events.clear();
    timeService.inc();
    vcsProperties.put(Constants.PASSWORD, "12345");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    events.clear();
    timeService.inc();
    // see TW-71026
    vcsProperties.put(Constants.PASSWORD, "gho_oPan0zUSMxvI7NoWDBjjBP965641HX2NHNbu");
    timeService.inc(350, TimeUnit.SECONDS);
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertFalse(registry.containsVcsRoot(1));
    assertTrue(registry.getRegistry().isEmpty());
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    {
        // add root to registry again
        events.clear();
        timeService.inc();
        vcsProperties.put(Constants.PASSWORD, "12345");
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertTrue(registry.containsVcsRoot(1));
        BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
        BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    }
    {
        // TW-72780
        events.clear();
        timeService.inc();
        vcsProperties.put(Constants.AUTH_METHOD, AuthenticationMethod.ANONYMOUS.name());
        timeService.inc(350, TimeUnit.SECONDS);
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertFalse(registry.containsVcsRoot(1));
        assertTrue(registry.getRegistry().isEmpty());
        BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    }
    {
        // TW-73295
        events.clear();
        timeService.inc();
        timeService.inc(350, TimeUnit.SECONDS);
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertFalse(registry.containsVcsRoot(1));
        assertTrue(registry.getRegistry().isEmpty());
        assertTrue(events.isEmpty());
    }
}
Also used : Expectations(org.jmock.Expectations) MockSVcsRoot(jetbrains.buildServer.vcs.MockSVcsRoot) HashMap(java.util.HashMap) RepositoryStateListener(jetbrains.buildServer.vcs.RepositoryStateListener) ServerResponsibility(jetbrains.buildServer.serverSide.ServerResponsibility) MockTimeService(jetbrains.buildServer.MockTimeService) BuildServerListener(jetbrains.buildServer.serverSide.BuildServerListener) Mockery(org.jmock.Mockery) GitHubPasswordAuthRootRegistryImpl(jetbrains.buildServer.buildTriggers.vcs.git.GitHubPasswordAuthRootRegistryImpl) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) VcsRootInstanceContext(jetbrains.buildServer.serverSide.impl.beans.VcsRootInstanceContext) Test(org.testng.annotations.Test)

Example 4 with VcsRootImpl

use of jetbrains.buildServer.vcs.impl.VcsRootImpl in project teamcity-git by JetBrains.

the class AgentVcsSupportTest method mirror_delete_can_be_disabled.

@TestFor(issues = "TW-43884")
public void mirror_delete_can_be_disabled() throws Exception {
    MockFS fs = new MockFS();
    LoggingGitMetaFactory loggingFactory = new LoggingGitMetaFactory();
    myVcsSupport = myBuilder.setGitMetaFactory(loggingFactory).setFS(fs).build();
    File repo = dataFile("repo_for_fetch.1");
    File remoteRepo = myTempFiles.createTempDir();
    copyRepository(repo, remoteRepo);
    // run build to prepare mirror
    VcsRootImpl root = vcsRoot().withAgentGitPath(getGitPath()).withFetchUrl(GitUtils.toURL(remoteRepo)).build();
    myVcsSupport.updateSources(root, CheckoutRules.DEFAULT, "add81050184d3c818560bdd8839f50024c188586", myCheckoutDir, createRunningBuild(true), false);
    // update remote repo: add personal branch
    delete(remoteRepo);
    File updatedRepo = dataFile("repo_for_fetch.2.personal");
    copyRepository(updatedRepo, remoteRepo);
    // create refs/heads/personal/1 so that incremental fetch will fail
    AtomicInteger invocationCount = new AtomicInteger(0);
    loggingFactory.addCallback(FetchCommand.class.getName() + ".call", new GitCommandProxyCallback() {

        @Override
        public Optional<Object> call(final Method method, final Object[] args) throws VcsException {
            if (invocationCount.getAndIncrement() == 0)
                throw new VcsException("TEST ERROR");
            return Optional.empty();
        }
    });
    File mirror = myBuilder.getMirrorManager().getMirrorDir(GitUtils.toURL(remoteRepo));
    VcsRootImpl root2 = vcsRoot().withAgentGitPath(getGitPath()).withBranch("refs/heads/personal").withFetchUrl(GitUtils.toURL(remoteRepo)).build();
    fs.makeDeleteFail(mirror);
    try {
        AgentRunningBuild build = runningBuild().useLocalMirrors(true).sharedConfigParams(AgentRuntimeProperties.FAIL_ON_CLEAN_CHECKOUT, "true").withAgentConfiguration(myBuilder.getAgentConfiguration()).sharedConfigParams("teamcity.git.fetchMirrorRetryTimeouts", "0").build();
        myVcsSupport.updateSources(root2, CheckoutRules.DEFAULT, "d47dda159b27b9a8c4cee4ce98e4435eb5b17168", myCheckoutDir, build, false);
        fail("Should fail");
    } catch (VcsException e) {
        File mirrorAfterBuild = myBuilder.getMirrorManager().getMirrorDir(GitUtils.toURL(remoteRepo));
        // should fail on first fetch attempt and not remap or delete the mirror
        then(mirrorAfterBuild).isEqualTo(mirror);
    }
}
Also used : AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VcsException(jetbrains.buildServer.vcs.VcsException) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) File(java.io.File) TestFor(jetbrains.buildServer.util.TestFor)

Example 5 with VcsRootImpl

use of jetbrains.buildServer.vcs.impl.VcsRootImpl in project teamcity-git by JetBrains.

the class AgentVcsSupportTest method should_handle_HEAD_pointing_to_invalid_object.

@TestFor(issues = "TW-74592")
public void should_handle_HEAD_pointing_to_invalid_object() throws Exception {
    File repo = dataFile("repo_for_fetch.1");
    File remoteRepo = myTempFiles.createTempDir();
    copyRepository(repo, remoteRepo);
    VcsRootImpl root = vcsRoot().withAgentGitPath(getGitPath()).withFetchUrl(GitUtils.toURL(remoteRepo)).withUseMirrors(true).build();
    // first build
    AgentRunningBuild build = createRunningBuild(map(PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY, PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY_ALTERNATES));
    myVcsSupport.updateSources(root, CheckoutRules.DEFAULT, "add81050184d3c818560bdd8839f50024c188586", myCheckoutDir, build, false);
    // create ref pointing to invalid object
    File gitDir = new File(myCheckoutDir, ".git");
    String invalidObject = "bba7fbcc200b4968e6abd2f7d475dc15306cafc1";
    FileUtil.writeFile(new File(gitDir, "HEAD"), invalidObject);
    // update remote repo
    delete(remoteRepo);
    File updatedRepo = dataFile("repo_for_fetch.3");
    copyRepository(updatedRepo, remoteRepo);
    // second build
    build = createRunningBuild(map(PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY, PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY_ALTERNATES));
    myVcsSupport.updateSources(root, CheckoutRules.DEFAULT, "bba7fbcc200b4968e6abd2f7d475dc15306cafc6", myCheckoutDir, build, false);
    assertEquals("ref: refs/heads/master", FileUtil.readText(new File(gitDir, "HEAD")).trim());
}
Also used : VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) File(java.io.File) TestFor(jetbrains.buildServer.util.TestFor)

Aggregations

VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)51 File (java.io.File)35 GitTestUtil.dataFile (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile)34 TestFor (jetbrains.buildServer.util.TestFor)26 Test (org.testng.annotations.Test)25 VcsException (jetbrains.buildServer.vcs.VcsException)10 FileUtil.writeFile (jetbrains.buildServer.util.FileUtil.writeFile)9 LockFile (org.eclipse.jgit.internal.storage.file.LockFile)9 StoredConfig (org.eclipse.jgit.lib.StoredConfig)7 BeforeMethod (org.testng.annotations.BeforeMethod)7 Method (java.lang.reflect.Method)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AgentRunningBuild (jetbrains.buildServer.agent.AgentRunningBuild)6 AfterMethod (org.testng.annotations.AfterMethod)6 CheckoutRules (jetbrains.buildServer.vcs.CheckoutRules)5 NotNull (org.jetbrains.annotations.NotNull)5 JSchException (com.jcraft.jsch.JSchException)4 Repository (org.eclipse.jgit.lib.Repository)4 ServerPaths (jetbrains.buildServer.serverSide.ServerPaths)3 VcsRootSshKeyManager (jetbrains.buildServer.ssh.VcsRootSshKeyManager)3