Skip to content

Commit

Permalink
Use mainline when no branch specified when getting repo current cover…
Browse files Browse the repository at this point in the history
…age (#1215)
  • Loading branch information
craigatk authored Mar 31, 2024
1 parent 8dffa36 commit 385bc04
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
}

0 comments on commit 385bc04

Please sign in to comment.