]> source.dussan.org Git - sonarqube.git/blob
7f88d3404e0819737513b1ab75c88b1dc718bb48
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.Date;
24 import org.sonar.api.issue.Issue;
25 import org.sonar.core.issue.DefaultIssue;
26 import org.sonar.core.issue.DefaultIssueComment;
27 import org.sonar.core.issue.FieldDiffs;
28 import org.sonar.core.issue.IssueChangeContext;
29 import org.sonar.core.util.Uuids;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.issue.IssueFieldsSetter;
32 import org.sonar.server.issue.workflow.IssueWorkflow;
33
34 /**
35  * Sets the appropriate fields when an issue is :
36  * <ul>
37  *   <li>newly created</li>
38  *   <li>merged the related base issue</li>
39  *   <li>relocated (only manual issues)</li>
40  * </ul>
41  */
42 public class IssueLifecycle {
43
44   private final IssueWorkflow workflow;
45   private final IssueChangeContext changeContext;
46   private final IssueFieldsSetter updater;
47   private final DebtCalculator debtCalculator;
48
49   public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
50     this(IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator);
51   }
52
53   @VisibleForTesting
54   IssueLifecycle(IssueChangeContext changeContext, IssueWorkflow workflow, IssueFieldsSetter updater, DebtCalculator debtCalculator) {
55     this.workflow = workflow;
56     this.updater = updater;
57     this.debtCalculator = debtCalculator;
58     this.changeContext = changeContext;
59   }
60
61   public void initNewOpenIssue(DefaultIssue issue) {
62     issue.setKey(Uuids.create());
63     issue.setCreationDate(changeContext.date());
64     issue.setUpdateDate(changeContext.date());
65     issue.setStatus(Issue.STATUS_OPEN);
66     issue.setEffort(debtCalculator.calculate(issue));
67   }
68
69   public void copyExistingOpenIssueFromLongLivingBranch(DefaultIssue raw, DefaultIssue base) {
70     raw.setKey(Uuids.create());
71     raw.setNew(false);
72     raw.setCopied(true);
73     copyFields(raw, base);
74
75     if (base.manualSeverity()) {
76       raw.setManualSeverity(true);
77       raw.setSeverity(base.severity());
78     }
79   }
80
81   public void mergeIssueFromShortLivingBranch(DefaultIssue raw, DefaultIssue fromShortLiving) {
82     raw.setCopied(true);
83     raw.setType(fromShortLiving.type());
84     raw.setResolution(fromShortLiving.resolution());
85     raw.setStatus(fromShortLiving.status());
86     raw.setAssignee(fromShortLiving.assignee());
87     raw.setAuthorLogin(fromShortLiving.authorLogin());
88     raw.setTags(fromShortLiving.tags());
89     raw.setAttributes(fromShortLiving.attributes());
90     if (fromShortLiving.manualSeverity()) {
91       raw.setManualSeverity(true);
92       raw.setSeverity(fromShortLiving.severity());
93     }
94     fromShortLiving.comments().forEach(c -> raw.addComment(DefaultIssueComment.copy(raw.key(), c)));
95     fromShortLiving.changes().forEach(c -> raw.addChange(FieldDiffs.copy(raw.key(), c)));
96   }
97
98   public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
99     raw.setKey(base.key());
100     raw.setNew(false);
101     copyFields(raw, base);
102
103     if (base.manualSeverity()) {
104       raw.setManualSeverity(true);
105       raw.setSeverity(base.severity());
106     } else {
107       updater.setPastSeverity(raw, base.severity(), changeContext);
108     }
109     // set component/module related fields from base in case current component has been moved
110     // (in which case base issue belongs to original file and raw issue to component)
111     raw.setComponentUuid(base.componentUuid());
112     raw.setComponentKey(base.componentKey());
113     raw.setModuleUuid(base.moduleUuid());
114     raw.setModuleUuidPath(base.moduleUuidPath());
115
116     // fields coming from raw
117     updater.setPastLine(raw, base.getLine());
118     updater.setPastLocations(raw, base.getLocations());
119     updater.setPastMessage(raw, base.getMessage(), changeContext);
120     updater.setPastGap(raw, base.gap(), changeContext);
121     updater.setPastEffort(raw, base.effort(), changeContext);
122   }
123
124   public void doAutomaticTransition(DefaultIssue issue) {
125     workflow.doAutomaticTransition(issue, changeContext);
126   }
127
128   private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) {
129     toIssue.setType(fromIssue.type());
130     toIssue.setCreationDate(fromIssue.creationDate());
131     toIssue.setUpdateDate(fromIssue.updateDate());
132     toIssue.setCloseDate(fromIssue.closeDate());
133     toIssue.setResolution(fromIssue.resolution());
134     toIssue.setStatus(fromIssue.status());
135     toIssue.setAssignee(fromIssue.assignee());
136     toIssue.setAuthorLogin(fromIssue.authorLogin());
137     toIssue.setTags(fromIssue.tags());
138     toIssue.setAttributes(fromIssue.attributes());
139     toIssue.setEffort(debtCalculator.calculate(toIssue));
140     toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule());
141     toIssue.setSelectedAt(fromIssue.selectedAt());
142   }
143 }