Skip to content

Commit

Permalink
Merge pull request #151 from Kevin-Lee/task/146/create-github-release
Browse files Browse the repository at this point in the history
Issue #146 - Add 'create GitHub release'
  • Loading branch information
kevin-lee authored Jan 22, 2021
2 parents 34ff9b3 + 676fe5f commit cc5a846
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/main/scala/kevinlee/github/GitHubApi.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package kevinlee.github

import cats.Monad
import cats.syntax.all._
import io.circe._
import kevinlee.github.data._
import kevinlee.http.{HttpClient, HttpRequest}

/** @author Kevin Lee
* @since 2021-01-14
*/
trait GitHubApi[F[_]] {
def createRelease(
params: GitHubRelease.RequestParams,
repo: GitHubRepoWithAuth,
): F[Either[GitHubError, Option[GitHubRelease.Response]]]
}

object GitHubApi {

def apply[F[_]: Monad](httpClient: HttpClient[F]): GitHubApi[F] = new GitHubApiF[F](httpClient)

final class GitHubApiF[F[_]: Monad](val httpClient: HttpClient[F]) extends GitHubApi[F] {
// TODO: make it configurable
val baseUrl: String = "https://api.github.com"

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

override def createRelease(
params: GitHubRelease.RequestParams,
repo: GitHubRepoWithAuth,
): F[Either[GitHubError, Option[GitHubRelease.Response]]] = {
val url = s"$baseUrl/repos/${repo.gitHubRepo.org.org}/${repo.gitHubRepo.repo.repo}/releases"
val body = Encoder[GitHubRelease.RequestParams].apply(params)
val httpRequest = HttpRequest.withHeadersAndBody(
HttpRequest.Method.post,
HttpRequest.Uri(url),
HttpRequest.Header("accept" -> DefaultAccept) ::
repo
.accessToken
.toHeaderList,
body,
)
httpClient
.request[Option[GitHubRelease.Response]](httpRequest)
.map(
_.leftMap(GitHubError.fromHttpError)
.flatMap(res => res.asRight[GitHubError])
)
}
}

}
12 changes: 12 additions & 0 deletions src/main/scala/kevinlee/github/data/github.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.estatico.newtype.macros.newtype

import java.io.File
import kevinlee.git.Git.TagName
import kevinlee.http.HttpRequest
import org.kohsuke.github.GHRelease

/** @author Kevin Lee
Expand Down Expand Up @@ -62,4 +63,15 @@ object GitHubRepoWithAuth {
override val toString: String = "***Protected***"
}

implicit final class AccessTokenOps(val maybeAccessToken: Option[AccessToken]) extends AnyVal {
def toHeaderList: List[HttpRequest.Header] =
maybeAccessToken
.toList
.map(token =>
HttpRequest.Header(
"Authorization" -> s"token ${token.accessToken}"
)
)
}

}

0 comments on commit cc5a846

Please sign in to comment.