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 java.util.Optional;
25 import org.sonar.api.issue.Issue;
26 import org.sonar.api.issue.IssueComment;
27 import org.sonar.core.issue.DefaultIssue;
28 import org.sonar.core.issue.DefaultIssueComment;
29 import org.sonar.core.issue.FieldDiffs;
30 import org.sonar.core.issue.IssueChangeContext;
31 import org.sonar.core.util.Uuids;
32 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
33 import org.sonar.server.issue.IssueFieldsSetter;
34 import org.sonar.server.issue.workflow.IssueWorkflow;
37 * Sets the appropriate fields when an issue is :
39 * <li>newly created</li>
40 * <li>merged the related base issue</li>
41 * <li>relocated (only manual issues)</li>
44 public class IssueLifecycle {
46 private final IssueWorkflow workflow;
47 private final IssueChangeContext changeContext;
48 private final IssueFieldsSetter updater;
49 private final DebtCalculator debtCalculator;
50 private final AnalysisMetadataHolder analysisMetadataHolder;
52 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
53 this(analysisMetadataHolder, IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator);
57 IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater,
58 DebtCalculator debtCalculator) {
59 this.analysisMetadataHolder = analysisMetadataHolder;
60 this.workflow = workflow;
61 this.updater = updater;
62 this.debtCalculator = debtCalculator;
63 this.changeContext = changeContext;
66 public void initNewOpenIssue(DefaultIssue issue) {
67 issue.setKey(Uuids.create());
68 issue.setCreationDate(changeContext.date());
69 issue.setUpdateDate(changeContext.date());
70 issue.setStatus(Issue.STATUS_OPEN);
71 issue.setEffort(debtCalculator.calculate(issue));
74 public void copyExistingOpenIssueFromLongLivingBranch(DefaultIssue raw, DefaultIssue base, String fromLongBranchName) {
75 raw.setKey(Uuids.create());
77 copyIssueAttributes(raw, base);
78 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_LONG_BRANCH, fromLongBranchName, analysisMetadataHolder.getBranch().get().getName());
81 public void mergeConfirmedOrResolvedFromShortLivingBranch(DefaultIssue raw, DefaultIssue base, String fromShortBranchName) {
82 copyIssueAttributes(raw, base);
83 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_SHORT_BRANCH, fromShortBranchName, analysisMetadataHolder.getBranch().get().getName());
86 private void copyIssueAttributes(DefaultIssue to, DefaultIssue from) {
89 if (from.manualSeverity()) {
90 to.setManualSeverity(true);
91 to.setSeverity(from.severity());
93 copyChanges(to, from);
96 private static void copyChanges(DefaultIssue raw, DefaultIssue base) {
97 base.comments().forEach(c -> raw.addComment(copy(raw.key(), c)));
98 base.changes().forEach(c -> copy(raw.key(), c).ifPresent(raw::addChange));
102 * Copy a comment from another issue
104 private static DefaultIssueComment copy(String issueKey, IssueComment c) {
105 DefaultIssueComment comment = new DefaultIssueComment();
106 comment.setIssueKey(issueKey);
107 comment.setKey(Uuids.create());
108 comment.setUserLogin(c.userLogin());
109 comment.setMarkdownText(c.markdownText());
110 comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
111 comment.setNew(true);
116 * Copy a diff from another issue
118 private static Optional<FieldDiffs> copy(String issueKey, FieldDiffs c) {
119 FieldDiffs result = new FieldDiffs();
120 result.setIssueKey(issueKey);
121 result.setUserLogin(c.userLogin());
122 result.setCreationDate(c.creationDate());
123 // Don't copy "file" changelogs as they refer to file uuids that might later be purged
124 c.diffs().entrySet().stream().filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
125 .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
126 if (result.diffs().isEmpty()) {
127 return Optional.empty();
129 return Optional.of(result);
132 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
133 raw.setKey(base.key());
135 copyFields(raw, base);
137 if (base.manualSeverity()) {
138 raw.setManualSeverity(true);
139 raw.setSeverity(base.severity());
141 updater.setPastSeverity(raw, base.severity(), changeContext);
143 // set component/module related fields from base in case current component has been moved
144 // (in which case base issue belongs to original file and raw issue to component)
145 raw.setComponentUuid(base.componentUuid());
146 raw.setComponentKey(base.componentKey());
147 raw.setModuleUuid(base.moduleUuid());
148 raw.setModuleUuidPath(base.moduleUuidPath());
150 // fields coming from raw
151 updater.setPastLine(raw, base.getLine());
152 updater.setPastLocations(raw, base.getLocations());
153 updater.setPastMessage(raw, base.getMessage(), changeContext);
154 updater.setPastGap(raw, base.gap(), changeContext);
155 updater.setPastEffort(raw, base.effort(), changeContext);
158 public void doAutomaticTransition(DefaultIssue issue) {
159 workflow.doAutomaticTransition(issue, changeContext);
162 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
163 toIssue.setType(fromIssue.type());
164 toIssue.setCreationDate(fromIssue.creationDate());
165 toIssue.setUpdateDate(fromIssue.updateDate());
166 toIssue.setCloseDate(fromIssue.closeDate());
167 toIssue.setResolution(fromIssue.resolution());
168 toIssue.setStatus(fromIssue.status());
169 toIssue.setAssignee(fromIssue.assignee());
170 toIssue.setAuthorLogin(fromIssue.authorLogin());
171 toIssue.setTags(fromIssue.tags());
172 toIssue.setAttributes(fromIssue.attributes());
173 toIssue.setEffort(debtCalculator.calculate(toIssue));
174 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
175 toIssue.setSelectedAt(fromIssue.selectedAt());