|
| 1 | +package git |
| 2 | + |
| 3 | +import sys.error |
| 4 | +import sttp.client3._ |
| 5 | +import sttp.model._ |
| 6 | +import os.Path |
| 7 | + |
| 8 | +import scala.util.Try |
| 9 | +import scala.util.Success |
| 10 | + |
| 11 | +import git.Lang.* |
| 12 | +import git.Commit |
| 13 | +import git.Contributor |
| 14 | + |
| 15 | +class Repo(val owner: String, val repoName: String): |
| 16 | + lazy val localRepo = cloneRepo() |
| 17 | + val client = SimpleHttpClient() |
| 18 | + |
| 19 | + def getCommits(page: Int = 1): List[Commit] = |
| 20 | + val request = basicRequest |
| 21 | + .header("Accept", "application/vnd.github+json") |
| 22 | + .get(uri"https://api.github.com/repos/$owner/$repoName/commits?per_page=100&page=$page") |
| 23 | + |
| 24 | + val response = client.send(request) |
| 25 | + val json = response.body match |
| 26 | + case Right(content) => content |
| 27 | + case Left(value) => error(value) |
| 28 | + |
| 29 | + val commits = Commit.commitsFromJson(json) |
| 30 | + if (isLast(response)) commits else commits ++ getCommits(page + 1) |
| 31 | + |
| 32 | + def getContributors(): List[Contributor] = |
| 33 | + getCommits() |
| 34 | + .groupBy(_.author) |
| 35 | + .mapValues(seq => seq.length) |
| 36 | + .map((author, commits) => Contributor(author, commits)) |
| 37 | + .toList |
| 38 | + |
| 39 | + def getOpenIssuesWithoutAnswers(page: Int = 1): List[Uri] = |
| 40 | + val request = basicRequest |
| 41 | + .header("Accept", "application/vnd.github+json") |
| 42 | + .get(uri"https://api.github.com/repos/$owner/$repoName/issues?per_page=100&page=$page") |
| 43 | + |
| 44 | + val response = client.send(request) |
| 45 | + val json = response.body match |
| 46 | + case Right(content) => content |
| 47 | + case Left(value) => error(value) |
| 48 | + |
| 49 | + val issues = |
| 50 | + for issue <- ujson.read(json).arr.toList |
| 51 | + if issue("state").str == "open" |
| 52 | + if Try(issue("pull_request")).isFailure |
| 53 | + if hasNoComments(issue("comments_url").str) |
| 54 | + yield uri"${issue("html_url").str}" |
| 55 | + |
| 56 | + if (isLast(response)) issues else issues ++ getOpenIssuesWithoutAnswers(page + 1) |
| 57 | + |
| 58 | + def lineCountPerLanguage(): Map[Lang, Int] = |
| 59 | + os.walk(localRepo, skip = _.last.equals(".git")) |
| 60 | + .filter(os.isFile) |
| 61 | + .map(path => Lang.fromExtension(path.ext) -> path) |
| 62 | + .map((lang, path) => (lang, getLineCount(path))) |
| 63 | + .groupBy(_._1) |
| 64 | + .mapValues(seq => seq.map(_._2).reduce(_ + _)) |
| 65 | + .toMap |
| 66 | + |
| 67 | + private def cloneRepo(): Path = |
| 68 | + val tmpDir = os.temp.dir() |
| 69 | + val address = uri"https://github.com/$owner/$repoName.git" |
| 70 | + os.proc("git", "clone", address.toString) |
| 71 | + .call(cwd = tmpDir) |
| 72 | + tmpDir |
| 73 | + |
| 74 | + private def getLineCount(path: Path): Int = |
| 75 | + Try(os.read.lines(path).filter(_.nonEmpty).size) match |
| 76 | + case Success(value) => value |
| 77 | + case _ => 0 |
| 78 | + |
| 79 | + private def hasNoComments(uri: String): Boolean = |
| 80 | + val request = basicRequest |
| 81 | + .header("Accept", "application/vnd.github+json") |
| 82 | + .get(uri"$uri") |
| 83 | + |
| 84 | + val response = client.send(request) |
| 85 | + val json = response.body match |
| 86 | + case Right(content) => content |
| 87 | + case Left(value) => error(value) |
| 88 | + |
| 89 | + ujson.read(json).arr.isEmpty |
| 90 | + |
| 91 | + private def isLast(response: Response[Either[String, String]]) = |
| 92 | + response.header(HeaderNames.Link) match |
| 93 | + case Some(value) => !value.contains("rel=\"next\"") |
| 94 | + case None => true |
| 95 | + |
0 commit comments