From e057eb70e4b674496b4a4acbc3c73747eb9bd5a9 Mon Sep 17 00:00:00 2001 From: Ivan Semenov Date: Wed, 25 Jul 2018 19:15:52 +0300 Subject: [PATCH] [github] Allow to connect to github with http --- .../github/GithubOpenInBrowserActionGroup.kt | 8 +-- .../github/api/GithubRepositoryPath.kt | 4 ++ .../plugins/github/api/GithubServerPath.java | 52 ++++++++++++++----- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserActionGroup.kt b/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserActionGroup.kt index f997efd51292..4ca04c93160c 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserActionGroup.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserActionGroup.kt @@ -143,7 +143,7 @@ open class GithubOpenInBrowserActionGroup } private fun openCommitInBrowser(path: GithubRepositoryPath, revisionHash: String) { - BrowserUtil.browse("https://$path/commit/$revisionHash") + BrowserUtil.browse("${path.toUrl()}/commit/$revisionHash") } private fun openFileInBrowser(project: Project, @@ -186,13 +186,13 @@ open class GithubOpenInBrowserActionGroup relativePath: String, branch: String, path: GithubRepositoryPath): String? { - val builder = StringBuilder("https://") + val builder = StringBuilder() if (StringUtil.isEmptyOrSpaces(relativePath)) { - builder.append(path).append("/tree/").append(branch) + builder.append(path.toUrl()).append("/tree/").append(branch) } else { - builder.append(path).append("/blob/").append(branch).append('/').append(relativePath) + builder.append(path.toUrl()).append("/blob/").append(branch).append('/').append(relativePath) } if (editor != null && editor.document.lineCount >= 1) { diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubRepositoryPath.kt b/plugins/github/src/org/jetbrains/plugins/github/api/GithubRepositoryPath.kt index c6e15c547aeb..1705a5deeb5c 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubRepositoryPath.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubRepositoryPath.kt @@ -2,6 +2,10 @@ package org.jetbrains.plugins.github.api data class GithubRepositoryPath(private val serverPath: GithubServerPath, private val repositoryPath: GithubFullPath) { + fun toUrl(): String { + return serverPath.toUrl() + "/" + repositoryPath.fullName + } + override fun toString(): String { return "$serverPath/${repositoryPath.fullName}" } diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubServerPath.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubServerPath.java index 3aa228d2c123..502cf1e1b125 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubServerPath.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubServerPath.java @@ -2,6 +2,7 @@ package org.jetbrains.plugins.github.api; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.io.URLUtil; import com.intellij.util.xmlb.annotations.Attribute; import com.intellij.util.xmlb.annotations.Tag; import org.jetbrains.annotations.NonNls; @@ -10,17 +11,18 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.github.exceptions.GithubParseException; import org.jetbrains.plugins.github.util.GithubUrlUtil; +import java.util.Locale; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.Locale; - /** * Github server reference allowing to specify custom port and path to instance */ @Tag("server") public class GithubServerPath { + @Attribute("useHttp") + @Nullable private final Boolean myUseHttp; @Attribute("host") @NotNull private final String myHost; @Attribute("port") @@ -29,19 +31,28 @@ public class GithubServerPath { @Nullable private final String mySuffix; public GithubServerPath() { - this("", null, null); + this(null, "", null, null); } public GithubServerPath(@NonNls @NotNull String host) { - this(host, null, null); + this(null, host, null, null); } - public GithubServerPath(@NonNls @NotNull String host, @Nullable Integer port, @NonNls @Nullable String suffix) { + public GithubServerPath(@Nullable Boolean useHttp, + @NonNls @NotNull String host, + @Nullable Integer port, + @NonNls @Nullable String suffix) { + myUseHttp = useHttp; myHost = host.toLowerCase(Locale.ENGLISH); myPort = port; mySuffix = suffix != null ? suffix.toLowerCase(Locale.ENGLISH) : null; } + @NotNull + public String getSchema() { + return (myUseHttp == null || !myUseHttp) ? "https" : "http"; + } + @NotNull public String getHost() { return myHost; @@ -62,7 +73,7 @@ public class GithubServerPath { return StringUtil.startsWithIgnoreCase(url, myHost + StringUtil.notNullize(mySuffix)); } - // 2 - host, 4 - port, 5 - path + // 1 - schema, 2 - host, 4 - port, 5 - path private final static Pattern URL_REGEX = Pattern.compile("^(https?://)?([^/?:]+)(:(\\d+))?((/[^/?#]+)*)?"); @NotNull @@ -70,6 +81,8 @@ public class GithubServerPath { Matcher matcher = URL_REGEX.matcher(uri); if (!matcher.matches()) throw new GithubParseException("Not a valid URL"); + String schema = matcher.group(1); + Boolean httpSchema = (schema == null || schema.isEmpty()) ? null : schema.equals("http://"); String host = matcher.group(2); if (host == null) throw new GithubParseException("Empty host"); @@ -89,12 +102,27 @@ public class GithubServerPath { String path = StringUtil.nullize(matcher.group(5)); - return new GithubServerPath(host, port, path); + return new GithubServerPath(httpSchema, host, port, path); + } + + @NotNull + public String toUrl() { + return getSchemaUrlPart() + myHost + getPortUrlPart() + StringUtil.notNullize(mySuffix); } public String toString() { - String port = myPort != null ? (":" + myPort.toString()) : ""; - return myHost + port + StringUtil.notNullize(mySuffix); + String schema = myUseHttp != null ? getSchemaUrlPart() : ""; + return schema + myHost + getPortUrlPart() + StringUtil.notNullize(mySuffix); + } + + @NotNull + private String getPortUrlPart() { + return myPort != null ? (":" + myPort.toString()) : ""; + } + + @NotNull + private String getSchemaUrlPart() { + return getSchema() + URLUtil.SCHEME_SEPARATOR; } @Override @@ -102,14 +130,14 @@ public class GithubServerPath { if (this == o) return true; if (!(o instanceof GithubServerPath)) return false; GithubServerPath path = (GithubServerPath)o; - return Objects.equals(myHost, path.myHost) && + return Objects.equals(myUseHttp, path.myUseHttp) && + Objects.equals(myHost, path.myHost) && Objects.equals(myPort, path.myPort) && Objects.equals(mySuffix, path.mySuffix); } @Override public int hashCode() { - - return Objects.hash(myHost, myPort, mySuffix); + return Objects.hash(myUseHttp, myHost, myPort, mySuffix); } }