All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.git.metrics.GitDiffMetric.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC4
Show newest version
package ru.tinkoff.plugins.buildmetrics.git.metrics

import ru.tinkoff.plugins.buildmetrics.api.metrics.Metric
import java.util.regex.Pattern

private const val DEFAULT_ERROR_ANSWER = "undefined"
private val DIFF_ADDED_REGEX by lazy { Pattern.compile("(\\d+)\\sinsertions\\(\\+\\)") }
private val DIFF_DELETED_REGEX by lazy { Pattern.compile("(\\d+)\\sdeletions\\(-\\)") }

internal fun gitDiffMetrics(gitDiffCmdString: String): List> {
    return listOf(
        gitDiffMetric("git_lines_added", gitDiffCmdString, DIFF_ADDED_REGEX),
        gitDiffMetric("git_lines_deleted", gitDiffCmdString, DIFF_DELETED_REGEX)
    )
}

private fun gitDiffMetric(metricName: String, gitDiffCmdString: String, regexPattern: Pattern): Metric {
    return Metric(
        name = metricName,
        value = findSubstring(gitDiffCmdString, regexPattern) ?: DEFAULT_ERROR_ANSWER
    )
}

private fun findSubstring(input: String, regexPattern: Pattern): String? {
    val matcher = regexPattern.matcher(input)
    return if (matcher.find()) matcher.group(1) else null
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy