c80951ff50118f9ef4658d01b5e53cfde0ae7eb4
[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             pmdStateFile.delete();
132         }
133         if (pmdTargetLocation.exists()) {
134             pmdTargetLocation.delete();
135         }
136     }
137
138     private void init() {
139         if (pmdCurrentStateFile == null) {
140             setPmdCurrentStateFile(
141                     getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
142                             pmdCurrentStateFilePath));
143
144             pmdReportFile.getParentFile().mkdirs();
145         }
146     }
147
148     private static void setPmdCurrentStateFile(File file) {
149         pmdCurrentStateFile = file;
150         pmdCurrentStateFile.getParentFile().mkdirs();
151     }
152
153     private Map<String, List<Violation>> readCurrentModulePMDReport() {
154         try {
155             PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
156             if (pmdReportFile.exists()) {
157                 List<String> lines = Files.readAllLines(pmdReportFile.toPath());
158                 lines.remove(0);
159                 for (String line : lines) {
160                     PMDState.addViolation(line, moduleCoordinates);
161                 }
162             }
163         } catch (IOException ioe) {
164             throw new UncheckedIOException(ioe);
165         }
166         return PMDState.getState();
167     }
168
169     private void warnDataIssuesIfAny() {
170         if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
171             getLog().error("PMD Check is skipped. problem while loading data.");
172         }
173     }
174
175     private Map<String, List<Violation>> reinitializeIfNeeded(boolean required, Map<String, List<Violation>> orig) {
176         if (required) {
177             return readCurrentPMDState(pmdCurrentStateFile);
178         } else {
179             return orig;
180         }
181     }
182
183 }