Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mfirry committed Nov 22, 2023
1 parent b5a8aae commit a484670
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions modules/webclient/src/main/scala/scaladex/client/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ object Client {
}
}

@js.native
trait Repo extends js.Object {
val default_branch: String = js.native
}

private def fetchAndReplaceReadme(element: Element, token: Option[String]): Unit = {

val organization = element.attributes.getNamedItem("data-organization").value
val repository = element.attributes.getNamedItem("data-repository").value
val headers = Map("Accept" -> "application/vnd.github.VERSION.html")
Expand All @@ -36,27 +42,33 @@ object Client {
.map(t => headers + ("Authorization" -> s"bearer $t"))
.getOrElse(headers)

val root = s"https://github.com/$organization/$repository"
def base(v: String) = s"$root/$v/master"
val raw = base("raw")
val blob = base("blob")

val request = new Request(
s"https://api.github.com/repos/$organization/$repository/readme",
new RequestInit {
headers = headersWithCreds.toJSDictionary
}
)
fetch(request).toFuture
.flatMap { res =>
if (res.status == 200) {
res.text().toFuture
} else {
Future.successful("No README found for this project, please check the repository")
def setReadme(): Future[Unit] = {
val readmeRequest: Request = new Request(
s"https://api.github.com/repos/$organization/$repository/readme",
new RequestInit {
headers = headersWithCreds.toJSDictionary
}
}
.foreach { res =>
element.innerHTML = res
)

fetch(readmeRequest).toFuture
.flatMap { res =>
if (res.status == 200) {
res.text().toFuture
} else {
Future.successful("No README found for this project, please check the repository")
}
}
.map(res => element.innerHTML = res)
}

def getDefaultBranchAndFixImages(): Future[Unit] = {
def extractDefaultBranch(text: String): String =
js.JSON.parse(text).asInstanceOf[Repo].default_branch

def fixImages(branch: String, organization: String, repository: String): Unit = {
val root = s"https://github.com/$organization/$repository"
val raw = s"$root/raw/$branch"
val blob = s"$root/blob/$branch"

element
.querySelectorAll("img,a")
Expand Down Expand Up @@ -84,6 +96,31 @@ object Client {
}
}
}

val repoRequest: Request = new Request(
s"https://api.github.com/repos/$organization/$repository",
new RequestInit {
headers = headersWithCreds.toJSDictionary
}
)
fetch(repoRequest).toFuture
.flatMap { res =>
if (res.status == 200) {
res.text().toFuture.map(extractDefaultBranch)
} else { Future.successful("master") }
}
.map { res =>
val resJson = js.JSON.parse(res)
val branch = resJson.asInstanceOf[Repo].default_branch
fixImages(branch, organization, repository)
}
}

for {
_ <- setReadme()
_ <- getDefaultBranchAndFixImages()
} yield ()

}

@js.native
Expand Down

0 comments on commit a484670

Please sign in to comment.