Search in sources :

Example 11 with CredentialedUser

use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.

the class CloneCloudRepositoryDialog method updateButtons.

/**
 * Check fields and display error in the wrapper if there is a problem.
 */
private void updateButtons() {
    CloudProject selectedProject = projectSelector.getSelectedProject();
    Optional<CredentialedUser> selectedUser = selectedProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(selectedProject.googleUsername());
    if (selectedProject != null && !selectedUser.isPresent()) {
        setErrorText(GctBundle.message("cloud.repository.dialog.invalid.project"));
        setOKActionEnabled(false);
        return;
    }
    if (!StringUtil.isEmpty(repositorySelector.getText()) && StringUtil.isEmpty(repositorySelector.getSelectedRepository())) {
        setErrorText(GctBundle.message("cloud.repository.dialog.invalid.repository"));
        setOKActionEnabled(false);
        return;
    }
    if (!selectedUser.isPresent() || StringUtil.isEmpty(repositorySelector.getSelectedRepository())) {
        setErrorText(null);
        setOKActionEnabled(false);
        return;
    }
    if (StringUtil.isEmpty(parentDirectory.getText()) || StringUtil.isEmpty(directoryName.getText())) {
        setErrorText(null);
        setOKActionEnabled(false);
        return;
    }
    File file = new File(parentDirectory.getText(), directoryName.getText());
    if (file.exists()) {
        setErrorText(GctBundle.message("clonefromgcp.destination.exists.error"));
        setOKActionEnabled(false);
        return;
    }
    if (!file.getParentFile().exists()) {
        setErrorText(GctBundle.message("clonefromgcp.parent.missing.error"));
        setOKActionEnabled(false);
        return;
    }
    setErrorText(null);
    setOKActionEnabled(true);
}
Also used : CloudProject(com.google.cloud.tools.intellij.project.CloudProject) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 12 with CredentialedUser

use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.

the class RepositorySelector method showPopup.

@Override
public void showPopup(RelativePoint showTarget) {
    Optional<CredentialedUser> user = cloudProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(cloudProject.googleUsername());
    if (user.isPresent()) {
        if (popup == null || popup.isDisposed()) {
            panel = new RepositoryPanel();
            ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null);
            popup = popupBuilder.createPopup();
        }
        if (!popup.isVisible()) {
            popup.show(showTarget);
        }
    } else {
        panel = new ProjectNotSelectedPanel();
        ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null);
        popup = popupBuilder.createPopup();
        popup.show(showTarget);
    }
}
Also used : ComponentPopupBuilder(com.intellij.openapi.ui.popup.ComponentPopupBuilder) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser)

Example 13 with CredentialedUser

use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.

the class SetupCloudRepositoryAction method uploadProjectToGoogleCloud.

