Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1268 #1305

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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