Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions github4s/src/main/scala/github4s/Decoders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package github4s

import cats.data.NonEmptyList
import cats.syntax.all._
import github4s.domain.RepoUrlKeys.{CommitComparisonResponse, FileComparison}
import github4s.domain._
import io.circe.Decoder.Result
import io.circe._
Expand Down Expand Up @@ -241,6 +242,8 @@ object Decoders {
)
)

implicit val decoderFileComparison: Decoder[FileComparison] = deriveDecoder[FileComparison]

implicit val decoderCreatePullRequestData: Decoder[CreatePullRequestData] =
deriveDecoder[CreatePullRequestData]
implicit val decoderCreatePullRequestIssue: Decoder[CreatePullRequestIssue] =
Expand All @@ -265,6 +268,8 @@ object Decoders {
implicit val decoderNewTagRequest: Decoder[NewTagRequest] = deriveDecoder[NewTagRequest]
implicit val decoderNewTreeRequest: Decoder[NewTreeRequest] = deriveDecoder[NewTreeRequest]
implicit val decoderNewCommitRequest: Decoder[NewCommitRequest] = deriveDecoder[NewCommitRequest]
implicit val decoderBranchUpdateRequest: Decoder[BranchUpdateRequest] =
deriveDecoder[BranchUpdateRequest]

implicit def decodeNonEmptyList[T](implicit D: Decoder[T]): Decoder[NonEmptyList[T]] = {

Expand Down Expand Up @@ -367,4 +372,9 @@ object Decoders {
implicit val decoderEditGistRequest: Decoder[EditGistRequest] = deriveDecoder[EditGistRequest]
implicit val decoderEditIssueRequest: Decoder[EditIssueRequest] = deriveDecoder[EditIssueRequest]
implicit val decoderMilestoneData: Decoder[MilestoneData] = deriveDecoder[MilestoneData]

implicit val decodeBranchUpdateResponse: Decoder[BranchUpdateResponse] =
deriveDecoder[BranchUpdateResponse]
implicit val decodeCommitComparisonResponse: Decoder[CommitComparisonResponse] =
deriveDecoder[CommitComparisonResponse]
}
9 changes: 9 additions & 0 deletions github4s/src/main/scala/github4s/Encoders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package github4s

import github4s.domain.RepoUrlKeys.{CommitComparisonResponse, FileComparison}
import github4s.domain._
import io.circe._
import io.circe.generic.semiauto.deriveEncoder
Expand Down Expand Up @@ -182,6 +183,9 @@ object Encoders {
deriveEncoder[NewReleaseRequest]
implicit val encoderNewStatusRequest: Encoder[NewStatusRequest] = deriveEncoder[NewStatusRequest]
implicit val encoderMilestoneData: Encoder[MilestoneData] = deriveEncoder[MilestoneData]
implicit val encodeBranchUpdateRequest: Encoder[BranchUpdateRequest] =
deriveEncoder[BranchUpdateRequest]

implicit val encoderCreateReviewComment: Encoder[CreateReviewComment] =
deriveEncoder[CreateReviewComment]
implicit val encodeNewPullRequestReview: Encoder[CreatePRReviewRequest] =
Expand Down Expand Up @@ -237,4 +241,9 @@ object Encoders {
implicit val encoderUser: Encoder[User] = deriveEncoder[User]
implicit val encoderComment: Encoder[Comment] = deriveEncoder[Comment]
implicit val encoderMilestone: Encoder[Milestone] = deriveEncoder[Milestone]
implicit val encodeBranchUpdateResponse: Encoder[BranchUpdateResponse] =
deriveEncoder[BranchUpdateResponse]
implicit val encodeFileComparison: Encoder[FileComparison] = deriveEncoder[FileComparison]
implicit val encodeCommitComparisonResponse: Encoder[CommitComparisonResponse] =
deriveEncoder[CommitComparisonResponse]
}
11 changes: 11 additions & 0 deletions github4s/src/main/scala/github4s/algebras/PullRequests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,15 @@ trait PullRequests[F[_]] {
reviewers: ReviewersRequest,
headers: Map[String, String] = Map()
): F[GHResponse[PullRequest]]

/**
* This is an experimental API and could be changed or removed
*/
def updateBranch(
owner: String,
repo: String,
pullRequest: Int,
expectedHeadSha: Option[String] = None,
headers: Map[String, String] = Map()
): F[GHResponse[BranchUpdateResponse]]
}
19 changes: 19 additions & 0 deletions github4s/src/main/scala/github4s/algebras/Repositories.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package github4s.algebras

import cats.data.NonEmptyList
import github4s.GHResponse
import github4s.domain.RepoUrlKeys.CommitComparisonResponse
import github4s.domain._

trait Repositories[F[_]] {
Expand Down Expand Up @@ -228,6 +229,24 @@ trait Repositories[F[_]] {
headers: Map[String, String] = Map()
): F[GHResponse[List[Commit]]]

/**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add what it does

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

* Compare any two commits in the same repository
*
* @param owner of the repo
* @param repo name of the repo
* @param commitSha commit to compare against base
* @param baseSha the base to compare against
* @param headers optional user headers to include in the request
* @return GhResponse[CommitComparisonResponse] comparison result
*/
def compareCommits(
owner: String,
repo: String,
commitSha: String,
baseSha: String,
headers: Map[String, String] = Map()
): F[GHResponse[CommitComparisonResponse]]

/**
* Retrieve list of branches for a repo
*
Expand Down
3 changes: 3 additions & 0 deletions github4s/src/main/scala/github4s/domain/PullRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,6 @@ final case class ReviewersResponse(
users: List[User],
teams: List[Team]
)

final case class BranchUpdateRequest(expected_head_sha: Option[String])
final case class BranchUpdateResponse(message: String, url: String)
29 changes: 29 additions & 0 deletions github4s/src/main/scala/github4s/domain/Repository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,33 @@ object RepoUrlKeys {
deployments_url
)

final case class CommitComparisonResponse(
status: String,
ahead_by: Int,
behind_by: Int,
total_commits: Int,
url: Option[String] = None,
html_url: Option[String] = None,
permalink_url: Option[String] = None,
diff_url: Option[String] = None,
patch_url: Option[String] = None,
base_commit: Option[Commit] = None,
merge_base_commit: Option[Commit] = None,
commits: Seq[Commit] = Seq.empty,
files: Seq[FileComparison] = Seq.empty
)

final case class FileComparison(
sha: String,
filename: String,
status: String,
additions: Int,
deletions: Int,
changes: Int,
blob_url: String,
raw_url: String,
contents_url: String,
patch: String
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,17 @@ class PullRequestsInterpreter[F[_]](implicit client: HttpClient[F]) extends Pull
headers,
reviewers
)

override def updateBranch(
owner: String,
repo: String,
pullRequest: Int,
expectedHeadSha: Option[String] = None,
headers: Map[String, String] = Map()
): F[GHResponse[BranchUpdateResponse]] =
client.put[BranchUpdateRequest, BranchUpdateResponse](
s"repos/$owner/$repo/pulls/$pullRequest/update-branch",
headers ++ Seq("Accept" -> "application/vnd.github.lydian-preview+json"),
BranchUpdateRequest(expectedHeadSha)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package github4s.interpreters
import cats.Functor
import cats.data.NonEmptyList
import cats.syntax.functor._
import github4s.internal.Base64._
import github4s.Decoders._
import github4s.Encoders._
import github4s.GHResponse
import github4s.algebras.Repositories
import github4s.domain.RepoUrlKeys.CommitComparisonResponse
import github4s.domain._
import github4s.http.HttpClient
import github4s.internal.Base64._

class RepositoriesInterpreter[F[_]: Functor](implicit
client: HttpClient[F]
Expand Down Expand Up @@ -169,6 +170,18 @@ class RepositoriesInterpreter[F[_]: Functor](implicit
pagination
)

override def compareCommits(
owner: String,
repo: String,
commitSha: String,
baseSha: String,
headers: Map[String, String] = Map()
): F[GHResponse[CommitComparisonResponse]] =
client.get[CommitComparisonResponse](
s"repos/$owner/$repo/compare/$baseSha...$commitSha",
headers
)

override def listBranches(
owner: String,
repo: String,
Expand Down Expand Up @@ -344,5 +357,4 @@ class RepositoriesInterpreter[F[_]: Functor](implicit
response.copy(result = Right(false))
case Left(error) => GHResponse(Left(error), response.statusCode, response.headers)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.eed3si9n.expecty.Expecty
import github4s.ArbitraryDerivation
import github4s.Decoders._
import github4s.Encoders._
import github4s.domain.RepoUrlKeys.CommitComparisonResponse
import github4s.domain._
import io.circe.{Decoder, Encoder, Printer}
import org.scalacheck.fortyseven.GenInstances._
Expand Down Expand Up @@ -133,6 +134,9 @@ class EncoderDecoderSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
test[Repository]
test[ReviewersRequest]
test[ReviewersResponse]
test[BranchUpdateRequest]
test[BranchUpdateResponse]
test[CommitComparisonResponse]
test[SearchIssuesResult]
test[SearchReposResult]
test[Stargazer]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,21 @@ trait PullRequestsSpec extends BaseIntegrationSpec {
)
removeReviewersResponse.statusCode shouldBe okStatusCode
}

"PullRequests >> Update Branch" should "merge target branch's head into selected" taggedAs Integration in {
val response = clientResource
.use { client =>
Github[IO](client, accessToken).pullRequests
.updateBranch(
validRepoOwner,
validRepoName,
validPullRequestNumber,
headers = headerUserAgent
)
}
.unsafeRunSync()

testIsRight[BranchUpdateResponse](response)
response.statusCode shouldBe okStatusCode
}
}
19 changes: 19 additions & 0 deletions github4s/src/test/scala/github4s/integration/ReposSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats.data.NonEmptyList
import cats.effect.{IO, Resource}
import cats.implicits._
import github4s.GHError.{NotFoundError, UnauthorizedError}
import github4s.domain.RepoUrlKeys.CommitComparisonResponse
import github4s.domain._
import github4s.utils.{BaseIntegrationSpec, Integration}
import github4s.{GHResponse, Github}
Expand Down Expand Up @@ -471,6 +472,24 @@ trait ReposSpec extends BaseIntegrationSpec {
response.statusCode shouldBe okStatusCode
}

"Repos >> Compare" should "compare against the base" taggedAs Integration in {
val response = clientResource
.use { client =>
Github[IO](client, accessToken).repos
.compareCommits(validRepoOwner, validRepoName, validCommitSha, validBase)
}
.unsafeRunSync()

testIsRight[CommitComparisonResponse](
response,
{ r =>
r.status shouldBe "behind"
r.behind_by should be > 0
}
)
response.statusCode shouldBe okStatusCode
}

it should "successfully return results when a valid repo is provided using <owner>/<name> syntax" taggedAs Integration in {
val response = clientResource
.use { client =>
Expand Down
21 changes: 21 additions & 0 deletions github4s/src/test/scala/github4s/unit/PullRequestsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,25 @@ class PullRequestsSpec extends BaseSpec {
.shouldNotFail
}

"PullRequests.updateBranch" should "call to httpClient.put with the right parameters" in {

implicit val httpClientMock: HttpClient[IO] =
httpClientMockPut[BranchUpdateRequest, BranchUpdateResponse](
url = s"repos/$validRepoOwner/$validRepoName/pulls/$validPullRequestNumber/update-branch",
req = BranchUpdateRequest(None),
response = IO.pure(validBranchUpdateResponse)
)

val pullRequests = new PullRequestsInterpreter[IO]

pullRequests
.updateBranch(
validRepoOwner,
validRepoName,
validPullRequestNumber,
None,
headerUserAgent
)
.shouldNotFail
}
}
22 changes: 22 additions & 0 deletions github4s/src/test/scala/github4s/unit/ReposSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats.data.NonEmptyList
import cats.effect.IO
import github4s.Decoders._
import github4s.Encoders._
import github4s.domain.RepoUrlKeys.CommitComparisonResponse
import github4s.domain._
import github4s.http.HttpClient
import github4s.internal.Base64._
Expand Down Expand Up @@ -434,4 +435,25 @@ class ReposSpec extends BaseSpec {

repos.searchRepos("", validSearchParams, None, headerUserAgent).shouldNotFail
}

"Repos.compare" should "call to httpClient.get with the right parameters" in {

implicit val httpClientMock: HttpClient[IO] = httpClientMockGet[CommitComparisonResponse](
url = s"repos/$validRepoOwner/$validRepoName/compare/$validCommitSha...$validMergeCommitSha",
response = IO.pure(validCommitComparisonResponse)
)

val repos = new RepositoriesInterpreter[IO]

repos
.compareCommits(
validRepoOwner,
validRepoName,
validCommitSha,
validMergeCommitSha,
headerUserAgent
)
.shouldNotFail
}

}
11 changes: 10 additions & 1 deletion github4s/src/test/scala/github4s/utils/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package github4s.utils

import java.util.UUID
import github4s.domain.RepoUrlKeys.CommitComparisonResponse

import java.util.UUID
import github4s.internal.Base64._
import github4s.domain._

Expand Down Expand Up @@ -489,6 +490,14 @@ trait TestData {
val validRequestedReviewersResponse =
ReviewersResponse(List(user), List(team))

val validBranchUpdateResponse =
BranchUpdateResponse(
"Updating pull request branch.",
s"https://github.com/repos/$validRepoOwner/$validRepoName/pulls/$validPullRequestNumber"
)

val validCommitComparisonResponse = CommitComparisonResponse("behind", 1, 2, 100)

val validProjectId = 4115271L
val invalidProjectId = 11111L

Expand Down
Loading