3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.issue;
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.base.Preconditions;
24 import java.util.Date;
25 import java.util.Optional;
26 import org.sonar.api.issue.Issue;
27 import org.sonar.api.rules.RuleType;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.DefaultIssueComment;
31 import org.sonar.core.issue.FieldDiffs;
32 import org.sonar.core.issue.IssueChangeContext;
33 import org.sonar.core.util.Uuids;
34 import org.sonar.server.issue.IssueFieldsSetter;
35 import org.sonar.server.issue.workflow.IssueWorkflow;
37 import static java.util.Objects.requireNonNull;
40 * Sets the appropriate fields when an issue is :
42 * <li>newly created</li>
43 * <li>merged the related base issue</li>
44 * <li>relocated (only manual issues)</li>
47 public class IssueLifecycle {
49 private final IssueWorkflow workflow;
50 private final IssueChangeContext changeContext;
51 private final RuleRepository ruleRepository;
52 private final IssueFieldsSetter updater;
53 private final DebtCalculator debtCalculator;
54 private final AnalysisMetadataHolder analysisMetadataHolder;
56 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator,
57 RuleRepository ruleRepository) {
58 this(analysisMetadataHolder, IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator, ruleRepository);
62 IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater,
63 DebtCalculator debtCalculator, RuleRepository ruleRepository) {
64 this.analysisMetadataHolder = analysisMetadataHolder;
65 this.workflow = workflow;
66 this.updater = updater;
67 this.debtCalculator = debtCalculator;
68 this.changeContext = changeContext;
69 this.ruleRepository = ruleRepository;
72 public void initNewOpenIssue(DefaultIssue issue) {
73 Preconditions.checkArgument(issue.isFromExternalRuleEngine() != (issue.type() == null), "At this stage issue type should be set for and only for external issues");
74 Rule rule = ruleRepository.getByKey(issue.ruleKey());
75 issue.setKey(Uuids.create());
76 issue.setCreationDate(changeContext.date());
77 issue.setUpdateDate(changeContext.date());
78 issue.setEffort(debtCalculator.calculate(issue));
80 setStatus(issue, rule);
83 private void setType(DefaultIssue issue, Rule rule) {
84 if (issue.isFromExternalRuleEngine()) {
87 issue.setType(requireNonNull(rule.getType(), "No rule type"));
90 private void setStatus(DefaultIssue issue, Rule rule) {
91 if (issue.isFromExternalRuleEngine() || rule.getType() != RuleType.SECURITY_HOTSPOT) {
92 issue.setStatus(Issue.STATUS_OPEN);
94 issue.setStatus(Issue.STATUS_TO_REVIEW);
98 public void copyExistingOpenIssueFromBranch(DefaultIssue raw, DefaultIssue base, String branchName) {
99 raw.setKey(Uuids.create());
101 copyAttributesOfIssueFromAnotherBranch(raw, base);
102 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, branchName, analysisMetadataHolder.getBranch().getName());
105 public void mergeConfirmedOrResolvedFromPr(DefaultIssue raw, DefaultIssue base, String pr) {
106 copyAttributesOfIssueFromAnotherBranch(raw, base);
107 String from = "#" + pr;
108 String to = analysisMetadataHolder.isPullRequest() ? ("#" + analysisMetadataHolder.getPullRequestKey()) : analysisMetadataHolder.getBranch().getName();
109 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, from, to);
112 private void copyAttributesOfIssueFromAnotherBranch(DefaultIssue to, DefaultIssue from) {
114 copyFields(to, from);
115 if (from.manualSeverity()) {
116 to.setManualSeverity(true);
117 to.setSeverity(from.severity());
119 copyChangesOfIssueFromOtherBranch(to, from);
122 private static void copyChangesOfIssueFromOtherBranch(DefaultIssue raw, DefaultIssue base) {
123 base.defaultIssueComments().forEach(c -> raw.addComment(copyComment(raw.key(), c)));
124 base.changes().forEach(c -> copyFieldDiffOfIssueFromOtherBranch(raw.key(), c).ifPresent(raw::addChange));
128 * Copy a comment from another issue
130 private static DefaultIssueComment copyComment(String issueKey, DefaultIssueComment c) {
131 DefaultIssueComment comment = new DefaultIssueComment();
132 comment.setIssueKey(issueKey);
133 comment.setKey(Uuids.create());
134 comment.setUserUuid(c.userUuid());
135 comment.setMarkdownText(c.markdownText());
136 comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
137 comment.setNew(true);
142 * Copy a diff from another issue
144 private static Optional<FieldDiffs> copyFieldDiffOfIssueFromOtherBranch(String issueKey, FieldDiffs c) {
145 FieldDiffs result = new FieldDiffs();
146 result.setIssueKey(issueKey);
147 result.setUserUuid(c.userUuid());
148 result.setCreationDate(c.creationDate());
149 // Don't copy "file" changelogs as they refer to file uuids that might later be purged
150 c.diffs().entrySet().stream().filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
151 .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
152 if (result.diffs().isEmpty()) {
153 return Optional.empty();
155 return Optional.of(result);
158 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
159 Preconditions.checkArgument(raw.isFromExternalRuleEngine() != (raw.type() == null), "At this stage issue type should be set for and only for external issues");
160 Rule rule = ruleRepository.getByKey(raw.ruleKey());
161 raw.setKey(base.key());
163 if (base.isChanged()) {
164 // In case issue was moved from module or folder to the root project
165 raw.setChanged(true);
168 copyFields(raw, base);
169 base.changes().forEach(raw::addChange);
171 if (base.manualSeverity()) {
172 raw.setManualSeverity(true);
173 raw.setSeverity(base.severity());
175 updater.setPastSeverity(raw, base.severity(), changeContext);
177 // set component/module related fields from base in case current component has been moved
178 // (in which case base issue belongs to original file and raw issue to component)
179 raw.setComponentUuid(base.componentUuid());
180 raw.setComponentKey(base.componentKey());
181 raw.setModuleUuid(base.moduleUuid());
182 raw.setModuleUuidPath(base.moduleUuidPath());
184 // fields coming from raw
185 updater.setPastLine(raw, base.getLine());
186 updater.setPastLocations(raw, base.getLocations());
187 updater.setPastMessage(raw, base.getMessage(), changeContext);
188 updater.setPastGap(raw, base.gap(), changeContext);
189 updater.setPastEffort(raw, base.effort(), changeContext);
192 public void doAutomaticTransition(DefaultIssue issue) {
193 workflow.doAutomaticTransition(issue, changeContext);
196 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
197 toIssue.setType(fromIssue.type());
198 toIssue.setCreationDate(fromIssue.creationDate());
199 toIssue.setUpdateDate(fromIssue.updateDate());
200 toIssue.setCloseDate(fromIssue.closeDate());
201 toIssue.setResolution(fromIssue.resolution());
202 toIssue.setStatus(fromIssue.status());
203 toIssue.setAssigneeUuid(fromIssue.assignee());
204 toIssue.setAuthorLogin(fromIssue.authorLogin());
205 toIssue.setTags(fromIssue.tags());
206 toIssue.setAttributes(fromIssue.attributes());
207 toIssue.setEffort(debtCalculator.calculate(toIssue));
208 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
209 toIssue.setSelectedAt(fromIssue.selectedAt());