Skip to content

Commit

Permalink
Merge pull request #63 from hmrc/BDOG-182
Browse files Browse the repository at this point in the history
BDOG-182 Bobby Explorer
  • Loading branch information
MartinIngham1 authored Apr 17, 2019
2 parents e95e902 + 644e8f5 commit a2e45fe
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 5 deletions.
34 changes: 34 additions & 0 deletions app/uk/gov/hmrc/cataloguefrontend/BobbyExplorerController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend

import javax.inject.Inject
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.cataloguefrontend.service.BobbyService
import uk.gov.hmrc.play.bootstrap.controller.FrontendController
import views.html.BobbyExplorerPage

import scala.concurrent.ExecutionContext

class BobbyExplorerController @Inject() (mcc : MessagesControllerComponents,
page : BobbyExplorerPage,
bobbyService: BobbyService
)( implicit val ec: ExecutionContext) extends FrontendController(mcc) {
def list(): Action[AnyContent] = Action.async { implicit request =>
bobbyService.getRules().map(r => Ok(page(r)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package uk.gov.hmrc.cataloguefrontend

import java.time.Clock

import com.google.inject.AbstractModule
import uk.gov.hmrc.cataloguefrontend.service.EventsReloadScheduler

class CatalogueFrontendModule extends AbstractModule {

override def configure(): Unit =
override def configure(): Unit = {
bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
bind(classOf[EventsReloadScheduler]).asEagerSingleton()

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package uk.gov.hmrc.cataloguefrontend.connector

import javax.inject.{Inject, Singleton}
import play.api.libs.json._
import uk.gov.hmrc.cataloguefrontend.connector.model.BobbyRuleSet
import uk.gov.hmrc.cataloguefrontend.service.ConfigService._
import uk.gov.hmrc.http.{HeaderCarrier, HttpReads, HttpResponse}
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig
import uk.gov.hmrc.play.bootstrap.http.HttpClient

import scala.concurrent.ExecutionContext
import scala.concurrent.{ExecutionContext, Future}

@Singleton
class ConfigConnector @Inject()(
Expand All @@ -50,4 +51,6 @@ class ConfigConnector @Inject()(

def configByKey(service: String)(implicit hc: HeaderCarrier) =
http.GET[ConfigByKey](s"$serviceConfigsBaseUrl/config-by-key/$service")

def bobbyRules()(implicit hc: HeaderCarrier): Future[BobbyRuleSet] = http.GET[BobbyRuleSet](s"$serviceConfigsBaseUrl/bobby/rules")
}
36 changes: 36 additions & 0 deletions app/uk/gov/hmrc/cataloguefrontend/connector/model/BobbyRule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend.connector.model

import java.time.LocalDate

import play.api.libs.json.{Json, Reads}

case class BobbyRule(organisation: String, name: String, range: String, reason: String, from: LocalDate) {
val groupArtifactName: String = {
val wildcard = "*"
if (organisation == wildcard && name == wildcard) "*" else s"$organisation:$name"
}
}
object BobbyRule {
implicit val reader: Reads[BobbyRule] = Json.reads[BobbyRule]
}

case class BobbyRuleSet(libraries: Seq[BobbyRule], plugins: Seq[BobbyRule])
object BobbyRuleSet {
implicit val reader: Reads[BobbyRuleSet] = Json.reads[BobbyRuleSet]
}
46 changes: 46 additions & 0 deletions app/uk/gov/hmrc/cataloguefrontend/service/BobbyService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend.service

import java.time.{Clock, LocalDate}

import javax.inject.Inject
import uk.gov.hmrc.cataloguefrontend.connector.ConfigConnector
import uk.gov.hmrc.cataloguefrontend.connector.model.{BobbyRule, BobbyRuleSet}
import uk.gov.hmrc.cataloguefrontend.service.model.BobbyRulesView
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.{ExecutionContext, Future}

class BobbyService @Inject()(configConnector: ConfigConnector, clock: Clock)(implicit val ec: ExecutionContext) {

def getRules()(implicit hc: HeaderCarrier) : Future[BobbyRulesView] = {
val today = LocalDate.now(clock)
def filterUpcoming(rule: BobbyRule) = rule.from.isAfter(today)
def filterActive(rule: BobbyRule) = rule.from.isBefore(today) || rule.from.isEqual(today)
def compareUpcoming(rule1: BobbyRule, rule2: BobbyRule) = compareRules(rule1, rule2)(_ isBefore _)
def compareActive(rule1: BobbyRule, rule2: BobbyRule) = compareRules(rule1, rule2)(_ isAfter _)
def compareRules(x: BobbyRule, y: BobbyRule)(sortDirection: (LocalDate, LocalDate) => Boolean) =
if (x.from == y.from) x.groupArtifactName < y.groupArtifactName else sortDirection(x.from, y.from)

configConnector.bobbyRules().map(r => BobbyRulesView(
upcoming = BobbyRuleSet(libraries = r.libraries.filter(filterUpcoming).sortWith(compareUpcoming), plugins = r.plugins.filter(filterUpcoming).sortWith(compareUpcoming)),
active = BobbyRuleSet(libraries = r.libraries.filter(filterActive).sortWith(compareActive), plugins = r.plugins.filter(filterActive).sortWith(compareActive)))
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend.service.model

import uk.gov.hmrc.cataloguefrontend.connector.model.BobbyRuleSet

case class BobbyRulesView (upcoming: BobbyRuleSet, active: BobbyRuleSet)
68 changes: 68 additions & 0 deletions app/views/BobbyExplorerPage.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*@

@import uk.gov.hmrc.cataloguefrontend.ViewMessages
@import uk.gov.hmrc.cataloguefrontend.connector.model.{BobbyRule, BobbyRuleSet}
@import uk.gov.hmrc.cataloguefrontend.service.model.BobbyRulesView

@this(viewMessages: ViewMessages)

@(rules: BobbyRulesView)(implicit request: Request[_])

@standard_layout(s"Bobby rules") {
<header>
<h1>Bobby Rules</h1>
</header>
@bobbyRulesTimeSection(rules.upcoming, "Upcoming", "table table-striped")
@bobbyRulesTimeSection(rules.active, "Active", "table table-striped-warning")
}

@bobbyRulesTimeSection(rulesByTime: BobbyRuleSet, heading: String, cssClass: String) = {
@if(rulesByTime.libraries.nonEmpty || rulesByTime.plugins.nonEmpty) {
<div><h2>@heading</h2></div>
@bobbyRulesArtifactTypeSection(rulesByTime.libraries, "Library", cssClass)
@bobbyRulesArtifactTypeSection(rulesByTime.plugins, "Plugin", cssClass)
}
}

@bobbyRulesArtifactTypeSection(rulesByArtifactType: Seq[BobbyRule], artifactType: String, cssClass: String) = {
@if(rulesByArtifactType.nonEmpty) {
<div>
<table class="@{artifactType.toLowerCase}-rules @cssClass">
<thead>
<tr>
<th>@artifactType</th>
<th>Version</th>
<th>Reason</th>
<th>Active from</th>
</tr>
</thead>
<tbody>
@rulesByArtifactType.map(bobbyRuleRow)
</tbody>
</table>
</div>
}
}

@bobbyRuleRow(rule: BobbyRule) = {
<tr class="bobby-rule">
<td>@rule.groupArtifactName</td>
<td>@rule.range</td>
<td>@rule.reason</td>
<td>@rule.from</td>
</tr>
}
1 change: 1 addition & 0 deletions app/views/standard_layout.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
Tools<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="@appRoutes.BobbyExplorerController.list">Bobby Explorer</a></li>
<li><a href="@appRoutes.DependencyExplorerController.landing">Dependency Explorer</a></li>
<li><a href="@appRoutes.JDKVersionController.compareAllEnvironments()">JDK Explorer</a></li>
<li><a href="@appRoutes.SearchByUrlController.searchLanding#">Search by URL</a></li>
Expand Down
2 changes: 2 additions & 0 deletions conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ GET /dependencyexplorer/results uk.gov.hmrc.cataloguefro

GET /jdkexplorer/:env uk.gov.hmrc.cataloguefrontend.JDKVersionController.findLatestVersions(env: String)
GET /jdkexplorer uk.gov.hmrc.cataloguefrontend.JDKVersionController.compareAllEnvironments

GET /bobbyrules uk.gov.hmrc.cataloguefrontend.BobbyExplorerController.list()
5 changes: 4 additions & 1 deletion public/catalogue-frontend.css
Original file line number Diff line number Diff line change
Expand Up @@ -7900,6 +7900,9 @@ th {
.table-striped > tbody > tr:nth-of-type(odd) {
background-color: #f9f9f9; }

.table-striped-warning > tbody > tr:nth-of-type(odd) {
background-color: #ffb6b6; }

.table-hover > tbody > tr:hover {
background-color: #f5f5f5; }

Expand Down Expand Up @@ -12052,4 +12055,4 @@ section span.other-teams-message {
.padded-table td {
padding-left: 2px;
padding-right: 2px;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend.connector.model

import java.time.LocalDate

object BobbyRuleFactory {
def aBobbyRule(organisation: String = "uk.gov.hmrc", name: String = "play-frontend", range: String = "[*-SNAPSHOT]", reason: String = "No snapshot dependencies permitted", from: LocalDate = LocalDate.of(2015, 3, 16)) =
BobbyRule(organisation, name, range, reason, from)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.cataloguefrontend.connector.model

import org.scalatest.{Matchers, WordSpec}
import uk.gov.hmrc.cataloguefrontend.connector.model.BobbyRuleFactory.aBobbyRule

class BobbyRuleSpec extends WordSpec with Matchers {

"groupArtifactName" should {

"return group ID and artifact ID" in {
aBobbyRule(organisation = "a", name = "b").groupArtifactName should be("a:b")
}

"return * for matching all" in {
aBobbyRule(organisation = "*", name = "*").groupArtifactName should be("*")
}

}
}
Loading

0 comments on commit a2e45fe

Please sign in to comment.