Skip to content
Permalink
Browse files

Change updatePullRequests config to be an enum

  • Loading branch information
kiranbayram committed Jan 8, 2020
1 parent 3441b4f commit a749f1d4fb69b5ab6c3ab3a8bdbc52e093bc060b
@@ -18,15 +18,13 @@ updates.ignore = [ { groupId = "org.acme", artifactId="foo", version = "1.0" } ]
# Default: None
updates.limit = 5
# If true, Scala Steward will update the PR it created to resolve conflicts as
# If "on-conflicts", Scala Steward will update the PR it created to resolve conflicts as
# long as you don't change it yourself.
# Default: true
updatePullRequests = true
# If true, Scala Steward will perform a check to see if the PR has conflicts in
# order to decide to update the PR (assuming that all other conditions are satisfied).
# Default: true
checkConflictsWhenUpdatingPRs = true
# If "always", Scala Steward will always update the PR it created as long as
# you don't change it yourself.
# If "never", Scala Steward will never update the PR
# Default: "on-conflicts"
updatePullRequests = "always" | "on-conflicts" | "never"
```

## Ignore lines
@@ -25,7 +25,7 @@ import org.scalasteward.core.data.ProcessResult.{Ignored, Updated}
import org.scalasteward.core.data.{ProcessResult, Update}
import org.scalasteward.core.edit.EditAlg
import org.scalasteward.core.git.{Branch, GitAlg}
import org.scalasteward.core.repoconfig.RepoConfigAlg
import org.scalasteward.core.repoconfig.{PullRequestUpdateStrategy, RepoConfigAlg}
import org.scalasteward.core.sbt.SbtAlg
import org.scalasteward.core.scalafix.MigrationAlg
import org.scalasteward.core.update.FilterAlg
@@ -164,7 +164,7 @@ final class NurtureAlg[F[_]](
} yield ()

def updatePullRequest(data: UpdateData): F[ProcessResult] =
if (data.repoConfig.updatePullRequests) {
if (data.repoConfig.updatePullRequests =!= PullRequestUpdateStrategy.Never) {
gitAlg.returnToCurrentBranch(data.repo) {
for {
_ <- gitAlg.checkoutBranch(data.repo, data.updateBranch)
@@ -185,8 +185,8 @@ final class NurtureAlg[F[_]](
if (distinctAuthors.length >= 2)
(false, s"PR has commits by ${distinctAuthors.mkString(", ")}").pure[F]
else {
if (!data.repoConfig.checkConflictsWhenUpdatingPRs)
(true, "Conflict check is disabled in repo configuration").pure[F]
if (data.repoConfig.updatePullRequests === PullRequestUpdateStrategy.Always)
(true, "PR update strategy is set to always").pure[F]
else
gitAlg.hasConflicts(data.repo, data.updateBranch, data.baseBranch).map {
case true => (true, s"PR has conflicts with ${data.baseBranch.name}")
@@ -0,0 +1,50 @@
/*
* Copyright 2018-2020 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.repoconfig

import cats.Eq
import io.circe.{Decoder, Encoder}

sealed trait PullRequestUpdateStrategy {
def name: String
}

object PullRequestUpdateStrategy {
final case object Always extends PullRequestUpdateStrategy { val name = "always" }
final case object OnConflicts extends PullRequestUpdateStrategy { val name = "on-conflicts" }
final case object Never extends PullRequestUpdateStrategy { val name = "never" }

val default = OnConflicts

def fromString(value: String): PullRequestUpdateStrategy =
value.trim.toLowerCase match {
case "on-conflicts" => OnConflicts
case "never" => Never
case "always" => Always
case _ => default
}

def fromBoolean(value: Boolean): PullRequestUpdateStrategy =
if (value) OnConflicts
else Never

implicit val prUpdateStrategyDecoder: Decoder[PullRequestUpdateStrategy] =
Decoder[Boolean].map(fromBoolean).or(Decoder[String].map(fromString))
implicit val prUpdateStrategyEncoder: Encoder[PullRequestUpdateStrategy] =
Encoder[String].contramap(_.name)
implicit val eqPRUpdateStrategy: Eq[PullRequestUpdateStrategy] = Eq.fromUniversalEquals
}
@@ -19,11 +19,11 @@ package org.scalasteward.core.repoconfig
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto._
import io.circe.{Decoder, Encoder}
import PullRequestUpdateStrategy.{prUpdateStrategyDecoder, prUpdateStrategyEncoder}

final case class RepoConfig(
updates: UpdatesConfig = UpdatesConfig(),
updatePullRequests: Boolean = true,
checkConflictsWhenUpdatingPRs: Boolean = true
updatePullRequests: PullRequestUpdateStrategy = PullRequestUpdateStrategy.default
)

object RepoConfig {
@@ -31,24 +31,54 @@ class RepoConfigAlgTest extends AnyFunSuite with Matchers {
)
}

test("config with 'updateBranch disabled'") {
test("config with 'updatePullRequests = false'") {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val content = "updatePullRequests = false"
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(updatePullRequests = false)
config shouldBe RepoConfig(updatePullRequests = PullRequestUpdateStrategy.Never)
}

test("config with 'checkConflictsWhenUpdatingPRs disabled'") {
test("config with 'updatePullRequests = true'") {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val content = "checkConflictsWhenUpdatingPRs = false"
val content = "updatePullRequests = true"
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(checkConflictsWhenUpdatingPRs = false)
config shouldBe RepoConfig(updatePullRequests = PullRequestUpdateStrategy.OnConflicts)
}

test("config with 'updatePullRequests = always") {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val content = """updatePullRequests = "always" """
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(updatePullRequests = PullRequestUpdateStrategy.Always)
}

test("config with 'updatePullRequests = on-conflicts") {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val content = """updatePullRequests = "on-conflicts" """
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(updatePullRequests = PullRequestUpdateStrategy.OnConflicts)
}

test("config with 'updatePullRequests = never") {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val content = """updatePullRequests = "never" """
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(updatePullRequests = PullRequestUpdateStrategy.Never)
}

test("malformed config") {

0 comments on commit a749f1d

Please sign in to comment.
You can’t perform that action at this time.