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

Issue 1268 #1304

Closed
wants to merge 4 commits into from
Closed
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
76 changes: 56 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,30 @@ 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")
}
}
.flatMap(res => Future { element.innerHTML = res })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.flatMap(res => Future { element.innerHTML = res })
.map(res => element.innerHTML = res)

}

def setLogo(): Future[Unit] = {
def getLogoAndSetHTML(branch: String, organization: String, repository: String): Unit = {
Comment on lines +64 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not just set the logo, it fixes all the URLs of all the images in the readme file.

Suggested change
def setLogo(): Future[Unit] = {
def getLogoAndSetHTML(branch: String, organization: String, repository: String): Unit = {
def getDefaultBranchAndFixImages(): Future[Unit] = {
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 +93,33 @@ 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
} else {
Future.successful("{\"default_branch\": \"master\"}")
}
Comment on lines +105 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`
For the fallback, I would avoid creating a JSON string if we then need to parse it and extract one field.

Suggested change
if (res.status == 200) {
res.text().toFuture
} else {
Future.successful("{\"default_branch\": \"master\"}")
}
def extractDefaultBranch(text: String): String =
js.JSON.parse(text).asInstanceOf[Repo].default_branch
if (res.status == 200) res.text().map(extractDefaultBranch).toFuture
else Future.successful("master")

}
.flatMap { res =>
val resJson = js.JSON.parse(res)
val branch = resJson.asInstanceOf[Repo].default_branch
Future(getLogoAndSetHTML(branch, organization, repository))
}
}

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

}

@js.native
Expand Down
Loading