87c9ca5f1f154721304950cf7404efb669369c1b
[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.readCurrentPMDState;
21 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.writeCurrentPMDState;
22 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.isReportEmpty;
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 java.util.stream.Collectors;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.MojoFailureException;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.plugins.annotations.ResolutionScope;
40 import org.apache.maven.project.MavenProject;
41
42 @Mojo(name = "post-verify-helper", threadSafe = true, defaultPhase = LifecyclePhase.VERIFY,
43         requiresDependencyResolution = ResolutionScope.NONE)
44 public class VerifyHelperMojo extends AbstractMojo {
45
46     private static final String SKIP_PMD = "skipPMD";
47
48     @Parameter(defaultValue = "${project}", readonly = true)
49     private MavenProject project;
50     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
51     private String moduleCoordinates;
52     @Parameter(defaultValue = "${session}")
53     private MavenSession session;
54     @Parameter
55     private File pmdTargetLocation;
56     @Parameter
57     private File pmdReportFile;
58     @Parameter
59     private File pmdStateFile;
60     @Parameter
61     private String pmdCurrentStateFilePath;
62     @Parameter
63     private String excludePackaging;
64     @Parameter
65     private Boolean validatePMDReport = Boolean.FALSE;
66     @Parameter
67     private String persistingModuleCoordinates;
68     @Parameter
69     private File pmdFailureReportLocation;
70     @Parameter
71     private File compiledFilesList;
72     @Parameter
73     private File compiledTestFilesList;
74
75     private static File pmdCurrentStateFile;
76
77     public void execute() throws MojoExecutionException, MojoFailureException {
78         if (project.getPackaging().equals(excludePackaging)) {
79             return;
80         }
81         if (moduleCoordinates.equals(persistingModuleCoordinates)) {
82             if (pmdStateFile.exists()) {
83                 pmdStateFile.delete();
84             }
85         }
86         if (pmdCurrentStateFile == null) {
87             pmdCurrentStateFile =
88                     getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
89                             pmdCurrentStateFilePath);
90             pmdCurrentStateFile.getParentFile().mkdirs();
91             pmdReportFile.getParentFile().mkdirs();
92         }
93         if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
94             getLog().error("PMD Check is skipped. problem while loading data.");
95         }
96         if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(pmdReportFile)) {
97             Map<String, List<Violation>> data = readCurrentPMDState(pmdCurrentStateFile);
98             Map<String, List<Violation>> cv = readCurrentModulePMDReport();
99             data.putAll(cv);
100             if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils
101                                                                    .evaluateCodeQuality(PMDState.getHistoricState(), cv,
102                                                                            pmdFailureReportLocation, getLog())) {
103                 if (validatePMDReport) {
104                     throw new MojoFailureException(
105                             "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation
106                                                                                                    .getAbsolutePath());
107                 } else {
108                     getLog().error(
109                             "\u001B[31m\u001B[1m Code Quality concerns raised by Quality Management System. For details refer "
110                                     + pmdFailureReportLocation.getAbsolutePath()
111                                     + " and address them before committing this code in Version Control System. \u001B[0m");
112                 }
113             }
114             Map<String, Object> checksumStore = HashMap.class.cast(data);
115             checksumStore.put(moduleCoordinates,
116                     project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
117                                                                                        .getProperty("testChecksum"));
118             writeCurrentPMDState(pmdCurrentStateFile, data);
119         }
120         if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) {
121             if (isReportEmpty(pmdReportFile)){
122                 HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile));
123                 data.put(moduleCoordinates,
124                         project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
125                                                                                            .getProperty("testChecksum"));
126                 writeCurrentPMDState(pmdCurrentStateFile, data);
127             }
128             pmdReportFile.delete();
129         }
130         if (pmdTargetLocation.exists()) {
131             pmdTargetLocation.delete();
132         }
133
134     }
135
136     private Map<String, List<Violation>> readCurrentModulePMDReport() {
137         try {
138             PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
139             if (pmdReportFile.exists()) {
140                 boolean isFirst = true;
141                 for (String line : Files.lines(pmdReportFile.toPath()).collect(Collectors.toList())) {
142                     if (isFirst) {
143                         isFirst = false;
144                     } else {
145                         PMDState.addViolation(line, moduleCoordinates);
146                     }
147                 }
148             }
149         } catch (IOException ioe) {
150             throw new UncheckedIOException(ioe);
151         }
152         return PMDState.getState();
153     }
154
155 }