Skip to content

Commit

Permalink
BDOG-175 handle qualifier searches in Dependency explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed May 8, 2019
1 parent e5657fb commit 5bbe296
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import scala.concurrent.{ExecutionContext, Future}

@Singleton
class DependencyExplorerController @Inject()(
mcc : MessagesControllerComponents,
trConnector: TeamsAndRepositoriesConnector,
service : DependenciesService,
page : DependencyExplorerPage
mcc : MessagesControllerComponents
, trConnector: TeamsAndRepositoriesConnector
, service : DependenciesService
, page : DependencyExplorerPage
)(implicit val ec: ExecutionContext
) extends FrontendController(mcc) {

Expand Down Expand Up @@ -96,14 +96,22 @@ class DependencyExplorerController @Inject()(
)
} yield
if (query.asCsv) {
val csv = CsvUtils.toCsv(toRows(results))
val source = Source.single(ByteString(csv, "UTF-8"))
val csv = CsvUtils.toCsv(toRows(results))
val source = Source.single(ByteString(csv, "UTF-8"))
Result(
header = ResponseHeader(200, Map("Content-Disposition" -> "inline; filename=\"depex.csv\"")),
body = HttpEntity.Streamed(source, None, Some("text/csv"))
)
}
else Ok(page(form.bindFromRequest(), teams, flags, groupArtefacts, Some(versionRange), Some(results), Some(pieData)))
else Ok(page(
form.bindFromRequest()
, teams
, flags
, groupArtefacts
, if (query.versionRange.isEmpty) None else Some(versionRange)
, Some(results)
, Some(pieData)
))
).merge
)
}
Expand All @@ -112,14 +120,15 @@ class DependencyExplorerController @Inject()(

/** @param versionRange replaces versionOp and version, supporting Maven version range */
case class SearchForm(
team : String,
flag : String,
group : String,
artefact : String,
versionOp : String,
version : String,
versionRange: String,
asCsv : Boolean = false)
team : String
, flag : String
, group : String
, artefact : String
, versionOp : String
, version : String
, versionRange: String
, asCsv : Boolean = false
)

// Forms.nonEmptyText, but has no constraint info label
def notEmpty = {
Expand All @@ -146,21 +155,23 @@ class DependencyExplorerController @Inject()(

object DependencyExplorerController {
case class PieData(
title : String,
results: Map[String, Int])
title : String
, results: Map[String, Int]
)


def toRows(seq: Seq[ServiceWithDependency]): Seq[Map[String, String]] =
seq.flatMap { serviceWithDependency =>
val m = Map(
"slugName" -> serviceWithDependency.slugName,
"slugVersion" -> serviceWithDependency.slugVersion,
"team" -> "",
"depGroup" -> serviceWithDependency.depGroup,
"depArtefact" -> serviceWithDependency.depArtefact,
"depVersion" -> serviceWithDependency.depVersion,
"depSemanticVersion" -> serviceWithDependency.depSemanticVersion.map(_.toString).getOrElse(""))
"slugName" -> serviceWithDependency.slugName
, "slugVersion" -> serviceWithDependency.slugVersion
, "team" -> ""
, "depGroup" -> serviceWithDependency.depGroup
, "depArtefact" -> serviceWithDependency.depArtefact
, "depVersion" -> serviceWithDependency.depVersion
, "depSemanticVersion" -> serviceWithDependency.depSemanticVersion.map(_.toString).getOrElse("")
)
if (serviceWithDependency.teams.isEmpty) Seq(m)
else serviceWithDependency.teams.map { team => m + ("team" -> team) }
else serviceWithDependency.teams.map(team => m + ("team" -> team))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import play.api.libs.json.{Json, Reads, __}
import play.api.libs.functional.syntax._


case class BobbyRule(group: String, artefact: String, range: BobbyVersionRange, reason: String, from: LocalDate) {
val groupArtifactName: String = {
val wildcard = "*"
if (group == wildcard && artefact == wildcard) "*" else s"$group:$artefact"
}
}
case class BobbyRule(
group : String
, artefact: String
, range : BobbyVersionRange
, reason : String
, from : LocalDate
)

object BobbyRule {
val reads: Reads[BobbyRule] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ case class Dependencies(
case class BobbyVersion(version: Version, inclusive: Boolean)

// TODO rename as VersionRange?
/** Iso to Either[Qualifier, (Option[LowerBound], Option[UpperBound])]*/
case class BobbyVersionRange(
lowerBound: Option[BobbyVersion]
, upperBound: Option[BobbyVersion]
Expand All @@ -89,6 +90,10 @@ case class BobbyVersionRange(
}

def isMatch(v: Version): Boolean = {
val qualFilter: Function1[Version, Boolean] = qualifier match {
case Some(qual) => _.toString.contains(qual)
case None => _ => false
}
val lbFilter: Function1[Version, Boolean] = lowerBound match {
case Some(BobbyVersion(version, true)) => _ >= version
case Some(BobbyVersion(version, false)) => _ > version
Expand All @@ -99,7 +104,7 @@ case class BobbyVersionRange(
case Some(BobbyVersion(version, false)) => _ < version
case None => _ => true
}
lbFilter(v) && ubFilter(v) // No arrow notation in scala?
qualFilter(v) || (lbFilter(v) && ubFilter(v))
}
}

Expand Down
5 changes: 4 additions & 1 deletion app/uk/gov/hmrc/cataloguefrontend/service/BobbyService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class BobbyService @Inject()(configConnector: ConfigConnector, clock: Clock)(imp

def sort(ruleset: BobbyRuleSet, sortDateLt: (LocalDate, LocalDate) => Boolean) = {
def sortRulesLt(x: BobbyRule, y: BobbyRule) =
if (x.from == y.from) x.groupArtifactName < y.groupArtifactName
if (x.from == y.from) {
if (x.group == y.group) x.artefact < y.artefact
else x.group < y.group
}
else sortDateLt(x.from, y.from)

BobbyRuleSet(
Expand Down
2 changes: 1 addition & 1 deletion app/views/BobbyExplorerPage.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h1>Bobby Rules</h1>

@bobbyRuleRow(rule: BobbyRule) = {
<tr id="rule-@rule.artefact" class="bobby-rule">
<td>@rule.groupArtifactName</td>
<td>@rule.group:@rule.artefact</td>
@defining(rule.range.rangeDescr) { rangeDescr =>
@rangeDescr match {
case Some((lbDescr, upDescr)) => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class DependencySpec extends FreeSpec with Matchers {

"should return BobbyRuleViolated if dependency has any broken bobby rules" in {
Dependency("library-abc", Version("1.2.3"), Some(Version("2.2.3")),
Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(1,1,1)))).versionState shouldBe Some(VersionState.BobbyRuleViolated)
Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(1,1,1)))).versionState shouldBe Some(VersionState.BobbyRuleViolated)
}

"should return BobbyRulePending if dependency will break future rules" in {
new Dependency(
"library-abc"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(9999,1,1))(now = LocalDate.of(200,1,2)))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(9999,1,1))(now = LocalDate.of(200,1,2)))
)
.versionState shouldBe Some(VersionState.BobbyRulePending)
}
Expand All @@ -64,8 +64,8 @@ class DependencySpec extends FreeSpec with Matchers {
"library-abc"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq( BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2))
, BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2))
, Seq( BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2))
, BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2))
)
).versionState shouldBe Some(VersionState.BobbyRuleViolated)
}
Expand All @@ -87,14 +87,14 @@ class DependencySpec extends FreeSpec with Matchers {
"library-abc"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2)))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2)))
)

