/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.teamcity.ui.actions; import com.intellij.openapi.util.Trinity; import jetbrains.buildServer.ConnectionStatusListener.ErrorProcessingKind; import jetbrains.buildServer.IncompatiblePluginError; import jetbrains.buildServer.RefreshSummaryCommand; import jetbrains.buildServer.Strings; import jetbrains.buildServer.responsibility.ResponsibilityEntry; import jetbrains.buildServer.serverProxy.RemoteBuildServerFacade; import jetbrains.buildServer.serverSide.auth.AuthenticationFailedException; import jetbrains.buildServer.users.User; import jetbrains.buildServer.users.UserSet; import jetbrains.buildServer.xmlrpc.RemoteCallException; import jetbrains.teamcity.Activator; import jetbrains.teamcity.HelpConstants; import jetbrains.teamcity.core.TeamCitySnapshot4Eclipse; import jetbrains.teamcity.core.workflow.IWorkItem; import jetbrains.teamcity.ui.UIUtil; import jetbrains.teamcity.ui.views.AssignResponsibilityDialog; import jetbrains.teamcity.ui.views.CommentsDialog; import org.eclipse.jface.action.Action; import org.eclipse.jface.window.Window; import org.eclipse.swt.custom.BusyIndicator; import org.eclipse.swt.widgets.Shell; import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; public class InvestigationAction extends Action { public static enum ActionType { GIVEUP, CLOSE, ASSIGN } private final ActionType myType; private final IWorkItem[] myItems; public InvestigationAction(@NotNull final ActionType type, @NotNull final IWorkItem... items) { myType = type; myItems = items; } @Override public void run() { switch (myType) { case ASSIGN: new AssignCommand(myItems).run(); break; case GIVEUP: new GiveUpCommand(myItems).run(); break; case CLOSE: new CloseCommand(myItems).run(); break; } } private static UserSet getProjectsCommitters(@NotNull final IWorkItem... items) { final HashSet projects = new HashSet(); for (IWorkItem item : items) { projects.add(item.getProject().getProjectId()); } final UserSet userSet = new UserSet() { private final HashSet users = new HashSet(); public Set getUsers() { return users; } }; // fill final RemoteBuildServerFacade facade = Activator.getDefault().getSnapshot().getServerFacade(); for (String id : projects) { userSet.getUsers().addAll(facade.getProjectCommitters(id).getUsers()); } return userSet; } private static class AssignCommand implements Runnable { private final IWorkItem[] items; AssignCommand(@NotNull final IWorkItem... items) { this.items = items; } @SuppressWarnings("unchecked") public void run() { final Shell shell = UIUtil.getWorkbenchShell(); final AtomicReference, UserSet>> result = new AtomicReference, UserSet>>(); BusyIndicator.showWhile(shell.getDisplay(), new Runnable() { public void run() { Activator.getDefault().getSnapshot().getProcessManager().performAction(new Runnable() { public void run() { final TeamCitySnapshot4Eclipse snapshot = Activator.getDefault().getSnapshot(); result.set(Trinity.create((User) snapshot.getUser(), getProjectsCommitters(items), snapshot.getServerFacade().getUsers())); } }, "Loading Project's Users credentials", ErrorProcessingKind.RETHROW); //$NON-NLS-1$ } }); final AssignResponsibilityDialog dialog = new AssignResponsibilityDialog(shell, result.get().first, result.get().second, result.get().third); if (dialog.open() == Window.OK) { BusyIndicator.showWhile(shell.getDisplay(), new Runnable() { public void run() { Activator.getDefault().getIDE().addToQueue(new RefreshSummaryCommand(Activator.getDefault().getSnapshot()) { @Override public void execute() throws AuthenticationFailedException, IncompatiblePluginError, RemoteCallException { final User user = dialog.getSelectedUser(); final String comment = dialog.getComment(); final ResponsibilityEntry.RemoveMethod method = dialog.getSelectedRemoveMethod(); for (final IWorkItem item : items) { item.assign(user, comment, method); } super.execute(); } }); } }); } } } private static class GiveUpCommand implements Runnable { private final IWorkItem[] items; GiveUpCommand(IWorkItem... items) { this.items = items; } public void run() { final CommentsDialog dialog = new CommentsDialog(UIUtil.getWorkbenchShell(), Strings.ACTION_GIVE_UP_RESPONSIBILITY_TEXT, "Enter comment:", null) { //$NON-NLS-1$ @Override protected String getHelpContextID() { return HelpConstants.GIVE_UP_INVESTIGATION_DLG; } }; if (dialog.open() == Window.OK) { BusyIndicator.showWhile(UIUtil.getWorkbenchShell().getDisplay(), new Runnable() { public void run() { Activator.getDefault().getIDE().addToQueue(new RefreshSummaryCommand(Activator.getDefault().getSnapshot()) { @Override public void execute() throws AuthenticationFailedException, IncompatiblePluginError, RemoteCallException { for (final IWorkItem item : items) { item.giveup(dialog.getComment()); } super.execute(); } }); } }); } } } private static class CloseCommand implements Runnable { private final IWorkItem[] items; CloseCommand(IWorkItem... items) { this.items = items; } public void run() { final CommentsDialog dialog = new CommentsDialog(UIUtil.getWorkbenchShell(), "Mark As Fixed", "Enter comment:", null) { //$NON-NLS-1$ //$NON-NLS-2$ @Override protected String getHelpContextID() { return HelpConstants.MARK_AS_FIXED_INVESTIGATION_DLG; } }; if (dialog.open() == Window.OK) { BusyIndicator.showWhile(UIUtil.getWorkbenchShell().getDisplay(), new Runnable() { public void run() { Activator.getDefault().getIDE().addToQueue(new RefreshSummaryCommand(Activator.getDefault().getSnapshot()) { @Override public void execute() throws AuthenticationFailedException, IncompatiblePluginError, RemoteCallException { for (final IWorkItem item : items) { item.close(dialog.getComment()); } super.execute(); } }); } }); } } } }