Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 59b21ccaff52e7f89af292e8a2ddcfc73402184b (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************************
 * Copyright (c) 2011 Red Hat 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:
 *     David Green <david.green@tasktop.com> - initial contribution
 *     Christian Trutz <christian.trutz@gmail.com> - initial contribution
 *     Chris Aniszczyk <caniszczyk@gmail.com> - initial contribution
 *******************************************************************************/
package org.eclipse.mylyn.github.internal;

import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskOperation;

/**
 * GitHub issue task data handler
 */
public class GitHubTaskDataHandler extends AbstractTaskDataHandler {

	private static final String DATA_VERSION = "1"; //$NON-NLS-1$
	private static final String MILESTONE_NONE_KEY = "0"; //$NON-NLS-1$
	private GitHubTaskAttributeMapper taskAttributeMapper = null;
	private final GitHubRepositoryConnector connector;

	/**
	 * Create GitHub issue task data handler for connector
	 * 
	 * @param connector
	 */
	public GitHubTaskDataHandler(GitHubRepositoryConnector connector) {
		this.connector = connector;
	}

	@Override
	public TaskAttributeMapper getAttributeMapper(TaskRepository taskRepository) {
		if (this.taskAttributeMapper == null)
			this.taskAttributeMapper = new GitHubTaskAttributeMapper(
					taskRepository);
		return this.taskAttributeMapper;
	}

	public TaskData createTaskData(TaskRepository repository,
			IProgressMonitor monitor, String user, String project, Issue issue) {

		String key = Integer.toString(issue.getNumber());
		TaskData data = new TaskData(getAttributeMapper(repository),
				GitHubRepositoryConnector.KIND, repository.getRepositoryUrl(),
				key);
		data.setVersion(DATA_VERSION);

		createOperations(data, issue);

		createAttribute(data, GitHubTaskAttributes.KEY, key);
		createAttribute(data, GitHubTaskAttributes.TITLE, issue.getTitle());
		createAttribute(data, GitHubTaskAttributes.BODY, issue.getBody());
		createAttribute(data, GitHubTaskAttributes.STATUS, issue.getState());
		createAttribute(data, GitHubTaskAttributes.CREATION_DATE,
				issue.getCreatedAt());
		createAttribute(data, GitHubTaskAttributes.MODIFICATION_DATE,
				issue.getUpdatedAt());
		createAttribute(data, GitHubTaskAttributes.CLOSED_DATE,
				issue.getClosedAt());

		User reporter = issue.getUser();
		createAttribute(data, GitHubTaskAttributes.REPORTER, reporter,
				repository);
		String reporterGravatar = reporter != null ? reporter.getGravatarUrl()
				: null;
		createAttribute(data, GitHubTaskAttributes.REPORTER_GRAVATAR,
				reporterGravatar);

		User assignee = issue.getAssignee();
		createAttribute(data, GitHubTaskAttributes.ASSIGNEE, assignee,
				repository);
		String assigneeGravatar = assignee != null ? assignee.getGravatarUrl()
				: null;
		createAttribute(data, GitHubTaskAttributes.ASSIGNEE_GRAVATAR,
				assigneeGravatar);

		createAttribute(data, GitHubTaskAttributes.COMMENT_NEW);
		createAttribute(data, GitHubTaskAttributes.LABELS, issue.getLabels());

		createMilestone(repository, data, issue);

		return data;
	}

	private void createMilestone(TaskRepository repository, TaskData data,
			Issue issue) {
		Milestone current = issue.getMilestone();
		String number = current != null ? Integer.toString(current.getNumber())
				: MILESTONE_NONE_KEY;
		TaskAttribute milestoneAttribute = createAttribute(data,
				GitHubTaskAttributes.MILESTONE, number);

		if (!this.connector.hasCachedMilestones(repository))
			try {
				this.connector.refreshMilestones(repository);
			} catch (CoreException ignore) {
				// Ignored
			}

		List<Milestone> cachedMilestones = this.connector
				.getMilestones(repository);
		milestoneAttribute.putOption(MILESTONE_NONE_KEY,
				Messages.GitHubTaskDataHandler_MilestoneNone);
		for (Milestone milestone : cachedMilestones)
			milestoneAttribute.putOption(
					Integer.toString(milestone.getNumber()),
					milestone.getTitle());
	}

	private void createOperations(TaskData data, Issue issue) {
		TaskAttribute operationAttribute = data.getRoot().createAttribute(
				TaskAttribute.OPERATION);
		operationAttribute.getMetaData().setType(TaskAttribute.TYPE_OPERATION);

		if (!data.isNew()) {
			String state = issue.getState();
			if (state != null) {
				addOperation(data, issue, GitHubTaskOperation.LEAVE, true);
				if (state.equals(IssueService.STATE_OPEN))
					addOperation(data, issue, GitHubTaskOperation.CLOSE, false);
				else if (state.equals(IssueService.STATE_CLOSED))
					addOperation(data, issue, GitHubTaskOperation.REOPEN, false);
			}
		}
	}

	private void addOperation(TaskData data, Issue issue,
			GitHubTaskOperation operation, boolean asDefault) {
		TaskAttribute attribute = data.getRoot().createAttribute(
				TaskAttribute.PREFIX_OPERATION + operation.getId());
		String label = createOperationLabel(issue, operation);
		TaskOperation.applyTo(attribute, operation.getId(), label);

		if (asDefault) {
			TaskAttribute operationAttribute = data.getRoot().getAttribute(
					TaskAttribute.OPERATION);
			TaskOperation.applyTo(operationAttribute, operation.getId(), label);
		}
	}

	private String createOperationLabel(Issue issue,
			GitHubTaskOperation operation) {
		return operation == GitHubTaskOperation.LEAVE ? operation.getLabel()
				+ issue.getState() : operation.getLabel();
	}

	public TaskData createTaskData(TaskRepository repository,
			IProgressMonitor monitor, String user, String project, Issue issue,
			List<Comment> comments) {
		TaskData taskData = createTaskData(repository, monitor, user, project,
				issue);
		taskData.setPartial(false);

		if (comments != null && !comments.isEmpty()) {
			int count = 1;
			TaskAttribute root = taskData.getRoot();
			for (Comment comment : comments) {
				TaskCommentMapper commentMapper = new TaskCommentMapper();
				User author = comment.getUser();
				IRepositoryPerson authorPerson = repository.createPerson(author
						.getLogin());
				authorPerson.setName(author.getName());
				commentMapper.setAuthor(authorPerson);
				commentMapper.setCreationDate(comment.getCreatedAt());
				commentMapper.setText(comment.getBody());
				commentMapper.setCommentId(comment.getUrl());
				commentMapper.setNumber(count);

				TaskAttribute attribute = root
						.createAttribute(TaskAttribute.PREFIX_COMMENT + count);
				commentMapper.applyTo(attribute);
				count++;
			}
		}

		return taskData;
	}

	private Issue createIssue(TaskData taskData) {
		Issue issue = new Issue();
		if (!taskData.isNew()) {
			issue.setNumber(Integer.parseInt(taskData.getTaskId()));
		}
		issue.setBody(getAttributeValue(taskData, GitHubTaskAttributes.BODY));
		issue.setTitle(getAttributeValue(taskData, GitHubTaskAttributes.TITLE));

		String assigneeValue = getAttributeValue(taskData,
				GitHubTaskAttributes.ASSIGNEE);
		if (assigneeValue != null) {
			User assignee = new User().setName(assigneeValue);
			issue.setAssignee(assignee);
		}

		String milestoneValue = getAttributeValue(taskData,
				GitHubTaskAttributes.MILESTONE);
		if (milestoneValue != null) {
			Milestone milestone = new Milestone().setNumber(Integer
					.parseInt(milestoneValue));
			issue.setMilestone(milestone);
		}
		return issue;
	}

	private String getAttributeValue(TaskData taskData,
			GitHubTaskAttributes attr) {
		TaskAttribute attribute = taskData.getRoot().getAttribute(attr.getId());
		return attribute == null ? null : attribute.getValue();
	}

	private TaskAttribute createAttribute(TaskData data,
			GitHubTaskAttributes attribute) {
		TaskAttribute attr = data.getRoot().createAttribute(attribute.getId());
		TaskAttributeMetaData metaData = attr.getMetaData();
		metaData.defaults().setType(attribute.getType())
				.setKind(attribute.getKind()).setLabel(attribute.getLabel())
				.setReadOnly(attribute.isReadOnly());
		return attr;
	}

	private TaskAttribute createAttribute(TaskData data,
			GitHubTaskAttributes attribute, String value) {
		TaskAttribute attr = createAttribute(data, attribute);
		if (value != null) {
			data.getAttributeMapper().setValue(attr, value);
		}
		return attr;
	}

	private TaskAttribute createAttribute(TaskData data,
			GitHubTaskAttributes attribute, Date value) {
		TaskAttribute attr = createAttribute(data, attribute);
		if (value != null) {
			data.getAttributeMapper().setDateValue(attr, value);
		}
		return attr;
	}

	private void createAttribute(TaskData data, GitHubTaskAttributes attribute,
			User value, TaskRepository repository) {
		TaskAttribute attr = createAttribute(data, attribute);
		if (value != null) {
			IRepositoryPerson person = repository
					.createPerson(value.getLogin());
			person.setName(value.getName());
			data.getAttributeMapper().setRepositoryPerson(attr, person);
		}
	}

	private void createAttribute(TaskData data, GitHubTaskAttributes attribute,
			List<Label> values) {
		TaskAttribute attr = createAttribute(data, attribute);
		if (values != null) {
			List<String> labels = new LinkedList<String>();
			for (Label label : values) {
				labels.add(label.getName());
			}
			data.getAttributeMapper().setValues(attr, labels);
		}
	}

	@Override
	public boolean initializeTaskData(TaskRepository repository, TaskData data,
			ITaskMapping initializationData, IProgressMonitor monitor)
			throws CoreException {

		data.setVersion(DATA_VERSION);

		for (GitHubTaskAttributes attr : GitHubTaskAttributes.values()) {
			if (attr.isInitTask()) {
				createAttribute(data, attr, (String) null);
			}
		}

		return true;
	}

	@Override
	public RepositoryResponse postTaskData(TaskRepository repository,
			TaskData taskData, Set<TaskAttribute> oldAttributes,
			IProgressMonitor monitor) throws CoreException {
		String taskId = taskData.getTaskId();
		Issue issue = createIssue(taskData);
		String user = GitHub.computeTaskRepositoryUser(repository.getUrl());
		String repo = GitHub.computeTaskRepositoryProject(repository.getUrl());
		try {

			GitHubClient client = new GitHubClient();
			GitHubCredentials credentials = GitHubCredentials
					.create(repository);
			client.setCredentials(credentials.getUsername(),
					credentials.getPassword());
			IssueService service = new IssueService(client);
			if (taskData.isNew()) {
				issue.setState(IssueService.STATE_OPEN);
				issue = service.createIssue(user, repo, issue);
				taskId = Integer.toString(issue.getNumber());
			} else {

				// Handle new comment
				String comment = getAttributeValue(taskData,
						GitHubTaskAttributes.COMMENT_NEW);
				if (comment != null && comment.length() > 0)
					service.createComment(user, repo, taskData.getTaskId(),
							comment);

				// Handle state change
				TaskAttribute operationAttribute = taskData.getRoot()
						.getAttribute(TaskAttribute.OPERATION);
				if (operationAttribute != null) {
					GitHubTaskOperation operation = GitHubTaskOperation
							.fromId(operationAttribute.getValue());
					if (operation != GitHubTaskOperation.LEAVE)
						switch (operation) {
						case REOPEN:
							issue.setState(IssueService.STATE_OPEN);
							break;
						case CLOSE:
							issue.setState(IssueService.STATE_CLOSED);
							break;
						default:
							break;
						}
				}

				service.editIssue(user, repo, issue);
			}
			return new RepositoryResponse(
					taskData.isNew() ? ResponseKind.TASK_CREATED
							: ResponseKind.TASK_UPDATED, taskId);
		} catch (IOException e) {
			throw new CoreException(GitHub.createErrorStatus(e));
		}

	}

}

Back to the top