Skip to content

Commit

Permalink
Remove more implicits
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Dec 19, 2024
1 parent 3087cc2 commit f2e683d
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.cataloguefrontend.createappconfigs
import cats.data.EitherT
import play.api.data.{Form, Forms}
import play.api.i18n.I18nSupport
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Result}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, RequestHeader, Result}
import play.api.{Configuration, Logger}
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector._
Expand Down Expand Up @@ -81,7 +81,8 @@ class CreateAppConfigsController @Inject()(
auth.authenticatedAction(
continueUrl = routes.CreateAppConfigsController.createAppConfigsLanding(serviceName),
retrieval = Retrieval.hasPredicate(createAppConfigsPermission(serviceName))
) .async { implicit request =>
) .async: request =>
given RequestHeader = request
(
for
repo <- EitherT.fromOptionF[Future, Result, GitRepository](
Expand Down Expand Up @@ -121,23 +122,22 @@ class CreateAppConfigsController @Inject()(
)
)
).merge
}

def createAppConfigs(serviceName: ServiceName): Action[AnyContent] =
auth.authorizedAction(
predicate = createAppConfigsPermission(serviceName),
continueUrl = routes.CreateAppConfigsController.createAppConfigsLanding(serviceName)
).async { implicit request =>
).async: request =>
given AuthenticatedRequest[AnyContent, Unit] = request
(
for
repo <- EitherT.fromOptionF[Future, Result, GitRepository](
teamsAndRepositoriesConnector.repositoryDetails(serviceName.asString),
NotFound(error_404_template())
)
serviceType <- EitherT.fromOption[Future](repo.serviceType, InternalServerError("No Service Type"))
configChecks <- EitherT.liftF[Future, Result, List[Check]](
configChecks <- EitherT.liftF[Future, Result, List[Check]]:
serviceCommissioningStatusConnector.commissioningStatus(serviceName)
)
baseConfig = checkAppConfigBaseExists(configChecks)
envConfigs = checkAppConfigEnvExists(configChecks)
isApi = serviceType == ServiceType.Backend && repo.tags.getOrElse(Set.empty[Tag]).contains(Tag.Api)
Expand Down Expand Up @@ -186,7 +186,6 @@ class CreateAppConfigsController @Inject()(
yield
Redirect(uk.gov.hmrc.cataloguefrontend.servicecommissioningstatus.routes.ServiceCommissioningStatusController.getCommissioningState(serviceName))
).merge
}

end CreateAppConfigsController

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.cataloguefrontend.dependency

