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
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.issue;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.util.Uuids;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.protobuf.DbIssues;
import org.sonar.db.rule.RuleDto;
import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.api.utils.DateUtils.dateToLong;
import static org.sonar.api.utils.DateUtils.longToDate;
public final class IssueDto implements Serializable {
private static final char TAGS_SEPARATOR = ',';
private static final Joiner TAGS_JOINER = Joiner.on(TAGS_SEPARATOR).skipNulls();
private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
private Long id;
private int type;
private String kee;
private String componentUuid;
private String projectUuid;
private Integer ruleId;
private String severity;
private boolean manualSeverity;
private String message;
private Integer line;
private Double gap;
private Long effort;
private String status;
private String resolution;
private String checksum;
private String assignee;
private String authorLogin;
private String issueAttributes;
private byte[] locations;
private long createdAt;
private long updatedAt;
// functional dates stored as Long
private Long issueCreationDate;
private Long issueUpdateDate;
private Long issueCloseDate;
/**
* Temporary date used only during scan
*/
private Long selectedAt;
// joins
private String ruleKey;
private String ruleRepo;
private String language;
private String componentKey;
private String moduleUuid;
private String moduleUuidPath;
private String projectKey;
private String filePath;
private String tags;
/**
* On batch side, component keys and uuid are useless
*/
public static IssueDto toDtoForComputationInsert(DefaultIssue issue, int ruleId, long now) {
return new IssueDto()
.setKee(issue.key())
.setType(issue.type())
.setLine(issue.line())
.setLocations((DbIssues.Locations) issue.getLocations())
.setMessage(issue.message())
.setGap(issue.gap())
.setEffort(issue.effortInMinutes())
.setResolution(issue.resolution())
.setStatus(issue.status())
.setSeverity(issue.severity())
.setManualSeverity(issue.manualSeverity())
.setChecksum(issue.checksum())
.setAssignee(issue.assignee())
.setRuleId(ruleId)
.setRuleKey(issue.ruleKey().repository(), issue.ruleKey().rule())
.setTags(issue.tags())
.setComponentUuid(issue.componentUuid())
.setComponentKey(issue.componentKey())
.setModuleUuid(issue.moduleUuid())
.setModuleUuidPath(issue.moduleUuidPath())
.setProjectUuid(issue.projectUuid())
.setProjectKey(issue.projectKey())
.setIssueAttributes(KeyValueFormat.format(issue.attributes()))
.setAuthorLogin(issue.authorLogin())
.setIssueCreationDate(issue.creationDate())
.setIssueCloseDate(issue.closeDate())
.setIssueUpdateDate(issue.updateDate())
.setSelectedAt(issue.selectedAt())
// technical dates
.setCreatedAt(now)
.setUpdatedAt(now);
}
/**
* On server side, we need component keys and uuid
*/
public static IssueDto toDtoForServerInsert(DefaultIssue issue, ComponentDto component, ComponentDto project, int ruleId, long now) {
return toDtoForComputationInsert(issue, ruleId, now)
.setComponent(component)
.setProject(project);
}
public static IssueDto toDtoForUpdate(DefaultIssue issue, long now) {
// Invariant fields, like key and rule, can't be updated
return new IssueDto()
.setKee(issue.key())
.setType(issue.type())
.setLine(issue.line())
.setLocations((DbIssues.Locations) issue.getLocations())
.setMessage(issue.message())
.setGap(issue.gap())
.setEffort(issue.effortInMinutes())
.setResolution(issue.resolution())
.setStatus(issue.status())
.setSeverity(issue.severity())
.setChecksum(issue.checksum())
.setManualSeverity(issue.manualSeverity())
.setAssignee(issue.assignee())
.setIssueAttributes(KeyValueFormat.format(issue.attributes()))
.setAuthorLogin(issue.authorLogin())
.setRuleKey(issue.ruleKey().repository(), issue.ruleKey().rule())
.setTags(issue.tags())
.setComponentUuid(issue.componentUuid())
.setComponentKey(issue.componentKey())
.setModuleUuid(issue.moduleUuid())
.setModuleUuidPath(issue.moduleUuidPath())
.setProjectUuid(issue.projectUuid())
.setProjectKey(issue.projectKey())
.setIssueCreationDate(issue.creationDate())
.setIssueCloseDate(issue.closeDate())
.setIssueUpdateDate(issue.updateDate())
.setSelectedAt(issue.selectedAt())
// technical date
.setUpdatedAt(now);
}
public static IssueDto createFor(Project project, RuleDto rule) {
return new IssueDto()
.setProjectUuid(project.getUuid())
.setRuleId(rule.getId())
.setKee(Uuids.create());
}
public String getKey() {
return getKee();
}
public Long getId() {
return id;
}
public IssueDto setId(@Nullable Long id) {
this.id = id;
return this;
}
public String getKee() {
return kee;
}
public IssueDto setKee(String s) {
this.kee = s;
return this;
}
public IssueDto setComponent(ComponentDto component) {
this.componentKey = component.getKey();
this.componentUuid = component.uuid();
this.moduleUuid = component.moduleUuid();
this.moduleUuidPath = component.moduleUuidPath();
this.filePath = component.path();
return this;
}
public IssueDto setProject(ComponentDto project) {
this.projectKey = project.getKey();
this.projectUuid = project.uuid();
return this;
}
public Integer getRuleId() {
return ruleId;
}
/**
* please use setRule(RuleDto rule)
*/
public IssueDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
return this;
}
@CheckForNull
public String getSeverity() {
return severity;
}
public IssueDto setSeverity(@Nullable String s) {
checkArgument(s == null || s.length() <= 10, "Value is too long for issue severity: %s", s);
this.severity = s;
return this;
}
public boolean isManualSeverity() {
return manualSeverity;
}
public IssueDto setManualSeverity(boolean manualSeverity) {
this.manualSeverity = manualSeverity;
return this;
}
@CheckForNull
public String getMessage() {
return message;
}
public IssueDto setMessage(@Nullable String s) {
checkArgument(s == null || s.length() <= 4000, "Value is too long for issue message: %s", s);
this.message = s;
return this;
}
@CheckForNull
public Integer getLine() {
return line;
}
public IssueDto setLine(@Nullable Integer i) {
checkArgument(i == null || i >= 0, "Value of issue line must be positive: %d", i);
this.line = i;
return this;
}
@CheckForNull
public Double getGap() {
return gap;
}
public IssueDto setGap(@Nullable Double d) {
checkArgument(d == null || d >= 0, "Value of issue gap must be positive: %d", d);
this.gap = d;
return this;
}
@CheckForNull
public Long getEffort() {
return effort;
}
public IssueDto setEffort(@Nullable Long l) {
checkArgument(l == null || l >= 0, "Value of issue effort must be positive: %d", l);
this.effort = l;
return this;
}
public String getStatus() {
return status;
}
public IssueDto setStatus(@Nullable String s) {
checkArgument(s == null || s.length() <= 20, "Value is too long for issue status: %s", s);
this.status = s;
return this;
}
@CheckForNull
public String getResolution() {
return resolution;
}
public IssueDto setResolution(@Nullable String s) {
checkArgument(s == null || s.length() <= 20, "Value is too long for issue resolution: %s", s);
this.resolution = s;
return this;
}
@CheckForNull
public String getChecksum() {
return checksum;
}
public IssueDto setChecksum(@Nullable String s) {
checkArgument(s == null || s.length() <= 1000, "Value is too long for issue checksum: %s", s);
this.checksum = s;
return this;
}
@CheckForNull
public String getAssignee() {
return assignee;
}
public IssueDto setAssignee(@Nullable String s) {
checkArgument(s == null || s.length() <= 255, "Value is too long for issue assignee: %s", s);
this.assignee = s;
return this;
}
@CheckForNull
public String getAuthorLogin() {
return authorLogin;
}
public IssueDto setAuthorLogin(@Nullable String s) {
checkArgument(s == null || s.length() <= 255, "Value is too long for issue author login: %s", s);
this.authorLogin = s;
return this;
}
@CheckForNull
public String getIssueAttributes() {
return issueAttributes;
}
public IssueDto setIssueAttributes(@Nullable String s) {
checkArgument(s == null || s.length() <= 4000, "Value is too long for issue attributes: %s", s);
this.issueAttributes = s;
return this;
}
/**
* Technical date
*/
public long getCreatedAt() {
return createdAt;
}
public IssueDto setCreatedAt(long createdAt) {
this.createdAt = createdAt;
return this;
}
/**
* Technical date
*/
public long getUpdatedAt() {
return updatedAt;
}
public IssueDto setUpdatedAt(long updatedAt) {
this.updatedAt = updatedAt;
return this;
}
public Long getIssueCreationTime() {
return issueCreationDate;
}
public IssueDto setIssueCreationTime(Long time) {
this.issueCreationDate = time;
return this;
}
public Date getIssueCreationDate() {
return longToDate(issueCreationDate);
}
public IssueDto setIssueCreationDate(@Nullable Date d) {
this.issueCreationDate = dateToLong(d);
return this;
}
public Long getIssueUpdateTime() {
return issueUpdateDate;
}
public IssueDto setIssueUpdateTime(Long time) {
this.issueUpdateDate = time;
return this;
}
public Date getIssueUpdateDate() {
return longToDate(issueUpdateDate);
}
public IssueDto setIssueUpdateDate(@Nullable Date d) {
this.issueUpdateDate = dateToLong(d);
return this;
}
public Long getIssueCloseTime() {
return issueCloseDate;
}
public IssueDto setIssueCloseTime(Long time) {
this.issueCloseDate = time;
return this;
}
public Date getIssueCloseDate() {
return longToDate(issueCloseDate);
}
public IssueDto setIssueCloseDate(@Nullable Date d) {
this.issueCloseDate = dateToLong(d);
return this;
}
public String getRule() {
return ruleKey;
}
public IssueDto setRule(RuleDto rule) {
Preconditions.checkNotNull(rule.getId(), "Rule must be persisted.");
this.ruleId = rule.getId();
this.ruleKey = rule.getRuleKey();
this.ruleRepo = rule.getRepositoryKey();
this.language = rule.getLanguage();
return this;
}
public String getRuleRepo() {
return ruleRepo;
}
public RuleKey getRuleKey() {
return RuleKey.of(ruleRepo, ruleKey);
}
public String getLanguage() {
return language;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setRule(RuleDto)} instead
*/
public IssueDto setLanguage(String language) {
this.language = language;
return this;
}
public String getComponentKey() {
return componentKey;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setComponent(ComponentDto)} instead
*/
public IssueDto setComponentKey(String componentKey) {
this.componentKey = componentKey;
return this;
}
/**
* Can be null on Views or Devs
*/
@CheckForNull
public String getComponentUuid() {
return componentUuid;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setComponent(ComponentDto)} instead
*/
public IssueDto setComponentUuid(@Nullable String s) {
checkArgument(s == null || s.length() <= 50, "Value is too long for column ISSUES.COMPONENT_UUID: %s", s);
this.componentUuid = s;
return this;
}
@CheckForNull
public String getModuleUuid() {
return moduleUuid;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setComponent(ComponentDto)} instead
*/
public IssueDto setModuleUuid(@Nullable String moduleUuid) {
this.moduleUuid = moduleUuid;
return this;
}
@CheckForNull
public String getModuleUuidPath() {
return moduleUuidPath;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setComponent(ComponentDto)} instead
*/
public IssueDto setModuleUuidPath(@Nullable String moduleUuidPath) {
this.moduleUuidPath = moduleUuidPath;
return this;
}
/**
* Used by the issue tracking mechanism, but it should used the component uuid instead
*/
public String getProjectKey() {
return projectKey;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setProject(ComponentDto)} instead
*/
public IssueDto setProjectKey(String projectKey) {
this.projectKey = projectKey;
return this;
}
/**
* Can be null on Views or Devs
*/
@CheckForNull
public String getProjectUuid() {
return projectUuid;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setProject(ComponentDto)} instead
*/
public IssueDto setProjectUuid(@Nullable String s) {
checkArgument(s == null || s.length() <= 50, "Value is too long for column ISSUES.PROJECT_UUID: %s", s);
this.projectUuid = s;
return this;
}
@CheckForNull
public Long getSelectedAt() {
return selectedAt;
}
public IssueDto setSelectedAt(@Nullable Long d) {
this.selectedAt = d;
return this;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setRule(RuleDto)} instead
*/
public IssueDto setRuleKey(String repo, String rule) {
this.ruleRepo = repo;
this.ruleKey = rule;
return this;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setProject(ComponentDto)} instead
*/
public String getFilePath() {
return filePath;
}
/**
* Should only be used to persist in E/S
* <p/>
* Please use {@link #setProject(ComponentDto)} instead
*/
public IssueDto setFilePath(String filePath) {
this.filePath = filePath;
return this;
}
public Set<String> getTags() {
return ImmutableSet.copyOf(TAGS_SPLITTER.split(tags == null ? "" : tags));
}
public IssueDto setTags(@Nullable Collection<String> tags) {
if (tags == null || tags.isEmpty()) {
setTagsString(null);
} else {
setTagsString(TAGS_JOINER.join(tags));
}
return this;
}
public IssueDto setTagsString(@Nullable String s) {
checkArgument(s == null || s.length() <= 4000, "Value is too long for column ISSUES.TAGS: %s", s);
this.tags = s;
return this;
}
public String getTagsString() {
return tags;
}
@CheckForNull
public byte[] getLocations() {
return locations;
}
@CheckForNull
public DbIssues.Locations parseLocations() {
if (locations != null) {
try {
return DbIssues.Locations.parseFrom(locations);
} catch (InvalidProtocolBufferException e) {
throw new IllegalStateException(String.format("Fail to read ISSUES.LOCATIONS [KEE=%s]", kee), e);
}
}
return null;
}
public IssueDto setLocations(@Nullable byte[] locations) {
this.locations = locations;
return this;
}
public IssueDto setLocations(@Nullable DbIssues.Locations locations) {
if (locations == null) {
this.locations = null;
} else {
this.locations = locations.toByteArray();
}
return this;
}
public int getType() {
return type;
}
public IssueDto setType(int type) {
this.type = type;
return this;
}
public IssueDto setType(RuleType type) {
this.type = type.getDbConstant();
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
public DefaultIssue toDefaultIssue() {
DefaultIssue issue = new DefaultIssue();
issue.setKey(kee);
issue.setType(RuleType.valueOf(type));
issue.setStatus(status);
issue.setResolution(resolution);
issue.setMessage(message);
issue.setGap(gap);
issue.setEffort(effort != null ? Duration.create(effort) : null);
issue.setLine(line);
issue.setChecksum(checksum);
issue.setSeverity(severity);
issue.setAssignee(assignee);
issue.setAttributes(KeyValueFormat.parse(MoreObjects.firstNonNull(issueAttributes, "")));
issue.setComponentKey(componentKey);
issue.setComponentUuid(componentUuid);
issue.setModuleUuid(moduleUuid);
issue.setModuleUuidPath(moduleUuidPath);
issue.setProjectUuid(projectUuid);
issue.setProjectKey(projectKey);
issue.setManualSeverity(manualSeverity);
issue.setRuleKey(getRuleKey());
issue.setTags(getTags());
issue.setLanguage(language);
issue.setAuthorLogin(authorLogin);
issue.setNew(false);
issue.setCreationDate(longToDate(issueCreationDate));
issue.setCloseDate(longToDate(issueCloseDate));
issue.setUpdateDate(longToDate(issueUpdateDate));
issue.setSelectedAt(selectedAt);
issue.setLocations(parseLocations());
return issue;
}
}
|