val pendingDep = new Dependency(
"library-xyz"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2)))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2)))
)

val goodDep = Dependency("library-lol", Version("1.2.3"), Some(Version("2.2.3")))
Expand All @@ -115,14 +115,14 @@ class DependencySpec extends FreeSpec with Matchers {
"library-abc"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2)))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(1,1,1))(now = LocalDate.of(2000,1,2)))
)

val pendingDep = new Dependency(
"library-xyz"
, Version("1.2.3")
, Some(Version("2.2.3"))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("1.2.3"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2)))
, Seq(BobbyRuleViolation("banned library", BobbyVersionRange("[1.2.3]"), LocalDate.of(9999,1,1))(now = LocalDate.of(2000,1,2)))
)

val goodDep = Dependency("library-lol", Version("1.2.3"), Some(Version("2.2.3")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class SlugInfoServiceSpec
when(boot.mockedServiceDependenciesConnector.getServicesWithDependency(SlugInfoFlag.Latest, group, artefact))
.thenReturn(Future(Seq(v100, v200, v205)))

await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Gte, version = Version("1.0.1"))) shouldBe Seq(v205, v200)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Lte, version = Version("1.0.1"))) shouldBe Seq(v100)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Eq, version = Version("2.0.0"))) shouldBe Seq(v200)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("[1.0.1,)"))) shouldBe Seq(v205, v200)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("(,1.0.1]"))) shouldBe Seq(v100)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("[2.0.0]"))) shouldBe Seq(v200)
}

"include non-parseable versions" in {
Expand All @@ -87,8 +87,8 @@ class SlugInfoServiceSpec
when(boot.mockedServiceDependenciesConnector.getServicesWithDependency(SlugInfoFlag.Latest, group, artefact))
.thenReturn(Future(Seq(v100, v200, v205, bad)))

await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Gte, version = Version("1.0.1"))) shouldBe Seq(v205, v200, bad)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Lte, version = Version("1.0.1"))) shouldBe Seq(v100, bad)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("[1.0.1,)"))) shouldBe Seq(v205, v200, bad)
await(boot.service.getServicesWithDependency(optTeam = None, SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("(,1.0.1]"))) shouldBe Seq(v100, bad)
}

"filter results by team" in {
Expand All @@ -98,8 +98,8 @@ class SlugInfoServiceSpec
when(boot.mockedServiceDependenciesConnector.getServicesWithDependency(SlugInfoFlag.Latest, group, artefact))
.thenReturn(Future(Seq(v100, v200, v205)))

await(boot.service.getServicesWithDependency(optTeam = Some("T1"), SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Gte, version = Version("1.0.1"))) shouldBe Seq(v200)
await(boot.service.getServicesWithDependency(optTeam = Some("T2"), SlugInfoFlag.Latest, group, artefact, versionOp = VersionOp.Gte, version = Version("1.0.1"))) shouldBe Seq(v205, v200)
await(boot.service.getServicesWithDependency(optTeam = Some("T1"), SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("[1.0.1,)"))) shouldBe Seq(v200)
await(boot.service.getServicesWithDependency(optTeam = Some("T2"), SlugInfoFlag.Latest, group, artefact, BobbyVersionRange("[1.0.1,)"))) shouldBe Seq(v205, v200)
}
}

Expand All @@ -125,7 +125,6 @@ class SlugInfoServiceSpec

await(boot.service.getJDKCountsForEnv(SlugInfoFlag.Latest)) shouldBe JDKUsageByEnv(SlugInfoFlag.Latest.s, Map.empty[String, Int])
}

}


Expand Down

0 comments on commit 5bbe296

Please sign in to comment.