Search in sources :

Example 6 with GithubConnection

use of org.jetbrains.plugins.github.api.GithubConnection in project intellij-community by JetBrains.

the class GithubUtil method getTwoFactorAuthData.

private static void getTwoFactorAuthData(@NotNull final Project project, @NotNull final GithubAuthDataHolder authHolder, @NotNull final ProgressIndicator indicator, @NotNull final GithubAuthData oldAuth) throws GithubOperationCanceledException {
    authHolder.runTransaction(oldAuth, () -> {
        if (authHolder.getAuthData().getAuthType() != GithubAuthData.AuthType.BASIC) {
            throw new GithubOperationCanceledException("Two factor authentication can be used only with Login/Password");
        }
        GithubApiUtil.askForTwoFactorCodeSMS(new GithubConnection(oldAuth, false));
        final Ref<String> codeRef = new Ref<>();
        ApplicationManager.getApplication().invokeAndWait(() -> {
            codeRef.set(Messages.showInputDialog(project, "Authentication Code", "Github Two-Factor Authentication", null));
        }, indicator.getModalityState());
        if (codeRef.isNull()) {
            throw new GithubOperationCanceledException("Can't get two factor authentication code");
        }
        GithubSettings settings = GithubSettings.getInstance();
        if (settings.getAuthType() == GithubAuthData.AuthType.BASIC && StringUtil.equalsIgnoreCase(settings.getLogin(), oldAuth.getBasicAuth().getLogin())) {
            settings.setValidGitAuth(false);
        }
        return oldAuth.copyWithTwoFactorCode(codeRef.get());
    });
}
Also used : GithubConnection(org.jetbrains.plugins.github.api.GithubConnection) Ref(com.intellij.openapi.util.Ref) GithubOperationCanceledException(org.jetbrains.plugins.github.exceptions.GithubOperationCanceledException)

Example 7 with GithubConnection

use of org.jetbrains.plugins.github.api.GithubConnection in project intellij-community by JetBrains.

the class GithubUtil method testConnection.

@NotNull
private static GithubUserDetailed testConnection(@NotNull Project project, @NotNull GithubAuthDataHolder authHolder, @NotNull final ProgressIndicator indicator) throws IOException {
    GithubAuthData auth = authHolder.getAuthData();
    try {
        final GithubConnection connection = new GithubConnection(auth, true);
        ScheduledFuture<?> future = null;
        try {
            future = addCancellationListener(indicator, connection);
            return GithubApiUtil.getCurrentUser(connection);
        } finally {
            connection.close();
            if (future != null)
                future.cancel(true);
        }
    } catch (GithubTwoFactorAuthenticationException e) {
        getTwoFactorAuthData(project, authHolder, indicator, auth);
        return testConnection(project, authHolder, indicator);
    }
}
Also used : GithubConnection(org.jetbrains.plugins.github.api.GithubConnection) GithubTwoFactorAuthenticationException(org.jetbrains.plugins.github.exceptions.GithubTwoFactorAuthenticationException) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GithubConnection

use of org.jetbrains.plugins.github.api.GithubConnection in project intellij-community by JetBrains.

the class GithubUtil method runTask.

public static <T> T runTask(@NotNull Project project, @NotNull GithubAuthDataHolder authHolder, @NotNull final ProgressIndicator indicator, @NotNull AuthLevel authLevel, @NotNull ThrowableConvertor<GithubConnection, T, IOException> task) throws IOException {
    GithubAuthData auth = authHolder.getAuthData();
    try {
        if (!authLevel.accepts(auth)) {
            throw new GithubAuthenticationException("Expected other authentication type: " + authLevel);
        }
        final GithubConnection connection = new GithubConnection(auth, true);
        ScheduledFuture<?> future = null;
        try {
            future = addCancellationListener(indicator, connection);
            return task.convert(connection);
        } finally {
            connection.close();
            if (future != null)
                future.cancel(true);
        }
    } catch (GithubTwoFactorAuthenticationException e) {
        getTwoFactorAuthData(project, authHolder, indicator, auth);
        return runTask(project, authHolder, indicator, authLevel, task);
    } catch (GithubAuthenticationException e) {
        getValidAuthData(project, authHolder, indicator, authLevel, auth);
        return runTask(project, authHolder, indicator, authLevel, task);
    }
}
Also used : GithubConnection(org.jetbrains.plugins.github.api.GithubConnection) GithubAuthenticationException(org.jetbrains.plugins.github.exceptions.GithubAuthenticationException) GithubTwoFactorAuthenticationException(org.jetbrains.plugins.github.exceptions.GithubTwoFactorAuthenticationException)

Example 9 with GithubConnection

use of org.jetbrains.plugins.github.api.GithubConnection in project intellij-community by JetBrains.

the class GithubCreateGistTestBase method deleteGist.

protected void deleteGist() throws IOException {
    if (GIST_ID != null) {
        GithubApiUtil.deleteGist(new GithubConnection(myGitHubSettings.getAuthData()), GIST_ID);
        GIST = null;
        GIST_ID = null;
    }
}
Also used : GithubConnection(org.jetbrains.plugins.github.api.GithubConnection)

Example 10 with GithubConnection

use of org.jetbrains.plugins.github.api.GithubConnection in project intellij-community by JetBrains.

the class GithubIssuesTest method testAssigneeIssues6.

public void testAssigneeIssues6() throws Exception {
    List<GithubIssue> result = GithubApiUtil.getIssuesAssigned(new GithubConnection(myAuth), myLogin2, REPO_NAME, "", 100, true);
    List<Long> issues = ContainerUtil.map(result, new Function<GithubIssue, Long>() {

        @Override
        public Long fun(GithubIssue githubIssue) {
            return githubIssue.getNumber();
        }
    });
    List<Long> expected = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L);
    assertTrue(Comparing.haveEqualElements(issues, expected));
}
Also used : GithubConnection(org.jetbrains.plugins.github.api.GithubConnection) GithubIssue(org.jetbrains.plugins.github.api.data.GithubIssue)

Aggregations

GithubConnection (org.jetbrains.plugins.github.api.GithubConnection)20 GithubIssue (org.jetbrains.plugins.github.api.data.GithubIssue)10 ArrayList (java.util.ArrayList)3 GithubRepo (org.jetbrains.plugins.github.api.data.GithubRepo)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2 GithubApiUtil (org.jetbrains.plugins.github.api.GithubApiUtil)2 GithubTwoFactorAuthenticationException (org.jetbrains.plugins.github.exceptions.GithubTwoFactorAuthenticationException)2 GithubTest (org.jetbrains.plugins.github.test.GithubTest)2 Assume.assumeNotNull (org.junit.Assume.assumeNotNull)2 Ref (com.intellij.openapi.util.Ref)1 Nullable (org.jetbrains.annotations.Nullable)1 GithubRepoDetailed (org.jetbrains.plugins.github.api.data.GithubRepoDetailed)1 GithubAuthenticationException (org.jetbrains.plugins.github.exceptions.GithubAuthenticationException)1 GithubOperationCanceledException (org.jetbrains.plugins.github.exceptions.GithubOperationCanceledException)1 GithubAuthData (org.jetbrains.plugins.github.util.GithubAuthData)1