Search in sources :

Example 1 with GithubAuthDataHolder

use of org.jetbrains.plugins.github.util.GithubAuthDataHolder in project intellij-community by JetBrains.

the class GithubShareAction method shareProjectOnGithub.

public static void shareProjectOnGithub(@NotNull final Project project, @Nullable final VirtualFile file) {
    BasicAction.saveAll();
    // get gitRepository
    final GitRepository gitRepository = GithubUtil.getGitRepository(project, file);
    final boolean gitDetected = gitRepository != null;
    final VirtualFile root = gitDetected ? gitRepository.getRoot() : project.getBaseDir();
    final GithubAuthDataHolder authHolder = GithubAuthDataHolder.createFromSettings();
    // check for existing git repo
    Set<String> existingRemotes = Collections.emptySet();
    if (gitDetected) {
        final String githubRemote = GithubUtil.findGithubRemoteUrl(gitRepository);
        if (githubRemote != null) {
            if (!checkExistingRemote(project, authHolder, githubRemote))
                return;
        }
        existingRemotes = ContainerUtil.map2Set(gitRepository.getRemotes(), GitRemote::getName);
    }
    // get available GitHub repos with modal progress
    final GithubInfo githubInfo = loadGithubInfoWithModal(authHolder, project);
    if (githubInfo == null) {
        return;
    }
    // Show dialog (window)
    final GithubShareDialog shareDialog = new GithubShareDialog(project, githubInfo.getRepositoryNames(), existingRemotes, githubInfo.getUser().canCreatePrivateRepo());
    DialogManager.show(shareDialog);
    if (!shareDialog.isOK()) {
        return;
    }
    final boolean isPrivate = shareDialog.isPrivate();
    final String name = shareDialog.getRepositoryName();
    final String description = shareDialog.getDescription();
    final String remoteName = shareDialog.getRemoteName();
    new Task.Backgroundable(project, "Sharing Project on GitHub...") {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            // create GitHub repo (network)
            LOG.info("Creating GitHub repository");
            indicator.setText("Creating GitHub repository...");
            final String url = createGithubRepository(project, authHolder, indicator, name, description, isPrivate);
            if (url == null) {
                return;
            }
            LOG.info("Successfully created GitHub repository");
            // creating empty git repo if git is not initialized
            LOG.info("Binding local project with GitHub");
            if (!gitDetected) {
                LOG.info("No git detected, creating empty git repo");
                indicator.setText("Creating empty git repo...");
                if (!createEmptyGitRepository(project, root, indicator)) {
                    return;
                }
            }
            GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
            final GitRepository repository = repositoryManager.getRepositoryForRoot(root);
            if (repository == null) {
                GithubNotifications.showError(project, "Failed to create GitHub Repository", "Can't find Git repository");
                return;
            }
            final String remoteUrl = GithubUrlUtil.getCloneUrl(githubInfo.getUser().getLogin(), name);
            //git remote add origin git@github.com:login/name.git
            LOG.info("Adding GitHub as a remote host");
            indicator.setText("Adding GitHub as a remote host...");
            if (!GithubUtil.addGithubRemote(project, repository, remoteName, remoteUrl)) {
                return;
            }
            // create sample commit for binding project
            if (!performFirstCommitIfRequired(project, root, repository, indicator, name, url)) {
                return;
            }
            //git push origin master
            LOG.info("Pushing to github master");
            indicator.setText("Pushing to github master...");
            if (!pushCurrentBranch(project, repository, remoteName, remoteUrl, name, url)) {
                return;
            }
            GithubNotifications.showInfoURL(project, "Successfully shared project on GitHub", name, url);
        }
    }.queue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) GithubAuthDataHolder(org.jetbrains.plugins.github.util.GithubAuthDataHolder) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitRepository(git4idea.repo.GitRepository) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GithubShareDialog(org.jetbrains.plugins.github.ui.GithubShareDialog)

Example 2 with GithubAuthDataHolder

use of org.jetbrains.plugins.github.util.GithubAuthDataHolder in project intellij-community by JetBrains.

the class GithubCreateGistTest method testAnonymous.

public void testAnonymous() throws Throwable {
    List<FileContent> expected = createContent();
    String url = GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(GithubAuthData.createAnonymous(myHost)), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);
    checkGistExists();
    checkGistAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
    // anonymous gists - undeletable
    GIST_ID = null;
    GIST = null;
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) GithubAuthDataHolder(org.jetbrains.plugins.github.util.GithubAuthDataHolder)

Example 3 with GithubAuthDataHolder

use of org.jetbrains.plugins.github.util.GithubAuthDataHolder in project intellij-community by JetBrains.

the class GithubShareAction method checkExistingRemote.

