Skip to content

Commit 35337ea

Browse files
OlivierBlanvillainfelixmulder
authored andcommitted
Use .pure at use site instead of warpping with Task.now
1 parent c183649 commit 35337ea

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

bot/src/dotty/tools/bot/PullRequestService.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.http4s.client.blaze._
55
import org.http4s.client.Client
66
import org.http4s.headers.Authorization
77

8+
import cats.syntax.applicative._
89
import scalaz.concurrent.Task
910
import scala.util.control.NonFatal
1011

@@ -17,6 +18,15 @@ import org.http4s.util._
1718

1819
import model.Github._
1920

21+
object TaskIsApplicative {
22+
implicit val taskIsApplicative = new cats.Applicative[Task] {
23+
def pure[A](x: A): Task[A] = Task.now(x)
24+
def ap[A, B](ff: Task[A => B])(fa: Task[A]): Task[B] =
25+
for(f <- ff; a <- fa) yield f(a)
26+
}
27+
}
28+
import TaskIsApplicative._
29+
2030
trait PullRequestService {
2131

2232
/** Username for authorized admin */
@@ -55,17 +65,14 @@ trait PullRequestService {
5565
def toUri(url: String): Task[Uri] =
5666
Uri.fromString(url).fold(Task.fail, Task.now)
5767

58-
def getRequest(endpoint: Uri): Task[Request] = Task.now {
68+
def getRequest(endpoint: Uri): Request =
5969
Request(uri = endpoint, method = Method.GET).putHeaders(authHeader)
60-
}
6170

62-
def postRequest(endpoint: Uri): Task[Request] = Task.now {
71+
def postRequest(endpoint: Uri): Request =
6372
Request(uri = endpoint, method = Method.POST).putHeaders(authHeader)
64-
}
6573

66-
def shutdownClient(client: Client): Task[Unit] = Task.now {
74+
def shutdownClient(client: Client): Unit =
6775
client.shutdownNow()
68-
}
6976

7077
sealed trait CommitStatus {
7178
def commit: Commit
@@ -81,7 +88,7 @@ trait PullRequestService {
8188
def checkUser(user: String, commit: Commit): Task[CommitStatus] = {
8289
val claStatus = for {
8390
endpoint <- toUri(claUrl(user))
84-
claReq <- getRequest(endpoint)
91+
claReq <- getRequest(endpoint).pure[Task]
8592
claRes <- httpClient.expect(claReq)(jsonOf[CLASignature])
8693
res = if (claRes.signed) Valid(user, commit) else Invalid(user, commit)
8794
} yield res
@@ -127,7 +134,7 @@ trait PullRequestService {
127134

128135
for {
129136
endpoint <- toUri(statusUrl(cm.commit.sha))
130-
req <- postRequest(endpoint).map(_.withBody(stat.asJson))
137+
req <- postRequest(endpoint).withBody(stat.asJson).pure[Task]
131138
res <- httpClient.expect(req)(jsonOf[StatusResponse])
132139
} yield res
133140
}
@@ -152,7 +159,7 @@ trait PullRequestService {
152159
def makeRequest(url: String): Task[List[Commit]] =
153160
for {
154161
endpoint <- toUri(url)
155-
req <- getRequest(endpoint)
162+
req <- getRequest(endpoint).pure[Task]
156163
res <- httpClient.fetch(req){ res =>
157164
val link = CaseInsensitiveString("Link")
158165
val next = findNext(res.headers.get(link)).map(makeRequest).getOrElse(Task.now(Nil))
@@ -176,7 +183,7 @@ trait PullRequestService {
176183

177184
// Send statuses to Github and exit
178185
_ <- sendStatuses(statuses, httpClient)
179-
_ <- shutdownClient(httpClient)
186+
_ <- shutdownClient(httpClient).pure[Task]
180187
resp <- Ok("All statuses checked")
181188
} yield resp
182189
}

bot/test/PRServiceTests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class PRServiceTests extends PullRequestService {
2121
.map(_.mkString)
2222
.getOrElse(throw new Exception(s"resource not found: $r"))
2323

24-
2524
@Test def canUnmarshalIssueJson = {
2625
val json = getResource("/test-pr.json")
2726
val issue: Issue = decode[Issue](json) match {

0 commit comments

Comments
 (0)