import cats.data.EitherT
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, RequestHeader}
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.model.{DependencyScope, given}
import uk.gov.hmrc.cataloguefrontend.dependency.view.html.{DependencyGraphsPage, DependenciesPage}
Expand Down Expand Up @@ -45,24 +45,24 @@ class DependenciesController @Inject() (
with CatalogueAuthBuilders:

def services(name: ServiceName): Action[AnyContent] =
BasicAuthAction.async:
implicit request =>
for
deployments <- whatsRunningWhereService.releasesForService(name).map(_.versions)
dependencies <- dependenciesService.search(name, deployments)
yield
Ok(dependenciesPage(name, dependencies.sortBy(_.version)(Ordering[Version].reverse)))
BasicAuthAction.async: request =>
given RequestHeader = request
for
deployments <- whatsRunningWhereService.releasesForService(name).map(_.versions)
dependencies <- dependenciesService.search(name, deployments)
yield
Ok(dependenciesPage(name, dependencies.sortBy(_.version)(Ordering[Version].reverse)))

def service(name: ServiceName, version: String): Action[AnyContent] =
BasicAuthAction.async:
implicit request =>
dependenciesService
.getServiceDependencies(name, Version(version))
.map(oDeps => Ok(dependenciesPage(name, oDeps.toSeq)))
BasicAuthAction.async: request =>
given RequestHeader = request
dependenciesService
.getServiceDependencies(name, Version(version))
.map(oDeps => Ok(dependenciesPage(name, oDeps.toSeq)))

def graphs(name: ServiceName, version: String, scope: String): Action[AnyContent] =
BasicAuthAction.async:
implicit request =>
BasicAuthAction.async: request =>
given RequestHeader = request
(for
scope <- EitherT
.fromEither[Future](Parser[DependencyScope].parse(scope))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DependencyExplorerController @Inject() (


def landing: Action[AnyContent] =
BasicAuthAction.async { implicit request =>
BasicAuthAction.async: request =>
given MessagesRequest[AnyContent] = request
for
teams <- trConnector.allTeams().map(_.map(_.name).sorted)
groupArtefacts <- dependenciesService.getGroupArtefacts()
Expand All @@ -80,7 +81,6 @@ class DependencyExplorerController @Inject() (
repoTypes = RepoType.values.toSeq.filterNot(_ == RepoType.Prototype)
)
)
}

def search(
group : String,
Expand All @@ -92,7 +92,8 @@ class DependencyExplorerController @Inject() (
`repoType[]`: Option[Seq[String]],
asCsv : Boolean
): Action[AnyContent] =
BasicAuthAction.async(implicit request =>
BasicAuthAction.async: request =>
given MessagesRequest[AnyContent] = request
for
teams <- trConnector.allTeams().map(_.map(_.name).sorted)
flags = SlugInfoFlag.values.toSeq
Expand Down Expand Up @@ -184,7 +185,6 @@ class DependencyExplorerController @Inject() (
)
}
yield res
)

/** @param versionRange replaces versionOp and version, supporting Maven version range */
case class SearchForm(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.cataloguefrontend.deployments
import cats.data.EitherT
import play.api.i18n.I18nSupport
import play.api.libs.json.Writes
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Result}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Request, Result}
import play.api.{Logging, Configuration}
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.{GitHubProxyConnector, RepoType, ServiceDependenciesConnector, TeamsAndRepositoriesConnector}
Expand Down Expand Up @@ -82,7 +82,8 @@ class DeployServiceController @Inject()(
auth.authenticatedAction(
continueUrl = routes.DeployServiceController.step1(serviceName),
retrieval = servicesRetrieval
).async { implicit request =>
).async: request =>
given AuthenticatedRequest[AnyContent, Set[Resource]] = request
(for
allServices <- EitherT.right[Result](teamsAndRepositoriesConnector.allRepositories(repoType = Some(RepoType.Service), archived = Some(false)))
accessibleRepos <- EitherT.pure[Future, Result](cleanseServices(request.retrieval))
Expand Down Expand Up @@ -121,14 +122,14 @@ class DeployServiceController @Inject()(
yield
Ok(deployServicePage(form, hasPerm, accessibleServices, latest, releases, environments, evaluations = None))
).merge
}

// Display service info - config warnings, vulnerabilities, etc
def step2(): Action[AnyContent] =
auth.authenticatedAction(
continueUrl = routes.DeployServiceController.step1(None)
, retrieval = servicesRetrieval
).async { implicit request =>
).async: request =>
given AuthenticatedRequest[AnyContent, Set[Resource]] = request
(for
allServices <- EitherT.right[Result](teamsAndRepositoriesConnector.allRepositories(repoType = Some(RepoType.Service), archived = Some(false)))
accessibleRepos <- EitherT.pure[Future, Result](cleanseServices(request.retrieval))
Expand Down Expand Up @@ -191,14 +192,14 @@ class DeployServiceController @Inject()(
Some((gitHubCompare, jvmChanges, configChanges, configWarnings, vulnerabilities))
))
).merge
}

// Deploy service and redirects (to avoid redeploying on refresh)
def step3(): Action[AnyContent] =
auth.authenticatedAction(
continueUrl = routes.DeployServiceController.step1(None)
, retrieval = Retrieval.username ~ servicesRetrieval
).async { implicit request =>
).async: request =>
given AuthenticatedRequest[AnyContent, Retrieval.Username ~ Set[Resource]] = request
val username ~ locations = request.retrieval
(for
allServices <- EitherT.right[Result](teamsAndRepositoriesConnector.allRepositories(repoType = Some(RepoType.Service), archived = Some(false)))
Expand Down Expand Up @@ -237,7 +238,6 @@ class DeployServiceController @Inject()(
, queueUrl = RedirectUrl(queueUrl)
))
).merge
}

