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
|
/*
* Copyright 2013 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.git;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
import com.gitblit.Constants;
import com.gitblit.models.TicketModel;
import com.gitblit.models.TicketModel.Change;
import com.gitblit.models.TicketModel.Field;
import com.gitblit.models.TicketModel.Patchset;
import com.gitblit.models.TicketModel.PatchsetType;
import com.gitblit.models.TicketModel.Status;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.StringUtils;
/**
*
* A subclass of ReceiveCommand which constructs a ticket change based on a
* patchset and data derived from the push ref.
*
* @author James Moger
*
*/
public class PatchsetCommand extends ReceiveCommand {
public static final String TOPIC = "t=";
public static final String RESPONSIBLE = "r=";
public static final String WATCH = "cc=";
public static final String MILESTONE = "m=";
protected final Change change;
protected boolean isNew;
protected long ticketId;
public static String getBasePatchsetBranch(long ticketNumber) {
StringBuilder sb = new StringBuilder();
sb.append(Constants.R_TICKETS_PATCHSETS);
long m = ticketNumber % 100L;
if (m < 10) {
sb.append('0');
}
sb.append(m);
sb.append('/');
sb.append(ticketNumber);
sb.append('/');
return sb.toString();
}
public static String getTicketBranch(long ticketNumber) {
return Constants.R_TICKET + ticketNumber;
}
public static String getReviewBranch(long ticketNumber) {
return "ticket-" + ticketNumber;
}
public static String getPatchsetBranch(long ticketId, long patchset) {
return getBasePatchsetBranch(ticketId) + patchset;
}
public static long getTicketNumber(String ref) {
if (ref.startsWith(Constants.R_TICKETS_PATCHSETS)) {
// patchset revision
// strip changes ref
String p = ref.substring(Constants.R_TICKETS_PATCHSETS.length());
// strip shard id
p = p.substring(p.indexOf('/') + 1);
// strip revision
p = p.substring(0, p.indexOf('/'));
// parse ticket number
return Long.parseLong(p);
} else if (ref.startsWith(Constants.R_TICKET)) {
String p = ref.substring(Constants.R_TICKET.length());
// parse ticket number
return Long.parseLong(p);
}
return 0L;
}
public PatchsetCommand(String username, Patchset patchset) {
super(patchset.isFF() ? ObjectId.fromString(patchset.parent) : ObjectId.zeroId(),
ObjectId.fromString(patchset.tip), null);
this.change = new Change(username);
this.change.patchset = patchset;
}
public PatchsetType getPatchsetType() {
return change.patchset.type;
}
public boolean isNewTicket() {
return isNew;
}
public long getTicketId() {
return ticketId;
}
public Change getChange() {
return change;
}
/**
* Creates a "new ticket" change for the proposal.
*
* @param commit
* @param mergeTo
* @param ticketId
* @parem pushRef
*/
public void newTicket(RevCommit commit, String mergeTo, long ticketId, String pushRef) {
this.ticketId = ticketId;
isNew = true;
change.setField(Field.title, getTitle(commit));
change.setField(Field.body, getBody(commit));
change.setField(Field.status, Status.New);
change.setField(Field.mergeTo, mergeTo);
change.setField(Field.type, TicketModel.Type.Proposal);
Set<String> watchSet = new TreeSet<String>();
watchSet.add(change.author);
// identify parameters passed in the push ref
if (!StringUtils.isEmpty(pushRef)) {
List<String> watchers = getOptions(pushRef, WATCH);
if (!ArrayUtils.isEmpty(watchers)) {
for (String cc : watchers) {
watchSet.add(cc.toLowerCase());
}
}
String milestone = getSingleOption(pushRef, MILESTONE);
if (!StringUtils.isEmpty(milestone)) {
// user provided milestone
change.setField(Field.milestone, milestone);
}
String responsible = getSingleOption(pushRef, RESPONSIBLE);
if (!StringUtils.isEmpty(responsible)) {
// user provided responsible
change.setField(Field.responsible, responsible);
watchSet.add(responsible);
}
String topic = getSingleOption(pushRef, TOPIC);
if (!StringUtils.isEmpty(topic)) {
// user provided topic
change.setField(Field.topic, topic);
}
}
// set the watchers
change.watch(watchSet.toArray(new String[watchSet.size()]));
}
/**
*
* @param commit
* @param mergeTo
* @param ticket
* @param pushRef
*/
public void updateTicket(RevCommit commit, String mergeTo, TicketModel ticket, String pushRef) {
this.ticketId = ticket.number;
if (ticket.isClosed()) {
// re-opening a closed ticket
change.setField(Field.status, Status.Open);
}
// ticket may or may not already have an integration branch
if (StringUtils.isEmpty(ticket.mergeTo) || !ticket.mergeTo.equals(mergeTo)) {
change.setField(Field.mergeTo, mergeTo);
}
if (ticket.isProposal() && change.patchset.commits == 1 && change.patchset.type.isRewrite()) {
// Gerrit-style title and description updates from the commit
// message
String title = getTitle(commit);
String body = getBody(commit);
if (!ticket.title.equals(title)) {
// title changed
change.setField(Field.title, title);
}
if (!ticket.body.equals(body)) {
// description changed
change.setField(Field.body, body);
}
}
Set<String> watchSet = new TreeSet<String>();
watchSet.add(change.author);
// update the patchset command metadata
if (!StringUtils.isEmpty(pushRef)) {
List<String> watchers = getOptions(pushRef, WATCH);
if (!ArrayUtils.isEmpty(watchers)) {
for (String cc : watchers) {
watchSet.add(cc.toLowerCase());
}
}
String milestone = getSingleOption(pushRef, MILESTONE);
if (!StringUtils.isEmpty(milestone) && !milestone.equals(ticket.milestone)) {
// user specified a (different) milestone
change.setField(Field.milestone, milestone);
}
String responsible = getSingleOption(pushRef, RESPONSIBLE);
if (!StringUtils.isEmpty(responsible) && !responsible.equals(ticket.responsible)) {
// user specified a (different) responsible
change.setField(Field.responsible, responsible);
watchSet.add(responsible);
}
String topic = getSingleOption(pushRef, TOPIC);
if (!StringUtils.isEmpty(topic) && !topic.equals(ticket.topic)) {
// user specified a (different) topic
change.setField(Field.topic, topic);
}
}
// update the watchers
watchSet.removeAll(ticket.getWatchers());
if (!watchSet.isEmpty()) {
change.watch(watchSet.toArray(new String[watchSet.size()]));
}
}
@Override
public String getRefName() {
return getPatchsetBranch();
}
public String getPatchsetBranch() {
return getBasePatchsetBranch(ticketId) + change.patchset.number;
}
public String getTicketBranch() {
return getTicketBranch(ticketId);
}
private String getTitle(RevCommit commit) {
String title = commit.getShortMessage();
return title;
}
/**
* Returns the body of the commit message
*
* @return
*/
private String getBody(RevCommit commit) {
String body = commit.getFullMessage().substring(commit.getShortMessage().length()).trim();
return body;
}
/** Extracts a ticket field from the ref name */
private static List<String> getOptions(String refName, String token) {
if (refName.indexOf('%') > -1) {
List<String> list = new ArrayList<String>();
String [] strings = refName.substring(refName.indexOf('%') + 1).split(",");
for (String str : strings) {
if (str.toLowerCase().startsWith(token)) {
String val = str.substring(token.length());
list.add(val);
}
}
return list;
}
return null;
}
/** Extracts a ticket field from the ref name */
private static String getSingleOption(String refName, String token) {
List<String> list = getOptions(refName, token);
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
/** Extracts a ticket field from the ref name */
public static String getSingleOption(ReceiveCommand cmd, String token) {
return getSingleOption(cmd.getRefName(), token);
}
/** Extracts a ticket field from the ref name */
public static List<String> getOptions(ReceiveCommand cmd, String token) {
return getOptions(cmd.getRefName(), token);
}
}
|