Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49054d2
commit 8169d2d
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.twalford | ||
|
||
import net.virtualvoid.sbt.graph.DependencyGraphPlugin | ||
import sbt._ | ||
import sbt.Keys._ | ||
import SnykTasks._ | ||
|
||
object SnykPlugin extends AutoPlugin { | ||
self => | ||
override def requires = DependencyGraphPlugin | ||
object autoImport { | ||
val SnykPlugin: AutoPlugin = self | ||
val snykOrganization = SnykTasks.snykOrganization | ||
} | ||
|
||
override def globalSettings: Seq[Def.Setting[_]] = Seq( | ||
Global / concurrentRestrictions += Tags.exclusive(snykTag), | ||
aggregate in snykAuth := false | ||
) | ||
|
||
override lazy val projectSettings: Seq[Setting[_]] = Seq( | ||
snykTest := snykTestTask.value, | ||
snykMonitor := snykMonitorTask.value, | ||
snykAuth := snykAuthTask.value | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.twalford | ||
|
||
import sbt.ConcurrentRestrictions.Tag | ||
import sbt.{Def, settingKey, taskKey} | ||
import sbt.Keys.{name, streams} | ||
import sbt.internal.util.ManagedLogger | ||
|
||
import scala.sys.process.Process | ||
|
||
object SnykTasks { | ||
private lazy val authEnvVar = "SNYK_TOKEN" | ||
|
||
val snykOrganization = settingKey[String]("The organization snyk should be run for") | ||
val snykAuth = taskKey[Unit]("Authorizes a local snyk instance") | ||
val snykTest = taskKey[Unit]("Runs snyk test on the local project") | ||
val snykMonitor = taskKey[Unit]("Runs snyk monitor on the local project") | ||
|
||
val snykTag = Tag("snyk-exclusive") | ||
|
||
|
||
lazy val snykTestTask = Def.task { | ||
val log = streams.value.log | ||
run(s"""snyk test -- "\\"project ${name.value}\\""""", log) | ||
}.tag(snykTag) | ||
|
||
lazy val snykMonitorTask = Def.task { | ||
val log = streams.value.log | ||
val projectName = name.value | ||
run(s"""snyk monitor --org=${snykOrganization.value} --project-name=$projectName -- "\\"project $projectName\\""""", log) | ||
}.tag(snykTag) | ||
|
||
lazy val snykAuthTask = Def.task { | ||
val log = streams.value.log | ||
checkForAuth(log) | ||
}.tag(snykTag) | ||
|
||
private def checkForAuth(log: ManagedLogger): Unit = { | ||
Option(System.getenv(authEnvVar)) match { | ||
case None => | ||
log.info("No auth set up, but presumed we're running locally. Requesting auth via `snyk auth`") | ||
run("snyk auth", log) | ||
case Some(auth) => | ||
log.debug("Snyk using environment variable authorization, continuing") | ||
run(s"snyk auth $auth", log) | ||
} | ||
} | ||
|
||
private def run(cmd: String, log: ManagedLogger): Unit = { | ||
val shell = if (sys.props("os.name").contains("Windows")) { | ||
List("cmd", "/c") | ||
} else { | ||
List("bash", "-c") | ||
} | ||
Process(shell ::: cmd.split(' ').toList) ! log | ||
} | ||
} |