From c088aa86178263b93455c5e15023110867a1cc75 Mon Sep 17 00:00:00 2001 From: Marco F Date: Tue, 21 Nov 2023 14:51:08 +0100 Subject: [PATCH 1/4] first attempt at fixing issue 1268 --- .../main/scala/scaladex/client/Client.scala | 97 ++++++++++++------- 1 file changed, 62 insertions(+), 35 deletions(-) diff --git a/modules/webclient/src/main/scala/scaladex/client/Client.scala b/modules/webclient/src/main/scala/scaladex/client/Client.scala index 419f187b7..af4e4e44a 100644 --- a/modules/webclient/src/main/scala/scaladex/client/Client.scala +++ b/modules/webclient/src/main/scala/scaladex/client/Client.scala @@ -26,7 +26,45 @@ object Client { } } + @js.native + trait Repo extends js.Object { + val default_branch: String = js.native + } + private def fetchAndReplaceReadme(element: Element, token: Option[String]): Unit = { + + def getLogoAndSetHTML(branch: String, organization: String, repository: String) = { + val root = s"https://github.com/$organization/$repository" + val raw = s"$root/raw/$branch" + val blob = s"$root/blob/$branch" + + element + .querySelectorAll("img,a") + .filter(e => !Seq("href", "src").flatMap(a => Option(e.getAttribute(a))).head.startsWith("http")) + .foreach { e => + val (at, newBase) = + if (e.tagName == "A") { + val attr = "href" + val href = + if (e.getAttribute(attr).startsWith("#")) root + else blob + + e.setAttribute("target", "_blank") + (attr, href) + } else ("src", raw) + + Option(e.getAttribute(at)) + .foreach { oldUrl => + if (oldUrl.nonEmpty) { + val newUrl = + if (!oldUrl.startsWith("/")) s"$newBase/$oldUrl" + else s"$newBase$oldUrl" + e.setAttribute(at, newUrl) + } + } + } + } + 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") @@ -36,18 +74,21 @@ 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( + val readmeRequest = new Request( s"https://api.github.com/repos/$organization/$repository/readme", new RequestInit { headers = headersWithCreds.toJSDictionary } ) - fetch(request).toFuture + + val repoRequest = new Request( + s"https://api.github.com/repos/$organization/$repository", + new RequestInit { + headers = headersWithCreds.toJSDictionary + } + ) + + def setReadme() = fetch(readmeRequest).toFuture .flatMap { res => if (res.status == 200) { res.text().toFuture @@ -55,35 +96,21 @@ object Client { Future.successful("No README found for this project, please check the repository") } } - .foreach { res => - element.innerHTML = res - - element - .querySelectorAll("img,a") - .filter(e => !Seq("href", "src").flatMap(a => Option(e.getAttribute(a))).head.startsWith("http")) - .foreach { e => - val (at, newBase) = - if (e.tagName == "A") { - val attr = "href" - val href = - if (e.getAttribute(attr).startsWith("#")) root - else blob - - e.setAttribute("target", "_blank") - (attr, href) - } else ("src", raw) - - Option(e.getAttribute(at)) - .foreach { oldUrl => - if (oldUrl.nonEmpty) { - val newUrl = - if (!oldUrl.startsWith("/")) s"$newBase/$oldUrl" - else s"$newBase$oldUrl" - e.setAttribute(at, newUrl) - } - } - } + .flatMap(res => Future { element.innerHTML = res }) + + def setLogo() = fetch(repoRequest).toFuture + .flatMap(res => res.text().toFuture) + .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 From c73d009f6f6fdf6a3ca555c8eb57fe52703d48eb Mon Sep 17 00:00:00 2001 From: Marco F Date: Tue, 21 Nov 2023 15:02:53 +0100 Subject: [PATCH 2/4] first attempt at fixing issue 1268 #2 --- .../webclient/src/main/scala/scaladex/client/Client.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/webclient/src/main/scala/scaladex/client/Client.scala b/modules/webclient/src/main/scala/scaladex/client/Client.scala index af4e4e44a..c581d73f6 100644 --- a/modules/webclient/src/main/scala/scaladex/client/Client.scala +++ b/modules/webclient/src/main/scala/scaladex/client/Client.scala @@ -74,21 +74,21 @@ object Client { .map(t => headers + ("Authorization" -> s"bearer $t")) .getOrElse(headers) - val readmeRequest = new Request( + val readmeRequest: Request = new Request( s"https://api.github.com/repos/$organization/$repository/readme", new RequestInit { headers = headersWithCreds.toJSDictionary } ) - val repoRequest = new Request( + val repoRequest: Request = new Request( s"https://api.github.com/repos/$organization/$repository", new RequestInit { headers = headersWithCreds.toJSDictionary } ) - def setReadme() = fetch(readmeRequest).toFuture + def setReadme(): Future[Unit] = fetch(readmeRequest).toFuture .flatMap { res => if (res.status == 200) { res.text().toFuture @@ -98,7 +98,7 @@ object Client { } .flatMap(res => Future { element.innerHTML = res }) - def setLogo() = fetch(repoRequest).toFuture + def setLogo(): Future[Unit] = fetch(repoRequest).toFuture .flatMap(res => res.text().toFuture) .flatMap { res => val resJson = js.JSON.parse(res) From d960fa30b611b787ebc30cb8cda4be7712c91541 Mon Sep 17 00:00:00 2001 From: Marco F Date: Tue, 21 Nov 2023 15:06:23 +0100 Subject: [PATCH 3/4] first attempt at fixing issue 1268 #3 --- .../main/scala/scaladex/client/Client.scala | 123 +++++++++--------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/modules/webclient/src/main/scala/scaladex/client/Client.scala b/modules/webclient/src/main/scala/scaladex/client/Client.scala index c581d73f6..305c0b877 100644 --- a/modules/webclient/src/main/scala/scaladex/client/Client.scala +++ b/modules/webclient/src/main/scala/scaladex/client/Client.scala @@ -33,38 +33,6 @@ object Client { private def fetchAndReplaceReadme(element: Element, token: Option[String]): Unit = { - def getLogoAndSetHTML(branch: String, organization: String, repository: String) = { - val root = s"https://github.com/$organization/$repository" - val raw = s"$root/raw/$branch" - val blob = s"$root/blob/$branch" - - element - .querySelectorAll("img,a") - .filter(e => !Seq("href", "src").flatMap(a => Option(e.getAttribute(a))).head.startsWith("http")) - .foreach { e => - val (at, newBase) = - if (e.tagName == "A") { - val attr = "href" - val href = - if (e.getAttribute(attr).startsWith("#")) root - else blob - - e.setAttribute("target", "_blank") - (attr, href) - } else ("src", raw) - - Option(e.getAttribute(at)) - .foreach { oldUrl => - if (oldUrl.nonEmpty) { - val newUrl = - if (!oldUrl.startsWith("/")) s"$newBase/$oldUrl" - else s"$newBase$oldUrl" - e.setAttribute(at, newUrl) - } - } - } - } - 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") @@ -74,37 +42,72 @@ object Client { .map(t => headers + ("Authorization" -> s"bearer $t")) .getOrElse(headers) - val readmeRequest: Request = new Request( - s"https://api.github.com/repos/$organization/$repository/readme", - new RequestInit { - headers = headersWithCreds.toJSDictionary - } - ) + def setReadme(): Future[Unit] = { + val readmeRequest: Request = new Request( + s"https://api.github.com/repos/$organization/$repository/readme", + new RequestInit { + headers = headersWithCreds.toJSDictionary + } + ) + + 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 }) + } - val repoRequest: Request = new Request( - s"https://api.github.com/repos/$organization/$repository", - new RequestInit { - headers = headersWithCreds.toJSDictionary + def setLogo(): Future[Unit] = { + def getLogoAndSetHTML(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") + .filter(e => !Seq("href", "src").flatMap(a => Option(e.getAttribute(a))).head.startsWith("http")) + .foreach { e => + val (at, newBase) = + if (e.tagName == "A") { + val attr = "href" + val href = + if (e.getAttribute(attr).startsWith("#")) root + else blob + + e.setAttribute("target", "_blank") + (attr, href) + } else ("src", raw) + + Option(e.getAttribute(at)) + .foreach { oldUrl => + if (oldUrl.nonEmpty) { + val newUrl = + if (!oldUrl.startsWith("/")) s"$newBase/$oldUrl" + else s"$newBase$oldUrl" + e.setAttribute(at, newUrl) + } + } + } } - ) - - def setReadme(): Future[Unit] = 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") + + val repoRequest: Request = new Request( + s"https://api.github.com/repos/$organization/$repository", + new RequestInit { + headers = headersWithCreds.toJSDictionary } - } - .flatMap(res => Future { element.innerHTML = res }) - - def setLogo(): Future[Unit] = fetch(repoRequest).toFuture - .flatMap(res => res.text().toFuture) - .flatMap { res => - val resJson = js.JSON.parse(res) - val branch = resJson.asInstanceOf[Repo].default_branch - Future(getLogoAndSetHTML(branch, organization, repository)) - } + ) + fetch(repoRequest).toFuture + .flatMap(res => res.text().toFuture) + .flatMap { res => + val resJson = js.JSON.parse(res) + val branch = resJson.asInstanceOf[Repo].default_branch + Future(getLogoAndSetHTML(branch, organization, repository)) + } + } for { _ <- setReadme() From d5fd4d210eea7fd7e7ece1d8a78939397154b1bb Mon Sep 17 00:00:00 2001 From: Marco F Date: Tue, 21 Nov 2023 18:02:27 +0100 Subject: [PATCH 4/4] first attempt at fixing issue 1268 #4 --- .../webclient/src/main/scala/scaladex/client/Client.scala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/webclient/src/main/scala/scaladex/client/Client.scala b/modules/webclient/src/main/scala/scaladex/client/Client.scala index 305c0b877..8fbc08e40 100644 --- a/modules/webclient/src/main/scala/scaladex/client/Client.scala +++ b/modules/webclient/src/main/scala/scaladex/client/Client.scala @@ -101,7 +101,13 @@ object Client { } ) fetch(repoRequest).toFuture - .flatMap(res => res.text().toFuture) + .flatMap { res => + if (res.status == 200) { + res.text().toFuture + } else { + Future.successful("{\"default_branch\": \"master\"}") + } + } .flatMap { res => val resJson = js.JSON.parse(res) val branch = resJson.asInstanceOf[Repo].default_branch