Skip to content

Commit

Permalink
Aligning repo and org current-coverage api responses (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigatk authored Mar 30, 2024
1 parent 67b01fa commit 8dffa36
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package projektor.server.api.organization

import projektor.server.api.repository.coverage.RepositoryCurrentCoverage

data class OrganizationCurrentCoverage(val repositories: List<RepositoryCurrentCoverage>)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import java.time.Instant
data class RepositoryCurrentCoverage(
val id: String,
val createdTimestamp: Instant,
val coveredPercentage: BigDecimal
val coveredPercentage: BigDecimal?,
val repo: String,
val project: String?,
val branch: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PreviousTestRunDatabaseRepository(private val dslContext: DSLContext) : Pr

override suspend fun findMostRecentRunWithCoverage(repoName: String, projectName: String?, branch: BranchSearch?): RecentTestRun? =
withContext(Dispatchers.IO) {
val recentTestRun = dslContext.select(TEST_RUN.PUBLIC_ID, TEST_RUN.CREATED_TIMESTAMP)
val recentTestRun = dslContext.select(TEST_RUN.PUBLIC_ID, TEST_RUN.CREATED_TIMESTAMP, GIT_METADATA.BRANCH_NAME)
.from(TEST_RUN)
.innerJoin(GIT_METADATA).on(TEST_RUN.ID.eq(GIT_METADATA.TEST_RUN_ID))
.innerJoin(CODE_COVERAGE_RUN).on(TEST_RUN.PUBLIC_ID.eq(CODE_COVERAGE_RUN.TEST_RUN_PUBLIC_ID))
Expand All @@ -69,7 +69,8 @@ class PreviousTestRunDatabaseRepository(private val dslContext: DSLContext) : Pr
recentTestRun?.let {
RecentTestRun(
publicId = PublicId(it.component1()),
createdTimestamp = it.component2().toInstant(ZoneOffset.UTC)
createdTimestamp = it.component2().toInstant(ZoneOffset.UTC),
branch = it.component3()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import java.time.Instant
data class RecentTestRun(
val publicId: PublicId,
val createdTimestamp: Instant,
val branch: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import kotlinx.coroutines.withContext
import org.jooq.DSLContext
import org.jooq.impl.DSL.firstValue
import org.simpleflatmapper.jdbc.JdbcMapperFactory
import projektor.database.generated.Tables.CODE_COVERAGE_RUN
import projektor.database.generated.Tables.GIT_METADATA
import projektor.database.generated.Tables.TEST_RUN
import projektor.database.generated.Tables.*
import kotlin.streams.toList

class OrganizationCoverageDatabaseRepository(private val dslContext: DSLContext) : OrganizationCoverageRepository {
Expand All @@ -22,12 +20,20 @@ class OrganizationCoverageDatabaseRepository(private val dslContext: DSLContext)
val resultSet = dslContext.selectDistinct(
firstValue(TEST_RUN.PUBLIC_ID).over().partitionBy(GIT_METADATA.REPO_NAME, GIT_METADATA.PROJECT_NAME).orderBy(TEST_RUN.CREATED_TIMESTAMP.desc()).`as`("public_id"),
GIT_METADATA.REPO_NAME,
GIT_METADATA.PROJECT_NAME
GIT_METADATA.PROJECT_NAME,
GIT_METADATA.BRANCH_NAME,
TEST_RUN.CREATED_TIMESTAMP,
)
.from(GIT_METADATA)
.innerJoin(TEST_RUN).on(GIT_METADATA.TEST_RUN_ID.eq(TEST_RUN.ID))
.innerJoin(CODE_COVERAGE_RUN).on(TEST_RUN.PUBLIC_ID.eq(CODE_COVERAGE_RUN.TEST_RUN_PUBLIC_ID))
.where(GIT_METADATA.ORG_NAME.eq(orgName).and(GIT_METADATA.IS_MAIN_BRANCH.eq(true)))
.innerJoin(CODE_COVERAGE_GROUP).on(CODE_COVERAGE_GROUP.CODE_COVERAGE_RUN_ID.eq(CODE_COVERAGE_RUN.ID))
.innerJoin(CODE_COVERAGE_STATS).on(CODE_COVERAGE_STATS.ID.eq(CODE_COVERAGE_GROUP.STATS_ID))
.where(
GIT_METADATA.ORG_NAME.eq(orgName)
.and(GIT_METADATA.IS_MAIN_BRANCH.eq(true))
.and(CODE_COVERAGE_STATS.LINE_COVERED.isNotNull)
)
.fetchResultSet()

val repositoryTestRuns: List<RepositoryTestRun> = resultSet.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package projektor.organization.coverage

import projektor.coverage.CoverageService
import projektor.server.api.organization.OrganizationCoverage
import projektor.server.api.organization.OrganizationCurrentCoverage
import projektor.server.api.organization.RepositoryCoverage
import projektor.server.api.repository.coverage.RepositoryCurrentCoverage

class OrganizationCoverageService(
private val organizationCoverageRepository: OrganizationCoverageRepository,
Expand All @@ -25,6 +27,25 @@ class OrganizationCoverageService(
return OrganizationCoverage(repositories)
}

suspend fun getCurrentCoverage(orgName: String): OrganizationCurrentCoverage {
val repositoryTestRuns = findReposWithCoverage(orgName)

val repositories = repositoryTestRuns.map { repositoryTestRun ->
val coveredPercentage = coverageService.getCoveredLinePercentage(repositoryTestRun.publicId)

RepositoryCurrentCoverage(
id = repositoryTestRun.publicId.id,
createdTimestamp = repositoryTestRun.createdTimestamp,
coveredPercentage = coveredPercentage,
repo = repositoryTestRun.repoName,
project = repositoryTestRun.projectName,
branch = repositoryTestRun.branchName
)
}

return OrganizationCurrentCoverage(repositories)
}

suspend fun findReposWithCoverage(orgName: String): List<RepositoryTestRun> =
organizationCoverageRepository.findReposWithCoverage(orgName)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package projektor.organization.coverage

import projektor.server.api.PublicId
import java.time.Instant

data class RepositoryTestRun(val repoName: String, val publicId: PublicId, val projectName: String?)
data class RepositoryTestRun(val repoName: String, val publicId: PublicId, val createdTimestamp: Instant, val projectName: String?, val branchName: String?)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class RepositoryCoverageService(
RepositoryCurrentCoverage(
id = mostRecentRunWithCoverage.publicId.id,
coveredPercentage = coveredPercentage,
createdTimestamp = mostRecentRunWithCoverage.createdTimestamp
createdTimestamp = mostRecentRunWithCoverage.createdTimestamp,
repo = repoName,
branch = mostRecentRunWithCoverage.branch,
project = projectName
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun Route.api(
get("/api/v1/org/{orgName}/coverage/current") {
val orgName = call.parameters.getOrFail("orgName")

val organizationCoverage = organizationCoverageService.getCoverage(orgName)
val organizationCoverage = organizationCoverageService.getCurrentCoverage(orgName)

if (organizationCoverage.repositories.isNotEmpty()) {
call.respond(HttpStatusCode.OK, organizationCoverage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.Test
import projektor.ApplicationTestCase
import projektor.incomingresults.randomPublicId
import projektor.server.api.organization.OrganizationCoverage
import projektor.server.api.organization.OrganizationCurrentCoverage
import projektor.server.example.coverage.JacocoXmlLoader
import strikt.api.expectThat
import strikt.assertions.doesNotContain
import strikt.assertions.hasSize
import strikt.assertions.isEqualTo
import strikt.assertions.isNotNull
import kotlin.test.assertNotNull

class ApiOrganizationApplicationTestCase : ApplicationTestCase() {
Expand All @@ -35,6 +35,9 @@ class ApiOrganizationApplicationTestCase : ApplicationTestCase() {
val anotherPublicId = randomPublicId()
val anotherRepo = "another-org/repo"

val noCodeCoveragePublicId = randomPublicId()
val noCodeCoverageRepo = "$orgName/no-coverage"

withTestApplication(::createTestApplication) {
handleRequest(HttpMethod.Get, "/api/v1/org/$orgName/coverage/current") {

Expand Down Expand Up @@ -84,48 +87,49 @@ class ApiOrganizationApplicationTestCase : ApplicationTestCase() {
coverageText = JacocoXmlLoader().serverAppReduced(),
repoName = anotherRepo
)

testRunDBGenerator.createSimpleTestRunInRepo(
publicId = noCodeCoveragePublicId,
repoName = noCodeCoverageRepo,
ci = true,
projectName = null
)
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)

val organizationCoverage = objectMapper.readValue(response.content, OrganizationCoverage::class.java)
val organizationCoverage = objectMapper.readValue(response.content, OrganizationCurrentCoverage::class.java)
assertNotNull(organizationCoverage)

expectThat(organizationCoverage.repositories).hasSize(4)

val repositoryDatas1 = organizationCoverage.repositories.filter { it.repoName == repo1 }
val repoNames = organizationCoverage.repositories.map { it.repo }

val repositoryDatas1 = organizationCoverage.repositories.filter { it.repo == repo1 }
expectThat(repositoryDatas1).hasSize(2)

val repo1DataProj1 = repositoryDatas1.find { it.projectName == "proj1" }
val repo1DataProj1 = repositoryDatas1.find { it.project == "proj1" }
assertNotNull(repo1DataProj1)
expectThat(repo1DataProj1.publicId).isEqualTo(publicId1.id)
expectThat(repo1DataProj1.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppLineCoveragePercentage)
}
expectThat(repo1DataProj1.id).isEqualTo(publicId1.id)
expectThat(repo1DataProj1.coveredPercentage).isEqualTo(JacocoXmlLoader.serverAppLineCoveragePercentage)

val repo1DataProj2 = repositoryDatas1.find { it.projectName == "proj2" }
val repo1DataProj2 = repositoryDatas1.find { it.project == "proj2" }
assertNotNull(repo1DataProj2)
expectThat(repo1DataProj2.publicId).isEqualTo(otherProjectRepo1.id)
expectThat(repo1DataProj2.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppReducedLineCoveragePercentage)
}
expectThat(repo1DataProj2.id).isEqualTo(otherProjectRepo1.id)
expectThat(repo1DataProj2.coveredPercentage).isEqualTo(JacocoXmlLoader.serverAppReducedLineCoveragePercentage)

val repositoryData2 = organizationCoverage.repositories.find { it.repoName == repo2 }
val repositoryData2 = organizationCoverage.repositories.find { it.repo == repo2 }
assertNotNull(repositoryData2)

expectThat(repositoryData2.publicId).isEqualTo(publicId2.id)

expectThat(repositoryData2.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.jacocoXmlParserLineCoveragePercentage)
}
expectThat(repositoryData2.id).isEqualTo(publicId2.id)
expectThat(repositoryData2.coveredPercentage).isEqualTo(JacocoXmlLoader.jacocoXmlParserLineCoveragePercentage)

val repositoryData3 = organizationCoverage.repositories.find { it.repoName == repo3 }
val repositoryData3 = organizationCoverage.repositories.find { it.repo == repo3 }
assertNotNull(repositoryData3)

expectThat(repositoryData3.publicId).isEqualTo(publicId3.id)
expectThat(repositoryData3.id).isEqualTo(publicId3.id)
expectThat(repositoryData3.coveredPercentage).isEqualTo(JacocoXmlLoader.junitResultsParserLineCoveragePercentage)

expectThat(repositoryData3.coverage).isNotNull().and {
get { overallStats }.get { lineStat }.get { coveredPercentage }.isEqualTo(JacocoXmlLoader.junitResultsParserLineCoveragePercentage)
}
expectThat(repoNames).doesNotContain(noCodeCoverageRepo)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ApiRepositoryApplicationTestCase : ApplicationTestCase() {
expectThat(currentCoverage) {
get { createdTimestamp }.isEqualTo(thirdTestRunDB.createdTimestamp.toInstant(ZoneOffset.UTC))
get { coveredPercentage }.isEqualTo(JacocoXmlLoader.jacocoXmlParserLineCoveragePercentage)
get { repo }.isEqualTo(repoName)
}
}
}
Expand Down Expand Up @@ -145,6 +146,7 @@ class ApiRepositoryApplicationTestCase : ApplicationTestCase() {
get { id }.isEqualTo(runInDifferentProjectPublicId.id)
get { createdTimestamp }.isEqualTo(currentCoverageTestRunDB.createdTimestamp.toInstant(ZoneOffset.UTC))
get { coveredPercentage }.isEqualTo(JacocoXmlLoader.junitResultsParserLineCoveragePercentage)
get { repo }.isEqualTo(repoName)
}
}
}
Expand Down Expand Up @@ -213,6 +215,8 @@ class ApiRepositoryApplicationTestCase : ApplicationTestCase() {
get { id }.isEqualTo(firstRunPublicId.id)
get { createdTimestamp }.isEqualTo(currentCoverageTestRunDB.createdTimestamp.toInstant(ZoneOffset.UTC))
get { coveredPercentage }.isEqualTo(JacocoXmlLoader.serverAppReducedLineCoveragePercentage)
get { repo }.isEqualTo(repoName)
get { branch }.isEqualTo("my-branch")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class PreviousTestRunServiceTest : DatabaseRepositoryTestCase() {
expectThat(recentTestRun).isNotNull().and {
get { publicId }.isEqualTo(newPublicId)
get { createdTimestamp }.isEqualTo(newTestRun.createdTimestamp.toInstant(ZoneOffset.UTC))
get { branch }.isEqualTo("main")
}
}
}

0 comments on commit 8dffa36

Please sign in to comment.