Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28e8ea50436a8f49d725b3b60ac537b56e4023b1 (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
/*******************************************************************************
 * Copyright (c) 2013, 2015 Tasktop Technologies and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 *     Tasktop Technologies - initial API and implementation
 *     Marc-Andre Laperle (Ericsson) - Add topic
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.core.client.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.mylyn.internal.gerrit.core.client.compat.PermissionLabel;

import com.google.gerrit.common.data.ApprovalDetail;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.PatchSetApproval;

/**
 * Data model object for
 * <a href="https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-info">ChangeInfo</a>.
 */
public class ChangeInfo {
	// e.g. "gerritcodereview#change"
	private String kind;

	// e.g. "myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940"
	private String id;

	// e.g. "myProject"
	private String project;

	// e.g. "master"
	private String branch;

	// e.g. "I8473b95934b5732ac55d26311a706c9c2bde9940"
	private String change_id;

	// e.g. "Topic"
	private String topic;

	// e.g. "Implementing Feature X"
	private String subject;

	// e.g. "ABANDONED"
	private Change.Status status;

	// e.g. "2013-02-01 09:59:32.126000000"
	private Timestamp created;

	// e.g. "2013-02-21 11:16:36.775000000",
	private Timestamp updated;

	private boolean reviewed;

	private boolean mergeable;

	private AccountInfo owner;

	private LinkedHashMap<String/*Label*/, LabelInfo> labels;

	private String current_revision;

	private Map<String/*commit ID*/, RevisionInfo> revisions;

	private Map<String/*Label*/, String[]> permitted_labels;

	// e.g. "0023412400000f7d"
	@SuppressWarnings("unused")
	private String _sortkey;

	// e.g. 3965
	private int _number;

	public int getNumber() {
		return _number;
	}

	public String getKind() {
		return kind;
	}

	public String getId() {
		return id;
	}

	public String getProject() {
		return project;
	}

	public String getBranch() {
		return branch;
	}

	public String getTopic() {
		return topic;
	}

	public String getChangeId() {
		return change_id;
	}

	public String getSubject() {
		return subject;
	}

	public Change.Status getStatus() {
		return status;
	}

	public Timestamp getCreated() {
		return created;
	}

	public Timestamp getUpdated() {
		return updated;
	}

	public boolean isReviewed() {
		return reviewed;
	}

	public boolean isMergeable() {
		return mergeable;
	}

	public AccountInfo getOwner() {
		return owner;
	}

	public Map<String, LabelInfo> getLabels() {
		return labels;
	}

	public String getCurrentRevision() {
		return current_revision;
	}

	public Map<String, RevisionInfo> getRevisions() {
		return revisions;
	}

	public Map<String, String[]> getPermittedLabels() {
		return permitted_labels;
	}

	public PatchSet.Id getCurrentPatchSetId() {
		Change.Id changeId = new Change.Id(_number);
		int patchSetId = revisions.get(current_revision).getNumber();
		return new PatchSet.Id(changeId, patchSetId);
	}

	public Set<ApprovalDetail> convertToApprovalDetails() {
		if (labels == null) {
			return Collections.<ApprovalDetail> emptySet();
		}
		Set<ApprovalDetail> result = new LinkedHashSet<ApprovalDetail>();
		for (Entry<String, LabelInfo> entry : labels.entrySet()) {
			List<ApprovalInfo> all = entry.getValue().getAll();
			if (all != null) {
				ApprovalCategory.Id approvalCategoryId = ApprovalUtil.findCategoryIdByNameWithDash(entry.getKey());
				if (approvalCategoryId == null) {
					continue;
				}
				for (ApprovalInfo approvalInfo : all) {
					Account.Id accountId = new Account.Id(approvalInfo.getId());
					ApprovalDetail approvalDetail = new ApprovalDetail(accountId);
					approvalDetail.add(new PatchSetApproval(
							new PatchSetApproval.Key(getCurrentPatchSetId(), accountId, approvalCategoryId),
							approvalInfo.getValue()));
					result.add(approvalDetail);
				}
			}
		}
		return result;
	}

	public Set<ApprovalType> convertToApprovalTypes() {
		if (labels == null) {
			return null;
		}
		Set<ApprovalType> result = new LinkedHashSet<ApprovalType>();
		for (Entry<String, LabelInfo> entry : labels.entrySet()) {
			ApprovalCategory approvalCategory = ApprovalUtil.findCategoryByNameWithDash(entry.getKey());
			if (approvalCategory == null) {
				// it's a custom approval type
				approvalCategory = new ApprovalCategory(new ApprovalCategory.Id(null), entry.getKey());
			}
			List<ApprovalCategoryValue> valueList = new ArrayList<ApprovalCategoryValue>();
			if (entry.getValue() != null && entry.getValue().getValues() != null) {
				// custom approval types may not provide values
				for (Entry<String, String> valueEntry : entry.getValue().getValues().entrySet()) {
					valueList.add(new ApprovalCategoryValue(new ApprovalCategoryValue.Id(approvalCategory.getId(),
							ApprovalUtil.parseShort(valueEntry.getKey())), valueEntry.getValue()));
				}
			}
			ApprovalType approvalType = new ApprovalType(approvalCategory, valueList);
			result.add(approvalType);
		}
		return result;
	}

	public List<PermissionLabel> convertToPermissionLabels() {
		if (permitted_labels == null) {
			return null;
		}
		List<PermissionLabel> result = new ArrayList<PermissionLabel>(permitted_labels.size());
		for (Entry<String, String[]> entry : permitted_labels.entrySet()) {
			List<Short> values = new ArrayList<Short>(entry.getValue().length);
			for (String value : entry.getValue()) {
				values.add(ApprovalUtil.parseShort(value));
			}
			PermissionLabel label = new PermissionLabel();
			label.setName(PermissionLabel.toLabelName(entry.getKey()));
			label.setMin(Collections.min(values).intValue());
			label.setMax(Collections.max(values).intValue());
			result.add(label);
		}
		return result;
	}

	/**
	 * Converts labels into a map of approvals given by the provided user.
	 *
	 * @param id
	 *            id of the current patch set
	 * @param account
	 *            the user whose approvals should be converted
	 * @return map of given approvals
	 * @see #labels
	 */
	public Map<ApprovalCategory.Id, PatchSetApproval> convertToPatchSetApprovals(PatchSet.Id id, Account account) {
		if (labels == null) {
			return null;
		}
		Map<ApprovalCategory.Id, PatchSetApproval> result = new HashMap<ApprovalCategory.Id, PatchSetApproval>(
				labels.size());
		for (Entry<String, LabelInfo> entry : labels.entrySet()) {
			ApprovalCategory approvalCategory = ApprovalUtil.findCategoryByNameWithDash(entry.getKey());
			if (approvalCategory == null) {
				continue;
			}
			if (entry.getValue().getAll() == null) {
				continue;
			}
			for (ApprovalInfo approvalInfo : entry.getValue().getAll()) {
				if (approvalInfo.getId() == account.getId().get()) {
					Account.Id accountId = new Account.Id(approvalInfo.getId());
					PatchSetApproval.Key key = new PatchSetApproval.Key(id, accountId, approvalCategory.getId());
					PatchSetApproval approval = new PatchSetApproval(key, approvalInfo.getValue());
					result.put(approvalCategory.getId(), approval);
				}
			}
		}
		return result;
	}

}

Back to the top