-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.scala
78 lines (64 loc) · 2.13 KB
/
index.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//> using platform js
//> using toolkit typelevel:0.1.17
//> using jsModuleKind common
import cats.data.*
import cats.effect.*
import cats.effect.std.*
import cats.syntax.all.*
import org.http4s.*
import org.http4s.circe.*
import org.http4s.ember.client.*
import io.circe.*
import scala.concurrent.duration.*
object index extends IOApp:
def getInput(input: String) =
Env[IO].get(s"INPUT_${input.toUpperCase}")
def getApiUrl =
Env[IO].get("GITHUB_API_URL").map(_.get)
def getRepo =
Env[IO].get("GITHUB_REPOSITORY").map(_.get)
def getSha =
Env[IO].get("GITHUB_SHA").map(_.get)
def getPollInterval =
OptionT(getInput("poll-interval"))
.subflatMap(_.toIntOption)
.map(_.seconds)
.getOrElse(30.seconds)
def run(args: List[String]) =
EmberClientBuilder.default[IO].build.use { client =>
(getPollInterval, getApiUrl, getRepo, getSha).flatMapN {
(pollInterval, apiUrl, repo, sha) =>
val getConclusion =
OptionT
.liftF(
client.expect[CheckSuites](
s"$apiUrl/repos/$repo/commits/$sha/check-suites"
)
)
.subflatMap(_.check_suites.find(_.app.slug == "cirrus-ci"))
.filter(_.status == "completed")
.subflatMap(_.conclusion)
.map(_ == "success")
.value
def go: IO[ExitCode] = getConclusion.flatMap {
case Some(conclusion) =>
if conclusion then
IO.println("Cirrus CI succeeded").as(ExitCode.Success)
else IO.println("Cirrus CI errored").as(ExitCode.Error)
case None =>
IO.println(
s"Cirrus CI not yet complete, polling again in $pollInterval ..."
) *> IO.sleep(pollInterval) *> go
}
go
}
}
case class CheckSuites(check_suites: List[CheckSuite]) derives Decoder
object CheckSuites:
given EntityDecoder[IO, CheckSuites] = jsonOf[IO, CheckSuites]
case class CheckSuite(
app: App,
status: String,
conclusion: Option[String]
) derives Decoder
case class App(slug: String) derives Decoder