9e783d9ed4c3e9aa4604b4bc72c26eadabed2eed
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on a "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.onboarding.pmd;
18
19 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.getStateFile;
20 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.isReportEmpty;
21 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.readCurrentPMDState;
22 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.writeCurrentPMDState;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.nio.file.Files;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.plugins.annotations.ResolutionScope;
39 import org.apache.maven.project.MavenProject;
40
41 @Mojo(name = "post-verify-helper", threadSafe = true, defaultPhase = LifecyclePhase.VERIFY,
42         requiresDependencyResolution = ResolutionScope.NONE)
43 public class VerifyHelperMojo extends AbstractMojo {
44
45     private static final String SKIP_PMD = "skipPMD";
46
47     @Parameter(defaultValue = "${project}", readonly = true)
48     private MavenProject project;
49     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
50     private String moduleCoordinates;
51     @Parameter(defaultValue = "${session}")
52     private MavenSession session;
53     @Parameter
54     private File pmdTargetLocation;
55     @Parameter
56     private File pmdReportFile;
57     @Parameter
58     private File pmdStateFile;
59     @Parameter
60     private String pmdCurrentStateFilePath;
61     @Parameter
62     private String excludePackaging;
63     @Parameter
64     private Boolean validatePMDReport = Boolean.FALSE;
65     @Parameter
66     private String persistingModuleCoordinates;
67     @Parameter
68     private File pmdFailureReportLocation;
69     @Parameter
70     private File compiledFilesList;
71     @Parameter
72     private File compiledTestFilesList;
73
74     private static File pmdCurrentStateFile;
75
76     public void execute() throws MojoExecutionException, MojoFailureException {
77         if (project.getPackaging().equals(excludePackaging)) {
78             return;
79         }
80         init();
81         warnDataIssuesIfAny();
82
83         if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(
84                 pmdReportFile)) {
85             Map<String, List<Violation>> data = readCurrentPMDState(pmdCurrentStateFile);
86             Map<String, List<Violation>> cv = readCurrentModulePMDReport();
87             data.putAll(cv);
88             boolean error = false;
89             if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils
90                                                                    .evaluateCodeQuality(PMDState.getHistoricState(), cv,
91                                                                            pmdFailureReportLocation, getLog())) {
92                 error = true;
93                 if (validatePMDReport) {
94                     throw new MojoFailureException(
95                             "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation
96                                                                                                    .getAbsolutePath());
97                 } else {
98                     getLog().error(
99                             "\u001B[31m\u001B[1m Code Quality concerns raised by Quality Management System. For details refer "
100                                     + pmdFailureReportLocation.getAbsolutePath()
101                                     + " and address them before committing this code in Version Control System. \u001B[0m");
102                 }
103             }
104             String moduleChecksum = project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
105                                                                                                        .getProperty(
106                                                                                                                "testChecksum");
107             data = reinitializeIfNeeded(!error, data);
108
109             Map<String, Object> checksumStore = HashMap.class.cast(data);
110             if (!moduleChecksum.equals(checksumStore.get(moduleCoordinates))) {
111                 checksumStore.put(moduleCoordinates, moduleChecksum);
112                 writeCurrentPMDState(pmdCurrentStateFile, data);
113             }
114         }
115         if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) {
116             if (isReportEmpty(pmdReportFile)) {
117                 HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile));
118                 data.put(moduleCoordinates,
119                         project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties().getProperty(
120                                 "testChecksum"));
121                 writeCurrentPMDState(pmdCurrentStateFile, data);
122             }
123             pmdReportFile.delete();
124         }
125         removeProcessFiles();
126
127     }
128
129     private void removeProcessFiles() {
130         if (moduleCoordinates.equals(persistingModuleCoordinates) && pmdStateFile.exists()) {
131             for (File file : pmdStateFile.getParentFile().listFiles()) {
132                 if (file.isFile()) {
133                     file.delete();
134                 }
135             }
136         }
137         if (pmdTargetLocation.exists()) {
138             pmdTargetLocation.delete();
139         }
140     }
141
142     private void init() {
143         if (pmdCurrentStateFile == null) {
144             setPmdCurrentStateFile(
145                     getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
146                             pmdCurrentStateFilePath));
147
148             pmdReportFile.getParentFile().mkdirs();
149         }
150     }
151
152     private static void setPmdCurrentStateFile(File file) {
153         pmdCurrentStateFile = file;
154         pmdCurrentStateFile.getParentFile().mkdirs();
155     }
156
157     private Map<String, List<Violation>> readCurrentModulePMDReport() {
158         try {
159             PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
160             if (pmdReportFile.exists()) {
161                 List<String> lines = Files.readAllLines(pmdReportFile.toPath());
162                 lines.remove(0);
163                 for (String line : lines) {
164                     PMDState.addViolation(line, moduleCoordinates);
165                 }
166             }
167         } catch (IOException ioe) {
168             throw new UncheckedIOException(ioe);
169         }
170         return PMDState.getState();
171     }
172
173     private void warnDataIssuesIfAny() {
174         if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
175             getLog().error("PMD Check is skipped. problem while loading data.");
176         }
177     }
178
179     private Map<String, List<Violation>> reinitializeIfNeeded(boolean required, Map<String, List<Violation>> orig) {
180         if (required) {
181             return readCurrentPMDState(pmdCurrentStateFile);
182         } else {
183             return orig;
184         }
185     }
186
187 }