Search in sources :

Example 1 with GithubShareDialog

use of org.jetbrains.plugins.github.ui.GithubShareDialog 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)

Aggregations

ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 GitRepository (git4idea.repo.GitRepository)1 GitRepositoryManager (git4idea.repo.GitRepositoryManager)1 GithubShareDialog (org.jetbrains.plugins.github.ui.GithubShareDialog)1 GithubAuthDataHolder (org.jetbrains.plugins.github.util.GithubAuthDataHolder)1