private val redirectUrlPolicy =
AbsoluteWithHostnameFromAllowlist:
Expand All @@ -252,7 +252,8 @@ class DeployServiceController @Inject()(
queueUrl : RedirectUrl,
buildUrl : Option[RedirectUrl]
): Action[AnyContent] =
BasicAuthAction.async { implicit request =>
BasicAuthAction.async: request =>
given Request[AnyContent] = request
(for
qUrl <- EitherT.fromEither[Future](queueUrl.getEither[RedirectUrlPolicy.Id](redirectUrlPolicy))
.leftMap(_ => BadRequest("Invalid queueUrl"))
Expand All @@ -276,7 +277,6 @@ class DeployServiceController @Inject()(
)
yield res
).merge
}

def step4sse(queueUrl: RedirectUrl, buildUrl: Option[RedirectUrl]): Action[AnyContent] =
import scala.concurrent.duration._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class DeploymentEventsController @Inject()(
import DeploymentEventsController._

def deploymentEvents(env: Environment = Production): Action[AnyContent] =
BasicAuthAction.async { implicit request =>
BasicAuthAction.async: request =>
given MessagesRequest[AnyContent] = request
form
.bindFromRequest()
.fold(
Expand Down Expand Up @@ -75,7 +76,6 @@ class DeploymentEventsController @Inject()(
)
)
)
}

end DeploymentEventsController

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import cats.data.OptionT
import cats.implicits._
import play.api.Logging
import play.api.mvc._

import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.{GitHubProxyConnector, RepoType, ServiceDependenciesConnector, TeamsAndRepositoriesConnector}
import uk.gov.hmrc.cataloguefrontend.deployments.view.html.{DeploymentTimelinePage, DeploymentTimelineSelectPage}
Expand Down Expand Up @@ -55,47 +54,47 @@ class DeploymentTimelineController @Inject()(
with Logging:

def graph(service: Option[ServiceName], to: LocalDate, from: LocalDate) =
BasicAuthAction.async:
implicit request =>
val start = to.atStartOfDayInstant
val end = from.atEndOfDayInstant
BasicAuthAction.async: request =>
given RequestHeader = request
val start = to.atStartOfDayInstant
val end = from.atEndOfDayInstant

for
services <- teamsAndRepositoriesConnector.allRepositories(repoType = Some(RepoType.Service))
serviceNames = services.map(s => ServiceName(s.name))
events <- service match
case Some(service) if start.isBefore(end) =>
deploymentGraphService.findEvents(service, start, end).map(_.filter(_.env != Environment.Integration)) // filter as only platform teams are interested in this env
case _ =>
Future.successful(Seq.empty[DeploymentTimelineEvent])
slugInfo <- events
.groupBy(_.version)
.keys
.toList
.foldLeftM[Future, Seq[ServiceDependencies]](Seq.empty): (xs, v) =>
service
.fold(Future.successful(Option.empty[ServiceDependencies])): serviceName =>
serviceDependenciesConnector.getSlugInfo(serviceName, Some(v))
.map:
xs ++ _.toSeq
yield Ok(deploymentTimelinePage(service, start, end, events, slugInfo, serviceNames))
for
services <- teamsAndRepositoriesConnector.allRepositories(repoType = Some(RepoType.Service))
serviceNames = services.map(s => ServiceName(s.name))
events <- service match
case Some(service) if start.isBefore(end) =>
deploymentGraphService.findEvents(service, start, end).map(_.filter(_.env != Environment.Integration)) // filter as only platform teams are interested in this env
case _ =>
Future.successful(Seq.empty[DeploymentTimelineEvent])
slugInfo <- events
.groupBy(_.version)
.keys
.toList
.foldLeftM[Future, Seq[ServiceDependencies]](Seq.empty): (xs, v) =>
service
.fold(Future.successful(Option.empty[ServiceDependencies])): serviceName =>
serviceDependenciesConnector.getSlugInfo(serviceName, Some(v))
.map:
xs ++ _.toSeq
yield Ok(deploymentTimelinePage(service, start, end, events, slugInfo, serviceNames))

def graphSelect(serviceName: ServiceName, deploymentId: String, fromDeploymentId: Option[String]) =
BasicAuthAction.async:
implicit request =>
(for
configChanges <- OptionT(serviceConfigsService.configChanges(deploymentId, fromDeploymentId))
previousVersion = configChanges.app.from.getOrElse(Version("0.1.0"))
deployedVersion = configChanges.app.to
environment = configChanges.env.environment
deployedSlug <- OptionT(serviceDependenciesConnector.getSlugInfo(serviceName, Some(deployedVersion)))
oPreviousSlug <- OptionT.liftF(serviceDependenciesConnector.getSlugInfo(serviceName, Some(previousVersion)))
oGitHubCompare <- OptionT.liftF:
gitHubProxyConnector
.compare(serviceName.asString, v1 = previousVersion, v2 = deployedVersion)
.recover:
case NonFatal(ex) => logger.error(s"Could not call git compare ${ex.getMessage}", ex); None
jvmChanges = (oPreviousSlug.map(_.java), deployedSlug.java)
yield
Ok(deploymentTimelineSelectPage(serviceName, environment, Some(previousVersion), deployedVersion, oGitHubCompare, jvmChanges, configChanges))
).getOrElse(NotFound("Could not compare deployments - data not found or initial deployment"))
BasicAuthAction.async: request =>
given RequestHeader = request
(for
configChanges <- OptionT(serviceConfigsService.configChanges(deploymentId, fromDeploymentId))
previousVersion = configChanges.app.from.getOrElse(Version("0.1.0"))
deployedVersion = configChanges.app.to
environment = configChanges.env.environment
deployedSlug <- OptionT(serviceDependenciesConnector.getSlugInfo(serviceName, Some(deployedVersion)))
oPreviousSlug <- OptionT.liftF(serviceDependenciesConnector.getSlugInfo(serviceName, Some(previousVersion)))
oGitHubCompare <- OptionT.liftF:
gitHubProxyConnector
.compare(serviceName.asString, v1 = previousVersion, v2 = deployedVersion)
.recover:
case NonFatal(ex) => logger.error(s"Could not call git compare ${ex.getMessage}", ex); None
jvmChanges = (oPreviousSlug.map(_.java), deployedSlug.java)
yield
Ok(deploymentTimelineSelectPage(serviceName, environment, Some(previousVersion), deployedVersion, oGitHubCompare, jvmChanges, configChanges))
).getOrElse(NotFound("Could not compare deployments - data not found or initial deployment"))
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
, oGitHubCompare : Option[GitHubProxyConnector.Compare]
, jvmChanges : (Option[ServiceJdkVersion], ServiceJdkVersion)
, configChanges : ConfigChanges
)(implicit request: Request[_])
)(implicit request: RequestHeader)

