From a570ab473568cd2c2777c88e7904d7a3f8bf71b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Zyba=C5=82a?= Date: Mon, 14 Feb 2022 13:45:49 +0100 Subject: [PATCH] Follow renames in content contributors script --- scaladoc-js/contributors/src/Globals.scala | 1 + .../ContentContributors.scala | 109 ++++++++++++------ .../scaladoc/renderers/HtmlRenderer.scala | 7 +- 3 files changed, 79 insertions(+), 38 deletions(-) diff --git a/scaladoc-js/contributors/src/Globals.scala b/scaladoc-js/contributors/src/Globals.scala index 68b37494fe7d..51bbf39ab3a3 100644 --- a/scaladoc-js/contributors/src/Globals.scala +++ b/scaladoc-js/contributors/src/Globals.scala @@ -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 } diff --git a/scaladoc-js/contributors/src/content-contributors/ContentContributors.scala b/scaladoc-js/contributors/src/content-contributors/ContentContributors.scala index 2ef1beb2185d..bbc9c4787dc6 100644 --- a/scaladoc-js/contributors/src/content-contributors/ContentContributors.scala +++ b/scaladoc-js/contributors/src/content-contributors/ContentContributors.scala @@ -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 + } + } }) diff --git a/scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala b/scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala index d71fe03a9c13..b61db9248a6d 100644 --- a/scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala +++ b/scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala @@ -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 _ => ""