Skip to content

Commit

Permalink
Merge pull request #1305 from mfirry/issue-1268
Browse files Browse the repository at this point in the history
Fixes #1268
  • Loading branch information
adpi2 authored Nov 22, 2023
2 parents b5a8aae + 247738e commit 2d84eb7
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 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,27 @@ 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(branch => fixImages(branch, organization, repository))
}

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

}

@js.native
Expand Down

0 comments on commit 2d84eb7

Please sign in to comment.