private static boolean checkExistingRemote(@NotNull final Project project, @NotNull final GithubAuthDataHolder authHolder, @NotNull String remote) {
    final GithubFullPath path = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(remote);
    if (path == null) {
        return GithubNotifications.showYesNoDialog(project, "Project Is Already on GitHub", "Can't connect to repository from configured remote. You could want to check .git config.\n" + "Do you want to proceed anyway?");
    }
    try {
        GithubRepo repo = GithubUtil.computeValueInModalIO(project, "Access to GitHub", indicator -> GithubUtil.runTask(project, authHolder, indicator, connection -> GithubApiUtil.getDetailedRepoInfo(connection, path.getUser(), path.getRepository())));
        int result = Messages.showDialog(project, "Successfully connected to " + repo.getHtmlUrl() + ".\n" + "Do you want to proceed anyway?", "Project Is Already on GitHub", new String[] { "Continue", "Open in Browser", Messages.CANCEL_BUTTON }, 2, Messages.getQuestionIcon());
        if (result == 0)
            return true;
        if (result == 1) {
            BrowserUtil.browse(repo.getHtmlUrl());
        }
        return false;
    } catch (GithubStatusCodeException e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            return GithubNotifications.showYesNoDialog(project, "Project Is Already on GitHub", "Can't connect to repository from configured remote. You could want to check .git config.\n" + "Do you want to proceed anyway?");
        }
        GithubNotifications.showErrorDialog(project, "Failed to Connect to GitHub", e);
        return false;
    } catch (IOException e) {
        GithubNotifications.showErrorDialog(project, "Failed to Connect to GitHub", e);
        return false;
    }
}
Also used : GithubStatusCodeException(org.jetbrains.plugins.github.exceptions.GithubStatusCodeException) git4idea.commands(git4idea.commands) GithubUrlUtil(org.jetbrains.plugins.github.util.GithubUrlUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashSet(com.intellij.util.containers.HashSet) HttpStatus(org.apache.http.HttpStatus) GithubApiUtil(org.jetbrains.plugins.github.api.GithubApiUtil) GitUtil(git4idea.GitUtil) Task(com.intellij.openapi.progress.Task) GithubRepo(org.jetbrains.plugins.github.api.data.GithubRepo) Messages(com.intellij.openapi.ui.Messages) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) Logger(com.intellij.openapi.diagnostic.Logger) VcsException(com.intellij.openapi.vcs.VcsException) GitRepository(git4idea.repo.GitRepository) GithubUserDetailed(org.jetbrains.plugins.github.api.data.GithubUserDetailed) GithubAuthDataHolder(org.jetbrains.plugins.github.util.GithubAuthDataHolder) ChangeListManager(com.intellij.openapi.vcs.changes.ChangeListManager) GitBundle(git4idea.i18n.GitBundle) GithubFullPath(org.jetbrains.plugins.github.api.GithubFullPath) BrowserUtil(com.intellij.ide.BrowserUtil) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ServiceManager(com.intellij.openapi.components.ServiceManager) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ApplicationManager(com.intellij.openapi.application.ApplicationManager) GitFileUtils(git4idea.util.GitFileUtils) DataProvider(com.intellij.openapi.actionSystem.DataProvider) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) java.util(java.util) SelectFilesDialog(com.intellij.openapi.vcs.changes.ui.SelectFilesDialog) ProjectLevelVcsManager(com.intellij.openapi.vcs.ProjectLevelVcsManager) GitRepositoryManager(git4idea.repo.GitRepositoryManager) NonNls(org.jetbrains.annotations.NonNls) GitUIUtil(git4idea.util.GitUIUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) GithubNotifications(org.jetbrains.plugins.github.util.GithubNotifications) GitInit(git4idea.actions.GitInit) Project(com.intellij.openapi.project.Project) VcsFileUtil(com.intellij.vcsUtil.VcsFileUtil) DialogManager(git4idea.DialogManager) GitLocalBranch(git4idea.GitLocalBranch) GithubShareDialog(org.jetbrains.plugins.github.ui.GithubShareDialog) Splitter(com.intellij.openapi.ui.Splitter) GitRemote(git4idea.repo.GitRemote) IOException(java.io.IOException) BasicAction(git4idea.actions.BasicAction) GithubUtil(org.jetbrains.plugins.github.util.GithubUtil) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) VcsDataKeys(com.intellij.openapi.vcs.VcsDataKeys) GithubIcons(icons.GithubIcons) CommitMessage(com.intellij.openapi.vcs.ui.CommitMessage) javax.swing(javax.swing) GithubStatusCodeException(org.jetbrains.plugins.github.exceptions.GithubStatusCodeException) GithubRepo(org.jetbrains.plugins.github.api.data.GithubRepo) IOException(java.io.IOException) GithubFullPath(org.jetbrains.plugins.github.api.GithubFullPath)

Aggregations

ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 GitRepository (git4idea.repo.GitRepository)2 GitRepositoryManager (git4idea.repo.GitRepositoryManager)2 GithubAuthDataHolder (org.jetbrains.plugins.github.util.GithubAuthDataHolder)2 BrowserUtil (com.intellij.ide.BrowserUtil)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 DataProvider (com.intellij.openapi.actionSystem.DataProvider)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)1 Project (com.intellij.openapi.project.Project)1 Messages (com.intellij.openapi.ui.Messages)1 Splitter (com.intellij.openapi.ui.Splitter)1 Ref (com.intellij.openapi.util.Ref)1 ProjectLevelVcsManager (com.intellij.openapi.vcs.ProjectLevelVcsManager)1 VcsDataKeys (com.intellij.openapi.vcs.VcsDataKeys)1