Repository: scala-steward-org/scala-steward
Pull request #1635 (2020-10-03 14:44:14)

Clean up and remove duplication in VCSApiAlg
This implements `createForkOrGetRepoWithDefaultBranch` in terms of
`createForkOrGetRepo` instead of duplicating its logic.


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
index 3e3fabd2..fcc07739 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
@@ -66,12 +66,12 @@ final class NurtureAlg[F[_]](implicit
   def cloneAndSync(repo: Repo): F[(Repo, Branch)] =
     for {
       _ <- logger.info(s"Clone and synchronize ${repo.show}")
-      repoOut <- vcsApiAlg.createForkOrGetRepo(config, repo)
+      repoOut <- vcsApiAlg.createForkOrGetRepo(repo, config.doNotFork)
       _ <-
         gitAlg
           .cloneExists(repo)
           .ifM(F.unit, vcsRepoAlg.clone(repo, repoOut) >> vcsRepoAlg.syncFork(repo, repoOut))
-      defaultBranch <- vcsRepoAlg.defaultBranch(repoOut)
+      defaultBranch <- vcsApiAlg.parentOrRepo(repoOut, config.doNotFork).map(_.default_branch)
     } yield (repoOut.repo, defaultBranch)
 
   def updateDependencies(
diff --git a/modules/core/src/main/scala/org/scalasteward/core/repocache/RepoCacheAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/repocache/RepoCacheAlg.scala
index 88c75169..c20696d2 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/repocache/RepoCacheAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/repocache/RepoCacheAlg.scala
@@ -48,7 +48,7 @@ final class RepoCacheAlg[F[_]](implicit
         logger.info(s"Skipping due to previous error"),
         for {
           ((repoOut, branchOut), cachedSha1) <- (
-            vcsApiAlg.createForkOrGetRepoWithDefaultBranch(config, repo),
+            vcsApiAlg.createForkOrGetRepoWithDefaultBranch(repo, config.doNotFork),
             repoCacheRepository.findSha1(repo)
           ).parTupled
           latestSha1 = branchOut.commit.sha
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSApiAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSApiAlg.scala
index 4b9de4a2..bf8ff364 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSApiAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSApiAlg.scala
@@ -16,11 +16,9 @@
 
 package org.scalasteward.core.vcs
 
-import cats.Monad
 import cats.syntax.all._
-import org.scalasteward.core.application.Config
 import org.scalasteward.core.git.Branch
-import org.scalasteward.core.util.MonadThrowable
+import org.scalasteward.core.util.{ApplicativeThrowable, MonadThrowable}
 import org.scalasteward.core.vcs.data._
 
 trait VCSApiAlg[F[_]] {
@@ -34,33 +32,27 @@ trait VCSApiAlg[F[_]] {
 
   def listPullRequests(repo: Repo, head: String, base: Branch): F[List[PullRequestOut]]
 
-  final def createForkOrGetRepo(config: Config, repo: Repo): F[RepoOut] =
-    if (config.doNotFork) getRepo(repo)
-    else createFork(repo)
+  final def createForkOrGetRepo(repo: Repo, doNotFork: Boolean): F[RepoOut] =
+    if (doNotFork) getRepo(repo) else createFork(repo)
 
-  final def createForkOrGetRepoWithDefaultBranch(config: Config, repo: Repo)(implicit
+  final def createForkOrGetRepoWithDefaultBranch(repo: Repo, doNotFork: Boolean)(implicit
       F: MonadThrowable[F]
   ): F[(RepoOut, BranchOut)] =
-    if (config.doNotFork) getRepoWithDefaultBranch(repo)
-    else createForkWithDefaultBranch(repo)
+    for {
+      forkOrRepo <- createForkOrGetRepo(repo, doNotFork)
+      defaultBranch <- getDefaultBranchOfParentOrRepo(forkOrRepo, doNotFork)
+    } yield (forkOrRepo, defaultBranch)
 
-  final def createForkWithDefaultBranch(repo: Repo)(implicit
+  final def getDefaultBranchOfParentOrRepo(repoOut: RepoOut, doNotFork: Boolean)(implicit
       F: MonadThrowable[F]
-  ): F[(RepoOut, BranchOut)] =
-    for {
-      fork <- createFork(repo)
-      parent <- fork.parentOrRaise[F]
-      branchOut <- getDefaultBranch(parent)
-    } yield (fork, branchOut)
+  ): F[BranchOut] =
+    parentOrRepo(repoOut, doNotFork).flatMap(getDefaultBranch)
 
-  final def getRepoWithDefaultBranch(repo: Repo)(implicit
-      F: Monad[F]
-  ): F[(RepoOut, BranchOut)] =
-    for {
-      repoOut <- getRepo(repo)
-      branchOut <- getDefaultBranch(repoOut)
-    } yield (repoOut, branchOut)
+  final def parentOrRepo(repoOut: RepoOut, doNotFork: Boolean)(implicit
+      F: ApplicativeThrowable[F]
+  ): F[RepoOut] =
+    if (doNotFork) F.pure(repoOut) else repoOut.parentOrRaise[F]
 
-  final def getDefaultBranch(repoOut: RepoOut): F[BranchOut] =
+  private def getDefaultBranch(repoOut: RepoOut): F[BranchOut] =
     getBranch(repoOut.repo, repoOut.default_branch)
 }
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
index 8b98cf31..530153ad 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
@@ -20,7 +20,7 @@ import cats.syntax.all._
 import org.http4s.Uri
 import org.http4s.Uri.UserInfo
 import org.scalasteward.core.application.Config
-import org.scalasteward.core.git.{Branch, GitAlg}
+import org.scalasteward.core.git.GitAlg
 import org.scalasteward.core.util
 import org.scalasteward.core.util.MonadThrowable
 import org.scalasteward.core.vcs.data.{Repo, RepoOut}
@@ -28,8 +28,6 @@ import org.scalasteward.core.vcs.data.{Repo, RepoOut}
 trait VCSRepoAlg[F[_]] {
   def clone(repo: Repo, repoOut: RepoOut): F[Unit]
 
-  def defaultBranch(repoOut: RepoOut): F[Branch]
-
   def syncFork(repo: Repo, repoOut: RepoOut): F[Unit]
 }
 
@@ -42,10 +40,6 @@ object VCSRepoAlg {
           _ <- gitAlg.setAuthor(repo, config.gitAuthor)
         } yield ()
 
-      override def defaultBranch(repoOut: RepoOut): F[Branch] =
-        if (config.doNotFork) repoOut.default_branch.pure[F]
-        else repoOut.parentOrRaise[F].map(_.default_branch)
-
       override def syncFork(repo: Repo, repoOut: RepoOut): F[Unit] =
         if (config.doNotFork) ().pure[F]
         else
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 52ea87f6..6b77eaa4 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -17,7 +17,7 @@ object Dependencies {
   val circeParser = "io.circe" %% "circe-parser" % circeGeneric.revision
   val circeRefined = "io.circe" %% "circe-refined" % circeGeneric.revision
   val commonsIo = "commons-io" % "commons-io" % "2.8.0"
-  val coursierCore = "io.get-coursier" %% "coursier" % "2.0.0"
+  val coursierCore = "io.get-coursier" %% "coursier" % "2.0.0-RC6-27"
   val coursierCatsInterop = "io.get-coursier" %% "coursier-cats-interop" % coursierCore.revision
   val cron4sCore = "com.github.alonsodomin.cron4s" %% "cron4s-core" % "0.6.1"
   val disciplineScalatest = ("org.typelevel" %% "discipline-scalatest" % "2.0.1")


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/bitbucket/http4s/Http4sBitbucketApiAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/bitbucket/http4s/Http4sBitbucketApiAlgTest.scala
index b8e98c63..48cdc913 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/bitbucket/http4s/Http4sBitbucketApiAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/bitbucket/http4s/Http4sBitbucketApiAlgTest.scala
@@ -161,29 +161,25 @@ class Http4sBitbucketApiAlgTest extends AnyFunSuite with Matchers {
   val pullRequest = PullRequestOut(prUrl, PullRequestState.Open, "scala-steward-pr")
 
   test("createForkOrGetRepo") {
-    val repoOut =
-      bitbucketApiAlg.createForkOrGetRepo(config, repo).unsafeRunSync()
+    val repoOut = bitbucketApiAlg.createForkOrGetRepo(repo, doNotFork = false).unsafeRunSync()
     repoOut shouldBe fork
   }
 
   test("createForkOrGetRepo without forking") {
-    val repoOut =
-      bitbucketApiAlg.createForkOrGetRepo(config.copy(doNotFork = true), repo).unsafeRunSync()
+    val repoOut = bitbucketApiAlg.createForkOrGetRepo(repo, doNotFork = true).unsafeRunSync()
     repoOut shouldBe parent
   }
 
   test("createForkOrGetRepoWithDefaultBranch") {
     val (repoOut, branchOut) =
-      bitbucketApiAlg.createForkOrGetRepoWithDefaultBranch(config, repo).unsafeRunSync()
+      bitbucketApiAlg.createForkOrGetRepoWithDefaultBranch(repo, doNotFork = false).unsafeRunSync()
     repoOut shouldBe fork
     branchOut shouldBe defaultBranch
   }
 
   test("createForkOrGetRepoWithDefaultBranch without forking") {
     val (repoOut, branchOut) =
-      bitbucketApiAlg
-        .createForkOrGetRepoWithDefaultBranch(config.copy(doNotFork = true), repo)
-        .unsafeRunSync()
+      bitbucketApiAlg.createForkOrGetRepoWithDefaultBranch(repo, doNotFork = true).unsafeRunSync()
     repoOut shouldBe parent
     branchOut shouldBe defaultBranch
   }
@@ -193,7 +189,7 @@ class Http4sBitbucketApiAlgTest extends AnyFunSuite with Matchers {
       "scala-steward-pr",
       "body",
       "master",
-      Branch("master")
+      master
     )
     val pr = bitbucketApiAlg.createPullRequest(repo, data).unsafeRunSync()
     pr shouldBe pullRequest
diff --git a/modules/core/src/test/scala/org/scalasteward/core/github/http4s/Http4sGitHubApiAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/github/http4s/Http4sGitHubApiAlgTest.scala
index a6bb8a58..5548dafa 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/github/http4s/Http4sGitHubApiAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/github/http4s/Http4sGitHubApiAlgTest.scala
@@ -85,29 +85,25 @@ class Http4sGitHubApiAlgTest extends AnyFunSuite with Matchers {
   )
 
   test("createForkOrGetRepo") {
-    val repoOut =
-      gitHubApiAlg.createForkOrGetRepo(config, repo).unsafeRunSync()
+    val repoOut = gitHubApiAlg.createForkOrGetRepo(repo, doNotFork = false).unsafeRunSync()
     repoOut shouldBe fork
   }
 
   test("createForkOrGetRepo without forking") {
-    val repoOut =
-      gitHubApiAlg.createForkOrGetRepo(config.copy(doNotFork = true), repo).unsafeRunSync()
+    val repoOut = gitHubApiAlg.createForkOrGetRepo(repo, doNotFork = true).unsafeRunSync()
     repoOut shouldBe parent
   }
 
   test("createForkOrGetRepoWithDefaultBranch") {
     val (repoOut, branchOut) =
-      gitHubApiAlg.createForkOrGetRepoWithDefaultBranch(config, repo).unsafeRunSync()
+      gitHubApiAlg.createForkOrGetRepoWithDefaultBranch(repo, doNotFork = false).unsafeRunSync()
     repoOut shouldBe fork
     branchOut shouldBe defaultBranch
   }
 
   test("createForkOrGetRepoWithDefaultBranch without forking") {
     val (repoOut, branchOut) =
-      gitHubApiAlg
-        .createForkOrGetRepoWithDefaultBranch(config.copy(doNotFork = true), repo)
-        .unsafeRunSync()
+      gitHubApiAlg.createForkOrGetRepoWithDefaultBranch(repo, doNotFork = true).unsafeRunSync()
     repoOut shouldBe parent
     branchOut shouldBe defaultBranch
   }