Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail the whole process if any of the mid-step pipes fail #238

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions plugin/src/main/scala/com/geirsson/CiReleasePlugin.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.geirsson

import com.geirsson.PipeFail.PipeFailOps
import com.github.sbt.git.GitPlugin
import com.github.sbt.git.SbtGit.GitKeys
import com.jsuereth.sbtpgp.SbtPgp
import com.jsuereth.sbtpgp.SbtPgp.autoImport._

import java.nio.file.Files
import java.nio.file.Paths
import java.nio.charset.StandardCharsets
Expand All @@ -14,6 +16,7 @@ import sbt._
import sbt.plugins.JvmPlugin
import sbtdynver.DynVerPlugin
import sbtdynver.DynVerPlugin.autoImport._

import scala.deprecated
import scala.sys.process._
import scala.util.control.NonFatal
Expand Down Expand Up @@ -76,7 +79,7 @@ object CiReleasePlugin extends AutoPlugin {
val TaggedVersion = """(\d{1,14})([\.\d{1,14}]*)((?:-\w+)*)""".r
val gpgVersion: Long = versionLine.split(" ").last match {
case TaggedVersion(m, _, _) => m.toLong
case _ => 0L
case _ => 0L
}
// https://dev.gnupg.org/T2313
val importCommand =
Expand All @@ -90,7 +93,7 @@ object CiReleasePlugin extends AutoPlugin {
s"unzip gpg.zip".!
s"gpg $importCommand gpg.key".!
} else {
(s"echo $secret" #| "base64 --decode" #| s"gpg $importCommand").!
(s"echo $secret" #|! "base64 --decode" #|! s"gpg $importCommand").!
}
}

Expand All @@ -108,9 +111,10 @@ object CiReleasePlugin extends AutoPlugin {
case None =>
import scala.sys.process._
val identifier = """([^\/]+?)"""
val GitHubHttps = s"https://github.com/$identifier/$identifier(?:\\.git)?".r
val GitHubGit = s"git://github.com:$identifier/$identifier(?:\\.git)?".r
val GitHubSsh = s"git@github.com:$identifier/$identifier(?:\\.git)?".r
val GitHubHttps =
s"https://github.com/$identifier/$identifier(?:\\.git)?".r
val GitHubGit = s"git://github.com:$identifier/$identifier(?:\\.git)?".r
val GitHubSsh = s"git@github.com:$identifier/$identifier(?:\\.git)?".r
try {
val remote = List("git", "ls-remote", "--get-url", "origin").!!.trim()
remote match {
Expand Down
35 changes: 35 additions & 0 deletions plugin/src/main/scala/com/geirsson/PipeFail.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.geirsson

import java.io.ByteArrayInputStream
import scala.sys.process.{ProcessBuilder, ProcessLogger}
import scala.util.{Failure, Success, Try}

object PipeFail {
implicit class PipeFailOps[R](p1: R) {
@volatile
private var error: Option[String] = None

def #|!(p2: R)(implicit ev: R => ProcessBuilder): ProcessBuilder = {
val logger = new ProcessLogger {
override def out(s: => String): Unit = ()

override def err(s: => String): Unit = {
error = Some(s)
}

override def buffer[T](f: => T): T = f
}
Try(p1.!!(logger)).map(result =>
(p2 #< new ByteArrayInputStream(result.getBytes))
) match {
case Failure(exception) =>
error match {
case Some(errorMessageFromPipe) =>
throw new RuntimeException(errorMessageFromPipe, exception)
case None => throw exception
}
case Success(value) => value
}
}
}
}