Repository: scala-steward-org/scala-steward
Pull request #1866 (2021-01-05 16:04:49)

BitbucketServer: do not hardcode default branch

codecov[bot]: # [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=h1) Report
> Merging [#1866](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=desc) (c581aad) into [master](https://codecov.io/gh/scala-steward-org/scala-steward/commit/572c6925b850fae4a582d3aad470c650d56c2822?el=desc) (572c692) will **increase** coverage by `0.25%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866/graphs/tree.svg?width=650&height=150&src=pr&token=udzMsc1Dne)](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1866      +/-   ##
==========================================
+ Coverage   75.33%   75.59%   +0.25%     
==========================================
  Files         127      126       -1     
  Lines        2141     2147       +6     
  Branches       64       59       -5     
==========================================
+ Hits         1613     1623      +10     
+ Misses        528      524       -4     
```


| [Impacted Files](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...re/vcs/bitbucketserver/BitbucketServerApiAlg.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866/diff?src=pr&el=tree#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS92Y3MvYml0YnVja2V0c2VydmVyL0JpdGJ1Y2tldFNlcnZlckFwaUFsZy5zY2FsYQ==) | `48.64% <71.42%> (+11.50%)` | :arrow_up: |
| [...g/scalasteward/core/vcs/bitbucketserver/Json.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866/diff?src=pr&el=tree#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS92Y3MvYml0YnVja2V0c2VydmVyL0pzb24uc2NhbGE=) | `95.45% <75.00%> (-4.55%)` | :arrow_down: |
| [...rg/scalasteward/core/vcs/bitbucketserver/Url.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866/diff?src=pr&el=tree#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS92Y3MvYml0YnVja2V0c2VydmVyL1VybC5zY2FsYQ==) | `85.71% <100.00%> (+8.79%)` | :arrow_up: |
| [...la-2.12/org/scalasteward/mill/plugin/package.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866/diff?src=pr&el=tree#diff-bW9kdWxlcy9taWxsLXBsdWdpbi9zcmMvbWFpbi9zY2FsYS0yLjEyL29yZy9zY2FsYXN0ZXdhcmQvbWlsbC9wbHVnaW4vcGFja2FnZS5zY2FsYQ==) | | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=footer). Last update [572c692...a8702b0](https://codecov.io/gh/scala-steward-org/scala-steward/pull/1866?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlg.scala
index fb44c511..012d036c 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlg.scala
@@ -33,7 +33,7 @@ final class BitbucketServerApiAlg[F[_]](
     useReviewers: Boolean
 )(implicit client: HttpJsonClient[F], F: MonadThrow[F])
     extends VCSApiAlg[F] {
-  val url = new Url(bitbucketApiHost)
+  private val url = new Url(bitbucketApiHost)
 
   override def closePullRequest(repo: Repo, number: PullRequestNumber): F[PullRequestOut] =
     getPullRequest(repo, number).flatMap { pr =>
@@ -83,10 +83,10 @@ final class BitbucketServerApiAlg[F[_]](
   private def useDefaultReviewers(repo: Repo): F[List[Reviewer]] =
     if (useReviewers) getDefaultReviewers(repo) else F.pure(List.empty[Reviewer])
 
-  def declinePullRequest(repo: Repo, number: PullRequestNumber, version: Int): F[Unit] =
+  private def declinePullRequest(repo: Repo, number: PullRequestNumber, version: Int): F[Unit] =
     client.post_(url.declinePullRequest(repo, number, version), modify(repo))
 
-  def getDefaultReviewers(repo: Repo): F[List[Reviewer]] =
+  private def getDefaultReviewers(repo: Repo): F[List[Reviewer]] =
     client.get[List[Json.Condition]](url.reviewers(repo), modify(repo)).map { conditions =>
       conditions.flatMap { condition =>
         condition.reviewers.map(reviewer => Reviewer(User(reviewer.name)))
@@ -94,18 +94,22 @@ final class BitbucketServerApiAlg[F[_]](
     }
 
   override def getBranch(repo: Repo, branch: Branch): F[BranchOut] =
-    client.get[Json.Branches](url.listBranch(repo, branch), modify(repo)).map { branches =>
-      BranchOut(Branch(branches.values.head.id), CommitOut(branches.values.head.latestCommit))
-    }
+    client
+      .get[Json.Branches](url.listBranch(repo, branch), modify(repo))
+      .map(_.values.head.toBranchOut)
+
+  private def getDefaultBranch(repo: Repo): F[Json.Branch] =
+    client.get[Json.Branch](url.defaultBranch(repo), modify(repo))
 
-  def getPullRequest(repo: Repo, number: PullRequestNumber): F[PR] =
+  private def getPullRequest(repo: Repo, number: PullRequestNumber): F[PR] =
     client.get[Json.PR](url.pullRequest(repo, number), modify(repo))
 
   override def getRepo(repo: Repo): F[RepoOut] =
     for {
-      r <- client.get[Json.Repo](url.repos(repo), modify(repo))
-      cloneUri = r.links("clone").find(_.name.contains("http")).get.href
-    } yield RepoOut(r.name, UserOut(repo.owner), None, cloneUri, Branch("master"))
+      jRepo <- client.get[Json.Repo](url.repos(repo), modify(repo))
+      cloneUrl <- jRepo.cloneUrlOrRaise[F]
+      defaultBranch <- getDefaultBranch(repo)
+    } yield RepoOut(jRepo.slug, UserOut(repo.owner), None, cloneUrl, defaultBranch.displayId)
 
   override def listPullRequests(repo: Repo, head: String, base: Branch): F[List[PullRequestOut]] =
     client
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Json.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Json.scala
index cc174d75..1f8990b6 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Json.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Json.scala
@@ -17,17 +17,25 @@
 package org.scalasteward.core.vcs.bitbucketserver
 
 import cats.data.NonEmptyList
+import cats.effect.ApplicativeThrow
 import io.circe.generic.semiauto.{deriveCodec, deriveDecoder, deriveEncoder}
 import io.circe.{Codec, Decoder, Encoder}
 import org.http4s.Uri
+import org.scalasteward.core.git
 import org.scalasteward.core.git.Sha1
 import org.scalasteward.core.util.uri.uriDecoder
-import org.scalasteward.core.vcs.data.{PullRequestNumber, PullRequestOut, PullRequestState}
+import org.scalasteward.core.vcs.data._
 
 object Json {
   case class Page[A](values: List[A])
 
-  case class Repo(id: Int, name: String, forkable: Boolean, project: Project, links: Links)
+  case class Repo(slug: String, links: Links) {
+    def cloneUrlOrRaise[F[_]](implicit F: ApplicativeThrow[F]): F[Uri] =
+      links
+        .get("clone")
+        .flatMap(_.find(_.name.contains("http")).map(_.href))
+        .fold(F.raiseError[Uri](new Throwable(s"$links does not contain a HTTP clone URL")))(F.pure)
+  }
 
   case class Project(key: String)
 
@@ -75,7 +83,10 @@ object Json {
 
   case class Branches(values: NonEmptyList[Branch])
 
-  case class Branch(id: String, latestCommit: Sha1)
+  case class Branch(displayId: git.Branch, latestCommit: Sha1) {
+    def toBranchOut: BranchOut =
+      BranchOut(displayId, CommitOut(latestCommit))
+  }
 
   case class Comment(text: String)
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Url.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Url.scala
index cc78d66c..c28cef1b 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Url.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/bitbucketserver/Url.scala
@@ -33,6 +33,9 @@ final class Url(apiHost: Uri) {
   def declinePullRequest(repo: Repo, number: PullRequestNumber, version: Int): Uri =
     (pullRequest(repo, number) / "decline").withQueryParam("version", version)
 
+  def defaultBranch(repo: Repo): Uri =
+    branches(repo) / "default"
+
   def listBranch(repo: Repo, branch: Branch): Uri =
     branches(repo).withQueryParam("filterText", branch.name)
 


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlgTest.scala
index 715223d0..2fce2e83 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/vcs/bitbucketserver/BitbucketServerApiAlgTest.scala
@@ -14,6 +14,18 @@ import org.scalasteward.core.vcs.data._
 class BitbucketServerApiAlgTest extends FunSuite {
   private val repo = Repo("scala-steward-org", "scala-steward")
   private val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
+    case GET -> Root / "rest" / "api" / "1.0" / "projects" / repo.owner / "repos" / repo.repo =>
+      Ok(s"""{
+            |  "slug": "${repo.repo}",
+            |  "links": { "clone": [ { "href": "http://example.org/scala-steward.git", "name": "http" } ] }
+            |}""".stripMargin)
+
+    case GET -> Root / "rest" / "api" / "1.0" / "projects" / repo.owner / "repos" / repo.repo / "branches" / "default" =>
+      Ok(s"""{
+            |  "displayId": "main",
+            |  "latestCommit": "00213685b18016c86961a7f015793aa09e722db2"
+            |}""".stripMargin)
+
     case GET -> Root / "rest" / "api" / "1.0" / "projects" / repo.owner / "repos" / repo.repo / "pull-requests" =>
       Ok("""{ "values": [
            |  {
@@ -53,7 +65,6 @@ class BitbucketServerApiAlgTest extends FunSuite {
     val pullRequests = bitbucketServerApiAlg
       .listPullRequests(repo, "update/sbt-1.4.2", Branch("main"))
       .unsafeRunSync()
-
     val expected = List(
       PullRequestOut(
         Uri.unsafeFromString("http://example.org"),
@@ -69,13 +80,12 @@ class BitbucketServerApiAlgTest extends FunSuite {
   test("closePullRequest") {
     val obtained =
       bitbucketServerApiAlg.closePullRequest(repo, PullRequestNumber(4711)).unsafeRunSync()
-    val expected =
-      PullRequestOut(
-        Uri.unsafeFromString("http://example.org"),
-        PullRequestState.Closed,
-        PullRequestNumber(4711),
-        "Update sbt to 1.4.6"
-      )
+    val expected = PullRequestOut(
+      Uri.unsafeFromString("http://example.org"),
+      PullRequestState.Closed,
+      PullRequestNumber(4711),
+      "Update sbt to 1.4.6"
+    )
     assertEquals(obtained, expected)
   }
 
@@ -85,4 +95,16 @@ class BitbucketServerApiAlgTest extends FunSuite {
       .unsafeRunSync()
     assertEquals(comment, Comment("Superseded by #1234"))
   }
+
+  test("getRepo") {
+    val obtained = bitbucketServerApiAlg.getRepo(repo).unsafeRunSync()
+    val expected = RepoOut(
+      "scala-steward",
+      UserOut("scala-steward-org"),
+      None,
+      Uri.unsafeFromString("http://example.org/scala-steward.git"),
+      Branch("main")
+    )
+    assertEquals(obtained, expected)
+  }
 }