[github] Allow to connect to github with http

This commit is contained in:
Ivan Semenov
2018-07-26 14:46:52 +03:00
parent 1cd2d1a2a4
commit e057eb70e4
3 changed files with 48 additions and 16 deletions
@@ -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) {
@@ -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}"
}
@@ -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);
}
}