Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9966598506b097356832d8ccaff207e8d4041d50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*******************************************************************************
 * Copyright (c) 2009 Atlassian and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Atlassian - initial API and implementation
 ******************************************************************************/

package com.atlassian.connector.eclipse.internal.crucible.ui.wizards;

import com.atlassian.connector.eclipse.internal.crucible.core.TaskRepositoryUtil;
import com.atlassian.connector.eclipse.internal.crucible.ui.CrucibleUiPlugin;
import com.atlassian.connector.eclipse.internal.crucible.ui.CrucibleUiUtil;
import com.atlassian.connector.eclipse.team.ui.AtlassianTeamUiPlugin;
import com.atlassian.connector.eclipse.team.ui.ICustomChangesetLogEntry;
import com.atlassian.connector.eclipse.team.ui.ITeamUiResourceConnector;
import com.atlassian.connector.eclipse.team.ui.LocalStatus;
import com.atlassian.connector.eclipse.team.ui.TeamUiUtils;
import com.atlassian.connector.eclipse.ui.commons.DecoratedResource;
import com.atlassian.connector.eclipse.ui.commons.ResourceEditorBean;
import com.atlassian.theplugin.commons.crucible.api.UploadItem;
import com.atlassian.theplugin.commons.crucible.api.model.BasicProject;
import com.atlassian.theplugin.commons.crucible.api.model.PermId;
import com.atlassian.theplugin.commons.crucible.api.model.Review;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.mylyn.internal.tasks.core.LocalTask;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.mylyn.tasks.ui.wizards.NewTaskWizard;
import org.eclipse.ui.INewWizard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;

/**
 * Wizard for creating a new review
 * 
 * @author Thomas Ehrnhoefer
 * @author Pawel Niewiadomski
 * @author Jacek Jaroczynski
 */
@SuppressWarnings("restriction")
public class ReviewWizard extends NewTaskWizard implements INewWizard {

	public enum Type {
		ADD_CHANGESET, ADD_PATCH, ADD_WORKSPACE_PATCH, ADD_SCM_RESOURCES, ADD_UPLOAD_ITEMS, ADD_RESOURCES, ADD_COMMENT_TO_FILE;
	}

	private CrucibleReviewDetailsPage detailsPage;

	private Review crucibleReview;

	private SelectScmChangesetsPage addChangeSetsPage;

	private CrucibleAddPatchPage addPatchPage;

//	private WorkspacePatchSelectionPage addWorkspacePatchPage;

	private DefineRepositoryMappingsPage defineMappingPage;

	private ResourceSelectionPage resourceSelectionPage;

	private final Set<Type> types;

	private SortedSet<ICustomChangesetLogEntry> preselectedLogEntries;

	private String previousPatch;

	private String previousPatchRepository;

	private final List<IResource> selectedWorkspaceResources = new ArrayList<IResource>();

	private IResource[] previousWorkspaceSelection;

	private List<UploadItem> uploadItems;

	private List<ResourceEditorBean> versionedCommentsToAdd = new ArrayList<ResourceEditorBean>();

	private SelectChangesetsFromCruciblePage addChangeSetsFromCruciblePage;

	private ITeamUiResourceConnector selectedWorkspaceTeamConnector;

	public ReviewWizard(TaskRepository taskRepository, Set<Type> types) {
		super(taskRepository, null);
		setWindowTitle("New Crucible Review");
		setNeedsProgressMonitor(true);
		this.types = types;
		this.selectedWorkspaceResources.addAll(Arrays.asList((IResource[]) ResourcesPlugin.getWorkspace()
				.getRoot()
				.getProjects()));
	}

	public ReviewWizard(Review review, Set<Type> types) {
		this(CrucibleUiUtil.getCrucibleTaskRepository(review), types);
		this.crucibleReview = review;
	}

	public ReviewWizard(Review review, Type type) {
		this(review, new HashSet<Type>(Arrays.asList(type)));
	}

	@Override
	public void addPages() {
		if (types.contains(Type.ADD_CHANGESET)) {
			addChangeSetsFromCruciblePage = new SelectChangesetsFromCruciblePage(getTaskRepository(),
					preselectedLogEntries);
			addPage(addChangeSetsFromCruciblePage);
		}

		if (types.contains(Type.ADD_PATCH)) {
			addPatchPage = new CrucibleAddPatchPage(getTaskRepository());
			addPage(addPatchPage);
		}

		// pre-commit
		if (types.contains(Type.ADD_WORKSPACE_PATCH)) {
//			addWorkspacePatchPage = new WorkspacePatchSelectionPage(getTaskRepository(), selectedWorkspaceResources);
//			addPage(addWorkspacePatchPage);
		}

		// post-commit for editor selection
		if (types.contains(Type.ADD_SCM_RESOURCES)) {

			if (selectedWorkspaceResources.size() > 0 && selectedWorkspaceResources.get(0) != null) {

				// single SCM integration selection supported
				final ITeamUiResourceConnector teamConnector = AtlassianTeamUiPlugin.getDefault()
						.getTeamResourceManager()
						.getTeamConnector(selectedWorkspaceResources.get(0));
				if (teamConnector == null) {
					MessageDialog.openInformation(getShell(), CrucibleUiPlugin.PRODUCT_NAME,
							"Cannot find Atlassian SCM Integration for '" + selectedWorkspaceResources.get(0).getName()
									+ "'.");
				} else {
					boolean missingMapping = false;
					Collection<String> scmPaths = new ArrayList<String>();
					// TODO use job below if there are plenty of resource (currently it is used for single resource)
					for (IResource resource : selectedWorkspaceResources) {
						try {
							LocalStatus status = teamConnector.getLocalRevision(resource);
							if (status.getScmPath() != null && status.getScmPath().length() > 0) {
								String scmPath = TeamUiUtils.getScmPath(resource, teamConnector);

								if (TaskRepositoryUtil.getMatchingSourceRepository(
										TaskRepositoryUtil.getScmRepositoryMappings(getTaskRepository()), scmPath) == null) {
									// we need to see mapping page
									missingMapping = true;
									scmPaths.add(scmPath);
								}

							}
						} catch (CoreException e) {
							// resource is probably not under version control
							// skip
						}
					}

					if (missingMapping) {
						defineMappingPage = new DefineRepositoryMappingsPage(scmPaths, getTaskRepository());
						addPage(defineMappingPage);
					}
				}
			}
		}

		// mixed review
		if (types.contains(Type.ADD_RESOURCES)) {
			resourceSelectionPage = new ResourceSelectionPage(getTaskRepository(), selectedWorkspaceTeamConnector,
					selectedWorkspaceResources);
			addPage(resourceSelectionPage);
		}

		// only add details page if review is not already existing
		if (crucibleReview == null) {
			detailsPage = new CrucibleReviewDetailsPage(getTaskRepository(), types.contains(Type.ADD_COMMENT_TO_FILE));
			addPage(detailsPage);
		}
	}

