3 * Copyright (C) 2009-2019 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.db.component.KeyType;
35 import org.sonar.server.issue.IssueFieldsSetter;
36 import org.sonar.server.issue.workflow.IssueWorkflow;
39 * Sets the appropriate fields when an issue is :
41 * <li>newly created</li>
42 * <li>merged the related base issue</li>
43 * <li>relocated (only manual issues)</li>
46 public class IssueLifecycle {
48 private final IssueWorkflow workflow;
49 private final IssueChangeContext changeContext;
50 private final RuleRepository ruleRepository;
51 private final IssueFieldsSetter updater;
52 private final DebtCalculator debtCalculator;
53 private final AnalysisMetadataHolder analysisMetadataHolder;
55 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator,
56 RuleRepository ruleRepository) {
57 this(analysisMetadataHolder, IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator, ruleRepository);
61 IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater,
62 DebtCalculator debtCalculator, RuleRepository ruleRepository) {
63 this.analysisMetadataHolder = analysisMetadataHolder;
64 this.workflow = workflow;
65 this.updater = updater;
66 this.debtCalculator = debtCalculator;
67 this.changeContext = changeContext;
68 this.ruleRepository = ruleRepository;
71 public void initNewOpenIssue(DefaultIssue issue) {
72 Preconditions.checkArgument(issue.isFromExternalRuleEngine() != (issue.type() == null), "At this stage issue type should be set for and only for external issues");
73 issue.setKey(Uuids.create());
74 issue.setCreationDate(changeContext.date());
75 issue.setUpdateDate(changeContext.date());
76 issue.setStatus(Issue.STATUS_OPEN);
77 issue.setEffort(debtCalculator.calculate(issue));
81 private void setType(DefaultIssue issue) {
82 if (!issue.isFromExternalRuleEngine()) {
83 Rule rule = ruleRepository.getByKey(issue.ruleKey());
84 issue.setType(rule.getType());
86 issue.setIsFromHotspot(issue.type() == RuleType.SECURITY_HOTSPOT);
89 public void copyExistingOpenIssueFromLongLivingBranch(DefaultIssue raw, DefaultIssue base, String fromLongBranchName) {
90 raw.setKey(Uuids.create());
92 copyAttributesOfIssueFromOtherBranch(raw, base);
93 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_LONG_BRANCH, fromLongBranchName, analysisMetadataHolder.getBranch().getName());
96 public void mergeConfirmedOrResolvedFromShortLivingBranchOrPr(DefaultIssue raw, DefaultIssue base, KeyType branchType, String fromShortBranchNameOrPR) {
97 copyAttributesOfIssueFromOtherBranch(raw, base);
98 String from = (branchType == KeyType.PULL_REQUEST ? "#" : "") + fromShortBranchNameOrPR;
99 String to = analysisMetadataHolder.isPullRequest() ? ("#" + analysisMetadataHolder.getPullRequestKey()) : analysisMetadataHolder.getBranch().getName();
100 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_SHORT_BRANCH, from, to);
103 private void copyAttributesOfIssueFromOtherBranch(DefaultIssue to, DefaultIssue from) {
105 copyFields(to, from);
106 if (from.manualSeverity()) {
107 to.setManualSeverity(true);
108 to.setSeverity(from.severity());
110 copyChangesOfIssueFromOtherBranch(to, from);
113 private static void copyChangesOfIssueFromOtherBranch(DefaultIssue raw, DefaultIssue base) {
114 base.defaultIssueComments().forEach(c -> raw.addComment(copyComment(raw.key(), c)));
115 base.changes().forEach(c -> copyFieldDiffOfIssueFromOtherBranch(raw.key(), c).ifPresent(raw::addChange));
119 * Copy a comment from another issue
121 private static DefaultIssueComment copyComment(String issueKey, DefaultIssueComment c) {
122 DefaultIssueComment comment = new DefaultIssueComment();
123 comment.setIssueKey(issueKey);
124 comment.setKey(Uuids.create());
125 comment.setUserUuid(c.userUuid());
126 comment.setMarkdownText(c.markdownText());
127 comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
128 comment.setNew(true);
133 * Copy a diff from another issue
135 private static Optional<FieldDiffs> copyFieldDiffOfIssueFromOtherBranch(String issueKey, FieldDiffs c) {
136 FieldDiffs result = new FieldDiffs();
137 result.setIssueKey(issueKey);
138 result.setUserUuid(c.userUuid());
139 result.setCreationDate(c.creationDate());
140 // Don't copy "file" changelogs as they refer to file uuids that might later be purged
141 c.diffs().entrySet().stream().filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
142 .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
143 if (result.diffs().isEmpty()) {
144 return Optional.empty();
146 return Optional.of(result);
149 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
150 Preconditions.checkArgument(raw.isFromExternalRuleEngine() != (raw.type() == null), "At this stage issue type should be set for and only for external issues");
151 raw.setKey(base.key());
153 if (base.isChanged()) {
154 // In case issue was moved from module or folder to the root project
155 raw.setChanged(true);
158 copyFields(raw, base);
159 base.changes().forEach(raw::addChange);
160 if (raw.isFromHotspot() != base.isFromHotspot()) {
161 // This is to force DB update of the issue
162 raw.setChanged(true);
164 if (raw.isFromHotspot() && !base.isFromHotspot()) {
165 // First analysis after rule type was changed to security_hotspot. Issue will be reset to an open hotspot
166 updater.setType(raw, RuleType.SECURITY_HOTSPOT, changeContext);
167 updater.setStatus(raw, Issue.STATUS_REOPENED, changeContext);
168 updater.setResolution(raw, null, changeContext);
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());