Closed dongxuwang closed 9 months ago
Thanks for the bug report.
After looking through the code, looks that's not easy to figure this out.
I guess we need to use the GithubClient
to get the default branch and store that into GithubInfo
(which is saved in a SQL table). Then use that info to get the correct URL of images.
If you have time to look into that, it would be helpful.
what if just before this (or in parallel) call to github api
val request = new Request(
s"https://api.github.com/repos/$organization/$repository/readme",
new RequestInit {
headers = headersWithCreds.toJSDictionary
}
)
One makes this other one https://api.github.com/repos/$organization/$repository/
with which you can get the default_branch
?
One makes this other one https://api.github.com/repos/$organization/$repository/ with which you can get the default_branch?
Yes that should solve the issue! Feel free to open a PR.
I'll give it a shot!
to be fair: it's not the solution... I see it as a workaround but it should a) serve the purpose b) have little impact on the structure of that part of the codebase.
I'll try and summarize a possible solution.
As of now def fetchAndReplaceReadme(element: Element, ...
does few things:
It retrives the readme information
val request = new Request(
s"https://api.github.com/repos/$organization/$repository/readme", ...)
Updates the markup for the given element
(element.innerHTML = res
)
Checks all img
and a
elements and for them checks whether they have href
or src
attributes and then sets them to some specific values that can be github.com/$organization/$repository/raw/$branch
or github.com/$organization/$repository/blob/$branch
(and branch is master)
Now what I had in mind is to keep point 1 and 2 as they and separate the rest.
So the first part would look like:
// Simplified version
fetch(readmeRequest).toFuture
.flatMap { res =>
res.text().toFuture
}
.foreach(res => element.innerHTML = res)
The second one would make the "new" GitHub API request (repoRequest
), parse the JSON and retrieve the default_branch
. Only then It can go on with updating href
attributes for the anchors and the images.
So something along these lines:
// Simplified version
@js.native
trait Repo extends js.Object {
val default_branch: String = js.native
}
(...)
val repoRequest = new Request(
s"https://api.github.com/repos/$organization/$repository/", ...)
fetch(repoRequest).toFuture
.flatMap(res => res.text().toFuture)
.foreach { res =>
val resJson = js.JSON.parse(res)
val branch = resJson.asInstanceOf[Repo].default_branch
setImagesAndAnchors(branch, organization, repository)
}
(And obviously setImagesAndAnchors
does what it says it does)
How does this look like?
How does this look like?
Looks good but the second part (or more precisely the setImagesAndAnchors
) should not happen before the first part finishes. So there should be some map
or flatMap
to synchronize them.
Cool. I'm just wondering if I've got an easy way to test that this works. Any hint?
Cool. I'm just wondering if I've got an easy way to test that this works. Any hint?
This is how you can run Scaladex locally: https://github.com/scalacenter/scaladex/blob/main/CONTRIBUTING.md#run-scaladex-locally. Then you should go to the scalacenter/bloop
project and see if the image appears.
Working on it have a look https://github.com/mfirry/scaladex/commit/d960fa30b611b787ebc30cb8cda4be7712c91541
still trying to understand if can i somehow handle unsuccessful API calls
@mfirry thanks for the update. It is looking good.
still trying to understand if can i somehow handle unsuccessful API calls
The API call should not fail and if it fails I would use master
as the default branch.
Current behavior
image in readme would crash when default branch is not master
see project logo in here
Expected Behavior
image in readme should display
Extra comments
Should tune
Client.scala
rather hard codemaster
Search terms
master
orblob
inClient.scala