Skip to main content
summaryrefslogtreecommitdiffstats
blob: db1089af972b378512102bcffd13e03ad38fd5a8 (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
/*******************************************************************************
 * Copyright (c) 2011, 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
 *******************************************************************************/

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

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

import org.eclipse.mylyn.internal.gerrit.core.HudsonCommentParser;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.SubmitRecord.Label;
import org.eclipse.mylyn.internal.gerrit.core.client.rest.CommitInfo;
import org.eclipse.mylyn.reviews.internal.core.BuildResult;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.ChangeDetail;
import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.ChangeMessage;

/**
 * Provides additional fields used by Gerrit 2.2.
 * 
 * @author Steffen Pingel
 */
public class ChangeDetailX extends ChangeDetail {

	/**
	 * Available since Gerrit 2.2.
	 */
	private boolean canRevert;

	/**
	 * Available since Gerrit 2.2.
	 */
	private boolean canSubmit;

	/**
	 * Available since Gerrit 2.4.
	 */
	private boolean canRebase;

	/**
	 * Available since Gerrit 2.8.
	 */
	private boolean canCherryPick;

	/**
	 * Available since Gerrit 2.3.
	 */
	private boolean canDeleteDraft;

	private boolean canEdit;

	private Timestamp createdOn;

	private Timestamp lastUpdatedOn;

	protected List<SubmitRecord> submitRecords;

	private Set<ApprovalType> approvalTypes;

	private Map<Integer, CommitInfo[]> parents;

	private LinkedHashMap<String, BuildResult> patchSetBuildStatuses;

	public boolean canRevert() {
		return canRevert;
	}

	public boolean canSubmit() {
		return canSubmit;
	}

	public boolean canRebase() {
		return canRebase;
	}

	public boolean canCherryPick() {
		return canCherryPick;
	}

	public boolean canEdit() {
		return canEdit;
	}

	public boolean canDeleteDraft() {
		return canDeleteDraft;
	}

	public List<SubmitRecord> getSubmitRecords() {
		return submitRecords;
	}

	public void setSubmitRecords(List<SubmitRecord> submitRecords) {
		this.submitRecords = submitRecords;
	}

	public Set<ApprovalType> getApprovalTypes() {
		return approvalTypes;
	}

	public void setApprovalTypes(Set<ApprovalType> approvalTypes) {
		this.approvalTypes = approvalTypes;
	}

	public Iterable<BuildResult> getPatchSetBuildStatuses() {
		return ImmutableSet.copyOf(this.patchSetBuildStatuses.values());
	}

	public void convertSubmitRecordsToApprovalTypes(ApprovalTypes knownApprovalTypes) {
		if (approvalTypes != null) {
			throw new IllegalStateException();
		}
		if (submitRecords == null) {
			return;
		}
		approvalTypes = new LinkedHashSet<ApprovalType>();
		for (SubmitRecord record : submitRecords) {
			for (Label label : record.getLabels()) {
				ApprovalType approvalType = findTypeByLabel(knownApprovalTypes, label.getLabel());
				if (approvalType == null) {
					// it's a custom approval type, that's all we know about it
					ApprovalCategory approvalCategory = new ApprovalCategory(new ApprovalCategory.Id(null),
							label.getLabel());
					approvalType = new ApprovalType(approvalCategory, Collections.<ApprovalCategoryValue> emptyList());
				}
				approvalTypes.add(approvalType);
			}
		}
	}

	@Override
	public List<ChangeMessage> getMessages() {
		HudsonCommentParser commentParser = new HudsonCommentParser();
		List<ChangeMessage> allMessages = super.getMessages();
		List<ChangeMessage> filteredMessages = new ArrayList<ChangeMessage>();
		// Builds up a map of the results
		LinkedHashMap<String, BuildResult> patchSetBuildResults = new LinkedHashMap<String, BuildResult>();
		for (ChangeMessage message : allMessages) {
			ImmutableList<BuildResult> results = commentParser.getBuildResult(message.getMessage());
			if (results.size() == 0) {
				filteredMessages.add(message);
			} else {
				for (BuildResult result : results) {
					patchSetBuildResults.put(result.getBuildUrl(), result);
				}
			}
		}
		this.patchSetBuildStatuses = patchSetBuildResults;
		return filteredMessages;
	}

	private ApprovalType findTypeByLabel(ApprovalTypes knownApprovalTypes, String label) {

		if (knownApprovalTypes == null || knownApprovalTypes.getApprovalTypes() == null) {
			return null;
		}
		for (ApprovalType type : knownApprovalTypes.getApprovalTypes()) {
			if (type.getCategory().getName().equals(label.replace('-', ' '))) {
				return type;
			}
		}
		return null;
	}

	public void setDateCreated(Timestamp ts) {
		createdOn = ts;
	}

	public void setLastModified(Timestamp ts) {
		lastUpdatedOn = ts;
	}

	public Timestamp getDateCreated() {
		return createdOn;
	}

	public Timestamp getLastModified() {
		return lastUpdatedOn;
	}

	public void setCanSubmit(boolean canSubmit) {
		this.canSubmit = canSubmit;
	}

	public void setCanRebase(boolean canRebase) {
		this.canRebase = canRebase;
	}

	public void setCanCherryPick(boolean canCherryPick) {
		this.canCherryPick = canCherryPick;
	}

	public Map<Integer, CommitInfo[]> getParents() {
		return parents;
	}

	public void setParents(Map<Integer, CommitInfo[]> parents) {
		this.parents = parents;
	}

}

Back to the top