From e178d626f0f004b8764a0f007940fa79bc04738d Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Thu, 8 Aug 2013 10:40:27 +0400 Subject: [PATCH] Github: autodetect private repo in token generation --- .../plugins/github/api/GithubApiUtil.java | 12 ++++- .../github/tasks/GithubRepositoryEditor.java | 52 +++---------------- 2 files changed, 17 insertions(+), 47 deletions(-) diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java index 107476f56160..4d76d327e7fd 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java @@ -409,6 +409,16 @@ public class GithubApiUtil { return response.getToken(); } + @NotNull + public static String getReadOnlyToken(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, @Nullable String note) + throws IOException { + GithubRepo repository = getDetailedRepoInfo(auth, user, repo); + + List scopes = repository.isPrivate() ? Collections.singletonList("repo") : Collections.emptyList(); + + return getScopedToken(auth, scopes, note); + } + @NotNull public static GithubUser getCurrentUser(@NotNull GithubAuthData auth) throws IOException { JsonElement result = getRequest(auth, "/user"); @@ -643,4 +653,4 @@ public class GithubApiUtil { return null; } -} \ No newline at end of file +} diff --git a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepositoryEditor.java b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepositoryEditor.java index 0a4f52f1077d..8f36399ff444 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepositoryEditor.java +++ b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepositoryEditor.java @@ -13,16 +13,16 @@ import com.intellij.util.ThrowableConvertor; import com.intellij.util.ui.FormBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.github.*; import org.jetbrains.plugins.github.GithubAuthData; +import org.jetbrains.plugins.github.GithubAuthenticationCanceledException; +import org.jetbrains.plugins.github.GithubNotifications; +import org.jetbrains.plugins.github.GithubUtil; import org.jetbrains.plugins.github.api.GithubApiUtil; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; /** * @author Dennis.Ushakov @@ -34,7 +34,6 @@ public class GithubRepositoryEditor extends BaseRepositoryEditor changeListener) { super(project, repository, changeListener); @@ -47,7 +46,6 @@ public class GithubRepositoryEditor extends BaseRepositoryEditor> scopesRef = new Ref>(); - final Ref exceptionRef = new Ref(); - ProgressManager.getInstance().run(new Task.Modal(myProject, "Access to GitHub", true) { - public void run(@NotNull ProgressIndicator indicator) { - try { - scopesRef - .set(GithubApiUtil.getTokenScopes(GithubAuthData.createTokenAuth(myURLText.getText().trim(), myToken.getText().trim()))); - } - catch (IOException e) { - exceptionRef.set(e); - } - } - }); - if (!exceptionRef.isNull()) { - GithubNotifications.showErrorDialog(myProject, "Can't check token scopes", exceptionRef.get()); - return; - } - Collection scopes = scopesRef.get(); - if (myPrivateRepo.isSelected()) { - scopes.remove("repo"); - } - if (scopes.isEmpty()) { - return; - } - GithubNotifications - .showWarningDialog(myProject, "Unneeded token scopes detected", "Unneeded scopes: " + StringUtil.join(scopes, ", ")); - } - } - private void generateToken() { final Ref tokenRef = new Ref(); final Ref exceptionRef = new Ref(); - final Collection scopes = myPrivateRepo.isSelected() ? Collections.singleton("repo") : Collections.emptyList(); ProgressManager.getInstance().run(new Task.Modal(myProject, "Access to GitHub", true) { public void run(@NotNull ProgressIndicator indicator) { try { tokenRef.set(GithubUtil.runWithValidBasicAuth(myProject, indicator, new ThrowableConvertor() { - @Nullable + @NotNull @Override public String convert(GithubAuthData auth) throws IOException { - return GithubApiUtil.getScopedToken(auth, scopes, "Intellij tasks plugin"); - + return GithubApiUtil.getReadOnlyToken(auth, myRepoAuthor.getText(), myRepoName.getText(), "Intellij tasks plugin"); } })); }