-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.scala
178 lines (157 loc) · 6.95 KB
/
Main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.rockthejvm
import org.http4s.Header.Raw
import org.http4s.client.Client
import org.http4s.{EntityDecoder, Header, HttpRoutes, Method, ParseFailure, Request, Response, Status, Uri}
import org.typelevel.ci.CIString
import com.rockthejvm.Main.syntax.*
import com.rockthejvm.Main.domain.*
import pureconfig.*
import pureconfig.generic.derivation.default.*
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.dsl.*
import cats.syntax.all.*
import cats.instances.all.*
import play.api.libs.json.*
import play.api.libs.*
import cats.implicits.*
import cats.effect.{IO, IOApp}
import org.http4s.dsl.io.*
import org.http4s.ember.server.EmberServerBuilder
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.syntax.LoggerInterpolator
import com.comcast.ip4s.*
import com.rockthejvm.Main.getContributorsPerRepo
object Main extends IOApp.Simple {
final case class Token(token: String) derives ConfigReader {
override def toString: String = token
}
given logger: Logger[IO] = Slf4jLogger.getLogger[IO]
override val run: IO[Unit] =
(for {
_ <- info"starting server".toResource
token <- IO.delay(ConfigSource.default.loadOrThrow[Token]).toResource
client <- EmberClientBuilder.default[IO].build
_ <- EmberServerBuilder
.default[IO]
.withHost(host"localhost")
.withPort(port"9000")
.withHttpApp(routes(client, token).orNotFound)
.build
} yield ()).useForever
def publicRepos(orgName: String) = s"https://api.github.com/orgs/$orgName"
def contributorsUrl(repoName: String, orgName: String, page: Int): String =
s"https://api.github.com/repos/$orgName/$repoName/contributors?per_page=100&page=$page"
def repos(orgName: String, page: Int): String =
s"https://api.github.com/orgs/$orgName/repos?per_page=100&page=$page"
def req(uri: Uri)(using token: Token) = Request[IO](Method.GET, uri)
.putHeaders(Raw(CIString("Authorization"), s"Bearer ${token.token}"))
def uri(url: String): IO[Uri] = IO.fromEither(Uri.fromString(url))
def fetch[A: Reads](uri: Uri, client: Client[IO], default: => A)(using token: Token): IO[A] =
client
.expect[String](req(uri))
.map(_.into[A])
.onError {
case org.http4s.client.UnexpectedStatus(Status.Unauthorized, _, _) =>
error"GitHub token is either expired or absent, please check `token` key in src/main/resources/application.conf"
case other => error"$other"
}
.handleErrorWith(_ => warn"returning default value: $default for $uri due to unexpected error" as default)
private def getContributorsPerRepo(
client: Client[IO],
repoName: RepoName,
orgName: String,
contributors: Vector[Contributor] = Vector.empty[Contributor],
page: Int = 1,
isEmpty: Boolean = false,
)(using token: Token): IO[Vector[Contributor]] =
if ((page > 1 && contributors.size % 100 != 0) || isEmpty) IO.pure(contributors)
else {
uri(contributorsUrl(repoName.value, orgName, page))
.flatMap { contributorUri =>
for {
_ <- info"requesting page: $page of $repoName contributors"
newContributors <- fetch[Vector[Contributor]](contributorUri, client, Vector.empty)
_ <- info"fetched ${newContributors.size} contributors on page: $page for $repoName"
next <- getContributorsPerRepo(
client = client,
repoName = repoName,
orgName = orgName,
contributors = contributors ++ newContributors,
page = page + 1,
isEmpty = newContributors.isEmpty,
)
} yield next
}
}
def routes(client: Client[IO], token: Token): HttpRoutes[IO] = {
given tk: Token = token
HttpRoutes.of[IO] {
case GET -> Root => Ok("hi :)")
case GET -> Root / "org" / orgName =>
for {
start <- IO.realTime
_ <- info"accepted $orgName"
publicReposUri <- uri(publicRepos(orgName))
_ <- info"fetching the amount of available repositories for $orgName"
publicRepos <- fetch[PublicRepos](publicReposUri, client, PublicRepos.Empty)
_ <- info"amount of public repositories for $orgName: $publicRepos"
pages = (1 to (publicRepos.value / 100) + 1).toVector
repositories <- pages.parUnorderedFlatTraverse { page =>
uri(repos(orgName, page))
.flatMap(fetch[Vector[RepoName]](_, client, Vector.empty[RepoName]))
}
_ <- info"$publicRepos repositories were collected for $orgName"
_ <- info"starting to fetch contributors for each repository"
contributors <- repositories
.parUnorderedFlatTraverse { repoName =>
for {
_ <- info"fetching contributors for $repoName"
contributors <- getContributorsPerRepo(client, repoName, orgName)
_ <- info"fetched amount of contributors for $repoName: ${contributors.size}"
} yield contributors
}
.map {
_.groupMapReduce(_.login)(_.contributions)(_ + _).toVector
.map(Contributor(_, _))
.sortWith(_.contributions > _.contributions)
}
_ <- info"returning aggregated & sorted contributors for $orgName"
response <- Ok(Contributions(contributors.size, contributors).toJson)
end <- IO.realTime
_ <- info"aggregation took ${(end - start).toSeconds} seconds"
} yield response
}
}
object syntax {
extension (self: String) def into[A](using r: Reads[A]): A = Json.parse(self).as[A]
extension [A](self: A) def toJson(using w: Writes[A]): String = Json.prettyPrint(w.writes(self))
}
object domain {
import play.api.libs.json.*
import play.api.libs.*
import Reads.{IntReads, StringReads}
opaque type PublicRepos = Int
object PublicRepos {
val Empty: PublicRepos = PublicRepos.apply(0)
def apply(value: Int): PublicRepos = value
}
extension (repos: PublicRepos) def value: Int = repos
given ReadsPublicRepos: Reads[PublicRepos] = (__ \ "public_repos").read[Int].map(PublicRepos.apply)
opaque type RepoName = String
object RepoName {
def apply(value: String): RepoName = value
}
extension (repoName: RepoName) def value: String = repoName
given ReadsRepo: Reads[RepoName] = (__ \ "name").read[String].map(RepoName.apply)
final case class Contributor(login: String, contributions: Long)
given ReadsContributor: Reads[Contributor] = json =>
(
(json \ "login").asOpt[String],
(json \ "contributions").asOpt[Long],
).tupled.fold(JsError("parse failure"))((lo, co) => JsSuccess(Contributor(lo, co)))
given WritesContributor: Writes[Contributor] = Json.writes[Contributor]
final case class Contributions(count: Long, contributors: Vector[Contributor])
given WritesContributions: Writes[Contributions] = Json.writes[Contributions]
}
}