Skip to content

Commit

Permalink
Split getRepoConfig into readRepoConfig{,OrDefault}
Browse files Browse the repository at this point in the history
This will be useful for #592
where we only want to cache actual `RepoConfig`s and not also the default
values.
  • Loading branch information
fthomas committed Jul 2, 2019
1 parent b9aa64b commit f90a396
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class NurtureAlg[F[_]](
def updateDependencies(repo: Repo, fork: Repo, baseBranch: Branch): F[Unit] =
for {
_ <- logger.info(s"Find updates for ${repo.show}")
repoConfig <- repoConfigAlg.getRepoConfig(repo)
repoConfig <- repoConfigAlg.readRepoConfigOrDefault(repo)
updates <- sbtAlg.getUpdatesForRepo(repo)
filtered <- filterAlg.localFilterMany(repoConfig, updates)
grouped = Update.group(filtered)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final case class RepoConfig(
)

object RepoConfig {
val default = RepoConfig()

implicit val customConfig: Configuration =
Configuration.default.withDefaults

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import cats.data.OptionT
import cats.implicits._
import io.chrisdavenport.log4cats.Logger
import io.circe.config.parser
import org.scalasteward.core.vcs.data.Repo
import org.scalasteward.core.io.{FileAlg, WorkspaceAlg}
import org.scalasteward.core.model.Update
import org.scalasteward.core.repoconfig.RepoConfigAlg._
import org.scalasteward.core.util.MonadThrowable
import org.scalasteward.core.vcs.data.Repo

final class RepoConfigAlg[F[_]](
implicit
Expand All @@ -33,16 +33,17 @@ final class RepoConfigAlg[F[_]](
workspaceAlg: WorkspaceAlg[F],
F: MonadThrowable[F]
) {
def getRepoConfig(repo: Repo): F[RepoConfig] =
def readRepoConfigOrDefault(repo: Repo): F[RepoConfig] =
readRepoConfig(repo).map(_.getOrElse(RepoConfig.default))

def readRepoConfig(repo: Repo): F[Option[RepoConfig]] =
workspaceAlg.repoDir(repo).flatMap { dir =>
val configFile = dir / repoConfigBasename
val maybeRepoConfig = OptionT(fileAlg.readFile(configFile)).flatMapF { content =>
parseRepoConfig(content) match {
case Right(config) => logger.info(s"Parsed $config") >> F.pure(config.some)
case Left(errorMsg) => logger.info(errorMsg).as(none[RepoConfig])
}
val maybeRepoConfig = OptionT(fileAlg.readFile(configFile)).map(parseRepoConfig).flatMapF {
case Right(config) => logger.info(s"Parsed $config") >> F.pure(config.some)
case Left(errorMsg) => logger.info(errorMsg).as(none[RepoConfig])
}
maybeRepoConfig.getOrElse(RepoConfig())
maybeRepoConfig.value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RepoConfigAlgTest extends FunSuite with Matchers {
|updates.ignore = [ { groupId = "org.acme", version = "1.0" } ]
|""".stripMargin
val initialState = MockState.empty.add(configFile, content)
val config = repoConfigAlg.getRepoConfig(repo).runA(initialState).unsafeRunSync()
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(
updates = UpdatesConfig(
Expand All @@ -32,7 +32,7 @@ class RepoConfigAlgTest extends FunSuite with Matchers {
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.getRepoConfig(repo).runA(initialState).unsafeRunSync()
val config = repoConfigAlg.readRepoConfigOrDefault(repo).runA(initialState).unsafeRunSync()

config shouldBe RepoConfig(updatePullRequests = false)
}
Expand All @@ -41,7 +41,8 @@ class RepoConfigAlgTest extends FunSuite with Matchers {
val repo = Repo("fthomas", "scala-steward")
val configFile = File.temp / "ws/fthomas/scala-steward/.scala-steward.conf"
val initialState = MockState.empty.add(configFile, """updates.ignore = [ "foo """)
val (state, config) = repoConfigAlg.getRepoConfig(repo).run(initialState).unsafeRunSync()
val (state, config) =
repoConfigAlg.readRepoConfigOrDefault(repo).run(initialState).unsafeRunSync()

config shouldBe RepoConfig()
state.logs.headOption.map { case (_, msg) => msg }.getOrElse("") should
Expand Down

0 comments on commit f90a396

Please sign in to comment.