Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mainline when no branch specified when getting repo current coverage #1215

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}
}
}
Loading