Github clone url autocomplete list loader

This commit is contained in:
Ivan Semenov
2018-02-12 18:49:24 +03:00
parent ea4a0fc5cf
commit d7e1cefacf
2 changed files with 86 additions and 3 deletions
@@ -0,0 +1,73 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.extensions;
import com.intellij.dvcs.hosting.RepositoryListLoader;
import com.intellij.dvcs.hosting.RepositoryListLoadingException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import git4idea.DialogManager;
import git4idea.remote.GitRepositoryHostingService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.github.api.GithubApiUtil;
import org.jetbrains.plugins.github.api.data.GithubRepo;
import org.jetbrains.plugins.github.ui.GithubLoginDialog;
import org.jetbrains.plugins.github.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class GithubRepositoryHostingService extends GitRepositoryHostingService {
@NotNull
@Override
public String getServiceDisplayName() {
return "GitHub";
}
@Override
@NotNull
public RepositoryListLoader getRepositoryListLoader(@NotNull Project project) {
return new RepositoryListLoader() {
@NotNull private final GithubAuthDataHolder myAuthDataHolder = GithubAuthDataHolder.createFromSettings();
@Override
public boolean isEnabled() {
return AuthLevel.LOGGED.accepts(myAuthDataHolder.getAuthData());
}
@Override
public boolean enable() {
GithubAuthData currentAuthData = myAuthDataHolder.getAuthData();
myAuthDataHolder.runTransaction(currentAuthData, () -> {
GithubLoginDialog dialog = new GithubLoginDialog(project, currentAuthData, AuthLevel.LOGGED);
DialogManager.show(dialog);
if (dialog.isOK()) {
GithubAuthData authData = dialog.getAuthData();
GithubSettings.getInstance().setAuthData(authData, dialog.isSavePasswordSelected());
return authData;
}
return currentAuthData;
});
return isEnabled();
}
@NotNull
@Override
public List<String> getAvailableRepositories(@NotNull ProgressIndicator progressIndicator) throws RepositoryListLoadingException {
try {
return GithubUtil.runTask(project, myAuthDataHolder, progressIndicator, connection -> GithubApiUtil.getAvailableRepos(connection))
.stream()
.sorted(Comparator.comparing(GithubRepo::getUserName).thenComparing(GithubRepo::getName))
.map(repo -> GithubUrlUtil
.getCloneUrl(GithubUrlUtil.getGitHostWithoutProtocol(myAuthDataHolder.getAuthData().getHost()),
repo.getUserName(),
repo.getName()))
.collect(Collectors.toList());
}
catch (Exception e) {
throw new RepositoryListLoadingException("Error connecting to Github", e);
}
}
};
}
}
@@ -104,7 +104,12 @@ public class GithubUrlUtil {
*/
@NotNull
public static String getGitHostWithoutProtocol() {
return removeTrailingSlash(removeProtocolPrefix(GithubSettings.getInstance().getHost()));
return getGitHostWithoutProtocol(GithubSettings.getInstance().getHost());
}
@NotNull
public static String getGitHostWithoutProtocol(@NotNull String host) {
return removeTrailingSlash(removeProtocolPrefix(host));
}
/*
@@ -209,11 +214,16 @@ public class GithubUrlUtil {
@NotNull
public static String getCloneUrl(@NotNull String user, @NotNull String repo) {
return getCloneUrl(getGitHostWithoutProtocol(), user, repo);
}
@NotNull
public static String getCloneUrl(@NotNull String host, @NotNull String user, @NotNull String repo) {
if (GithubSettings.getInstance().isCloneGitUsingSsh()) {
return "git@" + getGitHostWithoutProtocol() + ":" + user + "/" + repo + ".git";
return "git@" + host + ":" + user + "/" + repo + ".git";
}
else {
return getApiProtocol() + getGitHostWithoutProtocol() + "/" + user + "/" + repo + ".git";
return getApiProtocol() + host + "/" + user + "/" + repo + ".git";
}
}
}