2015-01-22 20:26:34 +02:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2015-01-22 20:26:34 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2015-01-22 20:26:34 +02:00
|
|
|
package akka
|
|
|
|
|
|
2017-10-30 03:13:14 +02:00
|
|
|
import com.lightbend.paradox.sbt.ParadoxPlugin
|
|
|
|
|
import com.lightbend.paradox.sbt.ParadoxPlugin.autoImport.paradox
|
2016-12-02 14:23:13 +01:00
|
|
|
import com.typesafe.tools.mima.plugin.MimaKeys.mimaReportBinaryIssues
|
2016-03-10 10:45:35 +02:00
|
|
|
import com.typesafe.tools.mima.plugin.MimaPlugin
|
2015-12-01 16:48:08 +01:00
|
|
|
import net.virtualvoid.sbt.graph.backend.SbtUpdateReport
|
|
|
|
|
import net.virtualvoid.sbt.graph.DependencyGraphKeys._
|
|
|
|
|
import net.virtualvoid.sbt.graph.ModuleGraph
|
2015-05-05 19:39:56 +02:00
|
|
|
import org.kohsuke.github._
|
2017-10-30 03:13:14 +02:00
|
|
|
import sbtunidoc.BaseUnidocPlugin.autoImport.unidoc
|
2015-05-05 19:39:56 +02:00
|
|
|
import sbt.Keys._
|
|
|
|
|
import sbt._
|
|
|
|
|
|
|
|
|
|
import scala.collection.immutable
|
2017-10-30 03:13:14 +02:00
|
|
|
import scala.sys.process._
|
2015-05-05 19:39:56 +02:00
|
|
|
import scala.util.matching.Regex
|
2015-01-22 20:26:34 +02:00
|
|
|
|
|
|
|
|
object ValidatePullRequest extends AutoPlugin {
|
|
|
|
|
|
2015-05-12 16:10:27 +02:00
|
|
|
override def trigger = allRequirements
|
|
|
|
|
override def requires = plugins.JvmPlugin
|
|
|
|
|
|
2015-05-10 02:01:12 +02:00
|
|
|
sealed trait BuildMode {
|
2015-05-21 09:48:49 +03:00
|
|
|
def task: Option[TaskKey[_]]
|
2015-05-10 02:01:12 +02:00
|
|
|
def log(projectName: String, l: Logger): Unit
|
|
|
|
|
}
|
|
|
|
|
case object BuildSkip extends BuildMode {
|
2015-05-21 09:48:49 +03:00
|
|
|
override def task = None
|
2015-05-10 02:01:12 +02:00
|
|
|
def log(projectName: String, l: Logger) =
|
|
|
|
|
l.info(s"Skipping validation of [$projectName], as PR does NOT affect this project...")
|
|
|
|
|
}
|
|
|
|
|
case object BuildQuick extends BuildMode {
|
2015-05-21 09:48:49 +03:00
|
|
|
override def task = Some(test in ValidatePR)
|
|
|
|
|
def log(projectName: String, l: Logger) =
|
2015-05-10 02:01:12 +02:00
|
|
|
l.info(s"Building [$projectName] in quick mode, as it's dependencies were affected by PR.")
|
|
|
|
|
}
|
|
|
|
|
case object BuildProjectChangedQuick extends BuildMode {
|
2015-05-21 09:48:49 +03:00
|
|
|
override def task = Some(test in ValidatePR)
|
2015-05-10 02:01:12 +02:00
|
|
|
def log(projectName: String, l: Logger) =
|
|
|
|
|
l.info(s"Building [$projectName] as the root `project/` directory was affected by this PR.")
|
|
|
|
|
}
|
|
|
|
|
final case class BuildCommentForcedAll(phrase: String, c: GHIssueComment) extends BuildMode {
|
2015-05-21 09:48:49 +03:00
|
|
|
override def task = Some(test in Test)
|
2015-05-10 02:01:12 +02:00
|
|
|
def log(projectName: String, l: Logger) =
|
|
|
|
|
l.info(s"GitHub PR comment [ ${c.getUrl} ] contains [$phrase], forcing BUILD ALL mode!")
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 19:39:56 +02:00
|
|
|
val ValidatePR = config("pr-validation") extend Test
|
|
|
|
|
|
|
|
|
|
override lazy val projectConfigurations = Seq(ValidatePR)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Assumptions:
|
|
|
|
|
Env variables set "by Jenkins" are assumed to come from this plugin:
|
|
|
|
|
https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// settings
|
|
|
|
|
val PullIdEnvVarName = "ghprbPullId" // Set by "GitHub pull request builder plugin"
|
|
|
|
|
|
|
|
|
|
val TargetBranchEnvVarName = "PR_TARGET_BRANCH"
|
|
|
|
|
val TargetBranchJenkinsEnvVarName = "ghprbTargetBranch"
|
|
|
|
|
|
|
|
|
|
val SourceBranchEnvVarName = "PR_SOURCE_BRANCH"
|
|
|
|
|
val SourcePullIdJenkinsEnvVarName = "ghprbPullId" // used to obtain branch name in form of "pullreq/17397"
|
|
|
|
|
val sourceBranch = settingKey[String]("Branch containing the changes of this PR")
|
|
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
val targetBranch = settingKey[String]("Target branch of this PR, defaults to `master`")
|
|
|
|
|
|
2015-05-05 19:39:56 +02:00
|
|
|
// asking github comments if this PR should be PLS BUILD ALL
|
2015-05-10 02:01:12 +02:00
|
|
|
val githubEnforcedBuildAll = taskKey[Option[BuildMode]]("Checks via GitHub API if comments included the PLS BUILD ALL keyword")
|
2015-05-05 19:39:56 +02:00
|
|
|
val buildAllKeyword = taskKey[Regex]("Magic phrase to be used to trigger building of the entire project instead of analysing dependencies")
|
|
|
|
|
|
|
|
|
|
// determining touched dirs and projects
|
|
|
|
|
val changedDirectories = taskKey[immutable.Set[String]]("List of touched modules in this PR branch")
|
2015-05-21 09:48:49 +03:00
|
|
|
val projectBuildMode = taskKey[BuildMode]("Determines what will run when this project is affected by the PR and should be rebuilt")
|
2015-05-05 19:39:56 +02:00
|
|
|
|
|
|
|
|
// running validation
|
2015-05-21 09:48:49 +03:00
|
|
|
val validatePullRequest = taskKey[Unit]("Validate pull request")
|
|
|
|
|
val additionalTasks = taskKey[Seq[TaskKey[_]]]("Additional tasks for pull request validation")
|
2015-01-22 20:26:34 +02:00
|
|
|
|
2017-10-30 03:13:14 +02:00
|
|
|
def changedDirectoryIsDependency(changedDirs: Set[String],
|
|
|
|
|
name: String,
|
|
|
|
|
graphsToTest: Seq[(Configuration, ModuleGraph)])(log: Logger): Boolean = {
|
|
|
|
|
graphsToTest exists { case (ivyScope, deps) =>
|
|
|
|
|
log.debug(s"Analysing [$ivyScope] scoped dependencies...")
|
|
|
|
|
|
|
|
|
|
deps.nodes.foreach { m ⇒ log.debug(" -> " + m.id) }
|
|
|
|
|
|
|
|
|
|
// if this project depends on a modified module, we must test it
|
|
|
|
|
deps.nodes.exists { m =>
|
|
|
|
|
// match just by name, we'd rather include too much than too little
|
|
|
|
|
val dependsOnModule = changedDirs.find(m.id.name contains _)
|
|
|
|
|
val depends = dependsOnModule.isDefined
|
|
|
|
|
if (depends) log.info(s"Project [$name] must be verified, because depends on [${dependsOnModule.get}]")
|
|
|
|
|
depends
|
|
|
|
|
}
|
2015-05-10 02:01:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
def localTargetBranch: Option[String] = sys.env.get("PR_TARGET_BRANCH")
|
|
|
|
|
def jenkinsTargetBranch: Option[String] = sys.env.get("ghprbTargetBranch")
|
|
|
|
|
def runningOnJenkins: Boolean = jenkinsTargetBranch.isDefined
|
|
|
|
|
def runningLocally: Boolean = !runningOnJenkins
|
|
|
|
|
|
2015-12-01 16:53:48 +01:00
|
|
|
override lazy val buildSettings = Seq(
|
|
|
|
|
sourceBranch in Global in ValidatePR := {
|
2015-05-05 19:39:56 +02:00
|
|
|
sys.env.get(SourceBranchEnvVarName) orElse
|
2015-12-01 16:53:48 +01:00
|
|
|
sys.env.get(SourcePullIdJenkinsEnvVarName).map("pullreq/" + _) getOrElse // Set by "GitHub pull request builder plugin"
|
|
|
|
|
"HEAD"
|
2015-05-05 19:39:56 +02:00
|
|
|
},
|
|
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
targetBranch in Global in ValidatePR := {
|
|
|
|
|
(localTargetBranch, jenkinsTargetBranch) match {
|
2017-10-30 03:13:14 +02:00
|
|
|
case (Some(local), _) => local // local override
|
|
|
|
|
case (None, Some(branch)) => s"origin/$branch" // usually would be "master" or "release-2.3" etc
|
|
|
|
|
case (None, None) => "origin/master" // defaulting to diffing with "master"
|
2016-01-22 00:56:25 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-01 16:53:48 +01:00
|
|
|
buildAllKeyword in Global in ValidatePR := """PLS BUILD ALL""".r,
|
2015-05-05 19:39:56 +02:00
|
|
|
|
2015-12-01 16:53:48 +01:00
|
|
|
githubEnforcedBuildAll in Global in ValidatePR := {
|
2017-10-30 03:13:14 +02:00
|
|
|
val log = streams.value.log
|
|
|
|
|
val buildAllMagicPhrase = (buildAllKeyword in ValidatePR).value
|
|
|
|
|
|
|
|
|
|
sys.env.get(PullIdEnvVarName).map(_.toInt) flatMap { prId =>
|
2015-05-10 02:01:12 +02:00
|
|
|
log.info("Checking GitHub comments for PR validation options...")
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
val gh = GitHubBuilder.fromEnvironment().withOAuthToken(GitHub.envTokenOrThrow).build()
|
|
|
|
|
val comments = gh.getRepository("akka/akka").getIssue(prId).getComments.asScala
|
|
|
|
|
|
|
|
|
|
def triggersBuildAll(c: GHIssueComment): Boolean = buildAllMagicPhrase.findFirstIn(c.getBody).isDefined
|
2017-10-30 03:13:14 +02:00
|
|
|
comments collectFirst { case c if triggersBuildAll(c) =>
|
|
|
|
|
BuildCommentForcedAll(buildAllMagicPhrase.toString(), c)
|
2015-05-05 19:39:56 +02:00
|
|
|
}
|
2015-05-10 02:01:12 +02:00
|
|
|
} catch {
|
2017-10-30 03:13:14 +02:00
|
|
|
case ex: Exception =>
|
2015-05-10 02:01:12 +02:00
|
|
|
log.warn("Unable to reach GitHub! Exception was: " + ex.getMessage)
|
|
|
|
|
None
|
|
|
|
|
}
|
2015-05-05 19:39:56 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-12-01 16:53:48 +01:00
|
|
|
changedDirectories in Global in ValidatePR := {
|
|
|
|
|
val log = streams.value.log
|
|
|
|
|
|
|
|
|
|
val prId = (sourceBranch in ValidatePR).value
|
|
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
val target = (targetBranch in ValidatePR).value
|
|
|
|
|
|
2015-12-01 16:53:48 +01:00
|
|
|
// TODO could use jgit
|
|
|
|
|
log.info(s"Diffing [$prId] to determine changed modules in PR...")
|
2016-01-22 00:56:25 +01:00
|
|
|
val diffOutput = s"git diff $target --name-only".!!.split("\n")
|
|
|
|
|
val diffedModuleNames =
|
|
|
|
|
diffOutput
|
2017-10-30 03:13:14 +02:00
|
|
|
.map(l => l.trim)
|
|
|
|
|
.filter(l =>
|
2016-01-25 19:33:52 -08:00
|
|
|
l.startsWith("akka-") ||
|
2017-10-30 03:13:14 +02:00
|
|
|
(l.startsWith("project") && l != "project/MiMa.scala")
|
|
|
|
|
)
|
2016-01-25 19:33:52 -08:00
|
|
|
.map(l ⇒ l.takeWhile(_ != '/'))
|
2015-12-01 16:53:48 +01:00
|
|
|
.toSet
|
|
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
val dirtyModuleNames: Set[String] =
|
|
|
|
|
if (runningOnJenkins) Set.empty
|
|
|
|
|
else {
|
|
|
|
|
val statusOutput = s"git status --short".!!.split("\n")
|
|
|
|
|
val dirtyDirectories = statusOutput
|
|
|
|
|
.map(l ⇒ l.trim.dropWhile(_ != ' ').drop(1))
|
|
|
|
|
.map(_.takeWhile(_ != '/'))
|
2017-10-30 03:13:14 +02:00
|
|
|
.filter(dir => dir.startsWith("akka-") || dir == "project")
|
2016-01-22 00:56:25 +01:00
|
|
|
.toSet
|
2016-12-30 12:29:30 +01:00
|
|
|
log.info("Detected uncommitted changes in directories (including in dependency analysis): " + dirtyDirectories.mkString("[", ",", "]"))
|
2016-01-22 00:56:25 +01:00
|
|
|
dirtyDirectories
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 03:13:14 +02:00
|
|
|
|
2016-01-22 00:56:25 +01:00
|
|
|
val allModuleNames = dirtyModuleNames ++ diffedModuleNames
|
|
|
|
|
log.info("Detected changes in directories: " + allModuleNames.mkString("[", ", ", "]"))
|
|
|
|
|
allModuleNames
|
2017-10-30 03:13:14 +02:00
|
|
|
}
|
|
|
|
|
)
|
2015-12-01 16:53:48 +01:00
|
|
|
|
|
|
|
|
override lazy val projectSettings = inConfig(ValidatePR)(Defaults.testTasks) ++ Seq(
|
|
|
|
|
testOptions in ValidatePR += Tests.Argument(TestFrameworks.ScalaTest, "-l", "performance"),
|
|
|
|
|
testOptions in ValidatePR += Tests.Argument(TestFrameworks.ScalaTest, "-l", "long-running"),
|
|
|
|
|
testOptions in ValidatePR += Tests.Argument(TestFrameworks.ScalaTest, "-l", "timing"),
|
|
|
|
|
|
2017-10-04 14:22:25 +02:00
|
|
|
// make it fork just like regular test running
|
|
|
|
|
fork in ValidatePR := (fork in Test).value,
|
|
|
|
|
testGrouping in ValidatePR := (testGrouping in Test).value,
|
|
|
|
|
javaOptions in ValidatePR := (javaOptions in Test).value,
|
|
|
|
|
|
2015-05-10 02:01:12 +02:00
|
|
|
projectBuildMode in ValidatePR := {
|
2015-05-05 19:39:56 +02:00
|
|
|
val log = streams.value.log
|
|
|
|
|
log.debug(s"Analysing project (for inclusion in PR validation): [${name.value}]")
|
2015-05-10 02:01:12 +02:00
|
|
|
val changedDirs = (changedDirectories in ValidatePR).value
|
|
|
|
|
val githubCommandEnforcedBuildAll = (githubEnforcedBuildAll in ValidatePR).value
|
|
|
|
|
|
2015-12-01 16:48:08 +01:00
|
|
|
val thisProjectId = CrossVersion(scalaVersion.value, scalaBinaryVersion.value)(projectID.value)
|
|
|
|
|
|
|
|
|
|
def graphFor(updateReport: UpdateReport, config: Configuration): (Configuration, ModuleGraph) =
|
2017-10-30 03:13:14 +02:00
|
|
|
config -> SbtUpdateReport.fromConfigurationReport(updateReport.configuration(config).get, thisProjectId)
|
2015-12-01 16:48:08 +01:00
|
|
|
|
|
|
|
|
def isDependency: Boolean =
|
|
|
|
|
changedDirectoryIsDependency(
|
|
|
|
|
changedDirs,
|
|
|
|
|
name.value,
|
|
|
|
|
Seq(
|
|
|
|
|
graphFor((update in Compile).value, Compile),
|
|
|
|
|
graphFor((update in Test).value, Test),
|
|
|
|
|
graphFor((update in Runtime).value, Runtime),
|
|
|
|
|
graphFor((update in Provided).value, Provided),
|
|
|
|
|
graphFor((update in Optional).value, Optional)))(log)
|
|
|
|
|
|
2015-05-10 02:01:12 +02:00
|
|
|
if (githubCommandEnforcedBuildAll.isDefined)
|
|
|
|
|
githubCommandEnforcedBuildAll.get
|
|
|
|
|
else if (changedDirs contains "project")
|
|
|
|
|
BuildProjectChangedQuick
|
2015-12-01 16:48:08 +01:00
|
|
|
else if (isDependency)
|
2015-05-10 02:01:12 +02:00
|
|
|
BuildQuick
|
|
|
|
|
else
|
|
|
|
|
BuildSkip
|
2015-05-05 19:39:56 +02:00
|
|
|
},
|
|
|
|
|
|
2015-05-21 09:48:49 +03:00
|
|
|
additionalTasks in ValidatePR := Seq.empty,
|
|
|
|
|
|
2015-05-05 19:39:56 +02:00
|
|
|
validatePullRequest := Def.taskDyn {
|
|
|
|
|
val log = streams.value.log
|
2015-05-10 02:01:12 +02:00
|
|
|
val buildMode = (projectBuildMode in ValidatePR).value
|
|
|
|
|
|
|
|
|
|
buildMode.log(name.value, log)
|
2015-05-21 09:48:49 +03:00
|
|
|
|
|
|
|
|
val validationTasks = buildMode.task.toSeq ++ (buildMode match {
|
2017-10-30 03:13:14 +02:00
|
|
|
case BuildSkip => Seq.empty // do not run the additional task if project is skipped during pr validation
|
|
|
|
|
case _ => (additionalTasks in ValidatePR).value
|
2015-05-21 09:48:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create a task for every validation task key and
|
|
|
|
|
// then zip all of the tasks together discarding outputs.
|
|
|
|
|
// Task failures are propagated as normal.
|
2017-10-30 03:13:14 +02:00
|
|
|
val zero: Def.Initialize[Seq[Task[Any]]] = Def.setting { Seq(task(()))}
|
|
|
|
|
validationTasks.map(taskKey => Def.task { taskKey.value } ).foldLeft(zero) { (acc, current) =>
|
|
|
|
|
acc.zipWith(current) { case (taskSeq, task) =>
|
|
|
|
|
taskSeq :+ task.asInstanceOf[Task[Any]]
|
2015-05-21 09:48:49 +03:00
|
|
|
}
|
2017-10-30 03:13:14 +02:00
|
|
|
} apply { tasks: Seq[Task[Any]] =>
|
|
|
|
|
tasks.join map { seq => () /* Ignore the sequence of unit returned */ }
|
2015-05-21 09:48:49 +03:00
|
|
|
}
|
2017-10-30 03:13:14 +02:00
|
|
|
}.value
|
|
|
|
|
)
|
2015-01-22 20:26:34 +02:00
|
|
|
}
|
2015-05-21 09:48:49 +03:00
|
|
|
|
|
|
|
|
/**
|
2017-10-30 03:13:14 +02:00
|
|
|
* This autoplugin adds Multi Jvm tests to validatePullRequest task.
|
|
|
|
|
* It is needed, because ValidatePullRequest autoplugin does not depend on MultiNode and
|
|
|
|
|
* therefore test:executeTests is not yet modified to include multi-jvm tests when ValidatePullRequest
|
|
|
|
|
* build strategy is being determined.
|
|
|
|
|
*
|
|
|
|
|
* Making ValidatePullRequest depend on MultiNode is impossible, as then ValidatePullRequest
|
|
|
|
|
* autoplugin would trigger only on projects which have both of these plugins enabled.
|
|
|
|
|
*/
|
2015-05-21 09:48:49 +03:00
|
|
|
object MultiNodeWithPrValidation extends AutoPlugin {
|
|
|
|
|
import ValidatePullRequest._
|
2018-06-27 07:04:05 +02:00
|
|
|
import com.typesafe.sbt.MultiJvmPlugin.MultiJvmKeys.MultiJvm
|
2015-05-21 09:48:49 +03:00
|
|
|
override def trigger = allRequirements
|
|
|
|
|
override def requires = ValidatePullRequest && MultiNode
|
2016-11-29 08:33:36 +01:00
|
|
|
override lazy val projectSettings =
|
|
|
|
|
if (MultiNode.multiNodeTestInTest) Seq(additionalTasks in ValidatePR += MultiNode.multiTest)
|
2018-06-27 07:04:05 +02:00
|
|
|
else Seq.empty
|
2015-05-21 09:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 10:45:35 +02:00
|
|
|
/**
|
2017-10-30 03:13:14 +02:00
|
|
|
* This autoplugin adds MiMa binary issue reporting to validatePullRequest task,
|
|
|
|
|
* when a project has MimaPlugin autoplugin enabled.
|
|
|
|
|
*/
|
2016-03-10 10:45:35 +02:00
|
|
|
object MimaWithPrValidation extends AutoPlugin {
|
|
|
|
|
import ValidatePullRequest._
|
|
|
|
|
|
|
|
|
|
override def trigger = allRequirements
|
|
|
|
|
override def requires = ValidatePullRequest && MimaPlugin
|
|
|
|
|
override lazy val projectSettings = Seq(
|
2017-10-30 03:13:14 +02:00
|
|
|
additionalTasks in ValidatePR += mimaReportBinaryIssues
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This autoplugin adds Paradox doc generation to validatePullRequest task,
|
|
|
|
|
* when a project has ParadoxPlugin autoplugin enabled.
|
|
|
|
|
*/
|
|
|
|
|
object ParadoxWithPrValidation extends AutoPlugin {
|
|
|
|
|
import ValidatePullRequest._
|
|
|
|
|
|
|
|
|
|
override def trigger = allRequirements
|
|
|
|
|
override def requires = ValidatePullRequest && ParadoxPlugin
|
|
|
|
|
override lazy val projectSettings = Seq(
|
|
|
|
|
additionalTasks in ValidatePR += paradox in Compile
|
|
|
|
|
)
|
2016-03-10 10:45:35 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-21 09:48:49 +03:00
|
|
|
object UnidocWithPrValidation extends AutoPlugin {
|
|
|
|
|
import ValidatePullRequest._
|
|
|
|
|
|
|
|
|
|
override def trigger = noTrigger
|
|
|
|
|
override lazy val projectSettings = Seq(
|
2017-10-30 03:13:14 +02:00
|
|
|
additionalTasks in ValidatePR += unidoc in Compile
|
|
|
|
|
)
|
2015-05-21 09:48:49 +03:00
|
|
|
}
|