From 529bf287f5902bc942636d4e851bfc5f0a98778f Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Thu, 30 May 2024 15:25:37 +0200 Subject: [PATCH] fix default artifact --- .../main/scala/scaladex/core/model/ProjectHeader.scala | 10 ++++++---- .../main/scala/scaladex/infra/sql/ArtifactTable.scala | 9 ++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/modules/core/shared/src/main/scala/scaladex/core/model/ProjectHeader.scala b/modules/core/shared/src/main/scala/scaladex/core/model/ProjectHeader.scala index b92cdc404..bfeebdeb0 100644 --- a/modules/core/shared/src/main/scala/scaladex/core/model/ProjectHeader.scala +++ b/modules/core/shared/src/main/scala/scaladex/core/model/ProjectHeader.scala @@ -1,5 +1,7 @@ package scaladex.core.model +import java.time.Instant + object ProjectHeader { def apply( ref: Project.Reference, @@ -52,18 +54,18 @@ final case class ProjectHeader( def byName(artifacts: Seq[Artifact]): Option[Artifact] = defaultArtifactName.toSeq .flatMap(defaultName => artifacts.filter(a => defaultName == a.artifactName)) - .maxByOption(_.binaryVersion) + .maxByOption(a => (a.binaryVersion, a.releaseDate)) def ofVersion(version: SemanticVersion): Artifact = filteredArtifacts .filter(_.version == version) - .maxBy(a => (a.binaryVersion, a.artifactName))( - Ordering.Tuple2(Ordering[BinaryVersion], Ordering[Artifact.Name].reverse) + .maxBy(a => (a.binaryVersion, a.artifactName, a.releaseDate))( + Ordering.Tuple3(Ordering[BinaryVersion], Ordering[Artifact.Name].reverse, Ordering[Instant]) ) // find version of latest stable artifact then default artifact of that version def byLatestVersion(artifacts: Seq[Artifact]): Option[Artifact] = - artifacts.sortBy(_.releaseDate).lastOption.map(a => ofVersion(a.version)) + artifacts.maxByOption(_.releaseDate).map(a => ofVersion(a.version)) if (preferStableVersion) { byName(stableArtifacts) diff --git a/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala b/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala index b65b7cf8c..ee001d2ed 100644 --- a/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala +++ b/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala @@ -94,16 +94,11 @@ object ArtifactTable { .map(bv => s"(platform='${bv.platform.label}' AND language_version='${bv.language.label}')") .mkString("(", " OR ", ")") val preReleaseFilter = if (preReleases) s"true" else "is_prerelease=false" - val releases = - s"""|SELECT organization, repository, artifact_name, version + Query[(Project.Reference, Artifact.Name), Artifact]( + s"""|SELECT ${fields.mkString(", ")} |FROM $table WHERE |organization=? AND repository=? AND artifact_name=? AND $binaryVersionFilter AND $preReleaseFilter |""".stripMargin - val artifactFields = fields.map(f => s"a.$f").mkString(", ") - Query[(Project.Reference, Artifact.Name), Artifact]( - s"""|SELECT $artifactFields FROM ($releases) r INNER JOIN $table a ON - | r.version = a.version AND r.organization = a.organization AND - | r.repository = a.repository AND r.artifact_name = a.artifact_name""".stripMargin ) }