/* * 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 jetbrains.buildServer.UserChangeInfo; import jetbrains.buildServer.UserChangeStatus; import jetbrains.buildServer.delayedCommit.RemoteRunSessionData; import jetbrains.teamcity.Activator; import jetbrains.teamcity.Constants; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.core.jobs.DelayedCommitJob; import jetbrains.teamcity.core.util.ChangeStatusHelper; import jetbrains.teamcity.ui.views.ChangesView; import jetbrains.teamcity.ui.views.CommentsDialog; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.window.Window; import org.eclipse.swt.custom.BusyIndicator; import org.eclipse.ui.PlatformUI; public class CommitAction extends Action implements ISelectionChangedListener { static final String COMMENTS_DIALOG_SETTING_ID = Constants.DialogSettings.REMOTE_RUN_DIALOG_SETTING_ID; private UserChangeInfo myChangeInfo; private RemoteRunSessionData mySessionData; private final ChangesView myChangesView; public CommitAction(final ChangesView changesView) { super(Messages.getString("CommitAction.action.name"), SharedImages.getImageDescriptorByPath(SharedImages.CHECKIN_ACTION)); //$NON-NLS-1$ myChangesView = changesView; } @Override public void run() { if (myChangeInfo != null && mySessionData != null) { // warn build status about final UserChangeStatus personalChangeStatus = myChangeInfo .getChangeStatus(); if (UserChangeStatus.FAILED.equals(personalChangeStatus)) { final boolean yes = MessageDialog .openQuestion( null, getText(), Messages .getString("CommitAction.confirmation.commit.broken.build")); //$NON-NLS-1$ if (!yes) { return; } } //ask for commit dialog final CommentsDialog dialog = new CommentsDialog( myChangesView.getSite().getShell(), Messages.getString("CommitAction.manual.commit.dialog.title"), //$NON-NLS-1$ Messages.getString("CommitAction.commit.comments.prompt"), mySessionData.getComment()); //$NON-NLS-1$ if (dialog.open() == Window.OK) { final DelayedCommitJob job = new DelayedCommitJob(mySessionData, dialog.getComment()); job.schedule(); PlatformUI.getWorkbench().getProgressService().showInDialog(null, job); } } } public void selectionChanged(SelectionChangedEvent event) { final StructuredSelection selection = (StructuredSelection) myChangesView.getSite().getSelectionProvider().getSelection(); final Object[] elements = selection.toArray(); if (elements.length == 1 && elements[0] instanceof IAdaptable) { final UserChangeInfo changeInfo = (UserChangeInfo) ((IAdaptable) elements[0]).getAdapter(UserChangeInfo.class); final RemoteRunSessionData sessionData = (RemoteRunSessionData) ((IAdaptable) elements[0]).getAdapter(RemoteRunSessionData.class); if (changeInfo != null && sessionData != null) { myChangeInfo = changeInfo; mySessionData = sessionData; if (isStateAllowed()) { setEnabled(true); return; } } } setEnabled(false); } private IDialogSettings getSettings() { final IDialogSettings dialogSettings = Activator.getDefault() .getDialogSettings(); final IDialogSettings settings = dialogSettings.getSection(COMMENTS_DIALOG_SETTING_ID); if (settings == null) { return dialogSettings.addNewSection(COMMENTS_DIALOG_SETTING_ID); } return settings; } private boolean isStateAllowed() { final boolean[] result = new boolean[] { false }; BusyIndicator.showWhile(myChangesView.getSite().getShell().getDisplay(), new Runnable() { public void run() { result[0] = !ChangeStatusHelper.isCommitSucceed(mySessionData, myChangeInfo); } }); return result[0]; } }