Skip to content

Commit fa565b9

Browse files
pikinier20olsdavis
authored andcommitted
Follow renames in content contributors script
1 parent 198f716 commit fa565b9

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

scaladoc-js/contributors/src/Globals.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ import scala.scalajs.js.annotation.JSGlobalScope
77
@JSGlobalScope
88
object Globals extends js.Object {
99
val githubContributorsUrl: String = js.native
10+
val githubContributorsFilename: String = js.native
1011
}
1112

scaladoc-js/contributors/src/content-contributors/ContentContributors.scala

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,83 @@ trait Author extends js.Object:
3333
trait CommitTop extends js.Object:
3434
def commit: CommitBottom
3535
def author: Author
36+
def url: String
3637

3738
trait Commits extends js.Array[CommitTop]
3839

40+
trait CommitDescription extends js.Object:
41+
def files: js.Array[FileChange]
42+
43+
trait FileChange extends js.Object:
44+
def filename: String
45+
def status: String
46+
def previous_filename: String
47+
3948
class ContentContributors:
49+
val indenticonsUrl = "https://github.com/identicons"
50+
def linkForFilename(filename: String) = Globals.githubContributorsUrl + s"/commits?path=$filename"
51+
def getAuthorsForFilename(filename: String): Future[List[FullAuthor]] = {
52+
val link = linkForFilename(filename)
53+
Ajax.get(link).map(_.responseText).flatMap { json =>
54+
val res = JSON.parse(json).asInstanceOf[Commits]
55+
val authors = res.map { commit =>
56+
commit.author match
57+
case null =>
58+
FullAuthor(commit.commit.author.name, "", s"$indenticonsUrl/${commit.commit.author.name}.png")
59+
case author =>
60+
FullAuthor(author.login, author.html_url, author.avatar_url)
61+
}
62+
val lastCommit = res.lastOption
63+
val lastCommitDescriptionLink = lastCommit.map(_.url)
64+
val previousFilename = lastCommitDescriptionLink
65+
.fold(Future.successful(None)) { link =>
66+
findRename(link, filename)
67+
}
68+
val previousAuthors = previousFilename.flatMap {
69+
case Some(filename) => getAuthorsForFilename(filename)
70+
case None => Future.successful(List.empty)
71+
}.fallbackTo(Future.successful(List.empty))
72+
73+
previousAuthors.map(_ ++ authors).map(_.distinct)
74+
}
75+
}
76+
def findRename(link: String, filename: String): Future[Option[String]] = {
77+
Ajax.get(link).map(_.responseText).map { json =>
78+
val res = JSON.parse(json).asInstanceOf[CommitDescription]
79+
val files = res.files
80+
files
81+
.find(_.filename == filename)
82+
.filter(_.status == "renamed")
83+
.map(_.previous_filename)
84+
}
85+
}
4086
document.addEventListener("DOMContentLoaded", (e: Event) => {
41-
val indenticonsUrl = "https://github.com/identicons"
42-
js.typeOf(Globals.githubContributorsUrl) match
43-
case "undefined" =>
44-
// don't do anything
45-
case url =>
46-
val request: Future[String] = Ajax.get(Globals.githubContributorsUrl).map(_.responseText)
47-
request.onComplete {
48-
case Success(json: String) =>
49-
val res = JSON.parse(json).asInstanceOf[Commits]
50-
val authors = res.map { commit =>
51-
commit.author match
52-
case null =>
53-
FullAuthor(commit.commit.author.name, "", s"$indenticonsUrl/${commit.commit.author.name}.png")
54-
case author =>
55-
FullAuthor(author.login, author.html_url, author.avatar_url)
56-
}.distinct
57-
val maybeDiv = Option(document.getElementById("documentation-contributors"))
58-
maybeDiv.foreach { div =>
59-
authors.foreach { case FullAuthor(name, url, img) =>
60-
val divN = document.createElement("div")
61-
val imgN = document.createElement("img").asInstanceOf[html.Image]
62-
imgN.src = img
63-
val autN = document.createElement("a").asInstanceOf[html.Anchor]
64-
autN.href = url
65-
autN.text = name
66-
divN.appendChild(imgN)
67-
divN.appendChild(autN)
68-
div.appendChild(divN)
69-
}
70-
71-
if authors.nonEmpty then
72-
div.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
73-
}
74-
case Failure(_) =>
75-
println(s"Couldn't fetch contributors for ${Globals.githubContributorsUrl}")
76-
}
87+
if js.typeOf(Globals.githubContributorsUrl) != "undefined" &&
88+
js.typeOf(Globals.githubContributorsFilename) != "undefined"
89+
then {
90+
getAuthorsForFilename(Globals.githubContributorsFilename.stripPrefix("/")).onComplete {
91+
case Success(authors) =>
92+
val maybeDiv = Option(document.getElementById("documentation-contributors"))
93+
maybeDiv.foreach { div =>
94+
authors.foreach { case FullAuthor(name, url, img) =>
95+
val divN = document.createElement("div")
96+
val imgN = document.createElement("img").asInstanceOf[html.Image]
97+
imgN.src = img
98+
val autN = document.createElement("a").asInstanceOf[html.Anchor]
99+
autN.href = url
100+
autN.text = name
101+
divN.appendChild(imgN)
102+
divN.appendChild(autN)
103+
div.appendChild(divN)
104+
}
105+
106+
if authors.nonEmpty then
107+
div.asInstanceOf[html.Div].parentElement.classList.toggle("hidden")
108+
}
109+
case Failure(err) =>
110+
println(s"Couldn't fetch contributors. $err")
111+
None
112+
}
113+
}
77114
})
78115

scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
8282
script(raw(s"""var pathToRoot = "${pathToRoot(page.link.dri)}";""")),
8383
(page.content match
8484
case ResolvedTemplate(loadedTemplate, _) =>
85-
val path = loadedTemplate.file.toPath
85+
val path = loadedTemplate.templateFile.file.toPath
8686
ctx.sourceLinks.repoSummary(path) match
8787
case Some(DefinedRepoSummary("github", org, repo)) =>
8888
val tag: TagArg = ctx.sourceLinks.fullPath(relativePath(path)).fold("") { githubContributors =>
89-
script(raw(s"""var githubContributorsUrl = "https://api.github.com/repos/$org/$repo/commits?path=$githubContributors";"""))
89+
Seq(
90+
script(raw(s"""var githubContributorsUrl = "https://api.github.com/repos/$org/$repo";""")),
91+
script(raw(s"""var githubContributorsFilename = "$githubContributors";"""))
92+
)
9093
}
9194
tag // for some reason inference fails so had to state the type explicitly
9295
case _ => ""

0 commit comments

Comments
 (0)