Skip to content

Instantly share code, notes, and snippets.

@afiore
Last active June 23, 2016 22:10
Show Gist options
  • Save afiore/c1223a87bb85cf1ce3e590e7b44d5bea to your computer and use it in GitHub Desktop.
Save afiore/c1223a87bb85cf1ce3e590e7b44d5bea to your computer and use it in GitHub Desktop.
Skip the test when the current build SHA1 has a successful status flag associated (Github API)
import org.kohsuke.github.{GHCommitState, GHCommitStatus, GitHub}
name := "gitStatus"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test"
val githubToken = taskKey[Option[String]]("Read github API token")
githubToken := sys.env.get("GITHUB_TOKEN")
val getSha1 = taskKey[Option[String]]("read GIT_SHA1 env variable")
getSha1 := sys.env.get("GIT_SHA1")
val githubRepo = taskKey[String]("Infer Github repo from remote.origin.url config setting")
githubRepo := {
val url = "git config --get remote.origin.url"!!
val Https = "https://github.com/(.*)\\.git".r
val Ssh = "git@github.com:(.*)\\.git".r
url match {
case Https(rid) => rid
case Ssh(rid) => rid
case _ => sys.error(s"Cannot extract a Github repo id from remote.origin.url $url")
}
}
val getBuildStatus = taskKey[Option[GHCommitStatus]]("Get current GIT_SHA build status")
getBuildStatus := {
for {
sha1 <- getSha1.value
token <- githubToken.value
repoId = githubRepo.value
client = GitHub.connectUsingOAuth(token)
repo = client.getRepository(repoId)
status <- Option(repo.getLastCommitStatus(sha1))
} yield status
}
val testUnlessGreen = taskKey[Unit]("run tests, unless this SHA has already built successfully on CI")
testUnlessGreen := getBuildStatus.flatMap {
case Some(s) if s.getState == GHCommitState.SUCCESS => nop
case _ => (test in Test).taskValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment