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 mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
70 raw.setKey(base.key());
71 raw.setType(base.type());
72 raw.setCreationDate(base.creationDate());
73 raw.setUpdateDate(base.updateDate());
74 raw.setCloseDate(base.closeDate());
75 raw.setResolution(base.resolution());
76 raw.setStatus(base.status());
77 raw.setAssignee(base.assignee());
78 raw.setAuthorLogin(base.authorLogin());
79 raw.setTags(base.tags());
80 raw.setAttributes(base.attributes());
81 raw.setEffort(debtCalculator.calculate(raw));
82 raw.setOnDisabledRule(base.isOnDisabledRule());
83 if (base.manualSeverity()) {
84 raw.setManualSeverity(true);
85 raw.setSeverity(base.severity());
87 updater.setPastSeverity(raw, base.severity(), changeContext);
89 // set component/module related fields from base in case current component has been moved
90 // (in which case base issue belongs to original file and raw issue to component)
91 raw.setComponentUuid(base.componentUuid());
92 raw.setComponentKey(base.componentKey());
93 raw.setModuleUuid(base.moduleUuid());
94 raw.setModuleUuidPath(base.moduleUuidPath());
96 // fields coming from raw
97 updater.setPastLine(raw, base.getLine());
98 updater.setPastLocations(raw, base.getLocations());
99 updater.setPastMessage(raw, base.getMessage(), changeContext);
100 updater.setPastGap(raw, base.gap(), changeContext);
101 updater.setPastEffort(raw, base.effort(), changeContext);
102 raw.setSelectedAt(base.selectedAt());
105 public void doAutomaticTransition(DefaultIssue issue) {
106 workflow.doAutomaticTransition(issue, changeContext);