3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.issue;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.Date;
24 import org.sonar.api.issue.Issue;
25 import org.sonar.core.issue.DefaultIssue;
26 import org.sonar.core.issue.IssueChangeContext;
27 import org.sonar.core.util.Uuids;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.server.issue.IssueFieldsSetter;
30 import org.sonar.server.issue.workflow.IssueWorkflow;
33 * Sets the appropriate fields when an issue is :
35 * <li>newly created</li>
36 * <li>reused in incremental analysis</li>
37 * <li>merged the related base issue</li>
38 * <li>relocated (only manual issues)</li>
41 public class IssueLifecycle {
43 private final IssueWorkflow workflow;
44 private final IssueChangeContext changeContext;
45 private final IssueFieldsSetter updater;
46 private final DebtCalculator debtCalculator;
48 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
49 this(IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator);
53 IssueLifecycle(IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
54 this.workflow = workflow;
55 this.updater = updater;
56 this.debtCalculator = debtCalculator;
57 this.changeContext = changeContext;
60 public void initNewOpenIssue(DefaultIssue issue) {
61 issue.setKey(Uuids.create());
62 issue.setCreationDate(changeContext.date());
63 issue.setUpdateDate(changeContext.date());
64 issue.setStatus(Issue.STATUS_OPEN);
65 issue.setEffort(debtCalculator.calculate(issue));
68 public void copyExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
69 raw.setKey(Uuids.create());
72 copyFields(raw, base);
74 if (base.manualSeverity()) {
75 raw.setManualSeverity(true);
76 raw.setSeverity(base.severity());
80 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
81 raw.setKey(base.key());
83 copyFields(raw, base);
85 if (base.manualSeverity()) {
86 raw.setManualSeverity(true);
87 raw.setSeverity(base.severity());
89 updater.setPastSeverity(raw, base.severity(), changeContext);
91 // set component/module related fields from base in case current component has been moved
92 // (in which case base issue belongs to original file and raw issue to component)
93 raw.setComponentUuid(base.componentUuid());
94 raw.setComponentKey(base.componentKey());
95 raw.setModuleUuid(base.moduleUuid());
96 raw.setModuleUuidPath(base.moduleUuidPath());
98 // fields coming from raw
99 updater.setPastLine(raw, base.getLine());
100 updater.setPastLocations(raw, base.getLocations());
101 updater.setPastMessage(raw, base.getMessage(), changeContext);
102 updater.setPastGap(raw, base.gap(), changeContext);
103 updater.setPastEffort(raw, base.effort(), changeContext);
106 public void doAutomaticTransition(DefaultIssue issue) {
107 workflow.doAutomaticTransition(issue, changeContext);
110 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
111 toIssue.setType(fromIssue.type());
112 toIssue.setCreationDate(fromIssue.creationDate());
113 toIssue.setUpdateDate(fromIssue.updateDate());
114 toIssue.setCloseDate(fromIssue.closeDate());
115 toIssue.setResolution(fromIssue.resolution());
116 toIssue.setStatus(fromIssue.status());
117 toIssue.setAssignee(fromIssue.assignee());
118 toIssue.setAuthorLogin(fromIssue.authorLogin());
119 toIssue.setTags(fromIssue.tags());
120 toIssue.setAttributes(fromIssue.attributes());
121 toIssue.setEffort(debtCalculator.calculate(toIssue));
122 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
123 toIssue.setSelectedAt(fromIssue.selectedAt());