Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d6807468538e7271afebdb2d9bf2335c603d1ac (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
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.mylyn.internal.github.core.pr;

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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.PullRequest;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GsonUtils;
import org.eclipse.egit.github.core.service.IssueService;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.GitHubTaskDataHandler;
import org.eclipse.mylyn.internal.github.core.issue.IssueConnector;
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.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;

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

	private static final String DATA_VERSION = "1.1"; //$NON-NLS-1$

	/**
	 * Create GitHub issue task data handler for connector
	 *
	 * @param connector
	 */
	public PullRequestTaskDataHandler(PullRequestConnector connector) {
	}

	/**
	 * Create task data for pull request
	 *
	 * @param repository
	 * @param monitor
	 * @param repo
	 * @param prComp
	 * @return task data
	 */
	public TaskData createTaskData(TaskRepository repository,
			IProgressMonitor monitor, IRepositoryIdProvider repo,
			PullRequestComposite prComp) {
		PullRequest pr = prComp.getRequest();
		String key = Integer.toString(pr.getNumber());
		TaskData data = new TaskData(getAttributeMapper(repository),
				PullRequestConnector.KIND, repository.getRepositoryUrl(), key);
		data.setVersion(DATA_VERSION);

		createOperations(data, pr);

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

		User reporter = pr.getUser();
		createAttribute(data, PullRequestAttribute.REPORTER.getMetadata(),
				reporter, repository);
		String reporterGravatar = reporter != null ? reporter.getAvatarUrl()
				: null;
		createAttribute(data,
				PullRequestAttribute.REPORTER_GRAVATAR.getMetadata(),
				reporterGravatar);

		createAttribute(data, PullRequestAttribute.COMMENT_NEW.getMetadata());

		createAttribute(data, PullRequestAttribute.MODEL.getMetadata(),
				GsonUtils.toJson(prComp));

		return data;
	}

	private void createOperations(TaskData data, PullRequest pr) {
		createOperationAttribute(data);

		if (data.isNew())
			return;

		// Merged pull requests cannot be reopened
		if (pr.isMerged())
			return;

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

	private void addOperation(TaskData data, PullRequest pr,
			PullRequestOperation operation, boolean isDefault) {
		String id = operation.getId();
		String label = createOperationLabel(pr, operation);
		addOperation(data, id, label, isDefault);
	}

	private String createOperationLabel(PullRequest pr,
			PullRequestOperation operation) {
		return operation == PullRequestOperation.LEAVE ? operation.getLabel()
				+ pr.getState() : operation.getLabel();
	}

	/**
	 * Create task data for pull request
	 *
	 * @param repository
	 * @param monitor
	 * @param repo
	 * @param pr
	 * @param comments
	 * @return task data
	 */
	public TaskData createTaskData(TaskRepository repository,
			IProgressMonitor monitor, IRepositoryIdProvider repo,
			PullRequestComposite pr, List<Comment> comments) {
		TaskData taskData = createTaskData(repository, monitor, repo, pr);
		taskData.setPartial(false);

		addComments(taskData.getRoot(), comments, repository);

		return taskData;
	}

	private PullRequest createPullRequest(TaskData taskData) {
		PullRequest pr = new PullRequest();
		if (!taskData.isNew())
			pr.setNumber(Integer.parseInt(taskData.getTaskId()));

		pr.setBody(getAttributeValue(taskData,
				PullRequestAttribute.BODY.getMetadata()));
		pr.setTitle(getAttributeValue(taskData,
				PullRequestAttribute.TITLE.getMetadata()));

		return pr;
	}

	@Override
	public boolean initializeTaskData(TaskRepository repository, TaskData data,
			ITaskMapping initializationData, IProgressMonitor monitor)
			throws CoreException {
		data.setVersion(DATA_VERSION);
		for (PullRequestAttribute attr : PullRequestAttribute.values())
			if (attr.getMetadata().isInitTask())
				createAttribute(data, attr.getMetadata(), (String) null);
		return true;
	}

	@Override
	public RepositoryResponse postTaskData(TaskRepository repository,
			TaskData taskData, Set<TaskAttribute> oldAttributes,
			IProgressMonitor monitor) throws CoreException {
		String taskId = taskData.getTaskId();
		PullRequest pr = createPullRequest(taskData);
		RepositoryId repo = PullRequestConnector.getRepository(repository
				.getRepositoryUrl());
		try {
			GitHubClient client = IssueConnector.createClient(repository);
			boolean collaborator = isCollaborator(client, repo);
			PullRequestService prService = new PullRequestService(client);
			IssueService issueService = new IssueService(client);
			if (taskData.isNew()) {
				pr.setState(IssueService.STATE_OPEN);
				pr = prService.createPullRequest(repo, pr);
				taskId = Integer.toString(pr.getNumber());
			} else {
				// Handle new comment
				String comment = getAttributeValue(taskData,
						PullRequestAttribute.COMMENT_NEW.getMetadata());
				if (comment != null && comment.length() > 0)
					issueService.createComment(repo.getOwner(), repo.getName(),
							taskId, comment);

				boolean reporter = attributeMatchesUser(client,
						PullRequestAttribute.REPORTER.getMetadata(), taskData);
				if (collaborator || reporter) {
					// Handle state change
					TaskAttribute operationAttribute = taskData.getRoot()
							.getAttribute(TaskAttribute.OPERATION);
					if (operationAttribute != null) {
						PullRequestOperation operation = PullRequestOperation
								.fromId(operationAttribute.getValue());
						if (operation == PullRequestOperation.REOPEN)
							pr.setState(IssueService.STATE_OPEN);
						else if (operation == PullRequestOperation.CLOSE)
							pr.setState(IssueService.STATE_CLOSED);
					}
					prService.editPullRequest(repo, pr);
				}
			}
			return new RepositoryResponse(
					taskData.isNew() ? ResponseKind.TASK_CREATED
							: ResponseKind.TASK_UPDATED, taskId);
		} catch (IOException e) {
			throw new CoreException(GitHub.createWrappedStatus(e));
		}
	}
}

Back to the top