Search in sources :

Example 1 with ListDebuggeesResponse

use of com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectRepositoryValidator method checkSyncStashState.

/**
 * Compares the current source tree with the state described by the Cloud Debugger api. Only local
 * and cloud repo Git repositories are supported.
 */
@NotNull
@Transient
public SyncResult checkSyncStashState() {
    if (processState.getProject() == null) {
        return new SyncResult(/*isInvalid*/
        true, /*needsStash*/
        false, /*needsSync*/
        false, /*target SHA*/
        null, /*target repo*/
        null, /* cloud repo */
        false, /* repoType */
        null);
    }
    GitRepositoryManager manager = GitUtil.getRepositoryManager(processState.getProject());
    List<GitRepository> repositories = manager.getRepositories();
    CloudRepoSourceContext cloudRepo = null;
    GerritSourceContext gerritRepo = null;
    GitSourceContext otherGitRepo = null;
    String repoType = null;
    boolean foundDebuggee = false;
    if (getCloudDebuggerClient() != null && !com.google.common.base.Strings.isNullOrEmpty(processState.getProjectNumber())) {
        ListDebuggeesResponse debuggees;
        try {
            debuggees = getCloudDebuggerClient().debuggees().list().setProject(processState.getProjectNumber()).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            for (Debuggee debuggee : debuggees.getDebuggees()) {
                if (processState.getDebuggeeId() != null && processState.getDebuggeeId().equals(debuggee.getId())) {
                    // implicit assumption this doesn't happen more than once
                    foundDebuggee = true;
                    List<SourceContext> contexts = debuggee.getSourceContexts();
                    if (contexts != null) {
                        for (SourceContext sourceContext : contexts) {
                            cloudRepo = sourceContext.getCloudRepo();
                            gerritRepo = sourceContext.getGerrit();
                            otherGitRepo = sourceContext.getGit();
                            if (cloudRepo != null) {
                                // shouldn't be more than one repo but if there is, we'll prefer cloud repos
                                break;
                            } else if (sourceContext.getCloudWorkspace() != null) {
                                repoType = GctBundle.getString("clouddebug.workspace");
                            }
                        }
                    }
                }
            }
        } catch (IOException ex) {
            LOG.warn("Error detecting server side source context", ex);
        }
    }
    if (!foundDebuggee) {
        return new SyncResult(/*isinvalid*/
        true, /*needsstash*/
        false, /*needssync*/
        false, /*target SHA*/
        null, /*target repo*/
        null, /* hasCloudRepository */
        false, /* repoType */
        GctBundle.getString("clouddebug.unknown.repository.type"));
    }
    GitRepository targetLocalRepo = null;
    String revisionId = null;
    // shouldn't be more than one repo but if there is, we pick cloud repos
    if (cloudRepo != null) {
        revisionId = cloudRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.cloud.repository");
    } else if (gerritRepo != null) {
        revisionId = gerritRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.gerrit");
    } else if (otherGitRepo != null) {
        revisionId = otherGitRepo.getRevisionId();
        repoType = GctBundle.getString("clouddebug.nongoogle.git");
    }
    if (revisionId != null) {
        for (GitRepository repository : repositories) {
            try {
                GitChangeUtils.resolveReference(processState.getProject(), repository.getRoot(), revisionId);
                targetLocalRepo = repository;
                break;
            } catch (VcsException ex) {
                LOG.warn("cloud revision not found in local repo.  continuing search...");
            }
        }
    }
    boolean needsStash = false;
    boolean needsSync = false;
    String syncSha = null;
    if (targetLocalRepo != null) {
        // check for local changes.
        try {
            if (GitUtil.hasLocalChanges(true, processState.getProject(), targetLocalRepo.getRoot()) || GitUtil.hasLocalChanges(false, processState.getProject(), targetLocalRepo.getRoot())) {
                needsStash = true;
            }
            if (!Strings.isNullOrEmpty(targetLocalRepo.getCurrentRevision()) && !Strings.isNullOrEmpty(revisionId) && targetLocalRepo.getCurrentRevision() != null && !targetLocalRepo.getCurrentRevision().equals(revisionId)) {
                syncSha = revisionId;
                needsSync = true;
            }
        } catch (VcsException vcsException) {
            LOG.error("Error detecting local changes during attach", vcsException);
        }
    }
    boolean hasRemoteRepository = cloudRepo != null || gerritRepo != null || otherGitRepo != null;
    return new SyncResult(/*isinvalid*/
    false, needsStash, needsSync, syncSha, targetLocalRepo, hasRemoteRepository, repoType);
}
Also used : ListDebuggeesResponse(com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitSourceContext(com.google.api.services.clouddebugger.v2.model.GitSourceContext) CloudRepoSourceContext(com.google.api.services.clouddebugger.v2.model.CloudRepoSourceContext) GerritSourceContext(com.google.api.services.clouddebugger.v2.model.GerritSourceContext) IOException(java.io.IOException) GerritSourceContext(com.google.api.services.clouddebugger.v2.model.GerritSourceContext) CloudRepoSourceContext(com.google.api.services.clouddebugger.v2.model.CloudRepoSourceContext) GitSourceContext(com.google.api.services.clouddebugger.v2.model.GitSourceContext) SourceContext(com.google.api.services.clouddebugger.v2.model.SourceContext) GitRepository(git4idea.repo.GitRepository) Debuggee(com.google.api.services.clouddebugger.v2.model.Debuggee) VcsException(com.intellij.openapi.vcs.VcsException) Transient(com.intellij.util.xmlb.annotations.Transient) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ListDebuggeesResponse

use of com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectDebuggeeBinding method refreshDebugTargetList.

/**
 * Refreshes the list of attachable debug targets based on the project selection.
 */
@SuppressWarnings("FutureReturnValueIgnored")
private void refreshDebugTargetList(CloudProject cloudProject) {
    targetSelector.removeAllItems();
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        try {
            String projectNumber = Optional.ofNullable(cloudProject.projectNumber()).map(Object::toString).orElse(null);
            if (projectNumber != null && getCloudDebuggerClient() != null) {
                final ListDebuggeesResponse debuggees = getCloudDebuggerClient().debuggees().list().setProject(projectNumber).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
                isCdbQueried = true;
                SwingUtilities.invokeLater(() -> {
                    DebugTarget targetSelection = null;
                    if (debuggees == null || debuggees.getDebuggees() == null || debuggees.getDebuggees().isEmpty()) {
                        disableTargetSelector(GctBundle.getString("clouddebug.nomodulesfound"));
                    } else {
                        targetSelector.setEnabled(true);
                        Map<String, DebugTarget> perModuleCache = new HashMap<>();
                        for (Debuggee debuggee : debuggees.getDebuggees()) {
                            DebugTarget item = new DebugTarget(debuggee, cloudProject.projectId());
                            if (!Strings.isNullOrEmpty(item.getModule()) && !Strings.isNullOrEmpty(item.getVersion())) {
                                // If we already have an existing item for that module+version,
                                // compare the minor versions and only use the latest minor version.
                                String key = String.format("%s:%s", item.getModule(), item.getVersion());
                                DebugTarget existing = perModuleCache.get(key);
                                if (existing != null && existing.getMinorVersion() > item.getMinorVersion()) {
                                    continue;
                                }
                                if (existing != null) {
                                    targetSelector.removeItem(existing);
                                }
                                perModuleCache.put(key, item);
                            }
                            if (inputState != null && !Strings.isNullOrEmpty(inputState.getDebuggeeId())) {
                                if (inputState.getDebuggeeId().equals(item.getId())) {
                                    targetSelection = item;
                                }
                            }
                            targetSelector.addItem(item);
                            okAction.setEnabled(true);
                        }
                    }
                    if (targetSelection != null) {
                        targetSelector.setSelectedItem(targetSelection);
                    }
                });
            }
        } catch (final IOException ex) {
            SwingUtilities.invokeLater(() -> disableTargetSelector(ex));
            LOG.warn("Error listing debuggees from Cloud Debugger API", ex);
        }
    });
}
Also used : HashMap(com.intellij.util.containers.HashMap) Debuggee(com.google.api.services.clouddebugger.v2.model.Debuggee) ListDebuggeesResponse(com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException)

Aggregations

Debuggee (com.google.api.services.clouddebugger.v2.model.Debuggee)2 ListDebuggeesResponse (com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse)2 IOException (java.io.IOException)2 CloudRepoSourceContext (com.google.api.services.clouddebugger.v2.model.CloudRepoSourceContext)1 GerritSourceContext (com.google.api.services.clouddebugger.v2.model.GerritSourceContext)1 GitSourceContext (com.google.api.services.clouddebugger.v2.model.GitSourceContext)1 SourceContext (com.google.api.services.clouddebugger.v2.model.SourceContext)1 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)1 VcsException (com.intellij.openapi.vcs.VcsException)1 HashMap (com.intellij.util.containers.HashMap)1 Transient (com.intellij.util.xmlb.annotations.Transient)1 GitRepository (git4idea.repo.GitRepository)1 GitRepositoryManager (git4idea.repo.GitRepositoryManager)1 NotNull (org.jetbrains.annotations.NotNull)1