PageRenderTime 102ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tusarnotifier/src/main/java/com/thalesgroup/hudson/plugins/tusarnotifier/TusarNotifier.java

https://github.com/jfroche/hudson-plugins
Java | 301 lines | 223 code | 51 blank | 27 comment | 21 complexity | 3c2fb2be1f65fb6b6547d663d44a7d2f MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2010 Thales Corporate Services SAS *
  3. * Author : Gregory Boissinot, Guillaume Tanier *
  4. * *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy *
  6. * of this software and associated documentation files (the "Software"), to deal*
  7. * in the Software without restriction, including without limitation the rights *
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included in *
  13. * all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
  21. * THE SOFTWARE. *
  22. *******************************************************************************/
  23. package com.thalesgroup.hudson.plugins.tusarnotifier;
  24. import com.google.inject.AbstractModule;
  25. import com.google.inject.Guice;
  26. import com.google.inject.Singleton;
  27. import com.thalesgroup.dtkit.metrics.hudson.api.descriptor.CoverageTypeDescriptor;
  28. import com.thalesgroup.dtkit.metrics.hudson.api.descriptor.MeasureTypeDescriptor;
  29. import com.thalesgroup.dtkit.metrics.hudson.api.descriptor.TestTypeDescriptor;
  30. import com.thalesgroup.dtkit.metrics.hudson.api.descriptor.ViolationsTypeDescriptor;
  31. import com.thalesgroup.dtkit.metrics.hudson.api.type.*;
  32. import com.thalesgroup.hudson.plugins.tusarnotifier.service.TusarNotifierConversionService;
  33. import com.thalesgroup.hudson.plugins.tusarnotifier.service.TusarNotifierLog;
  34. import com.thalesgroup.hudson.plugins.tusarnotifier.service.TusarNotifierReportProcessingService;
  35. import com.thalesgroup.hudson.plugins.tusarnotifier.service.TusarNotifierValidationService;
  36. import com.thalesgroup.hudson.plugins.tusarnotifier.transformer.TusarNotifierTransformer;
  37. import com.thalesgroup.hudson.plugins.tusarnotifier.transformer.TusarToolInfo;
  38. import com.thalesgroup.hudson.plugins.tusarnotifier.util.TusarNotifierLogger;
  39. import hudson.*;
  40. import hudson.model.*;
  41. import hudson.tasks.BuildStepDescriptor;
  42. import hudson.tasks.BuildStepMonitor;
  43. import hudson.tasks.Notifier;
  44. import hudson.tasks.Publisher;
  45. import net.sf.json.JSONObject;
  46. import org.kohsuke.stapler.StaplerRequest;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.ArrayList;
  50. import java.util.List;
  51. public class TusarNotifier extends Notifier {
  52. private transient final String generatedFolder = "generatedTUSARFiles";
  53. private transient final String generatedTests = generatedFolder + "/TESTS";
  54. private transient final String generatedCoverage = generatedFolder + "/COVERAGE";
  55. private transient final String generatedMeasures = generatedFolder + "/MEASURES";
  56. private transient final String generatedViolations = generatedFolder + "/VIOLATIONS";
  57. private TestType[] tests;
  58. private CoverageType[] coverages;
  59. private ViolationsType[] violations;
  60. private MeasureType[] measures;
  61. public TusarNotifier(TestType[] tests,
  62. CoverageType[] coverages,
  63. ViolationsType[] violations,
  64. MeasureType[] measures) {
  65. this.tests = tests;
  66. this.coverages = coverages;
  67. this.violations = violations;
  68. this.measures = measures;
  69. }
  70. @SuppressWarnings("unused")
  71. public TestType[] getTests() {
  72. return tests;
  73. }
  74. @SuppressWarnings("unused")
  75. public CoverageType[] getCoverages() {
  76. return coverages;
  77. }
  78. @SuppressWarnings("unused")
  79. public ViolationsType[] getViolations() {
  80. return violations;
  81. }
  82. @SuppressWarnings("unused")
  83. public MeasureType[] getMeasures() {
  84. return measures;
  85. }
  86. public BuildStepMonitor getRequiredMonitorService() {
  87. return BuildStepMonitor.NONE;
  88. }
  89. private boolean processInputMetricType(final AbstractBuild<?, ?> build, final BuildListener listener, MetricsType metricsType, FilePath outputFileParent) throws IOException, InterruptedException {
  90. final TusarNotifierLogger tusarNotifierLog = Guice.createInjector(new AbstractModule() {
  91. @Override
  92. protected void configure() {
  93. bind(BuildListener.class).toInstance(listener);
  94. }
  95. }).getInstance(TusarNotifierLogger.class);
  96. //Retrieves the pattern
  97. String newExpandedPattern = metricsType.getPattern();
  98. newExpandedPattern = newExpandedPattern.replaceAll("[\t\r\n]+", " ");
  99. newExpandedPattern = Util.replaceMacro(newExpandedPattern, build.getEnvironment(listener));
  100. //Build a new build info
  101. final TusarToolInfo tusarToolInfo = new TusarToolInfo(metricsType, new File(outputFileParent.toURI()), newExpandedPattern, build.getTimeInMillis());
  102. // Archiving tool reports into JUnit files
  103. TusarNotifierTransformer tusarNotifierTransformer = Guice.createInjector(new AbstractModule() {
  104. @Override
  105. protected void configure() {
  106. bind(BuildListener.class).toInstance(listener);
  107. bind(TusarToolInfo.class).toInstance(tusarToolInfo);
  108. bind(TusarNotifierValidationService.class).in(Singleton.class);
  109. bind(TusarNotifierConversionService.class).in(Singleton.class);
  110. bind(TusarNotifierLog.class).in(Singleton.class);
  111. bind(TusarNotifierReportProcessingService.class).in(Singleton.class);
  112. }
  113. }).getInstance(TusarNotifierTransformer.class);
  114. boolean resultTransformation = build.getWorkspace().act(tusarNotifierTransformer);
  115. if (!resultTransformation) {
  116. build.setResult(Result.FAILURE);
  117. tusarNotifierLog.info("Stopping recording.");
  118. return false;
  119. }
  120. return true;
  121. }
  122. @Override
  123. public boolean perform(final AbstractBuild<?, ?> build, Launcher launcher, final BuildListener listener)
  124. throws InterruptedException, IOException {
  125. final StringBuffer sb = new StringBuffer();
  126. final TusarNotifierLogger tusarNotifierLog = Guice.createInjector(new AbstractModule() {
  127. @Override
  128. protected void configure() {
  129. bind(BuildListener.class).toInstance(listener);
  130. }
  131. }).getInstance(TusarNotifierLogger.class);
  132. tusarNotifierLog.info("Starting converting.");
  133. TusarNotifierReportProcessingService tusarNotifierReportProcessingService = Guice.createInjector(new AbstractModule() {
  134. @Override
  135. protected void configure() {
  136. bind(BuildListener.class).toInstance(listener);
  137. }
  138. }).getInstance(TusarNotifierReportProcessingService.class);
  139. boolean isInvoked = false;
  140. // Apply conversion for all tests tools
  141. if (tests.length != 0) {
  142. FilePath outputFileParent = new FilePath(build.getWorkspace(), generatedTests);
  143. outputFileParent.mkdirs();
  144. for (TestType testsType : tests) {
  145. tusarNotifierLog.info("Processing " + testsType.getDescriptor().getDisplayName());
  146. if (!tusarNotifierReportProcessingService.isEmptyPattern(testsType.getPattern())) {
  147. boolean result = processInputMetricType(build, listener, testsType, outputFileParent);
  148. if (result) {
  149. isInvoked = true;
  150. }
  151. }
  152. }
  153. sb.append(";").append(generatedTests);
  154. }
  155. if (coverages.length != 0) {
  156. FilePath outputFileParent = new FilePath(build.getWorkspace(), generatedCoverage);
  157. outputFileParent.mkdirs();
  158. for (CoverageType coverageType : coverages) {
  159. tusarNotifierLog.info("Processing " + coverageType.getDescriptor().getDisplayName());
  160. if (!tusarNotifierReportProcessingService.isEmptyPattern(coverageType.getPattern())) {
  161. boolean result = processInputMetricType(build, listener, coverageType, outputFileParent);
  162. if (result) {
  163. isInvoked = true;
  164. }
  165. }
  166. }
  167. sb.append(";").append(generatedCoverage);
  168. }
  169. if (violations.length != 0) {
  170. FilePath outputFileParent = new FilePath(build.getWorkspace(), generatedViolations);
  171. outputFileParent.mkdirs();
  172. for (ViolationsType violationsType : violations) {
  173. tusarNotifierLog.info("Processing " + violationsType.getDescriptor().getDisplayName());
  174. if (!tusarNotifierReportProcessingService.isEmptyPattern(violationsType.getPattern())) {
  175. boolean result = processInputMetricType(build, listener, violationsType, outputFileParent);
  176. if (result) {
  177. isInvoked = true;
  178. }
  179. }
  180. }
  181. sb.append(";").append(generatedViolations);
  182. }
  183. if (measures.length != 0) {
  184. FilePath outputFileParent = new FilePath(build.getWorkspace(), generatedMeasures);
  185. outputFileParent.mkdirs();
  186. for (MeasureType measureType : measures) {
  187. tusarNotifierLog.info("Processing " + measureType.getDescriptor().getDisplayName());
  188. if (!tusarNotifierReportProcessingService.isEmptyPattern(measureType.getPattern())) {
  189. boolean result = processInputMetricType(build, listener, measureType, outputFileParent);
  190. if (result) {
  191. isInvoked = true;
  192. }
  193. }
  194. }
  195. sb.append(";").append(generatedMeasures);
  196. }
  197. // Remove the first character
  198. sb.delete(0, 1);
  199. List<ParameterValue> parameterValues = new ArrayList<ParameterValue>();
  200. parameterValues.add(new StringParameterValue("sonar.language", "tusar"));
  201. parameterValues.add(new StringParameterValue("sonar.tusar.reportsPaths", sb.toString()));
  202. build.addAction(new ParametersAction(parameterValues));
  203. return true;
  204. }
  205. @Extension(ordinal = 1)
  206. @SuppressWarnings("unused")
  207. public static final class TusarNotifierDescriptor extends BuildStepDescriptor<Publisher> {
  208. public TusarNotifierDescriptor() {
  209. super(TusarNotifier.class);
  210. load();
  211. }
  212. @Override
  213. public boolean isApplicable(Class<? extends AbstractProject> jobType) {
  214. return true;
  215. }
  216. @Override
  217. public String getHelpFile() {
  218. return "/plugin/tusarnotifier/help.html";
  219. }
  220. @Override
  221. public String getDisplayName() {
  222. return "TUSAR Notifier";
  223. }
  224. public DescriptorExtensionList<TestType, TestTypeDescriptor<?>> getListTestDescriptors() {
  225. return TestTypeDescriptor.all();
  226. }
  227. public DescriptorExtensionList<ViolationsType, ViolationsTypeDescriptor<?>> getListViolationDescriptors() {
  228. return ViolationsTypeDescriptor.all();
  229. }
  230. public DescriptorExtensionList<MeasureType, MeasureTypeDescriptor<?>> getListMeasureDescriptors() {
  231. return MeasureTypeDescriptor.all();
  232. }
  233. public DescriptorExtensionList<CoverageType, CoverageTypeDescriptor<?>> getListCoverageDescriptors() {
  234. return CoverageTypeDescriptor.all();
  235. }
  236. @Override
  237. public Publisher newInstance(StaplerRequest req, JSONObject formData)
  238. throws FormException {
  239. List<TestType> tests = Descriptor.newInstancesFromHeteroList(req, formData, "tests", getListTestDescriptors());
  240. List<CoverageType> coverages = Descriptor.newInstancesFromHeteroList(req, formData, "coverages", getListCoverageDescriptors());
  241. List<ViolationsType> violations = Descriptor.newInstancesFromHeteroList(req, formData, "violations", getListViolationDescriptors());
  242. List<MeasureType> measures = Descriptor.newInstancesFromHeteroList(req, formData, "measures", getListMeasureDescriptors());
  243. return new TusarNotifier(tests.toArray(new TestType[tests.size()]),
  244. coverages.toArray(new CoverageType[coverages.size()]),
  245. violations.toArray(new ViolationsType[violations.size()]),
  246. measures.toArray(new MeasureType[measures.size()])
  247. );
  248. }
  249. }
  250. }