]> source.dussan.org Git - sonarqube.git/blob
b395611cdd9fa36726f1cd72e8b6179fe7725609
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.issue;
21
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;
36
37 import static java.util.Objects.requireNonNull;
38
39 /**
40  * Sets the appropriate fields when an issue is :
41  * <ul>
42  * <li>newly created</li>
43  * <li>merged the related base issue</li>
44  * <li>relocated (only manual issues)</li>
45  * </ul>
46  */
47 public class IssueLifecycle {
48
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;
55
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);
59   }
60
61   @VisibleForTesting
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;
70   }
71
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));
79     setType(issue, rule);
80     setStatus(issue, rule);
81   }
82
83   private void setType(DefaultIssue issue, Rule rule) {
84     if (issue.isFromExternalRuleEngine()) {
85       return;
86     }
87     issue.setType(requireNonNull(rule.getType(), "No rule type"));
88   }
89
90   private void setStatus(DefaultIssue issue, Rule rule) {
91     if (rule.getType() == RuleType.SECURITY_HOTSPOT || issue.type() == RuleType.SECURITY_HOTSPOT) {
92       issue.setStatus(Issue.STATUS_TO_REVIEW);
93     } else {
94       issue.setStatus(Issue.STATUS_OPEN);
95     }
96   }
97
98   public void copyExistingOpenIssueFromBranch(DefaultIssue raw, DefaultIssue base, String branchName) {
99     raw.setKey(Uuids.create());
100     raw.setNew(false);
101     copyAttributesOfIssueFromAnotherBranch(raw, base);
102     raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, branchName, analysisMetadataHolder.getBranch().getName());
103   }
104
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);
110   }
111
112   public void copyExistingIssueFromSourceBranchToPullRequest(DefaultIssue raw, DefaultIssue base) {
113     Preconditions.checkState(analysisMetadataHolder.isPullRequest(), "This operation should be done only on pull request analysis");
114     copyAttributesOfIssueFromAnotherBranch(raw, base);
115     String from = analysisMetadataHolder.getBranch().getName();
116     String to = "#" + analysisMetadataHolder.getPullRequestKey();
117     raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_BRANCH, from, to);
118   }
119
120   public void copyAttributesOfIssueFromAnotherBranch(DefaultIssue to, DefaultIssue from) {
121     to.setCopied(true);
122     copyFields(to, from);
123     if (from.manualSeverity()) {
124       to.setManualSeverity(true);
125       to.setSeverity(from.severity());
126     }
127     copyChangesOfIssueFromOtherBranch(to, from);
128   }
129
130   private static void copyChangesOfIssueFromOtherBranch(DefaultIssue raw, DefaultIssue base) {
131     base.defaultIssueComments().forEach(c -> raw.addComment(copyComment(raw.key(), c)));
132     base.changes().forEach(c -> copyFieldDiffOfIssueFromOtherBranch(raw.key(), c).ifPresent(raw::addChange));
133   }
134
135   /**
136    * Copy a comment from another issue
137    */
138   private static DefaultIssueComment copyComment(String issueKey, DefaultIssueComment c) {
139     DefaultIssueComment comment = new DefaultIssueComment();
140     comment.setIssueKey(issueKey);
141     comment.setKey(Uuids.create());
142     comment.setUserUuid(c.userUuid());
143     comment.setMarkdownText(c.markdownText());
144     comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
145     comment.setNew(true);
146     return comment;
147   }
148
149   /**
150    * Copy a diff from another issue
151    */
152   private static Optional<FieldDiffs> copyFieldDiffOfIssueFromOtherBranch(String issueKey, FieldDiffs c) {
153     FieldDiffs result = new FieldDiffs();
154     result.setIssueKey(issueKey);
155     result.setUserUuid(c.userUuid());
156     result.setCreationDate(c.creationDate());
157     // Don't copy "file" changelogs as they refer to file uuids that might later be purged
158     c.diffs().entrySet().stream()
159       .filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
160       .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
161     if (result.diffs().isEmpty()) {
162       return Optional.empty();
163     }
164     return Optional.of(result);
165   }
166
167   public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
168     Preconditions.checkArgument(raw.isFromExternalRuleEngine() != (raw.type() == null), "At this stage issue type should be set for and only for external issues");
169     Rule rule = ruleRepository.getByKey(raw.ruleKey());
170     raw.setKey(base.key());
171     raw.setNew(false);
172     if (base.isChanged()) {
173       // In case issue was moved from module or folder to the root project
174       raw.setChanged(true);
175     }
176     setType(raw, rule);
177     copyFields(raw, base);
178     base.changes().forEach(raw::addChange);
179
180     if (base.manualSeverity()) {
181       raw.setManualSeverity(true);
182       raw.setSeverity(base.severity());
183     } else {
184       updater.setPastSeverity(raw, base.severity(), changeContext);
185     }
186     // set component/module related fields from base in case current component has been moved
187     // (in which case base issue belongs to original file and raw issue to component)
188     raw.setComponentUuid(base.componentUuid());
189     raw.setComponentKey(base.componentKey());
190     raw.setModuleUuid(base.moduleUuid());
191     raw.setModuleUuidPath(base.moduleUuidPath());
192
193     // fields coming from raw
194     updater.setPastLine(raw, base.getLine());
195     updater.setPastLocations(raw, base.getLocations());
196     updater.setPastMessage(raw, base.getMessage(), changeContext);
197     updater.setPastGap(raw, base.gap(), changeContext);
198     updater.setPastEffort(raw, base.effort(), changeContext);
199   }
200
201   public void doAutomaticTransition(DefaultIssue issue) {
202     workflow.doAutomaticTransition(issue, changeContext);
203   }
204
205   private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
206     toIssue.setType(fromIssue.type());
207     toIssue.setCreationDate(fromIssue.creationDate());
208     toIssue.setUpdateDate(fromIssue.updateDate());
209     toIssue.setCloseDate(fromIssue.closeDate());
210     toIssue.setResolution(fromIssue.resolution());
211     toIssue.setStatus(fromIssue.status());
212     toIssue.setAssigneeUuid(fromIssue.assignee());
213     toIssue.setAuthorLogin(fromIssue.authorLogin());
214     toIssue.setTags(fromIssue.tags());
215     toIssue.setAttributes(fromIssue.attributes());
216     toIssue.setEffort(debtCalculator.calculate(toIssue));
217     toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
218     toIssue.setSelectedAt(fromIssue.selectedAt());
219   }
220 }