Search in sources :

Example 1 with OAuthConnectionDescriptor

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

the class SpaceExternalChangeViewerExtension method getAvailableProperties.

@Nullable
@Override
public Map<String, String> getAvailableProperties(@NotNull VcsRoot vcs) {
    final VcsRootInstance vcsRoot = (VcsRootInstance) vcs;
    if (!Constants.VCS_NAME.equals(vcsRoot.getVcsName()))
        return null;
    String url = vcsRoot.getProperty(Constants.FETCH_URL);
    if (url == null)
        return null;
    URIish uri;
    try {
        uri = new URIish(url);
    } catch (URISyntaxException e) {
        return null;
    }
    final String gitPath = uri.getPath();
    if (uri.getHost().endsWith(".jetbrains.space")) {
        // Space is on the Jetbrains side
        final String[] strings = gitPath.substring(1).split("/");
        if (strings.length == 3) {
            final String orgName = strings[0];
            final String project = strings[1];
            final String repository = strings[2].replaceFirst("\\.git$", "");
            final String repositoryUrl = String.format("https://%s.jetbrains.space/p/%s/repositories/%s", orgName, project, repository);
            return createResponse(repositoryUrl);
        }
    } else {
        final SVcsRoot vcsRootParent = vcsRoot.getParent();
        final List<OAuthConnectionDescriptor> connections = myOAuthConnectionsManager.getAvailableConnectionsOfType(vcsRootParent.getProject(), SpaceOAuthProvider.TYPE);
        if (connections.isEmpty())
            return null;
        for (OAuthConnectionDescriptor connection : connections) {
            final SpaceConnectDescriber spaceConnectDescriber = new SpaceConnectDescriber(connection);
            final String spaceAddress = spaceConnectDescriber.getAddress();
            if (uri.getHost().contains(spaceAddress)) {
                // Selfhosted Space
                final String[] strings = gitPath.substring(1).split("/");
                if (strings.length == 2) {
                    final String project = strings[0];
                    final String repository = strings[1].replaceFirst("\\.git$", "");
                    final String repositoryUrl = String.format("https://%s/p/%s/repositories/%s", spaceAddress, project, repository);
                    return createResponse(repositoryUrl);
                }
            }
        }
    }
    return null;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) SpaceConnectDescriber(jetbrains.buildServer.serverSide.oauth.space.SpaceConnectDescriber) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) URISyntaxException(java.net.URISyntaxException) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with OAuthConnectionDescriptor

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

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot.

@Test
void jetbrainsTeamVcsRoot() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(selfHosted).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final SProject project = Mockito.mock(SProject.class);
    final SVcsRoot sVcsRoot = Mockito.mock(SVcsRoot.class);
    Mockito.doReturn(sVcsRoot).when(vcsRoot).getParent();
    Mockito.doReturn(project).when(sVcsRoot).getProject();
    final OAuthConnectionDescriptor connection = Mockito.mock(OAuthConnectionDescriptor.class);
    Mockito.doReturn(ImmutableList.of(connection)).when(myOAuthConnectionsManager).getAvailableConnectionsOfType(project, SpaceOAuthProvider.TYPE);
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, selfHostedServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection).getParameters();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertEquals(availableProperties, ImmutableMap.of(PropertyType.CHANGE_SET_TYPE, "https://jetbrains.team/p/tc/repositories/TeamCity/revision/${changeSetId}", PropertyType.LINK_TEXT, "Open in Space", PropertyType.LINK_ICON_CLASS, "tc-icon_space"));
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) SProject(jetbrains.buildServer.serverSide.SProject) Test(org.testng.annotations.Test)

Example 3 with OAuthConnectionDescriptor

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

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot_twoConnections.

@Test
void jetbrainsTeamVcsRoot_twoConnections() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(selfHosted).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final SProject project = Mockito.mock(SProject.class);
    final SVcsRoot sVcsRoot = Mockito.mock(SVcsRoot.class);
    Mockito.doReturn(sVcsRoot).when(vcsRoot).getParent();
    Mockito.doReturn(project).when(sVcsRoot).getProject();
    final OAuthConnectionDescriptor connection1 = Mockito.mock(OAuthConnectionDescriptor.class);
    final OAuthConnectionDescriptor connection2 = Mockito.mock(OAuthConnectionDescriptor.class);
    Mockito.doReturn(ImmutableList.of(connection1, connection2)).when(myOAuthConnectionsManager).getAvailableConnectionsOfType(project, SpaceOAuthProvider.TYPE);
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, hostedOnJetBrainsSideServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection1).getParameters();
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, selfHostedServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection2).getParameters();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertEquals(availableProperties, ImmutableMap.of(PropertyType.CHANGE_SET_TYPE, "https://jetbrains.team/p/tc/repositories/TeamCity/revision/${changeSetId}", PropertyType.LINK_TEXT, "Open in Space", PropertyType.LINK_ICON_CLASS, "tc-icon_space"));
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) SProject(jetbrains.buildServer.serverSide.SProject) Test(org.testng.annotations.Test)

Example 4 with OAuthConnectionDescriptor

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

the class SpaceExternalChangeViewerExtensionTest method jetbrainsSpaceVcsRoot.

@Test
void jetbrainsSpaceVcsRoot() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(hostedOnJetBrainsSide).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final OAuthConnectionDescriptor connection = Mockito.mock(OAuthConnectionDescriptor.class);
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, hostedOnJetBrainsSideServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection).getParameters();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertEquals(availableProperties, ImmutableMap.of(PropertyType.CHANGE_SET_TYPE, "https://golubinov.jetbrains.space/p/oauthspace/repositories/test-epo/revision/${changeSetId}", PropertyType.LINK_TEXT, "Open in Space", PropertyType.LINK_ICON_CLASS, "tc-icon_space"));
}
Also used : VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) Test(org.testng.annotations.Test)

Aggregations

OAuthConnectionDescriptor (jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor)4 SpaceExternalChangeViewerExtension (jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension)3 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)3 VcsRootInstanceImpl (jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl)3 Test (org.testng.annotations.Test)3 SProject (jetbrains.buildServer.serverSide.SProject)2 URISyntaxException (java.net.URISyntaxException)1 SpaceConnectDescriber (jetbrains.buildServer.serverSide.oauth.space.SpaceConnectDescriber)1 VcsRootInstance (jetbrains.buildServer.vcs.VcsRootInstance)1 URIish (org.eclipse.jgit.transport.URIish)1 Nullable (org.jetbrains.annotations.Nullable)1