From aec4aa7694983a5bfe191ede6bfef5740bb65310 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Thu, 27 Jun 2013 15:08:53 +0400 Subject: [PATCH] Github: read config while creating gist --- .../github/GithubCreateGistAction.java | 12 ++++---- .../jetbrains/plugins/github/GithubUtil.java | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java index 6df1e5bee5e7..b271c614fa91 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java @@ -120,7 +120,7 @@ public class GithubCreateGistAction extends DumbAwareAction { final Ref authDataRef = new Ref(); ProgressManager.getInstance().run(new Task.Modal(project, "Access to GitHub", true) { public void run(@NotNull ProgressIndicator indicator) { - authDataRef.set(GithubUtil.getValidAuthData(project, indicator)); + authDataRef.set(GithubUtil.getValidAuthDataFromConfig(project, indicator)); } }); if (authDataRef.isNull()) { @@ -204,7 +204,7 @@ public class GithubCreateGistAction extends DumbAwareAction { boolean isPrivate, @NotNull String description) { if (contents.isEmpty()) { - GithubNotifications.showWarning(project, "Failed to create gist", "Can't create empty gist"); + GithubNotifications.showWarning(project, FAILED_TO_CREATE_GIST, "Can't create empty gist"); return null; } String requestBody = prepareJsonRequest(description, isPrivate, contents); @@ -218,7 +218,7 @@ public class GithubCreateGistAction extends DumbAwareAction { } if (jsonElement == null) { LOG.info("Null JSON response returned by GitHub"); - showError(project, "Failed to create gist", "Empty JSON response returned by GitHub", null, null); + showError(project, FAILED_TO_CREATE_GIST, "Empty JSON response returned by GitHub", null, null); return null; } if (!jsonElement.isJsonObject()) { @@ -228,14 +228,14 @@ public class GithubCreateGistAction extends DumbAwareAction { JsonElement htmlUrl = jsonElement.getAsJsonObject().get("html_url"); if (htmlUrl == null) { LOG.info("Invalid JSON response: " + jsonElement); - showError(project, "Invalid GitHub response", "No html_url property", jsonElement.toString(), null); + showError(project, FAILED_TO_CREATE_GIST, "Invalid GitHub response", jsonElement.toString(), null); return null; } return htmlUrl.getAsString(); } catch (IOException e) { LOG.info("Exception when creating a Gist", e); - showError(project, "Failed to create gist", "", null, e); + showError(project, FAILED_TO_CREATE_GIST, "", null, e); return null; } } @@ -290,7 +290,7 @@ public class GithubCreateGistAction extends DumbAwareAction { } String content = readFile(file); if (content == null) { - showError(project, FAILED_TO_CREATE_GIST, "Couldn't read the contents of the file " + file, null, null); + GithubNotifications.showWarning(project, FAILED_TO_CREATE_GIST, "Couldn't read the contents of the file " + file); LOG.info("Couldn't read the contents of the file " + file); return Collections.emptyList(); } diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java b/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java index a812291810c5..df1a095457ce 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java @@ -102,6 +102,34 @@ public class GithubUtil { return dialog.getAuthData(); } + @Nullable + public static GithubAuthData getValidAuthDataFromConfig(@NotNull Project project, @NotNull ProgressIndicator indicator) { + GithubAuthData auth = getAuthData(); + boolean valid = false; + try { + valid = checkAuthData(auth); + } + catch (IOException e) { + // ignore + } + if (!valid) { + final GithubLoginDialog dialog = new GithubLoginDialog(project); + ApplicationManager.getApplication().invokeAndWait(new Runnable() { + @Override + public void run() { + dialog.show(); + } + }, indicator.getModalityState()); + if (!dialog.isOK()) { + return null; + } + return dialog.getAuthData(); + } + else { + return auth; + } + } + public static boolean checkAuthData(GithubAuthData auth) throws IOException { if (StringUtil.isEmptyOrSpaces(auth.getHost()) || StringUtil.isEmptyOrSpaces(auth.getLogin()) ||