Skip to content

Commit

Permalink
Git Metrics with value source (#366)
Browse files Browse the repository at this point in the history
* adding git metrics under value source

* format metrics
  • Loading branch information
cdsap authored Sep 13, 2023
1 parent 08edb9f commit f672502
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions library/core/talaiot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
api(project(":library:core:talaiot-logger"))
api(project(":library:core:talaiot-request"))
implementation("com.gradle:gradle-enterprise-gradle-plugin:3.5.2")
implementation("io.github.cdsap:commandline-value-source:0.1.0")
testImplementation("io.github.rybalkinsd:kohttp:0.10.0")
testImplementation(project(":library:core:talaiot-test-utils"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Talaiot<T : TalaiotExtension>(
}

private fun populateMetrics(executionReport: ExecutionReport, target: Project) {
MetricsProvider(MetricsConfiguration().build(), executionReport, target).get()
MetricsProvider(MetricsConfiguration().build(target), executionReport, target).get()
}

private fun createTalaiotPublisher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.github.cdsap.talaiot.metrics.RootProjectNameMetric
import io.github.cdsap.talaiot.metrics.SimpleMetric
import io.github.cdsap.talaiot.metrics.UserMetric
import io.github.cdsap.talaiot.metrics.base.Metric
import org.gradle.api.Project

/**
* Configuration for the Metrics extensions
Expand Down Expand Up @@ -123,10 +124,10 @@ class MetricsConfiguration {
}
}

private fun addGitMetrics() {
private fun addGitMetrics(target: Project) {
with(metrics) {
add(GitUserMetric())
add(GitBranchMetric())
add(GitUserMetric(target))
add(GitBranchMetric(target))
}
}

Expand Down Expand Up @@ -244,12 +245,12 @@ class MetricsConfiguration {
}
}

internal fun build(): List<Metric<*, *>> {
internal fun build(target: Project): List<Metric<*, *>> {
if (defaultMetrics) {
addDefaultMetrics()
}
if (gitMetrics) {
addGitMetrics()
addGitMetrics(target)
}
if (performanceMetrics) {
addPerformanceMetrics()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package io.github.cdsap.talaiot.metrics

import io.github.cdsap.talaiot.metrics.base.CmdMetric
import io.github.cdsap.valuesourceprocess.CommandLineWithOutputValue
import org.gradle.api.Project

class GitBranchMetric : CmdMetric(
cmd = "git rev-parse --abbrev-ref HEAD",
class GitBranchMetric(val project: Project) : SimpleMetric<String>(
provider = {
project.providers.of(CommandLineWithOutputValue::class.java) {
it.parameters.commands.set("git rev-parse --abbrev-ref HEAD")
}.get().replace("\n", "")
},
assigner = { report, value -> report.environment.gitBranch = value }
)
class GitUserMetric : CmdMetric(
cmd = "git config --get user.name",

class GitUserMetric(val project: Project) : SimpleMetric<String>(
provider = {
project.providers.of(CommandLineWithOutputValue::class.java) {
it.parameters.commands.set("git config --get user.name")
}.get().replace("\n", "")
},
assigner = { report, value -> report.environment.gitUser = value }
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.cdsap.talaiot.metrics

import com.nhaarman.mockitokotlin2.mock
import io.github.cdsap.talaiot.assertions.shouldContainExactlyTypesOfInAnyOrder
import io.github.cdsap.talaiot.configuration.MetricsConfiguration
import io.github.cdsap.talaiot.entities.CustomProperties
Expand All @@ -8,12 +9,14 @@ import io.github.cdsap.talaiot.mock.AdbVersionMetric
import io.github.cdsap.talaiot.mock.KotlinVersionMetric
import io.kotlintest.shouldBe
import io.kotlintest.specs.BehaviorSpec
import org.gradle.api.Project

class MetricsConfigurationTest : BehaviorSpec({
given("metrics configuration") {
val target = mock<Project>()
`when`("configuration is not changed") {
val metricsConfiguration = MetricsConfiguration()
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("included all metrics") {
val expectedMetricsTypes = defaultMetricsTypes +
environmentMetricsTypes +
Expand All @@ -32,7 +35,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = false
environmentMetrics = false
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("only default metrics are included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(defaultMetricsTypes)
}
Expand All @@ -46,7 +49,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = false
environmentMetrics = true
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("only environment metrics are included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(environmentMetricsTypes)
}
Expand All @@ -59,7 +62,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = false
environmentMetrics = false
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("only performance metrics are included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(performanceMetricsTypes)
}
Expand All @@ -72,7 +75,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = false
environmentMetrics = false
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("only git metrics are included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(gitMetricsTypes)
}
Expand All @@ -86,7 +89,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = true
environmentMetrics = false
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("only gradle switches metrics is included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(gradleSwitchesMetricsTypes)
}
Expand All @@ -100,7 +103,7 @@ class MetricsConfigurationTest : BehaviorSpec({
gradleSwitchesMetrics = false
environmentMetrics = false
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("BuildIdMetric is not included") {
assert(metrics.count { it is BuildIdMetric } == 0)
metrics.shouldContainExactlyTypesOfInAnyOrder(performanceMetricsTypes)
Expand All @@ -115,7 +118,7 @@ class MetricsConfigurationTest : BehaviorSpec({
environmentMetrics = false
generateBuildId = true
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("BuildIdMetric is included") {
val expectedMetricsTypes = performanceMetricsTypes + BuildIdMetric::class
metrics.shouldContainExactlyTypesOfInAnyOrder(expectedMetricsTypes)
Expand All @@ -132,7 +135,7 @@ class MetricsConfigurationTest : BehaviorSpec({

customMetrics(AdbVersionMetric())
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("AdbMetric is included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(listOf(AdbVersionMetric::class))
}
Expand All @@ -150,7 +153,7 @@ class MetricsConfigurationTest : BehaviorSpec({
KotlinVersionMetric()
)
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("AdbMetric and KotlinVersionMetric are included") {
val expectedMetricsTypes = listOf(AdbVersionMetric::class, KotlinVersionMetric::class)
metrics.shouldContainExactlyTypesOfInAnyOrder(expectedMetricsTypes)
Expand All @@ -168,7 +171,7 @@ class MetricsConfigurationTest : BehaviorSpec({
KotlinVersionMetric()
)
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("default metrics and KotlinVersionMetric are included") {
metrics.shouldContainExactlyTypesOfInAnyOrder(defaultMetricsTypes + KotlinVersionMetric::class)
}
Expand All @@ -188,7 +191,7 @@ class MetricsConfigurationTest : BehaviorSpec({

customBuildMetrics(expectedBuildProperties)
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("add custom build metrics to the report") {
metrics.shouldContainExactlyTypesOfInAnyOrder(listOf(SimpleMetric::class, SimpleMetric::class))
val resultingReport = ExecutionReport()
Expand Down Expand Up @@ -220,7 +223,7 @@ class MetricsConfigurationTest : BehaviorSpec({

customTaskMetrics(expectedTaskProperties)
}
val metrics = metricsConfiguration.build()
val metrics = metricsConfiguration.build(target)
then("add custom build metrics to the report") {
metrics.shouldContainExactlyTypesOfInAnyOrder(listOf(SimpleMetric::class, SimpleMetric::class))
val resultingReport = ExecutionReport()
Expand Down

0 comments on commit f672502

Please sign in to comment.