Search in sources :

Example 1 with SpaceConnectDescriber

use of jetbrains.buildServer.serverSide.oauth.space.SpaceConnectDescriber 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)

Aggregations

URISyntaxException (java.net.URISyntaxException)1 OAuthConnectionDescriptor (jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor)1 SpaceConnectDescriber (jetbrains.buildServer.serverSide.oauth.space.SpaceConnectDescriber)1 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)1 VcsRootInstance (jetbrains.buildServer.vcs.VcsRootInstance)1 URIish (org.eclipse.jgit.transport.URIish)1 Nullable (org.jetbrains.annotations.Nullable)1