PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/main/scala/com/atlassian/labs/opup/task/ShowResultsTask.scala

https://bitbucket.org/jwalton/opup
Scala | 162 lines | 124 code | 37 blank | 1 comment | 10 complexity | af6968dd503b4eedefc26dee3948cd24 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup.task
  2. import java.io.File
  3. import java.io.FileInputStream
  4. import java.io.FileWriter
  5. import java.util.ArrayList
  6. import java.util.Arrays
  7. import java.util.HashMap
  8. import java.util.List
  9. import java.util.Map
  10. import java.util.Properties
  11. import java.util.TreeMap
  12. import scala.collection.JavaConverters.asScalaBufferConverter
  13. import org.apache.commons.io.FileUtils
  14. import org.apache.maven.artifact.versioning.ArtifactVersion
  15. import org.apache.maven.artifact.versioning.DefaultArtifactVersion
  16. import org.apache.maven.model.io.xpp3.MavenXpp3Reader
  17. import org.apache.velocity.VelocityContext
  18. import org.apache.velocity.app.VelocityEngine
  19. class ShowResultsTask(opupDirectory: File, passFailFile: File, val relativeLogDirectory: String) {
  20. val pomFile = new File("pom.xml")
  21. def generateResults(resultsFile: File) : Unit = {
  22. val commits = new HashMap[String, String]();
  23. for (c <- FileUtils.readLines(new File(opupDirectory, "commit.keys")).asScala)
  24. {
  25. val sa = c.split(",", 2);
  26. commits.put(sa(0), sa(1));
  27. }
  28. val passes = new HashMap[String, Boolean]();
  29. for (pf <- FileUtils.readLines(passFailFile).asScala)
  30. {
  31. val sa = pf.split(",");
  32. passes.put(sa(0), sa(1).equals("good"));
  33. }
  34. val allResults = new ArrayList[ItemResults]();
  35. for (change <- FileUtils.readLines(new File(opupDirectory, "change.keys")).asScala)
  36. {
  37. val sa = change.split(",");
  38. val buildResults = new TreeMap[ArtifactVersion, BuildResult]();
  39. for (vs <- sa.tail)
  40. {
  41. val k = sa(0) + ':' + vs
  42. var result = new BuildResult(false, false, null);
  43. val commit = commits.get(k);
  44. if (commit != null)
  45. {
  46. val pf = Option(passes.get(commit));
  47. pf.foreach(p =>
  48. {
  49. result = new BuildResult(true, p, relativeLogDirectory + "/build-log-" + commit + ".txt")
  50. })
  51. }
  52. buildResults.put(new DefaultArtifactVersion(vs), result);
  53. }
  54. implicitise(buildResults, Arrays.asList(sa: _*).subList(1, sa.length));
  55. val results = new ItemResults(sa(0), buildResults)
  56. allResults.add(results);
  57. }
  58. val ve = new VelocityEngine();
  59. val props = new Properties();
  60. props.setProperty("resource.loader", "class");
  61. props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  62. props.setProperty("eventhandler.referenceinsertion.class", "org.apache.velocity.app.event.implement.EscapeHtmlReference");
  63. ve.init(props);
  64. val ctxt = new VelocityContext();
  65. val mainPom = new MavenXpp3Reader().read(new FileInputStream(pomFile))
  66. ctxt.put("groupId", mainPom.getGroupId());
  67. ctxt.put("artifactId", mainPom.getArtifactId());
  68. ctxt.put("version", mainPom.getVersion());
  69. ctxt.put("results", allResults);
  70. val template = ve.getTemplate("/com/atlassian/labs/opup/results.vm");
  71. val w = new FileWriter(resultsFile)
  72. try
  73. {
  74. template.merge(ctxt, w);
  75. w.flush();
  76. }
  77. finally
  78. {
  79. w.close();
  80. }
  81. }
  82. def implicitise(buildResults: Map[ArtifactVersion, BuildResult], subList: List[String]) =
  83. {
  84. /* Current version passes implicitly */
  85. val current = new DefaultArtifactVersion(subList.get(0));
  86. if (!buildResults.get(current).ran)
  87. {
  88. val br = new BuildResult(false, true, null)
  89. buildResults.put(current, br);
  90. }
  91. val implied = new HashMap[ArtifactVersion, BuildResult]();
  92. var haveRealResults = false;
  93. var noFailures = true;
  94. for (s <- subList.asScala)
  95. {
  96. val v = new DefaultArtifactVersion(s);
  97. val real = buildResults.get(v);
  98. if (real.ran)
  99. {
  100. haveRealResults = true;
  101. noFailures &= real.passed;
  102. }
  103. else
  104. {
  105. val ibr = new BuildResult(false, noFailures, null);
  106. implied.put(v, ibr);
  107. }
  108. }
  109. if (haveRealResults)
  110. {
  111. buildResults.putAll(implied);
  112. }
  113. }
  114. }
  115. case class BuildResult(val ran: Boolean, val passed: Boolean, logfile: String)
  116. {
  117. def getRan() = ran
  118. def getPassed() = passed
  119. def getLogfile() = logfile
  120. }
  121. class ItemResults(key: String, results: Map[ArtifactVersion, BuildResult])
  122. {
  123. def getKey() = key
  124. def getResults() = results
  125. }