private static void uploadProjectToGoogleCloud(@NotNull final Project project, @Nullable final VirtualFile file) {
    BasicAction.saveAll();
    project.save();
    final GitRepository gitRepository = getGitRepository(project, file);
    final boolean gitDetected = gitRepository != null;
    final VirtualFile root = gitDetected ? gitRepository.getRoot() : project.getBaseDir();
    SetupCloudRepositoryDialog dialog = new SetupCloudRepositoryDialog(project, gitRepository, GctBundle.message("uploadtogcp.title"), GctBundle.message("uploadtogcp.oktext"));
    DialogManager.show(dialog);
    if (!dialog.isOK() || dialog.getCredentialedUser() == null || Strings.isNullOrEmpty(dialog.getProjectId())) {
        return;
    }
    String remoteName = dialog.getRemoteName();
    if (gitRepository != null && hasRemote(gitRepository.getRemotes(), remoteName)) {
        Notification notification = new Notification(NOTIFICATION_GROUP_ID, GctBundle.message("uploadtogcp.remotename.collision.title"), GctBundle.message("uploadtogcp.remotename.collision", remoteName), NotificationType.ERROR);
        notification.notify(project);
        return;
    }
    final String projectId = dialog.getProjectId();
    final String repositoryId = dialog.getRepositoryId();
    final CredentialedUser user = dialog.getCredentialedUser();
    // finish the job in background
    new Task.Backgroundable(project, GctBundle.message("uploadtogcp.backgroundtitle")) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            // creating empty git repo if git is not initialized
            LOG.info("Binding local project with Git");
            if (!gitDetected) {
                LOG.info("No git detected, creating empty git repo");
                indicator.setText(GctBundle.message("uploadtogcp.indicatorinit"));
                if (!createEmptyGitRepository(project, root, indicator)) {
                    return;
                }
            }
            GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
            final GitRepository repository = repositoryManager.getRepositoryForRoot(root);
            if (repository == null) {
                SwingUtilities.invokeLater(() -> {
                    Notification notification = new Notification(NOTIFICATION_GROUP_ID, GctBundle.message("uploadtogcp.generic.failure.title"), GctBundle.message("uploadtogcp.failedtocreategit"), NotificationType.ERROR);
                    notification.notify(project);
                });
                LOG.error("GitRepository is null for root " + root);
                return;
            }
            final String remoteUrl = GcpHttpAuthDataProvider.getGcpUrl(projectId, repositoryId);
            LOG.info("Adding Google as a remote host");
            indicator.setText(GctBundle.message("uploadtogcp.addingremote"));
            if (!addGitRemote(project, repository, remoteName, remoteUrl)) {
                return;
            }
            boolean succeeded = false;
            try {
                PropertiesComponent.getInstance(project).setValue(GcpHttpAuthDataProvider.GCP_USER, user.getEmail());
                LOG.info("Fetching from Google remote");
                indicator.setText(GctBundle.message("uploadtogcp.fetching"));
                if (!fetchGit(project, indicator, repository, remoteName)) {
                    return;
                }
                // create sample commit for binding project
                if (!performFirstCommitIfRequired(project, root, repository, indicator)) {
                    return;
                }
                // git push origin master
                LOG.info("Pushing to Google master");
                indicator.setText(GctBundle.message("uploadtogcp.pushingtotgcp"));
                if (!pushCurrentBranch(project, repository, remoteName, remoteUrl)) {
                    return;
                }
                succeeded = true;
            } finally {
                if (!succeeded) {
                    // remove the remote if possible on a failure, so the user can try again.
                    removeGitRemote(project, repository, remoteName);
                }
            }
            showInfoUrl(project, remoteName, GctBundle.message("uploadtogcp.success"), remoteUrl);
        }
    }.queue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GitRepositoryManager(git4idea.repo.GitRepositoryManager) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) Notification(com.intellij.notification.Notification)

Example 14 with CredentialedUser

use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.

the class RepositorySelectorTest method createInitializedSelector.

private RepositorySelector createInitializedSelector() {
    String mockUserId = "mockUser";
    CredentialedUser mockUser = mock(CredentialedUser.class);
    when(googleLoginService.getLoggedInUser(mockUserId)).thenReturn(Optional.of(mockUser));
    return new RepositorySelector(CloudProject.create("my-project", "my-project", mockUserId), false);
}
Also used : CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) Matchers.anyString(org.mockito.Matchers.anyString)

Example 15 with CredentialedUser

use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessHandlerTest method setupMocks.

private void setupMocks() {
    GoogleLoginState googleLoginState = mock(GoogleLoginState.class);
    CredentialedUser credentialedUser = mock(CredentialedUser.class);
    when(credentialedUser.getGoogleLoginState()).thenReturn(googleLoginState);
    LinkedHashMap<String, CredentialedUser> users = new LinkedHashMap<String, CredentialedUser>();
    users.put("mockUser@foo.bar", credentialedUser);
    when(mockLoginService.getAllUsers()).thenReturn(users);
    CloudDebugProcessState cloudDebugProcessState = mock(CloudDebugProcessState.class);
    when(cloudDebugProcessState.getUserEmail()).thenReturn("mockUser@foo.bar");
    CloudDebugProcess cloudDebugProcess = mock(CloudDebugProcess.class);
    when(cloudDebugProcess.getProcessState()).thenReturn(cloudDebugProcessState);
    handler = new CloudDebugProcessHandler(cloudDebugProcess);
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)31 IOException (java.io.IOException)9 GoogleLoginState (com.google.gdt.eclipse.login.common.GoogleLoginState)6 Nullable (org.jetbrains.annotations.Nullable)6 IntegratedGoogleLoginService (com.google.cloud.tools.intellij.login.IntegratedGoogleLoginService)5 CloudProject (com.google.cloud.tools.intellij.project.CloudProject)5 LinkedHashMap (java.util.LinkedHashMap)5 Credential (com.google.api.client.auth.oauth2.Credential)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)4 ArrayList (java.util.ArrayList)3 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 HttpTransport (com.google.api.client.http.HttpTransport)2 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)2 Iam (com.google.api.services.iam.v1.Iam)2 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)2 ServiceAccountKey (com.google.api.services.iam.v1.model.ServiceAccountKey)2 GoogleApiClientFactory (com.google.cloud.tools.intellij.resources.GoogleApiClientFactory)2 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)2