Skip to content

Commit

Permalink
Merge pull request #1436 from adpi2/fix-binary-version-filter
Browse files Browse the repository at this point in the history
Fix artifacts page filters
  • Loading branch information
adpi2 authored Aug 13, 2024
2 parents 3fcd4f5 + 51f860d commit e38c282
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import java.util.UUID
import scala.concurrent.Future

import scaladex.core.model._
import scaladex.core.web.ArtifactsPageParams

trait WebDatabase {
// artifacts
// insertArtifact return a boolean. It's true if a new project is inserted, false otherwise
def insertArtifact(artifact: Artifact, dependencies: Seq[ArtifactDependency], time: Instant): Future[Boolean]
def getArtifacts(groupId: Artifact.GroupId, artifactId: Artifact.ArtifactId): Future[Seq[Artifact]]
def getArtifacts(projectRef: Project.Reference): Future[Seq[Artifact]]
def getArtifacts(
ref: Project.Reference,
artifactName: Artifact.Name,
params: ArtifactsPageParams
): Future[Seq[Artifact]]
def getArtifacts(ref: Project.Reference, artifactName: Artifact.Name, preReleases: Boolean): Future[Seq[Artifact]]
def getArtifacts(ref: Project.Reference, artifactName: Artifact.Name, version: SemanticVersion): Future[Seq[Artifact]]
def getArtifactsByName(projectRef: Project.Reference, artifactName: Artifact.Name): Future[Seq[Artifact]]
def getLatestArtifacts(ref: Project.Reference, preferStableVersions: Boolean): Future[Seq[Artifact]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import scaladex.core.model.SemanticVersion
import scaladex.core.model.UserInfo
import scaladex.core.model.UserState
import scaladex.core.service.SchedulerDatabase
import scaladex.core.web.ArtifactsPageParams

class InMemoryDatabase extends SchedulerDatabase {
private val allProjects = mutable.Map[Project.Reference, Project]()
Expand Down Expand Up @@ -161,9 +160,9 @@ class InMemoryDatabase extends SchedulerDatabase {
override def getArtifacts(
ref: Project.Reference,
artifactName: Artifact.Name,
params: ArtifactsPageParams
preReleases: Boolean
): Future[Seq[Artifact]] =
// does not filter with params
// TODO: use preReleases to filter
Future.successful(allArtifacts.getOrElse(ref, Seq.empty).filter(_.artifactName == artifactName))
override def getProjectDependencies(
ref: Project.Reference,
Expand Down
9 changes: 2 additions & 7 deletions modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import scaladex.core.model.SemanticVersion
import scaladex.core.model.UserInfo
import scaladex.core.model.UserState
import scaladex.core.service.SchedulerDatabase
import scaladex.core.web.ArtifactsPageParams
import scaladex.infra.sql.ArtifactDependencyTable
import scaladex.infra.sql.ArtifactTable
import scaladex.infra.sql.DoobieUtils
Expand Down Expand Up @@ -235,13 +234,9 @@ class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) exten
override def getArtifacts(
ref: Project.Reference,
artifactName: Artifact.Name,
params: ArtifactsPageParams
preReleases: Boolean
): Future[Seq[Artifact]] =
run(
ArtifactTable
.selectArtifactByParams(params.binaryVersions, params.preReleases)
.to[Seq](ref, artifactName)
)
run(ArtifactTable.selectArtifactByParams(preReleases).to[Seq](ref, artifactName))

override def countVersions(ref: Project.Reference): Future[Long] =
run(ArtifactTable.countVersionsByProject.unique(ref))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.time.Instant
import doobie._
import doobie.util.update.Update
import scaladex.core.model.Artifact
import scaladex.core.model.BinaryVersion
import scaladex.core.model.Language
import scaladex.core.model.Platform
import scaladex.core.model.Project
Expand Down Expand Up @@ -83,21 +82,12 @@ object ArtifactTable {
where = Seq("organization=?", "repository=?", "artifact_name=?", "version=?")
)

def selectArtifactByParams(
binaryVersions: Seq[BinaryVersion],
preReleases: Boolean
): Query[(Project.Reference, Artifact.Name), Artifact] = {
val binaryVersionFilter =
if (binaryVersions.isEmpty) "true"
else
binaryVersions
.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"
def selectArtifactByParams(preReleases: Boolean): Query[(Project.Reference, Artifact.Name), Artifact] = {
val preReleaseFilter = if (preReleases) "true" else "is_prerelease=false"
Query[(Project.Reference, Artifact.Name), Artifact](
s"""|SELECT ${fields.mkString(", ")}
|FROM $table WHERE
|organization=? AND repository=? AND artifact_name=? AND $binaryVersionFilter AND $preReleaseFilter
|FROM $table
|WHERE organization=? AND repository=? AND artifact_name=? AND $preReleaseFilter
|""".stripMargin
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.scalatest.matchers.should.Matchers
import scaladex.core.model.Jvm
import scaladex.core.model.Scala
import scaladex.core.model.ScalaJs
import scaladex.core.test.Values._
import scaladex.infra.BaseDatabaseSuite

class ArtifactTableTests extends AnyFunSpec with BaseDatabaseSuite with Matchers {
Expand All @@ -29,9 +28,8 @@ class ArtifactTableTests extends AnyFunSpec with BaseDatabaseSuite with Matchers
it("check getReleasesFromArtifacts")(check(getReleasesFromArtifacts))
it("check countVersionsByProject")(check(countVersionsByProject))
it("check selectArtifactByParams") {
check(selectArtifactByParams(Seq.empty, false))
check(selectArtifactByParams(Seq.empty, true))
check(selectArtifactByParams(Seq(`_sjs0.6_2.13`), false))
check(selectArtifactByParams(false))
check(selectArtifactByParams(true))
}
it("check selectMavenReferenceWithNoReleaseDate")(check(selectMavenReferenceWithNoReleaseDate))
it("check selectLatestArtifacts") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ProjectPages(env: Env, database: WebDatabase, searchEngine: SearchEngine)(
path(projectM / "artifacts" / artifactNameM) { (ref, artifactName) =>
artifactsParams { params =>
getProjectOrRedirect(ref, user) { project =>
val artifactsF = database.getArtifacts(ref, artifactName, params)
val artifactsF = database.getArtifacts(ref, artifactName, params.preReleases)
val headerF = service.getProjectHeader(project).map(_.get)
for (artifacts <- artifactsF; header <- headerF) yield {
val binaryVersions = artifacts
Expand Down

0 comments on commit e38c282

Please sign in to comment.