Skip to content

Take redirections into account in UrlChecker #3586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ final class SelfCheckAlg[F[_]](config: Config)(implicit

private def checkUrlChecker: F[Unit] =
config.urlCheckerTestUrls.traverse_ { url =>
urlChecker.exists(url).flatMap { urlExists =>
urlChecker.validate(url).flatMap { res =>
val msg = s"Self check of UrlChecker failed: checking that $url exists failed"
F.whenA(!urlExists)(logger.warn(msg))
F.whenA(res.notExists)(logger.warn(msg))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
.traverse { case (_, dependency) =>
coursierAlg
.getMetadata(dependency, resolvers)
.flatMap(_.filterUrls(urlChecker.exists))
.flatMap(_.filterUrls(uri => urlChecker.validate(uri).map(_.exists)))
.tupleLeft(dependency)
}
.map(_.toMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@
/** A URL of a resource that provides additional information for an update. */
sealed trait UpdateInfoUrl {
def url: Uri

def withUrl(url: Uri): UpdateInfoUrl
}

object UpdateInfoUrl {
final case class CustomChangelog(url: Uri) extends UpdateInfoUrl
final case class CustomReleaseNotes(url: Uri) extends UpdateInfoUrl
final case class GitHubReleaseNotes(url: Uri) extends UpdateInfoUrl
final case class VersionDiff(url: Uri) extends UpdateInfoUrl
final case class CustomChangelog(url: Uri) extends UpdateInfoUrl {
override def withUrl(url: Uri): UpdateInfoUrl = CustomChangelog(url)

Check warning on line 31 in modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala#L31

Added line #L31 was not covered by tests
}
final case class CustomReleaseNotes(url: Uri) extends UpdateInfoUrl {
override def withUrl(url: Uri): UpdateInfoUrl = CustomReleaseNotes(url)

Check warning on line 34 in modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala#L34

Added line #L34 was not covered by tests
}
final case class GitHubReleaseNotes(url: Uri) extends UpdateInfoUrl {
override def withUrl(url: Uri): UpdateInfoUrl = GitHubReleaseNotes(url)

Check warning on line 37 in modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala#L37

Added line #L37 was not covered by tests
}
final case class VersionDiff(url: Uri) extends UpdateInfoUrl {
override def withUrl(url: Uri): UpdateInfoUrl = VersionDiff(url)

Check warning on line 40 in modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrl.scala#L40

Added line #L40 was not covered by tests
}

implicit val updateInfoUrlOrder: Order[UpdateInfoUrl] =
Order.by {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.scalasteward.core.nurture

import cats.Monad
import cats.{Functor, Monad, Parallel}
import cats.syntax.all.*
import org.http4s.Uri
import org.scalasteward.core.application.Config.ForgeCfg
Expand All @@ -31,22 +31,37 @@
final class UpdateInfoUrlFinder[F[_]](implicit
config: ForgeCfg,
urlChecker: UrlChecker[F],
parallel: Parallel[F],
F: Monad[F]
) {
def findUpdateInfoUrls(
dependency: DependencyMetadata,
versionUpdate: Version.Update
): F[List[UpdateInfoUrl]] = {
val updateInfoUrls: List[UpdateInfoUrl] =
dependency.releaseNotesUrl.toList.map(CustomReleaseNotes.apply) ++
dependency.forgeRepo.toSeq.flatMap(forgeRepo =>
possibleUpdateInfoUrls(forgeRepo, versionUpdate)
)
val updateInfoUrls: F[List[UpdateInfoUrl]] =
dependency.forgeRepo.toList
.flatTraverse(forgeRepo => possibleUpdateInfoUrls(urlChecker)(forgeRepo, versionUpdate))
.map(dependency.releaseNotesUrl.toList.map(CustomReleaseNotes.apply) ++ _)

updateInfoUrls
.sorted(UpdateInfoUrl.updateInfoUrlOrder.toOrdering)
.distinctBy(_.url)
.filterA(updateInfoUrl => urlChecker.exists(updateInfoUrl.url))
for {
urls <- updateInfoUrls
.map(
_.sorted(UpdateInfoUrl.updateInfoUrlOrder.toOrdering)
.distinctBy(_.url)
)
checkedUrls <-
urls.parFlatTraverse(updateInfoUrl =>
urlChecker
.validate(updateInfoUrl.url)
.map(
_.fold(
exists = List(updateInfoUrl),
notExists = List.empty,
redirectTo = u => List(updateInfoUrl.withUrl(u))

Check warning on line 60 in modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrlFinder.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/nurture/UpdateInfoUrlFinder.scala#L60

Added line #L60 was not covered by tests
)
)
)
} yield checkedUrls
}
}

Expand Down Expand Up @@ -81,17 +96,31 @@
repoForge.diffUrlFor(tagName(update.currentVersion), tagName(update.nextVersion))
)

private[nurture] def possibleUpdateInfoUrls(
private[nurture] def possibleUpdateInfoUrls[F[_]: Functor](urlChecker: UrlChecker[F])(
forgeRepo: ForgeRepo,
update: Version.Update
): List[UpdateInfoUrl] = {
): F[List[UpdateInfoUrl]] = {
def customUrls(wrap: Uri => UpdateInfoUrl, fileNames: List[String]): List[UpdateInfoUrl] =
fileNames.map(f => wrap(forgeRepo.fileUrlFor(f)))

gitHubReleaseNotesFor(forgeRepo, update.nextVersion) ++
customUrls(CustomReleaseNotes.apply, possibleReleaseNotesFilenames) ++
customUrls(CustomChangelog.apply, possibleChangelogFilenames) ++
possibleVersionDiffs(forgeRepo, update)
for {
forgeRepoMaybe <- urlChecker
.validate(forgeRepo.repoUrl)
.map(
_.fold(
exists = forgeRepo.some,
notExists = none,
redirectTo = u => forgeRepo.copy(repoUrl = u).some
)
)
urls =
forgeRepoMaybe.toList.flatMap(forgeRepoChecked =>
gitHubReleaseNotesFor(forgeRepoChecked, update.nextVersion) ++
customUrls(CustomReleaseNotes.apply, possibleReleaseNotesFilenames) ++
customUrls(CustomChangelog.apply, possibleChangelogFilenames) ++
possibleVersionDiffs(forgeRepoChecked, update)
)
} yield urls
}

private def gitHubReleaseNotesFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,51 @@ import cats.effect.Sync
import cats.syntax.all.*
import com.github.benmanes.caffeine.cache.Caffeine
import org.http4s.client.Client
import org.http4s.headers.Location
import org.http4s.{Method, Request, Status, Uri}
import org.scalasteward.core.application.Config
import org.typelevel.log4cats.Logger
import scalacache.Entry
import scalacache.caffeine.CaffeineCache

trait UrlChecker[F[_]] {
def exists(url: Uri): F[Boolean]
def validate(url: Uri): F[UrlChecker.UrlValidationResult]
}

final case class UrlCheckerClient[F[_]](client: Client[F]) extends AnyVal

object UrlChecker {

sealed abstract class UrlValidationResult extends Product with Serializable {
def exists: Boolean =
fold(exists = true, notExists = false, redirectTo = _ => false)

def notExists: Boolean =
fold(exists = false, notExists = true, redirectTo = _ => false)

def fold[A](exists: => A, notExists: => A, redirectTo: Uri => A): A =
this match {
case UrlValidationResult.Exists => exists
case UrlValidationResult.NotExists => notExists
case UrlValidationResult.RedirectTo(url) => redirectTo(url)
}
}

object UrlValidationResult {
case object Exists extends UrlValidationResult
case object NotExists extends UrlValidationResult
final case class RedirectTo(url: Uri) extends UrlValidationResult
}

private def buildCache[F[_]](config: Config)(implicit
F: Sync[F]
): F[CaffeineCache[F, String, Status]] =
): F[CaffeineCache[F, String, UrlValidationResult]] =
F.delay {
val cache = Caffeine
.newBuilder()
.maximumSize(16384L)
.expireAfterWrite(config.cacheTtl.length, config.cacheTtl.unit)
.build[String, Entry[Status]]()
.build[String, Entry[UrlValidationResult]]()
CaffeineCache(cache)
}

Expand All @@ -52,16 +75,45 @@ object UrlChecker {
): F[UrlChecker[F]] =
buildCache(config).map { statusCache =>
new UrlChecker[F] {
override def exists(url: Uri): F[Boolean] =
status(url).map(_ === Status.Ok).handleErrorWith { throwable =>
logger.debug(throwable)(s"Failed to check if $url exists").as(false)
}
override def validate(url: Uri): F[UrlValidationResult] =
check(url).handleErrorWith(th =>
logger
.debug(th)(s"Failed to check if $url exists")
.as(UrlValidationResult.NotExists)
)

/** While checking for the [[Uri]]s presence, we perform up to 3 recursive lookups when
* receiving a `MovedPermanently` response.
*/
private def check(url: Uri): F[UrlValidationResult] = {
def lookup(url: Uri, maxDepth: Int): F[Option[Uri]] =
Option(maxDepth).filter(_ > 0).flatTraverse { depth =>
val req = Request[F](method = Method.HEAD, uri = url)

modify(req).flatMap(r =>
urlCheckerClient.client.run(r).use {
case resp if resp.status === Status.Ok =>
F.pure(url.some)

case resp if resp.status === Status.MovedPermanently =>
resp.headers
.get[Location]
.flatTraverse(location => lookup(location.uri, depth - 1))

case _ =>
F.pure(none)
}
)
}

private def status(url: Uri): F[Status] =
statusCache.cachingF(url.renderString)(None) {
val req = Request[F](method = Method.HEAD, uri = url)
modify(req).flatMap(urlCheckerClient.client.status)
lookup(url, maxDepth = 3).map {
case Some(u) if u === url => UrlValidationResult.Exists
case Some(u) => UrlValidationResult.RedirectTo(u)
case None => UrlValidationResult.NotExists
}
}
}
}
}
}
Loading