From 385bc04c45c3468d97516ad9badc24701110e722 Mon Sep 17 00:00:00 2001 From: Craig Atkinson Date: Sun, 31 Mar 2024 08:17:38 -0500 Subject: [PATCH] Use mainline when no branch specified when getting repo current coverage (#1215) --- .../main/kotlin/projektor/route/ApiRoutes.kt | 9 +++- .../api/ApiOrganizationApplicationTestCase.kt | 8 +++ .../api/ApiRepositoryApplicationTestCase.kt | 52 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/server/server-app/src/main/kotlin/projektor/route/ApiRoutes.kt b/server/server-app/src/main/kotlin/projektor/route/ApiRoutes.kt index f501ed62e..38b256be0 100644 --- a/server/server-app/src/main/kotlin/projektor/route/ApiRoutes.kt +++ b/server/server-app/src/main/kotlin/projektor/route/ApiRoutes.kt @@ -8,6 +8,7 @@ import io.ktor.server.util.* import projektor.organization.coverage.OrganizationCoverageService import projektor.repository.coverage.RepositoryCoverageService import projektor.server.api.repository.BranchSearch +import projektor.server.api.repository.BranchType fun Route.api( organizationCoverageService: OrganizationCoverageService, @@ -33,10 +34,16 @@ fun Route.api( val projectName = call.request.queryParameters["project"] val branchName = call.request.queryParameters["branch"] + val branchSearch = if (branchName != null) { + BranchSearch(branchName = branchName) + } else { + BranchSearch(branchType = BranchType.MAINLINE) + } + val repositoryCurrentCoverage = repositoryCoverageService.fetchRepositoryCurrentCoverage( fullRepoName, projectName, - BranchSearch(branchName = branchName) + branchSearch ) repositoryCurrentCoverage?.let { call.respond(HttpStatusCode.OK, it) } diff --git a/server/server-app/src/test/kotlin/projektor/api/ApiOrganizationApplicationTestCase.kt b/server/server-app/src/test/kotlin/projektor/api/ApiOrganizationApplicationTestCase.kt index 67bfeaa7b..3d09163f9 100644 --- a/server/server-app/src/test/kotlin/projektor/api/ApiOrganizationApplicationTestCase.kt +++ b/server/server-app/src/test/kotlin/projektor/api/ApiOrganizationApplicationTestCase.kt @@ -111,23 +111,31 @@ class ApiOrganizationApplicationTestCase : ApplicationTestCase() { assertNotNull(repo1DataProj1) expectThat(repo1DataProj1.id).isEqualTo(publicId1.id) expectThat(repo1DataProj1.coveredPercentage).isEqualTo(JacocoXmlLoader.serverAppLineCoveragePercentage) + expectThat(repo1DataProj1.repo).isEqualTo(repo1) + expectThat(repo1DataProj1.branch).isEqualTo("main") val repo1DataProj2 = repositoryDatas1.find { it.project == "proj2" } assertNotNull(repo1DataProj2) expectThat(repo1DataProj2.id).isEqualTo(otherProjectRepo1.id) expectThat(repo1DataProj2.coveredPercentage).isEqualTo(JacocoXmlLoader.serverAppReducedLineCoveragePercentage) + expectThat(repo1DataProj2.repo).isEqualTo(repo1) + expectThat(repo1DataProj2.branch).isEqualTo("main") val repositoryData2 = organizationCoverage.repositories.find { it.repo == repo2 } assertNotNull(repositoryData2) expectThat(repositoryData2.id).isEqualTo(publicId2.id) expectThat(repositoryData2.coveredPercentage).isEqualTo(JacocoXmlLoader.jacocoXmlParserLineCoveragePercentage) + expectThat(repositoryData2.repo).isEqualTo(repo2) + expectThat(repositoryData2.branch).isEqualTo("main") val repositoryData3 = organizationCoverage.repositories.find { it.repo == repo3 } assertNotNull(repositoryData3) expectThat(repositoryData3.id).isEqualTo(publicId3.id) expectThat(repositoryData3.coveredPercentage).isEqualTo(JacocoXmlLoader.junitResultsParserLineCoveragePercentage) + expectThat(repositoryData3.repo).isEqualTo(repo3) + expectThat(repositoryData3.branch).isEqualTo("main") expectThat(repoNames).doesNotContain(noCodeCoverageRepo) } diff --git a/server/server-app/src/test/kotlin/projektor/api/ApiRepositoryApplicationTestCase.kt b/server/server-app/src/test/kotlin/projektor/api/ApiRepositoryApplicationTestCase.kt index db0abfaf8..906f28acd 100644 --- a/server/server-app/src/test/kotlin/projektor/api/ApiRepositoryApplicationTestCase.kt +++ b/server/server-app/src/test/kotlin/projektor/api/ApiRepositoryApplicationTestCase.kt @@ -221,4 +221,56 @@ class ApiRepositoryApplicationTestCase : ApplicationTestCase() { } } } + + @Test + fun `when no branch specific should fetch current coverage from mainline branch`() { + val orgName = RandomStringUtils.randomAlphabetic(12) + val repoName = "$orgName/repo" + + val otherBranchRunPublicId1 = randomPublicId() + val mainRunPublicId = randomPublicId() + val otherBranchRunPublicId2 = randomPublicId() + + withTestApplication(::createTestApplication) { + handleRequest(HttpMethod.Get, "/api/v1/repo/$repoName/coverage/current") { + + testRunDBGenerator.createTestRunWithCoverageAndGitMetadata( + publicId = otherBranchRunPublicId1, + coverageText = JacocoXmlLoader().serverAppReduced(), + repoName = repoName, + branchName = "other" + ) + + testRunDBGenerator.createTestRunWithCoverageAndGitMetadata( + publicId = mainRunPublicId, + coverageText = JacocoXmlLoader().serverApp(), + repoName = repoName, + branchName = "main" + ) + + testRunDBGenerator.createTestRunWithCoverageAndGitMetadata( + publicId = otherBranchRunPublicId2, + coverageText = JacocoXmlLoader().jacocoXmlParser(), + repoName = repoName, + branchName = "other" + ) + }.apply { + expectThat(response.status()).isEqualTo(HttpStatusCode.OK) + val responseBody = response.content + assertNotNull(responseBody) + + val currentCoverage: RepositoryCurrentCoverage = objectMapper.readValue(responseBody) + + val currentCoverageTestRunDB = testRunDao.fetchOneByPublicId(mainRunPublicId.id) + + expectThat(currentCoverage) { + get { id }.isEqualTo(mainRunPublicId.id) + get { createdTimestamp }.isEqualTo(currentCoverageTestRunDB.createdTimestamp.toInstant(ZoneOffset.UTC)) + get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppLineCoveragePercentage) + get { repo }.isEqualTo(repoName) + get { branch }.isEqualTo("main") + } + } + } + } }