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