Repository: scala-steward-org/scala-steward
Pull request #3369 (2024-05-31 11:40:12)

Only return a failure exit code when all repos have failed
When even one repository fails in a scala steward run the workspace is not persisted by GitHub Actions because GH Actions won't persist the workspace of a failing job. Persisting workspace is crucial for respecting `pullRequests.frequency` configuration because the workspace is how scala steward records that it has opened a pull request. This means that one failing repository can cause a lot of user annoyance because their configuration is no longer respected and they get too many PRs opened. This change aims to fix that by only returning a failure code if *all* repos have failed. So long as at least one repository succeeded the exit code will be success and the workspace will persist. If administrators need to know what repos are failing they can use the jobs summary introduced by: https://github.com/scala-steward-org/scala-steward/pull/3071

See more here: https://github.com/guardian/scala-steward-public-repos/issues/60

cc @alejandrohdezma


Patch:
diff --git a/docs/help.md b/docs/help.md
index 2dc529eb..4930fb26 100644
--- a/docs/help.md
+++ b/docs/help.md
@@ -4,7 +4,7 @@ All command line arguments for the `scala-steward` application.
 
 ```
 Usage:
-    scala-steward --workspace <file> --repos-file <uri> [--repos-file <uri>]... [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--forge-type <forge-type>] [--forge-api-host <uri>] --forge-login <string> [--do-not-fork] [--add-labels] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-use-default-reviewers] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--gitlab-required-reviewers <integer>] [--gitlab-remove-source-branch] [--azure-repos-organization <string>] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>]... [--default-maven-repo <string>] [--refresh-backoff-period <duration>]
+    scala-steward --workspace <file> --repos-file <uri> [--repos-file <uri>]... [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--forge-type <forge-type>] [--forge-api-host <uri>] --forge-login <string> [--do-not-fork] [--add-labels] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-use-default-reviewers] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--gitlab-required-reviewers <integer>] [--gitlab-remove-source-branch] [--azure-repos-organization <string>] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>]... [--default-maven-repo <string>] [--refresh-backoff-period <duration>] [--exit-code-success-if-any-repo-succeeds]
     scala-steward validate-repo-config
 
 
@@ -94,6 +94,8 @@ Options and flags:
         default: https://repo1.maven.org/maven2/
     --refresh-backoff-period <duration>
         Period of time a failed build won't be triggered again; default: 0days
+    --exit-code-success-if-any-repo-succeeds
+        Whether the Scala Steward process should exit with success (exit code 0) if any repo succeeds; default: false
 
 Subcommands:
     validate-repo-config
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala b/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
index dfbebffe..87a90575 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala
@@ -18,12 +18,17 @@ package org.scalasteward.core.application
 
 import better.files.File
 import cats.data.Validated
+import cats.effect.ExitCode
 import cats.syntax.all._
 import com.monovore.decline.Opts.{flag, option, options}
 import com.monovore.decline._
 import org.http4s.Uri
 import org.http4s.syntax.literals._
 import org.scalasteward.core.application.Config._
+import org.scalasteward.core.application.ExitCodePolicy.{
+  SuccessIfAnyRepoSucceeds,
+  SuccessOnlyIfAllReposSucceed
+}
 import org.scalasteward.core.data.Resolver
 import org.scalasteward.core.forge.ForgeType
 import org.scalasteward.core.forge.ForgeType.{AzureRepos, GitHub}
@@ -31,6 +36,7 @@ import org.scalasteward.core.forge.github.GitHubApp
 import org.scalasteward.core.git.Author
 import org.scalasteward.core.util.Nel
 import org.scalasteward.core.util.dateTime.renderFiniteDuration
+
 import scala.concurrent.duration._
 
 object Cli {
@@ -337,6 +343,13 @@ object Cli {
       .withDefault(default)
   }
 
+  private val exitCodePolicy: Opts[ExitCodePolicy] = flag(
+    "exit-code-success-if-any-repo-succeeds",
+    s"Whether the Scala Steward process should exit with success (exit code ${ExitCode.Success.code}) if any repo succeeds; default: false"
+  ).orFalse.map { ifAnyRepoSucceeds =>
+    if (ifAnyRepoSucceeds) SuccessIfAnyRepoSucceeds else SuccessOnlyIfAllReposSucceed
+  }
+
   private val regular: Opts[Usage] = (
     workspace,
     reposFiles,
@@ -355,7 +368,8 @@ object Cli {
     gitHubApp,
     urlCheckerTestUrls,
     defaultMavenRepo,
-    refreshBackoffPeriod
+    refreshBackoffPeriod,
+    exitCodePolicy
   ).mapN(Config.apply).map(Usage.Regular.apply)
 
   private val validateRepoConfig: Opts[Usage] =
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/Config.scala b/modules/core/src/main/scala/org/scalasteward/core/application/Config.scala
index 3385af72..11d66671 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/Config.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/Config.scala
@@ -63,7 +63,8 @@ final case class Config(
     githubApp: Option[GitHubApp],
     urlCheckerTestUrls: Nel[Uri],
     defaultResolver: Resolver,
-    refreshBackoffPeriod: FiniteDuration
+    refreshBackoffPeriod: FiniteDuration,
+    exitCodePolicy: ExitCodePolicy
 ) {
   def forgeSpecificCfg: ForgeSpecificCfg =
     forgeCfg.tpe match {
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/ExitCodePolicy.scala b/modules/core/src/main/scala/org/scalasteward/core/application/ExitCodePolicy.scala
new file mode 100644
index 00000000..9edcf1a7
--- /dev/null
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/ExitCodePolicy.scala
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2018-2023 Scala Steward contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.scalasteward.core.application
+
+import cats.effect.ExitCode
+
+trait ExitCodePolicy {
+  def exitCodeFor(runResults: RunResults): ExitCode
+}
+
+object ExitCodePolicy {
+  def successIf(isSuccess: RunResults => Boolean): ExitCodePolicy =
+    (runResults: RunResults) => if (isSuccess(runResults)) ExitCode.Success else ExitCode.Error
+
+  val SuccessIfAnyRepoSucceeds: ExitCodePolicy = successIf(_.successRepos.nonEmpty)
+
+  val SuccessOnlyIfAllReposSucceed: ExitCodePolicy = successIf(_.reposWithFailures.nonEmpty)
+}
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/RunResults.scala b/modules/core/src/main/scala/org/scalasteward/core/application/RunResults.scala
index 0a903485..eb6c7bcb 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/RunResults.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/RunResults.scala
@@ -16,15 +16,12 @@
 
 package org.scalasteward.core.application
 
-import cats.effect.ExitCode
 import cats.syntax.all._
 import org.scalasteward.core.data.Repo
 
 case class RunResults(results: List[Either[(Repo, Throwable), Repo]]) {
   val (reposWithFailures, successRepos) = results.separate
 
-  val exitCode: ExitCode = if (reposWithFailures.isEmpty) ExitCode.Success else ExitCode.Error
-
   val markdownSummary: String = {
     val failuresSummaryOpt = Option.when(reposWithFailures.nonEmpty) {
       (Seq(s"# Job failed for ${reposWithFailures.size} out of ${results.size} repos") ++ (for {
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
index ac7c91c3..afbe9633 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
@@ -98,7 +98,7 @@ final class StewardAlg[F[_]](config: Config)(implicit
               for {
                 summaryFile <- workspaceAlg.runSummaryFile
                 _ <- fileAlg.writeFile(summaryFile, runResults.markdownSummary)
-              } yield runResults.exitCode
+              } yield config.exitCodePolicy.exitCodeFor(runResults)
             }
       } yield exitCode
     }
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 9ba9e446..28be42ac 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -38,7 +38,7 @@ object Dependencies {
   val munit = "org.scalameta" %% "munit" % "1.0.0"
   val munitCatsEffect = "org.typelevel" %% "munit-cats-effect" % "2.0.0"
   val munitScalacheck = "org.scalameta" %% "munit-scalacheck" % munit.revision
-  val refined = "eu.timepit" %% "refined" % "0.11.2"
+  val refined = "eu.timepit" %% "refined" % "0.11.1"
   val refinedScalacheck = "eu.timepit" %% "refined-scalacheck" % refined.revision
   val scalacacheCaffeine = "com.github.cb372" %% "scalacache-caffeine" % "1.0.0-M6"
   val scalacheck = "org.scalacheck" %% "scalacheck" % "1.18.0"


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala b/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
index ffbe23d4..b2e3ebf2 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/application/CliTest.scala
@@ -6,9 +6,14 @@ import munit.FunSuite
 import org.http4s.syntax.literals._
 import org.scalasteward.core.application.Cli.ParseResult._
 import org.scalasteward.core.application.Cli.{EnvVar, Usage}
+import org.scalasteward.core.application.ExitCodePolicy.{
+  SuccessIfAnyRepoSucceeds,
+  SuccessOnlyIfAllReposSucceed
+}
 import org.scalasteward.core.forge.ForgeType
 import org.scalasteward.core.forge.github.GitHubApp
 import org.scalasteward.core.util.Nel
+
 import scala.concurrent.duration._
 
 class CliTest extends FunSuite {
@@ -207,6 +212,19 @@ class CliTest extends FunSuite {
     assert(!obtained.forgeCfg.addLabels)
   }
 
+  test("parseArgs: exit code policy: --exit-code-success-if-any-repo-succeeds") {
+    val params = minimumRequiredParams ++ List(
+      List("--exit-code-success-if-any-repo-succeeds")
+    )
+    val Success(Usage.Regular(obtained)) = Cli.parseArgs(params.flatten)
+    assert(obtained.exitCodePolicy == SuccessIfAnyRepoSucceeds)
+  }
+
+  test("parseArgs: exit code policy: default") {
+    val Success(Usage.Regular(obtained)) = Cli.parseArgs(minimumRequiredParams.flatten)
+    assert(obtained.exitCodePolicy == SuccessOnlyIfAllReposSucceed)
+  }
+
   test("parseArgs: validate pull request labeling enabled") {
     val params = minimumRequiredParams ++ List(
       List("--forge-type", "bitbucket"),
diff --git a/modules/core/src/test/scala/org/scalasteward/core/application/StewardAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/application/StewardAlgTest.scala
index 1e686cac..72a194e7 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/application/StewardAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/application/StewardAlgTest.scala
@@ -8,6 +8,9 @@ import org.scalasteward.core.mock.{MockConfig, MockState}
 class StewardAlgTest extends CatsEffectSuite {
   test("runF") {
     val exitCode = stewardAlg.runF.runA(MockState.empty.addUris(MockConfig.reposFile -> ""))
-    assertIO(exitCode, ExitCode.Success)
+    assertIO(
+      exitCode,
+      ExitCode.Error
+    ) // We have not passed any repos to Scala Steward therefore it's reasonable that it will return error code.
   }
 }