Skip to content

Commit

Permalink
Merge pull request #159 from Kevin-Lee/task/146/use-http4s-to-use-git…
Browse files Browse the repository at this point in the history
…hub-api

Close #146 - Use http4s instead of github4s
  • Loading branch information
kevin-lee authored Jan 31, 2021
2 parents 32d0a21 + ed0738f commit 41e82be
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 86 deletions.
118 changes: 118 additions & 0 deletions src/main/scala/kevinlee/github/GitHubApi.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package kevinlee.github

import cats.Monad
import cats.data.{EitherT, NonEmptyList}
import cats.syntax.all._
import effectie.cats.EffectConstructor
import effectie.cats.Effectful._
import kevinlee.git.Git
import kevinlee.github.data._
import kevinlee.http.{HttpClient, HttpRequest}
Expand All @@ -15,6 +17,14 @@ import scala.concurrent.ExecutionContext
*/
trait GitHubApi[F[_]] {

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
def release(
repo: GitHubRepoWithAuth,
tagName: Git.TagName,
changelog: Changelog,
assets: List[File],
)(implicit ec: ExecutionContext): F[Either[GitHubError, GitHubRelease.Response]]

def findReleaseByTagName(
tagName: Git.TagName,
repo: GitHubRepoWithAuth,
Expand Down Expand Up @@ -52,6 +62,114 @@ object GitHubApi {

val DefaultAccept: String = "application/vnd.github.v3+json"

val contentTypeMap: javax.activation.MimetypesFileTypeMap = {
val map = new javax.activation.MimetypesFileTypeMap()
map.addMimeTypes("application/zip jar zip")
map.addMimeTypes("application/xml pom xml")
map
}

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
override def release(
repo: GitHubRepoWithAuth,
tagName: Git.TagName,
changelog: Changelog,
assets: List[File],
)(implicit ec: ExecutionContext): F[Either[GitHubError, GitHubRelease.Response]] = (for {
maybeExistingRelease <- EitherT(
findReleaseByTagName(
tagName,
repo,
)
)

maybeRelease <- EitherT(
maybeExistingRelease match {
case Some(_) =>
pureOf(
GitHubError
.releaseAlreadyExists(tagName)
.asLeft[Option[GitHubRelease.Response]]
)

case None =>
this.createRelease(
GitHubRelease.CreateRequestParams(
tagName,
GitHubRelease.ReleaseName(tagName.value).some,
GitHubRelease.Description(changelog.changelog).some,
GitHubRelease.Draft.no,
GitHubRelease.Prerelease.no,
),
repo,
)
}
)

release <- EitherT(
maybeRelease match {
case Some(release) =>
assets
.traverse { file =>
val contentType = contentTypeMap.getContentType(file)
val filename = file.getName
EitherT(
uploadAssetToRelease(
GitHubRelease.UploadAssetParams(
GitHubRelease.ReleaseId(release.id.id),
GitHubRelease.UploadAssetParams.AssetName(filename),
none[GitHubRelease.UploadAssetParams.AssetLabel],
GitHubRelease
.UploadAssetParams
.AssetFile(
file,
List(GitHubRelease.Asset.ContentType(contentType)),
),
),
repo,
)
).transform {
case Left(err) =>
GitHubRelease
.Asset
.FailedAssetUpload(file, err.some)
.asLeft[GitHubRelease.Asset]

case Right((file, Some(asset))) =>
asset.asRight[GitHubRelease.Asset.FailedAssetUpload]

case Right((file, None)) =>
GitHubRelease
.Asset
.FailedAssetUpload(file, none[GitHubError])
.asLeft[GitHubRelease.Asset]

}.value
}
.map { results =>
results.partitionBifold(identity) match {
case ((Nil, assets)) =>
release
.copy(assets = release.assets ++ assets)
.asRight[GitHubError]

case ((failed :: rest, succeeded)) =>
GitHubError
.assetUploadFailure(
NonEmptyList(failed, rest),
succeeded,
)
.asLeft[GitHubRelease.Response]
}
}

case None =>
pureOf(GitHubError.noReleaseCreated.asLeft[GitHubRelease.Response])
}
)

} yield release).value

def findReleaseByTagName(
tagName: Git.TagName,
repo: GitHubRepoWithAuth,
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/kevinlee/http/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object HttpClient {
// if (httpRequest.isBodyMultipart)
// Set.empty[MediaRange]
// else
implicitly[EntityDecoder[F, A]].consumes,
implicitly[EntityDecoder[F, A]].consumes,
)
)

Expand All @@ -79,8 +79,8 @@ object HttpClient {
)
// .leftFlatMap(err => EitherT(effectOf(HttpError.recoverFromOptional404[A](err))))
)(
err => error(err.show),
res => info(String.valueOf(res)),
err => debug(err.show),
res => debug(String.valueOf(res)),
)
} yield res

Expand Down
3 changes: 0 additions & 3 deletions src/main/scala/kevinlee/http/HttpRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ object HttpRequest {
uri.withQueryParam(param.param._1, param.param._2)
}
}
println(s"""
|uriWithParams: ${uriWithParams.toString}
|""".stripMargin)
httpRequest.httpMethod match {
case HttpRequest.Method.Get =>
httpRequest
Expand Down
Loading

0 comments on commit 41e82be

Please sign in to comment.