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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
|
/*
* Copyright 2014 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.models;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jgit.util.RelativeDateFormatter;
/**
* The Gitblit Ticket model, its component classes, and enums.
*
* @author James Moger
*
*/
public class TicketModel implements Serializable, Comparable<TicketModel> {
private static final long serialVersionUID = 1L;
public String project;
public String repository;
public long number;
public Date created;
public String createdBy;
public Date updated;
public String updatedBy;
public String title;
public String body;
public String topic;
public Type type;
public Status status;
public String responsible;
public String milestone;
public String mergeSha;
public String mergeTo;
public List<Change> changes;
public Integer insertions;
public Integer deletions;
/**
* Builds an effective ticket from the collection of changes. A change may
* Add or Subtract information from a ticket, but the collection of changes
* is only additive.
*
* @param changes
* @return the effective ticket
*/
public static TicketModel buildTicket(Collection<Change> changes) {
TicketModel ticket;
List<Change> effectiveChanges = new ArrayList<Change>();
Map<String, Change> comments = new HashMap<String, Change>();
for (Change change : changes) {
if (change.comment != null) {
if (comments.containsKey(change.comment.id)) {
Change original = comments.get(change.comment.id);
Change clone = copy(original);
clone.comment.text = change.comment.text;
clone.comment.deleted = change.comment.deleted;
int idx = effectiveChanges.indexOf(original);
effectiveChanges.remove(original);
effectiveChanges.add(idx, clone);
comments.put(clone.comment.id, clone);
} else {
effectiveChanges.add(change);
comments.put(change.comment.id, change);
}
} else {
effectiveChanges.add(change);
}
}
// effective ticket
ticket = new TicketModel();
for (Change change : effectiveChanges) {
if (!change.hasComment()) {
// ensure we do not include a deleted comment
change.comment = null;
}
ticket.applyChange(change);
}
return ticket;
}
public TicketModel() {
// the first applied change set the date appropriately
created = new Date(0);
changes = new ArrayList<Change>();
status = Status.New;
type = Type.defaultType;
}
public boolean isOpen() {
return !status.isClosed();
}
public boolean isClosed() {
return status.isClosed();
}
public boolean isMerged() {
return isClosed() && !isEmpty(mergeSha);
}
public boolean isProposal() {
return Type.Proposal == type;
}
public boolean isBug() {
return Type.Bug == type;
}
public Date getLastUpdated() {
return updated == null ? created : updated;
}
public boolean hasPatchsets() {
return getPatchsets().size() > 0;
}
/**
* Returns true if multiple participants are involved in discussing a ticket.
* The ticket creator is excluded from this determination because a
* discussion requires more than one participant.
*
* @return true if this ticket has a discussion
*/
public boolean hasDiscussion() {
for (Change change : getComments()) {
if (!change.author.equals(createdBy)) {
return true;
}
}
return false;
}
/**
* Returns the list of changes with comments.
*
* @return
*/
public List<Change> getComments() {
List<Change> list = new ArrayList<Change>();
for (Change change : changes) {
if (change.hasComment()) {
list.add(change);
}
}
return list;
}
/**
* Returns the list of participants for the ticket.
*
* @return the list of participants
*/
public List<String> getParticipants() {
Set<String> set = new LinkedHashSet<String>();
for (Change change : changes) {
if (change.isParticipantChange()) {
set.add(change.author);
}
}
if (responsible != null && responsible.length() > 0) {
set.add(responsible);
}
return new ArrayList<String>(set);
}
public boolean hasLabel(String label) {
return getLabels().contains(label);
}
public List<String> getLabels() {
return getList(Field.labels);
}
public boolean isResponsible(String username) {
return username.equals(responsible);
}
public boolean isAuthor(String username) {
return username.equals(createdBy);
}
public boolean isReviewer(String username) {
return getReviewers().contains(username);
}
public List<String> getReviewers() {
return getList(Field.reviewers);
}
public boolean isWatching(String username) {
return getWatchers().contains(username);
}
public List<String> getWatchers() {
return getList(Field.watchers);
}
public boolean isVoter(String username) {
return getVoters().contains(username);
}
public List<String> getVoters() {
return getList(Field.voters);
}
public List<String> getMentions() {
return getList(Field.mentions);
}
protected List<String> getList(Field field) {
Set<String> set = new TreeSet<String>();
for (Change change : changes) {
if (change.hasField(field)) {
String values = change.getString(field);
for (String value : values.split(",")) {
switch (value.charAt(0)) {
case '+':
set.add(value.substring(1));
break;
case '-':
set.remove(value.substring(1));
break;
default:
set.add(value);
}
}
}
}
if (!set.isEmpty()) {
return new ArrayList<String>(set);
}
return Collections.emptyList();
}
public Attachment getAttachment(String name) {
Attachment attachment = null;
for (Change change : changes) {
if (change.hasAttachments()) {
Attachment a = change.getAttachment(name);
if (a != null) {
attachment = a;
}
}
}
return attachment;
}
public boolean hasAttachments() {
for (Change change : changes) {
if (change.hasAttachments()) {
return true;
}
}
return false;
}
public List<Attachment> getAttachments() {
List<Attachment> list = new ArrayList<Attachment>();
for (Change change : changes) {
if (change.hasAttachments()) {
list.addAll(change.attachments);
}
}
return list;
}
public List<Patchset> getPatchsets() {
List<Patchset> list = new ArrayList<Patchset>();
for (Change change : changes) {
if (change.patchset != null) {
list.add(change.patchset);
}
}
return list;
}
public List<Patchset> getPatchsetRevisions(int number) {
List<Patchset> list = new ArrayList<Patchset>();
for (Change change : changes) {
if (change.patchset != null) {
if (number == change.patchset.number) {
list.add(change.patchset);
}
}
}
return list;
}
public Patchset getPatchset(String sha) {
for (Change change : changes) {
if (change.patchset != null) {
if (sha.equals(change.patchset.tip)) {
return change.patchset;
}
}
}
return null;
}
public Patchset getPatchset(int number, int rev) {
for (Change change : changes) {
if (change.patchset != null) {
if (number == change.patchset.number && rev == change.patchset.rev) {
return change.patchset;
}
}
}
return null;
}
public Patchset getCurrentPatchset() {
Patchset patchset = null;
for (Change change : changes) {
if (change.patchset != null) {
if (patchset == null) {
patchset = change.patchset;
} else if (patchset.compareTo(change.patchset) == 1) {
patchset = change.patchset;
}
}
}
return patchset;
}
public boolean isCurrent(Patchset patchset) {
if (patchset == null) {
return false;
}
Patchset curr = getCurrentPatchset();
if (curr == null) {
return false;
}
return curr.equals(patchset);
}
public List<Change> getReviews(Patchset patchset) {
if (patchset == null) {
return Collections.emptyList();
}
// collect the patchset reviews by author
// the last review by the author is the
// official review
Map<String, Change> reviews = new LinkedHashMap<String, TicketModel.Change>();
for (Change change : changes) {
if (change.hasReview()) {
if (change.review.isReviewOf(patchset)) {
reviews.put(change.author, change);
}
}
}
return new ArrayList<Change>(reviews.values());
}
public boolean isApproved(Patchset patchset) {
if (patchset == null) {
return false;
}
boolean approved = false;
boolean vetoed = false;
for (Change change : getReviews(patchset)) {
if (change.hasReview()) {
if (change.review.isReviewOf(patchset)) {
if (Score.approved == change.review.score) {
approved = true;
} else if (Score.vetoed == change.review.score) {
vetoed = true;
}
}
}
}
return approved && !vetoed;
}
public boolean isVetoed(Patchset patchset) {
if (patchset == null) {
return false;
}
for (Change change : getReviews(patchset)) {
if (change.hasReview()) {
if (change.review.isReviewOf(patchset)) {
if (Score.vetoed == change.review.score) {
return true;
}
}
}
}
return false;
}
public Review getReviewBy(String username) {
for (Change change : getReviews(getCurrentPatchset())) {
if (change.author.equals(username)) {
return change.review;
}
}
return null;
}
public boolean isPatchsetAuthor(String username) {
for (Change change : changes) {
if (change.hasPatchset()) {
if (change.author.equals(username)) {
return true;
}
}
}
return false;
}
public void applyChange(Change change) {
if (changes.size() == 0) {
// first change created the ticket
created = change.date;
createdBy = change.author;
status = Status.New;
} else if (created == null || change.date.after(created)) {
// track last ticket update
updated = change.date;
updatedBy = change.author;
}
if (change.isMerge()) {
// identify merge patchsets
if (isEmpty(responsible)) {
responsible = change.author;
}
status = Status.Merged;
}
if (change.hasFieldChanges()) {
for (Map.Entry<Field, String> entry : change.fields.entrySet()) {
Field field = entry.getKey();
Object value = entry.getValue();
switch (field) {
case type:
type = TicketModel.Type.fromObject(value, type);
break;
case status:
status = TicketModel.Status.fromObject(value, status);
break;
case title:
title = toString(value);
break;
case body:
body = toString(value);
break;
case topic:
topic = toString(value);
break;
case responsible:
responsible = toString(value);
break;
case milestone:
milestone = toString(value);
break;
case mergeTo:
mergeTo = toString(value);
break;
case mergeSha:
mergeSha = toString(value);
break;
default:
// unknown
break;
}
}
}
// add the change to the ticket
changes.add(change);
}
protected String toString(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
public String toIndexableString() {
StringBuilder sb = new StringBuilder();
if (!isEmpty(title)) {
sb.append(title).append('\n');
}
if (!isEmpty(body)) {
sb.append(body).append('\n');
}
for (Change change : changes) {
if (change.hasComment()) {
sb.append(change.comment.text);
sb.append('\n');
}
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("#");
sb.append(number);
sb.append(": " + title + "\n");
for (Change change : changes) {
sb.append(change);
sb.append('\n');
}
return sb.toString();
}
@Override
public int compareTo(TicketModel o) {
return o.created.compareTo(created);
}
@Override
public boolean equals(Object o) {
if (o instanceof TicketModel) {
return number == ((TicketModel) o).number;
}
return super.equals(o);
}
@Override
public int hashCode() {
return (repository + number).hashCode();
}
/**
* Encapsulates a ticket change
*/
public static class Change implements Serializable, Comparable<Change> {
private static final long serialVersionUID = 1L;
public final Date date;
public final String author;
public Comment comment;
public Map<Field, String> fields;
public Set<Attachment> attachments;
public Patchset patchset;
public Review review;
private transient String id;
public Change(String author) {
this(author, new Date());
}
public Change(String author, Date date) {
this.date = date;
this.author = author;
}
public boolean isStatusChange() {
return hasField(Field.status);
}
public Status getStatus() {
Status state = Status.fromObject(getField(Field.status), null);
return state;
}
public boolean isMerge() {
return hasField(Field.status) && hasField(Field.mergeSha);
}
public boolean hasPatchset() {
return patchset != null;
}
public boolean hasReview() {
return review != null;
}
public boolean hasComment() {
return comment != null && !comment.isDeleted() && comment.text != null;
}
public Comment comment(String text) {
comment = new Comment(text);
comment.id = TicketModel.getSHA1(date.toString() + author + text);
try {
Pattern mentions = Pattern.compile("\\s@([A-Za-z0-9-_]+)");
Matcher m = mentions.matcher(text);
while (m.find()) {
String username = m.group(1);
plusList(Field.mentions, username);
}
} catch (Exception e) {
// ignore
}
return comment;
}
public Review review(Patchset patchset, Score score, boolean addReviewer) {
if (addReviewer) {
plusList(Field.reviewers, author);
}
review = new Review(patchset.number, patchset.rev);
review.score = score;
return review;
}
public boolean hasAttachments() {
return !TicketModel.isEmpty(attachments);
}
public void addAttachment(Attachment attachment) {
if (attachments == null) {
attachments = new LinkedHashSet<Attachment>();
}
attachments.add(attachment);
}
public Attachment getAttachment(String name) {
if (attachments != null) {
for (Attachment attachment : attachments) {
if (attachment.name.equalsIgnoreCase(name)) {
return attachment;
}
}
}
return null;
}
public boolean isParticipantChange() {
if (hasComment()
|| hasReview()
|| hasPatchset()
|| hasAttachments()) {
return true;
}
if (TicketModel.isEmpty(fields)) {
return false;
}
// identify real ticket field changes
Map<Field, String> map = new HashMap<Field, String>(fields);
map.remove(Field.watchers);
map.remove(Field.voters);
return !map.isEmpty();
}
public boolean hasField(Field field) {
return !TicketModel.isEmpty(getString(field));
}
public boolean hasFieldChanges() {
return !TicketModel.isEmpty(fields);
}
public String getField(Field field) {
if (fields != null) {
return fields.get(field);
}
return null;
}
public void setField(Field field, Object value) {
if (fields == null) {
fields = new LinkedHashMap<Field, String>();
}
if (value == null) {
fields.put(field, null);
} else if (Enum.class.isAssignableFrom(value.getClass())) {
fields.put(field, ((Enum<?>) value).name());
} else {
fields.put(field, value.toString());
}
}
public void remove(Field field) {
if (fields != null) {
fields.remove(field);
}
}
public String getString(Field field) {
String value = getField(field);
if (value == null) {
return null;
}
return value;
}
public void watch(String... username) {
plusList(Field.watchers, username);
}
public void unwatch(String... username) {
minusList(Field.watchers, username);
}
public void vote(String... username) {
plusList(Field.voters, username);
}
public void unvote(String... username) {
minusList(Field.voters, username);
}
public void label(String... label) {
plusList(Field.labels, label);
}
public void unlabel(String... label) {
minusList(Field.labels, label);
}
protected void plusList(Field field, String... items) {
modList(field, "+", items);
}
protected void minusList(Field field, String... items) {
modList(field, "-", items);
}
private void modList(Field field, String prefix, String... items) {
List<String> list = new ArrayList<String>();
for (String item : items) {
list.add(prefix + item);
}
if (hasField(field)) {
String flat = getString(field);
if (isEmpty(flat)) {
// field is empty, use this list
setField(field, join(list, ","));
} else {
// merge this list into the existing field list
Set<String> set = new TreeSet<String>(Arrays.asList(flat.split(",")));
set.addAll(list);
setField(field, join(set, ","));
}
} else {
// does not have a list for this field
setField(field, join(list, ","));
}
}
public String getId() {
if (id == null) {
id = getSHA1(Long.toHexString(date.getTime()) + author);
}
return id;
}
@Override
public int compareTo(Change c) {
return date.compareTo(c.date);
}
@Override
public int hashCode() {
return getId().hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof Change) {
return getId().equals(((Change) o).getId());
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(RelativeDateFormatter.format(date));
if (hasComment()) {
sb.append(" commented on by ");
} else if (hasPatchset()) {
sb.append(MessageFormat.format(" {0} uploaded by ", patchset));
} else {
sb.append(" changed by ");
}
sb.append(author).append(" - ");
if (hasComment()) {
if (comment.isDeleted()) {
sb.append("(deleted) ");
}
sb.append(comment.text).append(" ");
}
if (hasFieldChanges()) {
for (Map.Entry<Field, String> entry : fields.entrySet()) {
sb.append("\n ");
sb.append(entry.getKey().name());
sb.append(':');
sb.append(entry.getValue());
}
}
return sb.toString();
}
}
/**
* Returns true if the string is null or empty.
*
* @param value
* @return true if string is null or empty
*/
static boolean isEmpty(String value) {
return value == null || value.trim().length() == 0;
}
/**
* Returns true if the collection is null or empty
*
* @param collection
* @return
*/
static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.size() == 0;
}
/**
* Returns true if the map is null or empty
*
* @param map
* @return
*/
static boolean isEmpty(Map<?, ?> map) {
return map == null || map.size() == 0;
}
/**
* Calculates the SHA1 of the string.
*
* @param text
* @return sha1 of the string
*/
static String getSHA1(String text) {
try {
byte[] bytes = text.getBytes("iso-8859-1");
return getSHA1(bytes);
} catch (UnsupportedEncodingException u) {
throw new RuntimeException(u);
}
}
/**
* Calculates the SHA1 of the byte array.
*
* @param bytes
* @return sha1 of the byte array
*/
static String getSHA1(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(bytes, 0, bytes.length);
byte[] digest = md.digest();
return toHex(digest);
} catch (NoSuchAlgorithmException t) {
throw new RuntimeException(t);
}
}
/**
* Returns the hex representation of the byte array.
*
* @param bytes
* @return byte array as hex string
*/
static String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] & 0xff) < 0x10) {
sb.append('0');
}
sb.append(Long.toString(bytes[i] & 0xff, 16));
}
return sb.toString();
}
/**
* Join the list of strings into a single string with a space separator.
*
* @param values
* @return joined list
*/
static String join(Collection<String> values) {
return join(values, " ");
}
/**
* Join the list of strings into a single string with the specified
* separator.
*
* @param values
* @param separator
* @return joined list
*/
static String join(String[] values, String separator) {
return join(Arrays.asList(values), separator);
}
/**
* Join the list of strings into a single string with the specified
* separator.
*
* @param values
* @param separator
* @return joined list
*/
static String join(Collection<String> values, String separator) {
StringBuilder sb = new StringBuilder();
for (String value : values) {
sb.append(value).append(separator);
}
if (sb.length() > 0) {
// truncate trailing separator
sb.setLength(sb.length() - separator.length());
}
return sb.toString().trim();
}
/**
* Produce a deep copy of the given object. Serializes the entire object to
* a byte array in memory. Recommended for relatively small objects.
*/
@SuppressWarnings("unchecked")
static <T> T copy(T original) {
T o = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(byteOut);
oos.writeObject(original);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream ois = new ObjectInputStream(byteIn);
try {
o = (T) ois.readObject();
} catch (ClassNotFoundException cex) {
// actually can not happen in this instance
}
} catch (IOException iox) {
// doesn't seem likely to happen as these streams are in memory
throw new RuntimeException(iox);
}
return o;
}
public static class Patchset implements Serializable, Comparable<Patchset> {
private static final long serialVersionUID = 1L;
public int number;
public int rev;
public String tip;
public String parent;
public String base;
public int insertions;
public int deletions;
public int commits;
public int added;
public PatchsetType type;
public boolean isFF() {
return PatchsetType.FastForward == type;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof Patchset) {
return hashCode() == o.hashCode();
}
return false;
}
@Override
public int compareTo(Patchset p) {
if (number > p.number) {
return -1;
} else if (p.number > number) {
return 1;
} else {
// same patchset, different revision
if (rev > p.rev) {
return -1;
} else if (p.rev > rev) {
return 1;
} else {
// same patchset & revision
return 0;
}
}
}
@Override
public String toString() {
return "patchset " + number + " revision " + rev;
}
}
public static class Comment implements Serializable {
private static final long serialVersionUID = 1L;
public String text;
public String id;
public Boolean deleted;
public CommentSource src;
public String replyTo;
Comment(String text) {
this.text = text;
}
public boolean isDeleted() {
return deleted != null && deleted;
}
@Override
public String toString() {
return text;
}
}
public static class Attachment implements Serializable {
private static final long serialVersionUID = 1L;
public final String name;
public long size;
public byte[] content;
public Boolean deleted;
public Attachment(String name) {
this.name = name;
}
public boolean isDeleted() {
return deleted != null && deleted;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof Attachment) {
return name.equalsIgnoreCase(((Attachment) o).name);
}
return false;
}
@Override
public String toString() {
return name;
}
}
public static class Review implements Serializable {
private static final long serialVersionUID = 1L;
public final int patchset;
public final int rev;
public Score score;
public Review(int patchset, int revision) {
this.patchset = patchset;
this.rev = revision;
}
public boolean isReviewOf(Patchset p) {
return patchset == p.number && rev == p.rev;
}
@Override
public String toString() {
return "review of patchset " + patchset + " rev " + rev + ":" + score;
}
}
public static enum Score {
approved(2), looks_good(1), not_reviewed(0), needs_improvement(-1), vetoed(
-2);
final int value;
Score(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return name().toLowerCase().replace('_', ' ');
}
public static Score fromScore(int score) {
for (Score s : values()) {
if (s.getValue() == score) {
return s;
}
}
throw new NoSuchElementException(String.valueOf(score));
}
}
public static enum Field {
title, body, responsible, type, status, milestone, mergeSha, mergeTo,
topic, labels, watchers, reviewers, voters, mentions;
}
public static enum Type {
Enhancement, Task, Bug, Proposal, Question;
public static Type defaultType = Task;
public static Type [] choices() {
return new Type [] { Enhancement, Task, Bug, Question };
}
@Override
public String toString() {
return name().toLowerCase().replace('_', ' ');
}
public static Type fromObject(Object o, Type defaultType) {
if (o instanceof Type) {
// cast and return
return (Type) o;
} else if (o instanceof String) {
// find by name
for (Type type : values()) {
String str = o.toString();
if (type.name().equalsIgnoreCase(str)
|| type.toString().equalsIgnoreCase(str)) {
return type;
}
}
} else if (o instanceof Number) {
// by ordinal
int id = ((Number) o).intValue();
if (id >= 0 && id < values().length) {
return values()[id];
}
}
return defaultType;
}
}
public static enum Status {
New, Open, Closed, Resolved, Fixed, Merged, Wontfix, Declined, Duplicate, Invalid, Abandoned, On_Hold;
public static Status [] requestWorkflow = { Open, Resolved, Declined, Duplicate, Invalid, Abandoned, On_Hold };
public static Status [] bugWorkflow = { Open, Fixed, Wontfix, Duplicate, Invalid, Abandoned, On_Hold };
public static Status [] proposalWorkflow = { Open, Resolved, Declined, Abandoned, On_Hold };
public static Status [] milestoneWorkflow = { Open, Closed, Abandoned, On_Hold };
@Override
public String toString() {
return name().toLowerCase().replace('_', ' ');
}
public static Status fromObject(Object o, Status defaultStatus) {
if (o instanceof Status) {
// cast and return
return (Status) o;
} else if (o instanceof String) {
// find by name
String name = o.toString();
for (Status state : values()) {
if (state.name().equalsIgnoreCase(name)
|| state.toString().equalsIgnoreCase(name)) {
return state;
}
}
} else if (o instanceof Number) {
// by ordinal
int id = ((Number) o).intValue();
if (id >= 0 && id < values().length) {
return values()[id];
}
}
return defaultStatus;
}
public boolean isClosed() {
return ordinal() > Open.ordinal();
}
}
public static enum CommentSource {
Comment, Email
}
public static enum PatchsetType {
Proposal, FastForward, Rebase, Squash, Rebase_Squash, Amend;
public boolean isRewrite() {
return (this != FastForward) && (this != Proposal);
}
@Override
public String toString() {
return name().toLowerCase().replace('_', '+');
}
public static PatchsetType fromObject(Object o) {
if (o instanceof PatchsetType) {
// cast and return
return (PatchsetType) o;
} else if (o instanceof String) {
// find by name
String name = o.toString();
for (PatchsetType type : values()) {
if (type.name().equalsIgnoreCase(name)
|| type.toString().equalsIgnoreCase(name)) {
return type;
}
}
} else if (o instanceof Number) {
// by ordinal
int id = ((Number) o).intValue();
if (id >= 0 && id < values().length) {
return values()[id];
}
}
return null;
}
}
}
|