	@Override
	public boolean canFinish() {
		if (detailsPage != null) {
			return detailsPage.isPageComplete();
		}
		return super.canFinish();
	}

	@Override
	public boolean performFinish() {

		setErrorMessage(null);

		crucibleReview = detailsPage.getReview();
		LocalTask task = TasksUiInternal.createNewLocalTask("Review: " + crucibleReview.getSummary());
		crucibleReview.setPermId(new PermId(task.getTaskId()));

		if (detailsPage != null) {
			// save project selection
			final BasicProject selectedProject = detailsPage.getSelectedProject();
			CrucibleUiPlugin.getDefault().updateLastSelectedProject(getTaskRepository(),
					selectedProject != null ? selectedProject.getKey() : null);

			// save checkbox selections
			CrucibleUiPlugin.getDefault().updateAllowAnyoneOption(getTaskRepository(),
					detailsPage.isAllowAnyoneToJoin());
			CrucibleUiPlugin.getDefault().updateStartReviewOption(getTaskRepository(),
					detailsPage.isStartReviewImmediately());
		}

		if (addPatchPage != null) {
			String patchToAdd = addPatchPage.hasPatch() ? addPatchPage.getPatch() : null;
			String patchRepositoryToAdd = addPatchPage.hasPatch() ? addPatchPage.getPatchRepository() : null;

			if (patchToAdd != null && patchRepositoryToAdd != null && !patchToAdd.equals(previousPatch)
					&& !patchRepositoryToAdd.equals(previousPatchRepository)) {
				// create patch review
			}
		}

//		if (addWorkspacePatchPage != null) {
//			final IResource[] selection = addWorkspacePatchPage.getSelection();
//
//			if (selection != null && selection.length > 0 && !Arrays.equals(selection, previousWorkspaceSelection)
//					&& addWorkspacePatchPage.getSelectedTeamResourceConnector() != null) {
//				// create pre-commit review
//			}
//		}

		if (addChangeSetsPage != null || addChangeSetsFromCruciblePage != null) {
			final Map<String, Set<String>> changesetsToAdd = addChangeSetsPage != null ? addChangeSetsPage.getSelectedChangesets()
					: addChangeSetsFromCruciblePage.getSelectedChangesets();
			if (changesetsToAdd != null && changesetsToAdd.size() > 0) {
				// create review from changeset
			}
		}

		if (types.contains(Type.ADD_SCM_RESOURCES)) {
			if (selectedWorkspaceResources != null) {
				// create review from editor selection (post-commit)
			}
		}

		if (types.contains(Type.ADD_UPLOAD_ITEMS)) {
			if (uploadItems.size() > 0) {
				// create review from editor selection (pre-commit)
			}
		}

		if (resourceSelectionPage != null && types.contains(Type.ADD_RESOURCES)) {
			final List<DecoratedResource> resources = resourceSelectionPage.getSelection();
			if (resources != null && resources.size() > 0) {
				// create review from workbench selection (post- and pre-commit)
			}
		}

		TasksUiUtil.openTask(task);
		TasksUi.getTaskActivityManager().activateTask(task);
		CrucibleUiPlugin.getDefault()
				.getActiveReviewManager()
				.reviewAdded(task.getRepositoryUrl(), task.getTaskId(), crucibleReview);

		return true;
	}

	private void setErrorMessage(String message) {
		IWizardPage page = getContainer().getCurrentPage();
		if (page instanceof WizardPage) {
			((WizardPage) page).setErrorMessage(message != null ? message.replace("\n", " ") : null);
		}
	}

	public void setLogEntries(SortedSet<ICustomChangesetLogEntry> logEntries) {
		this.preselectedLogEntries = logEntries;
	}

	public void setRoots(ITeamUiResourceConnector teamConnector, List<IResource> list) {
		this.selectedWorkspaceResources.clear();
		this.selectedWorkspaceResources.addAll(list);
		this.selectedWorkspaceTeamConnector = teamConnector;
	}

	public void setUploadItems(List<UploadItem> uploadItems) {
		this.uploadItems = uploadItems;
	}

	public void setFilesCommentData(List<ResourceEditorBean> list) {
		this.versionedCommentsToAdd = list;
	}

}

Back to the top