Skip to content

Commit

Permalink
Merge pull request #14478 from dotty-staging/scaladoc/follow-renames
Browse files Browse the repository at this point in the history
Scaladoc: Follow renames in content contributors script
  • Loading branch information
pikinier20 committed Feb 15, 2022
2 parents a98abb5 + a570ab4 commit 1476a30
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
1 change: 1 addition & 0 deletions scaladoc-js/contributors/src/Globals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import scala.scalajs.js.annotation.JSGlobalScope
@JSGlobalScope
object Globals extends js.Object {
val githubContributorsUrl: String = js.native
val githubContributorsFilename: String = js.native
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,83 @@ trait Author extends js.Object:
trait CommitTop extends js.Object:
def commit: CommitBottom
def author: Author
def url: String

trait Commits extends js.Array[CommitTop]

trait CommitDescription extends js.Object:
def files: js.Array[FileChange]

trait FileChange extends js.Object:
def filename: String
def status: String
def previous_filename: String

class ContentContributors:
val indenticonsUrl = "https://github.com/identicons"
def linkForFilename(filename: String) = Globals.githubContributorsUrl + s"/commits?path=$filename"
def getAuthorsForFilename(filename: String): Future[List[FullAuthor]] = {
val link = linkForFilename(filename)
Ajax.get(link).map(_.responseText).flatMap { json =>
val res = JSON.parse(json).asInstanceOf[Commits]
val authors = res.map { commit =>
commit.author match
case null =>
FullAuthor(commit.commit.author.name, "", s"$indenticonsUrl/${commit.commit.author.name}.png")
case author =>
FullAuthor(author.login, author.html_url, author.avatar_url)
}
val lastCommit = res.lastOption
val lastCommitDescriptionLink = lastCommit.map(_.url)
val previousFilename = lastCommitDescriptionLink
.fold(Future.successful(None)) { link =>
findRename(link, filename)
}
val previousAuthors = previousFilename.flatMap {
case Some(filename) => getAuthorsForFilename(filename)
case None => Future.successful(List.empty)
}.fallbackTo(Future.successful(List.empty))

previousAuthors.map(_ ++ authors).map(_.distinct)
}
}
def findRename(link: String, filename: String): Future[Option[String]] = {
Ajax.get(link).map(_.responseText).map { json =>
val res = JSON.parse(json).asInstanceOf[CommitDescription]
val files = res.files
files
.find(_.filename == filename)
.filter(_.status == "renamed")
.map(_.previous_filename)
}
}
document.addEventListener("DOMContentLoaded", (e: Event) => {
val indenticonsUrl = "https://github.com/identicons"
js.typeOf(Globals.githubContributorsUrl) match
case "undefined" =>
// don't do anything
case url =>
val request: Future[String] = Ajax.get(Globals.githubContributorsUrl).map(_.responseText)
request.onComplete {
case Success(json: String) =>
val res = JSON.parse(json).asInstanceOf[Commits]
val authors = res.map { commit =>
commit.author match
case null =>
FullAuthor(commit.commit.author.name, "", s"$indenticonsUrl/${commit.commit.author.name}.png")
case author =>
FullAuthor(author.login, author.html_url, author.avatar_url)
}.distinct
val maybeDiv = Option(document.getElementById("documentation-contributors"))
maybeDiv.foreach { div =>
authors.foreach { case FullAuthor(name, url, img) =>
val divN = document.createElement("div")
val imgN = document.createElement("img").asInstanceOf[html.Image]
imgN.src = img
val autN = document.createElement("a").asInstanceOf[html.Anchor]
autN.href = url
autN.text = name
divN.appendChild(imgN)
divN.appendChild(autN)
div.appendChild(divN)
}

if authors.nonEmpty then
div.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
}
case Failure(_) =>
println(s"Couldn't fetch contributors for ${Globals.githubContributorsUrl}")
}
if js.typeOf(Globals.githubContributorsUrl) != "undefined" &&
js.typeOf(Globals.githubContributorsFilename) != "undefined"
then {
getAuthorsForFilename(Globals.githubContributorsFilename.stripPrefix("/")).onComplete {
case Success(authors) =>
val maybeDiv = Option(document.getElementById("documentation-contributors"))
maybeDiv.foreach { div =>
authors.foreach { case FullAuthor(name, url, img) =>
val divN = document.createElement("div")
val imgN = document.createElement("img").asInstanceOf[html.Image]
imgN.src = img
val autN = document.createElement("a").asInstanceOf[html.Anchor]
autN.href = url
autN.text = name
divN.appendChild(imgN)
divN.appendChild(autN)
div.appendChild(divN)
}

if authors.nonEmpty then
div.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
}
case Failure(err) =>
println(s"Couldn't fetch contributors. $err")
None
}
}
})

Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
script(raw(s"""var pathToRoot = "${pathToRoot(page.link.dri)}";""")),
(page.content match
case ResolvedTemplate(loadedTemplate, _) =>
val path = loadedTemplate.file.toPath
val path = loadedTemplate.templateFile.file.toPath
ctx.sourceLinks.repoSummary(path) match
case Some(DefinedRepoSummary("github", org, repo)) =>
val tag: TagArg = ctx.sourceLinks.fullPath(relativePath(path)).fold("") { githubContributors =>
script(raw(s"""var githubContributorsUrl = "https://api.github.com/repos/$org/$repo/commits?path=$githubContributors";"""))
Seq(
script(raw(s"""var githubContributorsUrl = "https://api.github.com/repos/$org/$repo";""")),
script(raw(s"""var githubContributorsFilename = "$githubContributors";"""))
)
}
tag // for some reason inference fails so had to state the type explicitly
case _ => ""
Expand Down

0 comments on commit 1476a30

Please sign in to comment.