@@ -5,6 +5,7 @@ import org.http4s.client.blaze._
5
5
import org .http4s .client .Client
6
6
import org .http4s .headers .Authorization
7
7
8
+ import cats .syntax .applicative ._
8
9
import scalaz .concurrent .Task
9
10
import scala .util .control .NonFatal
10
11
@@ -17,6 +18,15 @@ import org.http4s.util._
17
18
18
19
import model .Github ._
19
20
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
+
20
30
trait PullRequestService {
21
31
22
32
/** Username for authorized admin */
@@ -55,17 +65,14 @@ trait PullRequestService {
55
65
def toUri (url : String ): Task [Uri ] =
56
66
Uri .fromString(url).fold(Task .fail, Task .now)
57
67
58
- def getRequest (endpoint : Uri ): Task [ Request ] = Task .now {
68
+ def getRequest (endpoint : Uri ): Request =
59
69
Request (uri = endpoint, method = Method .GET ).putHeaders(authHeader)
60
- }
61
70
62
- def postRequest (endpoint : Uri ): Task [ Request ] = Task .now {
71
+ def postRequest (endpoint : Uri ): Request =
63
72
Request (uri = endpoint, method = Method .POST ).putHeaders(authHeader)
64
- }
65
73
66
- def shutdownClient (client : Client ): Task [ Unit ] = Task .now {
74
+ def shutdownClient (client : Client ): Unit =
67
75
client.shutdownNow()
68
- }
69
76
70
77
sealed trait CommitStatus {
71
78
def commit : Commit
@@ -81,7 +88,7 @@ trait PullRequestService {
81
88
def checkUser (user : String , commit : Commit ): Task [CommitStatus ] = {
82
89
val claStatus = for {
83
90
endpoint <- toUri(claUrl(user))
84
- claReq <- getRequest(endpoint)
91
+ claReq <- getRequest(endpoint).pure[ Task ]
85
92
claRes <- httpClient.expect(claReq)(jsonOf[CLASignature ])
86
93
res = if (claRes.signed) Valid (user, commit) else Invalid (user, commit)
87
94
} yield res
@@ -127,7 +134,7 @@ trait PullRequestService {
127
134
128
135
for {
129
136
endpoint <- toUri(statusUrl(cm.commit.sha))
130
- req <- postRequest(endpoint).map(_. withBody(stat.asJson))
137
+ req <- postRequest(endpoint).withBody(stat.asJson).pure[ Task ]
131
138
res <- httpClient.expect(req)(jsonOf[StatusResponse ])
132
139
} yield res
133
140
}
@@ -152,7 +159,7 @@ trait PullRequestService {
152
159
def makeRequest (url : String ): Task [List [Commit ]] =
153
160
for {
154
161
endpoint <- toUri(url)
155
- req <- getRequest(endpoint)
162
+ req <- getRequest(endpoint).pure[ Task ]
156
163
res <- httpClient.fetch(req){ res =>
157
164
val link = CaseInsensitiveString (" Link" )
158
165
val next = findNext(res.headers.get(link)).map(makeRequest).getOrElse(Task .now(Nil ))
@@ -176,7 +183,7 @@ trait PullRequestService {
176
183
177
184
// Send statuses to Github and exit
178
185
_ <- sendStatuses(statuses, httpClient)
179
- _ <- shutdownClient(httpClient)
186
+ _ <- shutdownClient(httpClient).pure[ Task ]
180
187
resp <- Ok (" All statuses checked" )
181
188
} yield resp
182
189
}
0 commit comments