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.DefaultIssueComment;
27 import org.sonar.core.issue.FieldDiffs;
28 import org.sonar.core.issue.IssueChangeContext;
29 import org.sonar.core.util.Uuids;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.issue.IssueFieldsSetter;
32 import org.sonar.server.issue.workflow.IssueWorkflow;
35 * Sets the appropriate fields when an issue is :
37 * <li>newly created</li>
38 * <li>merged the related base issue</li>
39 * <li>relocated (only manual issues)</li>
42 public class IssueLifecycle {
44 private final IssueWorkflow workflow;
45 private final IssueChangeContext changeContext;
46 private final IssueFieldsSetter updater;
47 private final DebtCalculator debtCalculator;
48 private final AnalysisMetadataHolder analysisMetadataHolder;
50 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
51 this(analysisMetadataHolder, IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator);
55 IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater,
56 DebtCalculator debtCalculator) {
57 this.analysisMetadataHolder = analysisMetadataHolder;
58 this.workflow = workflow;
59 this.updater = updater;
60 this.debtCalculator = debtCalculator;
61 this.changeContext = changeContext;
64 public void initNewOpenIssue(DefaultIssue issue) {
65 issue.setKey(Uuids.create());
66 issue.setCreationDate(changeContext.date());
67 issue.setUpdateDate(changeContext.date());
68 issue.setStatus(Issue.STATUS_OPEN);
69 issue.setEffort(debtCalculator.calculate(issue));
72 public void copyExistingOpenIssueFromLongLivingBranch(DefaultIssue raw, DefaultIssue base, String fromLongBranchName) {
73 raw.setKey(Uuids.create());
75 copyIssueAttributes(raw, base);
76 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_LONG_BRANCH, fromLongBranchName, analysisMetadataHolder.getBranch().get().getName());
79 public void mergeConfirmedOrResolvedFromShortLivingBranch(DefaultIssue raw, DefaultIssue base, String fromShortBranchName) {
80 copyIssueAttributes(raw, base);
81 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_SHORT_BRANCH, fromShortBranchName, analysisMetadataHolder.getBranch().get().getName());
84 private void copyIssueAttributes(DefaultIssue to, DefaultIssue from) {
87 if (from.manualSeverity()) {
88 to.setManualSeverity(true);
89 to.setSeverity(from.severity());
91 copyChanges(to, from);
94 private static void copyChanges(DefaultIssue raw, DefaultIssue base) {
95 base.comments().forEach(c -> raw.addComment(DefaultIssueComment.copy(raw.key(), c)));
96 base.changes().forEach(c -> raw.addChange(FieldDiffs.copy(raw.key(), c)));
99 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
100 raw.setKey(base.key());
102 copyFields(raw, base);
104 if (base.manualSeverity()) {
105 raw.setManualSeverity(true);
106 raw.setSeverity(base.severity());
108 updater.setPastSeverity(raw, base.severity(), changeContext);
110 // set component/module related fields from base in case current component has been moved
111 // (in which case base issue belongs to original file and raw issue to component)
112 raw.setComponentUuid(base.componentUuid());
113 raw.setComponentKey(base.componentKey());
114 raw.setModuleUuid(base.moduleUuid());
115 raw.setModuleUuidPath(base.moduleUuidPath());
117 // fields coming from raw
118 updater.setPastLine(raw, base.getLine());
119 updater.setPastLocations(raw, base.getLocations());
120 updater.setPastMessage(raw, base.getMessage(), changeContext);
121 updater.setPastGap(raw, base.gap(), changeContext);
122 updater.setPastEffort(raw, base.effort(), changeContext);
125 public void doAutomaticTransition(DefaultIssue issue) {
126 workflow.doAutomaticTransition(issue, changeContext);
129 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
130 toIssue.setType(fromIssue.type());
131 toIssue.setCreationDate(fromIssue.creationDate());
132 toIssue.setUpdateDate(fromIssue.updateDate());
133 toIssue.setCloseDate(fromIssue.closeDate());
134 toIssue.setResolution(fromIssue.resolution());
135 toIssue.setStatus(fromIssue.status());
136 toIssue.setAssignee(fromIssue.assignee());
137 toIssue.setAuthorLogin(fromIssue.authorLogin());
138 toIssue.setTags(fromIssue.tags());
139 toIssue.setAttributes(fromIssue.attributes());
140 toIssue.setEffort(debtCalculator.calculate(toIssue));
141 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
142 toIssue.setSelectedAt(fromIssue.selectedAt());