3 * Copyright (C) 2009-2024 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 javax.inject.Inject;
27 import org.jetbrains.annotations.NotNull;
28 import org.sonar.api.issue.Issue;
29 import org.sonar.api.rules.CleanCodeAttribute;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
32 import org.sonar.core.issue.DefaultIssue;
33 import org.sonar.core.issue.DefaultIssueComment;
34 import org.sonar.core.issue.FieldDiffs;
35 import org.sonar.core.issue.IssueChangeContext;
36 import org.sonar.core.util.Uuids;
37 import org.sonar.db.component.BranchType;
38 import org.sonar.server.issue.IssueFieldsSetter;
39 import org.sonar.server.issue.workflow.IssueWorkflow;
41 import static java.util.Objects.requireNonNull;
42 import static java.util.Optional.ofNullable;
43 import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByScanBuilder;
46 * Sets the appropriate fields when an issue is :
48 * <li>newly created</li>
49 * <li>merged the related base issue</li>
50 * <li>relocated (only manual issues)</li>
53 public class IssueLifecycle {
55 private final IssueWorkflow workflow;
56 private final IssueChangeContext changeContext;
57 private final RuleRepository ruleRepository;
58 private final IssueFieldsSetter updater;
59 private final DebtCalculator debtCalculator;
60 private final AnalysisMetadataHolder analysisMetadataHolder;
63 public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator,
64 RuleRepository ruleRepository) {
65 this(analysisMetadataHolder, issueChangeContextByScanBuilder(new Date(analysisMetadataHolder.getAnalysisDate())).build(), workflow, updater, debtCalculator, ruleRepository);
69 IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater,
70 DebtCalculator debtCalculator, RuleRepository ruleRepository) {
71 this.analysisMetadataHolder = analysisMetadataHolder;
72 this.workflow = workflow;
73 this.updater = updater;
74 this.debtCalculator = debtCalculator;
75 this.changeContext = changeContext;
76 this.ruleRepository = ruleRepository;
79 public void initNewOpenIssue(DefaultIssue issue) {
80 Preconditions.checkArgument(issue.isFromExternalRuleEngine() != (issue.type() == null), "At this stage issue type should be set for and only for external issues");
81 Rule rule = ruleRepository.getByKey(issue.ruleKey());
82 issue.setKey(Uuids.create());
83 issue.setCreationDate(changeContext.date());
84 issue.setUpdateDate(changeContext.date());
85 issue.setEffort(debtCalculator.calculate(issue));
87 setStatus(issue, rule);
88 setCleanCodeAttribute(issue, rule);
91 private static void setCleanCodeAttribute(DefaultIssue issue, Rule rule) {
92 issue.setCleanCodeAttribute(ofNullable(rule.cleanCodeAttribute()).orElse(CleanCodeAttribute.defaultCleanCodeAttribute()));
95 private static void setType(DefaultIssue issue, Rule rule) {
96 if (issue.isFromExternalRuleEngine()) {
99 issue.setType(requireNonNull(rule.getType(), "No rule type"));
102 private static void setStatus(DefaultIssue issue, Rule rule) {
103 if (rule.getType() == RuleType.SECURITY_HOTSPOT || issue.type() == RuleType.SECURITY_HOTSPOT) {
104 issue.setStatus(Issue.STATUS_TO_REVIEW);
106 issue.setStatus(Issue.STATUS_OPEN);
110 public void copyExistingOpenIssueFromBranch(DefaultIssue raw, DefaultIssue base, String branchName) {
111 raw.setKey(Uuids.create());
113 copyAttributesOfIssueFromAnotherBranch(raw, base);
114 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, branchName, analysisMetadataHolder.getBranch().getName());
117 public void mergeConfirmedOrResolvedFromPrOrBranch(DefaultIssue raw, DefaultIssue base, BranchType branchType, String prOrBranchKey) {
118 copyAttributesOfIssueFromAnotherBranch(raw, base);
119 String from = (branchType == BranchType.PULL_REQUEST) ? "#" + prOrBranchKey : prOrBranchKey;
120 String to = analysisMetadataHolder.isPullRequest() ? ("#" + analysisMetadataHolder.getPullRequestKey()) : analysisMetadataHolder.getBranch().getName();
121 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, from, to);
124 public void copyExistingIssueFromSourceBranchToPullRequest(DefaultIssue raw, DefaultIssue base) {
125 Preconditions.checkState(analysisMetadataHolder.isPullRequest(), "This operation should be done only on pull request analysis");
126 copyAttributesOfIssueFromAnotherBranch(raw, base);
127 String from = analysisMetadataHolder.getBranch().getName();
128 String to = "#" + analysisMetadataHolder.getPullRequestKey();
129 raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, from, to);
132 public void copyAttributesOfIssueFromAnotherBranch(DefaultIssue to, DefaultIssue from) {
134 copyFields(to, from);
135 if (from.manualSeverity()) {
136 to.setManualSeverity(true);
137 to.setSeverity(from.severity());
139 to.setCleanCodeAttribute(from.getCleanCodeAttribute());
140 copyChangesOfIssueFromOtherBranch(to, from);
143 private static void copyChangesOfIssueFromOtherBranch(DefaultIssue raw, DefaultIssue base) {
144 base.defaultIssueComments().forEach(c -> raw.addComment(copyComment(raw.key(), c)));
145 base.changes().forEach(c -> copyFieldDiffOfIssueFromOtherBranch(raw.key(), c).ifPresent(raw::addChange));
149 * Copy a comment from another issue
151 private static DefaultIssueComment copyComment(String issueKey, DefaultIssueComment c) {
152 DefaultIssueComment comment = new DefaultIssueComment();
153 comment.setIssueKey(issueKey);
154 comment.setKey(Uuids.create());
155 comment.setUserUuid(c.userUuid());
156 comment.setMarkdownText(c.markdownText());
157 comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
158 comment.setNew(true);
163 * Copy a diff from another issue
165 private static Optional<FieldDiffs> copyFieldDiffOfIssueFromOtherBranch(String issueKey, FieldDiffs source) {
166 FieldDiffs result = new FieldDiffs();
167 result.setIssueKey(issueKey);
168 source.userUuid().ifPresent(result::setUserUuid);
169 source.webhookSource().ifPresent(result::setWebhookSource);
170 source.externalUser().ifPresent(result::setExternalUser);
171 result.setCreationDate(source.creationDate());
172 // Don't copy "file" changelogs as they refer to file uuids that might later be purged
173 source.diffs().entrySet().stream()
174 .filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
175 .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
176 if (result.diffs().isEmpty()) {
177 return Optional.empty();
179 return Optional.of(result);
182 public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
183 Preconditions.checkArgument(raw.isFromExternalRuleEngine() != (raw.type() == null), "At this stage issue type should be set for and only for external issues");
184 Rule rule = ruleRepository.getByKey(raw.ruleKey());
185 raw.setKey(base.key());
187 if (base.isChanged()) {
188 // In case issue was moved from module or folder to the root project
189 raw.setChanged(true);
192 setCleanCodeAttribute(raw, rule);
193 copyFields(raw, base);
194 base.changes().forEach(raw::addChange);
196 if (base.manualSeverity()) {
197 raw.setManualSeverity(true);
198 raw.setSeverity(base.severity());
200 updater.setPastSeverity(raw, base.severity(), changeContext);
202 // set component/module related fields from base in case current component has been moved
203 // (in which case base issue belongs to original file and raw issue to component)
204 raw.setComponentUuid(base.componentUuid());
205 raw.setComponentKey(base.componentKey());
207 // fields coming from raw
208 updater.setPastLine(raw, base.getLine());
209 updater.setPastLocations(raw, base.getLocations());
210 updater.setRuleDescriptionContextKey(raw, base.getRuleDescriptionContextKey().orElse(null));
211 updater.setPastMessage(raw, base.getMessage(), base.getMessageFormattings(), changeContext);
212 updater.setPastGap(raw, base.gap(), changeContext);
213 updater.setPastEffort(raw, base.effort(), changeContext);
214 updater.setCodeVariants(raw, requireNonNull(base.codeVariants()), changeContext);
215 updater.setImpacts(raw, base.impacts(), changeContext);
216 updater.setCleanCodeAttribute(raw, base.getCleanCodeAttribute(), changeContext);
217 updater.setPrioritizedRule(raw, base.isPrioritizedRule(), changeContext);
220 public void doAutomaticTransition(DefaultIssue issue) {
221 workflow.doAutomaticTransition(issue, changeContext);
224 public void doManualTransition(DefaultIssue issue, String transitionKey, String userUuid) {
225 workflow.doManualTransition(issue, transitionKey, getIssueChangeContextWithUser(userUuid));
228 public void addComment(DefaultIssue issue, String comment, String userUuid) {
229 updater.addComment(issue, comment, getIssueChangeContextWithUser(userUuid));
233 private IssueChangeContext getIssueChangeContextWithUser(String userUuid) {
234 return IssueChangeContext.newBuilder()
235 .setDate(changeContext.date())
236 .setWebhookSource(changeContext.getWebhookSource())
237 .setUserUuid(userUuid).build();
240 private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
241 toIssue.setType(fromIssue.type());
242 toIssue.setCreationDate(fromIssue.creationDate());
243 toIssue.setUpdateDate(fromIssue.updateDate());
244 toIssue.setCloseDate(fromIssue.closeDate());
245 toIssue.setResolution(fromIssue.resolution());
246 toIssue.setStatus(fromIssue.status());
247 toIssue.setAssigneeUuid(fromIssue.assignee());
248 toIssue.setAssigneeLogin(fromIssue.assigneeLogin());
249 toIssue.setAuthorLogin(fromIssue.authorLogin());
250 toIssue.setTags(fromIssue.tags());
251 toIssue.setEffort(debtCalculator.calculate(toIssue));
252 toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
253 toIssue.setSelectedAt(fromIssue.selectedAt());
254 toIssue.setIsNewCodeReferenceIssue(fromIssue.isNewCodeReferenceIssue());
255 toIssue.setPrioritizedRule(fromIssue.isPrioritizedRule());