@standard_layout("Deployment Timeline", active = "deployments", showNavBar = false) {
<div class="mt-3 mr-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.cataloguefrontend.healthindicators

import play.api.data.{Form, Forms}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, MessagesRequest}
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.{RepoType, TeamsAndRepositoriesConnector}
import uk.gov.hmrc.cataloguefrontend.healthindicators.HealthIndicatorsFilter.form
Expand All @@ -42,18 +42,19 @@ class HealthIndicatorsController @Inject() (
with CatalogueAuthBuilders:

def breakdownForRepo(name: String): Action[AnyContent] =
BasicAuthAction.async { implicit request =>
BasicAuthAction.async: request =>
given MessagesRequest[AnyContent] = request
for
indicator <- healthIndicatorsConnector.getIndicator(name)
history <- healthIndicatorsConnector.getHistoricIndicators(name)
average <- healthIndicatorsConnector.getAveragePlatformScore()
yield indicator match
case Some(indicator) => Ok(HealthIndicatorsPage(indicator, history, average.map(_.averageScore)))
case None => NotFound(error_404_template())
}

def indicatorsForRepoType(): Action[AnyContent] =
BasicAuthAction.async { implicit request =>
BasicAuthAction.async: request =>
given MessagesRequest[AnyContent] = request
form
.bindFromRequest()
.fold(
Expand All @@ -68,7 +69,7 @@ class HealthIndicatorsController @Inject() (
indicators = indicatorsWithTeams.filter(t => validForm.team.fold(true)(t.owningTeams.contains))
yield Ok(HealthIndicatorsLeaderBoard(indicators, RepoType.values.toSeq, teams.sortBy(_.name), form.fill(validForm)))
)
}

end HealthIndicatorsController

object HealthIndicatorsController:
Expand Down
Loading

0 comments on commit f2e683d

Please sign in to comment.