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 javax.annotation.Nullable;
25 import org.sonar.api.issue.Issue;
26 import org.sonar.core.issue.DefaultIssue;
27 import org.sonar.core.issue.IssueChangeContext;
28 import org.sonar.core.util.Uuids;
29 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.issue.IssueFieldsSetter;
31 import org.sonar.server.issue.workflow.IssueWorkflow;
34 * Sets the appropriate fields when an issue is :
36 * <li>newly created</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 copyResolution(DefaultIssue raw, String status, @Nullable String resolution) {
81 raw.setStatus(status);
82 raw.setResolution(resolution);
85 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
86 raw.setKey(base.key());
88 copyFields(raw, base);
90 if (base.manualSeverity()) {
91 raw.setManualSeverity(true);
92 raw.setSeverity(base.severity());
94 updater.setPastSeverity(raw, base.severity(), changeContext);
96 // set component/module related fields from base in case current component has been moved
97 // (in which case base issue belongs to original file and raw issue to component)
98 raw.setComponentUuid(base.componentUuid());
99 raw.setComponentKey(base.componentKey());
100 raw.setModuleUuid(base.moduleUuid());
101 raw.setModuleUuidPath(base.moduleUuidPath());
103 // fields coming from raw
104 updater.setPastLine(raw, base.getLine());
105 updater.setPastLocations(raw, base.getLocations());
106 updater.setPastMessage(raw, base.getMessage(), changeContext);
107 updater.setPastGap(raw, base.gap(), changeContext);
108 updater.setPastEffort(raw, base.effort(), changeContext);
111 public void doAutomaticTransition(DefaultIssue issue) {
112 workflow.doAutomaticTransition(issue, changeContext);
115 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
116 toIssue.setType(fromIssue.type());
117 toIssue.setCreationDate(fromIssue.creationDate());
118 toIssue.setUpdateDate(fromIssue.updateDate());
119 toIssue.setCloseDate(fromIssue.closeDate());
120 toIssue.setResolution(fromIssue.resolution());
121 toIssue.setStatus(fromIssue.status());
122 toIssue.setAssignee(fromIssue.assignee());
123 toIssue.setAuthorLogin(fromIssue.authorLogin());
124 toIssue.setTags(fromIssue.tags());
125 toIssue.setAttributes(fromIssue.attributes());
126 toIssue.setEffort(debtCalculator.calculate(toIssue));
127 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
128 toIssue.setSelectedAt(fromIssue.selectedAt());