Search in sources :

Example 1 with ProjectManager

use of jetbrains.buildServer.serverSide.ProjectManager in project teamcity-rest by JetBrains.

the class AgentPoolFinder method getPoolProjects.

// todo: TeamCity API: what is the due way to do this? http://youtrack.jetbrains.com/issue/TW-33307
/**
 * Gets all projects associated with the specified pool.
 */
@NotNull
public List<SProject> getPoolProjects(@NotNull final AgentPool agentPool) {
    final Set<String> projectIds = myAgentPoolManager.getPoolProjects(agentPool.getAgentPoolId());
    final ProjectManager projectManager = myServiceLocator.getSingletonService(ProjectManager.class);
    final List<SProject> result = new ArrayList<SProject>(projectIds.size());
    for (String projectId : projectIds) {
        final SProject project = projectManager.findProjectById(projectId);
        if (project != null) {
            result.add(project);
        } else {
        // todo: include not present projects into the list
        }
    }
    return result;
}
Also used : SProject(jetbrains.buildServer.serverSide.SProject) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ProjectManager

use of jetbrains.buildServer.serverSide.ProjectManager 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 3 with ProjectManager

use of jetbrains.buildServer.serverSide.ProjectManager in project teamcity-rest by JetBrains.

the class AgentPoolFinder method getPoolOwnerProject.

@Nullable
public PontentiallyInaccessibleProject getPoolOwnerProject(@NotNull final AgentPool projectAgentPool) {
    if (!projectAgentPool.isProjectPool())
        return null;
    ProjectAgentPoolImpl projectPool;
    try {
        projectPool = (ProjectAgentPoolImpl) projectAgentPool;
    } catch (ClassCastException e) {
        // should never happen
        return null;
    }
    final ProjectManager projectManager = myServiceLocator.getSingletonService(ProjectManager.class);
    try {
        return new PontentiallyInaccessibleProject(projectPool.getProjectId(), projectManager.findProjectById(projectPool.getProjectId()));
    } catch (AccessDeniedException e) {
        return new PontentiallyInaccessibleProject(projectPool.getProjectId(), null);
    }
}
Also used : ProjectAgentPoolImpl(jetbrains.buildServer.serverSide.agentPools.ProjectAgentPoolImpl) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ProjectManager

use of jetbrains.buildServer.serverSide.ProjectManager in project teamcity-git by JetBrains.

the class GitUrlSupportTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    super.setUp();
    ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
    PluginConfig config = new PluginConfigBuilder(paths).build();
    myMirrorManager = new MirrorManagerImpl(config, new HashCalculatorImpl(), new RemoteRepositoryUrlInvestigatorImpl());
    myProjectMock = mock(SProject.class);
    final Mock pmMock = mock(ProjectManager.class);
    final SProject project = (SProject) myProjectMock.proxy();
    pmMock.stubs().method("findProjectById").will(returnValue(project));
    ProjectManager pm = (ProjectManager) pmMock.proxy();
    final Mock sshMock = mock(ServerSshKeyManager.class);
    sshMock.stubs().method("getKeys").with(eq(project)).will(returnValue(myTestKeys));
    ServerSshKeyManager ssh = (ServerSshKeyManager) sshMock.proxy();
    Mock epMock = mock(ExtensionsProvider.class);
    epMock.stubs().method("getExtensions").with(eq(ServerSshKeyManager.class)).will(returnValue(Collections.singleton(ssh)));
    myGitVcsSupport = gitSupport().withServerPaths(paths).withTestConnectionSupport(vcsRoot -> {
        if (myTestConnectionMocked != null && myTestConnectionMocked)
            return null;
        return myGitVcsSupport.testConnection(vcsRoot);
    }).build();
    myUrlSupport = new GitUrlSupport(myGitVcsSupport) {

        @NotNull
        @Override
        protected VcsRoot createDummyRoot(@NotNull final Map<String, String> props, @Nullable final SProject curProject) {
            return new VcsRootImpl(-1, Constants.VCS_NAME, props);
        }
    };
    myUrlSupport.setProjectManager(pm);
    myUrlSupport.setExtensionsProvider((ExtensionsProvider) epMock.proxy());
}
Also used : SProject(jetbrains.buildServer.serverSide.SProject) NotNull(org.jetbrains.annotations.NotNull) Mock(org.jmock.Mock) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) ServerSshKeyManager(jetbrains.buildServer.ssh.ServerSshKeyManager) ServerPaths(jetbrains.buildServer.serverSide.ServerPaths) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

ProjectManager (jetbrains.buildServer.serverSide.ProjectManager)4 SProject (jetbrains.buildServer.serverSide.SProject)2 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)2 NotNull (org.jetbrains.annotations.NotNull)2 HashMap (java.util.HashMap)1 MockTimeService (jetbrains.buildServer.MockTimeService)1 GitHubPasswordAuthRootRegistryImpl (jetbrains.buildServer.buildTriggers.vcs.git.GitHubPasswordAuthRootRegistryImpl)1 BuildServerListener (jetbrains.buildServer.serverSide.BuildServerListener)1 ServerPaths (jetbrains.buildServer.serverSide.ServerPaths)1 ServerResponsibility (jetbrains.buildServer.serverSide.ServerResponsibility)1 ProjectAgentPoolImpl (jetbrains.buildServer.serverSide.agentPools.ProjectAgentPoolImpl)1 AccessDeniedException (jetbrains.buildServer.serverSide.auth.AccessDeniedException)1 VcsRootInstanceContext (jetbrains.buildServer.serverSide.impl.beans.VcsRootInstanceContext)1 ServerSshKeyManager (jetbrains.buildServer.ssh.ServerSshKeyManager)1 MockSVcsRoot (jetbrains.buildServer.vcs.MockSVcsRoot)1 RepositoryStateListener (jetbrains.buildServer.vcs.RepositoryStateListener)1 VcsRootInstanceImpl (jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl)1 Nullable (org.jetbrains.annotations.Nullable)1 Expectations (org.jmock.Expectations)1 Mock (org.jmock.Mock)1