Skip to content

Commit

Permalink
Add todos, remove self type, extract transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed May 21, 2022
1 parent 4aece70 commit 20fe87f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/io/pg/messaging/messaging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ object Processor {

}

trait Channel[F[_], A] extends Publisher[F, A] { self =>
trait Channel[F[_], A] extends Publisher[F, A] {
def consume: fs2.Stream[F, A]
}

object Channel {
given[F[_]]: Invariant[Channel[F, *]] with {

given [F[_]]: Invariant[Channel[F, *]] with {

def imap[A, B](chan: Channel[F, A])(f: A => B)(g: B => A): Channel[F, B] = new {
def consume: fs2.Stream[F,B] = chan.consume.map(f)
def consume: fs2.Stream[F, B] = chan.consume.map(f)
def publish(b: B): F[Unit] = chan.publish(g(b))
}

}

def fromQueue[F[_]: Functor, A](q: Queue[F, A]): Channel[F, A] =
Expand Down
2 changes: 2 additions & 0 deletions gitlab/src/main/scala/io/pg/gitlab/Gitlab.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ object GitlabEndpoints {
}

object ApprovalRule {
// todo: use configured codec when https://github.com/circe/circe/pull/1800 is available
given CirceCodec[ApprovalRule] = CirceCodec.forProduct3("id", "name", "rule_type")(apply)(r => (r.id, r.name, r.ruleType))
}

final case class MergeRequestApprovals(approvalsRequired: Int)

object MergeRequestApprovals {
// todo: use configured codec when https://github.com/circe/circe/pull/1800 is available
given CirceCodec[MergeRequestApprovals] = CirceCodec.forProduct1("approvals_required")(apply)(_.approvalsRequired)
}

Expand Down
1 change: 1 addition & 0 deletions gitlab/src/main/scala/io/pg/gitlab/webhook/webhook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.circe.Codec
final case class WebhookEvent(project: Project, objectKind: String /* for logs */ )

object WebhookEvent {
// todo: use configured codec when https://github.com/circe/circe/pull/1800 is available
given Codec[WebhookEvent] = Codec.forProduct2("project", "object_kind")(apply)(we => (we.project, we.objectKind))
}

Expand Down
35 changes: 18 additions & 17 deletions src/main/scala/io/pg/webhook/webhook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.http4s.dsl.Http4sDsl
import cats.MonadThrow
import io.pg.gitlab.Gitlab.MergeRequestInfo
import io.pg.MergeRequestState.Mergeability
import io.pg.MergeRequestState

object WebhookRouter {

Expand All @@ -36,29 +37,29 @@ object WebhookRouter {
MergeRequests[F]
.build(proj)
.nested
.map { s =>
transport.MergeRequestState(
projectId = s.projectId,
mergeRequestIid = s.mergeRequestIid,
description = s.description,
status = s.status match {
case MergeRequestInfo.Status.Success => transport.MergeRequestState.Status.Success
case MergeRequestInfo.Status.Other(s) => transport.MergeRequestState.Status.Other(s)
},
mergeability = s.mergeability match {
case Mergeability.CanMerge => transport.MergeRequestState.Mergeability.CanMerge
case Mergeability.HasConflicts => transport.MergeRequestState.Mergeability.HasConflicts
case Mergeability.NeedsRebase => transport.MergeRequestState.Mergeability.NeedsRebase
},
authorUsername = s.authorUsername
)
}
.map(mergeRequestToTransport)
.value
.flatMap(Ok(_))
}

}

private def mergeRequestToTransport(mr: MergeRequestState): io.pg.transport.MergeRequestState = transport.MergeRequestState(
projectId = mr.projectId,
mergeRequestIid = mr.mergeRequestIid,
description = mr.description,
status = mr.status match {
case MergeRequestInfo.Status.Success => transport.MergeRequestState.Status.Success
case MergeRequestInfo.Status.Other(s) => transport.MergeRequestState.Status.Other(s)
},
mergeability = mr.mergeability match {
case Mergeability.CanMerge => transport.MergeRequestState.Mergeability.CanMerge
case Mergeability.HasConflicts => transport.MergeRequestState.Mergeability.HasConflicts
case Mergeability.NeedsRebase => transport.MergeRequestState.Mergeability.NeedsRebase
},
authorUsername = mr.authorUsername
)

}

object WebhookProcessor {
Expand Down

0 comments on commit 20fe87f

Please sign in to comment.