From 8c2280fe3f8827e0b80d04a7c9d1ad716cd86d0d Mon Sep 17 00:00:00 2001 From: jordanrowe <37838443+jordanrowe@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:37:20 +0000 Subject: [PATCH] BDOG-3289 refactor TestType parsing --- .../connectors/GithubConnector.scala | 25 +++++------- .../connectors/ManifestDetailsTest.scala | 40 +++++++------------ 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/app/uk/gov/hmrc/teamsandrepositories/connectors/GithubConnector.scala b/app/uk/gov/hmrc/teamsandrepositories/connectors/GithubConnector.scala index 88762356..ea814eb3 100644 --- a/app/uk/gov/hmrc/teamsandrepositories/connectors/GithubConnector.scala +++ b/app/uk/gov/hmrc/teamsandrepositories/connectors/GithubConnector.scala @@ -378,6 +378,11 @@ case class GhRepository( .repoType .getOrElse(repoTypeHeuristics.inferredRepoType) + val testType: Option[TestType] = + manifestDetails + .testType + .orElse(ManifestDetails.deriveTestType(name)) + val prototypeName: Option[String] = Option.when(repoType == RepoType.Prototype)( manifestDetails @@ -395,7 +400,7 @@ case class GhRepository( isPrivate = isPrivate, repoType = repoType, serviceType = manifestDetails.serviceType, - testType = manifestDetails.testType, + testType = testType, tags = manifestDetails.tags, digitalServiceName = manifestDetails.digitalServiceName, owningTeams = manifestDetails.owningTeams.sorted, @@ -436,7 +441,7 @@ object GhRepository: .split("-").map(_.capitalize).mkString("-") .trim - private def deriveTestType(repoName: String): Option[TestType] = + def deriveTestType(repoName: String): Option[TestType] = repoName match case name if "(?i)(performance|perf)(-test(s)?)".r.findFirstIn(name).isDefined => Some(TestType.Performance) case name if "(?i)(acceptance|ui|journey|api|contract)(-test(s)?)".r.findFirstIn(name).isDefined => Some(TestType.Acceptance) @@ -448,20 +453,10 @@ object GhRepository: logger.warn(s"repository.yaml for $repoName is not valid YAML and could not be parsed. Parsing Exception: ${exception.getMessage}") None case Success(config) => - val repoType = Parser[RepoType].parse(config.get[String]("type").getOrElse("")).toOption - val serviceType = - if repoType.contains(RepoType.Service) then - Parser[ServiceType].parse(config.get[String]("service-type").getOrElse("")).toOption - else None - val testType = - if repoType.contains(RepoType.Test) then - Parser[TestType].parse(config.get[String]("test-type").getOrElse("")).toOption - .orElse(deriveTestType(repoName)) - else None val manifestDetails = ManifestDetails( - repoType = repoType - , serviceType = serviceType - , testType = testType + repoType = Parser[RepoType].parse(config.get[String]("type").getOrElse("")).toOption + , serviceType = Parser[ServiceType].parse(config.get[String]("service-type").getOrElse("")).toOption + , testType = Parser[TestType].parse(config.get[String]("test-type").getOrElse("")).toOption , tags = config.getArray("tags").map(_.flatMap(str => Parser[Tag].parse(str).toOption).toSet) , digitalServiceName = config.get[String]("digital-service").map(formatDigitalServiceName) , description = config.get[String]("description") diff --git a/test/uk/gov/hmrc/teamsandrepositories/connectors/ManifestDetailsTest.scala b/test/uk/gov/hmrc/teamsandrepositories/connectors/ManifestDetailsTest.scala index f98baeb2..6f4180f5 100644 --- a/test/uk/gov/hmrc/teamsandrepositories/connectors/ManifestDetailsTest.scala +++ b/test/uk/gov/hmrc/teamsandrepositories/connectors/ManifestDetailsTest.scala @@ -209,42 +209,30 @@ class ManifestDetailsTest extends AnyWordSpecLike with Matchers: tt shouldBe TestType.Performance "derive test type from repo name" in: - val manifest = - """ - |type: test - |""".stripMargin - - val performance = ManifestDetails.parse("example-performance-tests", manifest) - performance.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from performance-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-performance-tests") + .fold(fail("Unable to parse test-type from performance-tests in repo name")): tt => tt shouldBe TestType.Performance - val perf = ManifestDetails.parse("example-perf-tests" , manifest) - perf.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from perf-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-perf-tests") + .fold(fail("Unable to parse test-type from perf-tests in repo name")): tt => tt shouldBe TestType.Performance - val acceptance = ManifestDetails.parse("example-acceptance-tests" , manifest) - acceptance.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from acceptance-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-acceptance-tests") + .fold(fail("Unable to parse test-type from acceptance-tests in repo name")): tt => tt shouldBe TestType.Acceptance - val ui = ManifestDetails.parse("example-ui-tests" , manifest) - ui.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from ui-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-ui-tests") + .fold(fail("Unable to parse test-type from ui-tests in repo name")): tt => tt shouldBe TestType.Acceptance - val journey = ManifestDetails.parse("example-journey-tests" , manifest) - journey.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from journey-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-journey-tests") + .fold(fail("Unable to parse test-type from journey-tests in repo name")): tt => tt shouldBe TestType.Acceptance - val api = ManifestDetails.parse("example-api-tests" , manifest) - api.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from api-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-api-tests") + .fold(fail("Unable to parse test-type from api-tests in repo name")): tt => tt shouldBe TestType.Acceptance - val contract = ManifestDetails.parse("example-contract-tests" , manifest) - contract.fold(fail("Unable to parse yaml")): data => - data.testType.fold(fail("Unable to parse test-type from contract-tests in repo name")): tt => + ManifestDetails.deriveTestType("example-contract-tests") + .fold(fail("Unable to parse test-type from contract-tests in repo name")): tt => tt shouldBe TestType.Acceptance