Skip to content

Commit

Permalink
updating formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Nov 26, 2024
1 parent 468ada3 commit 1f473cb
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fun GitAdapter.sortIntoTagSets(
}
}.filter { it.second.isNotEmpty() }

private fun List<TagRef>.tagPairs(): List<Pair<TagRef?, TagRef?>> =
listOf(Pair(null, firstOrNull())) + mapIndexed { index, commitRef ->
commitRef to getOrNull(index + 1)
}
private fun List<TagRef>.tagPairs(): List<Pair<TagRef?, TagRef?>> = listOf(Pair(null, firstOrNull())) + mapIndexed { index, commitRef ->
commitRef to getOrNull(index + 1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ private fun mergedStoryIds(messageDigResults: List<CommitInspectionResult>) = me
}
}

private fun CommitRef.commitInspectionResult(digResult: MessageDigResult) =
CommitInspectionResult(
storyId = digResult.storyId,
ease = digResult.ease,
authors = listOf(committerEmail, authorEmail) + digResult.coauthors,
semver = digResult.semver,
)
private fun CommitRef.commitInspectionResult(digResult: MessageDigResult) = CommitInspectionResult(
storyId = digResult.storyId,
ease = digResult.ease,
authors = listOf(committerEmail, authorEmail) + digResult.coauthors,
semver = digResult.semver,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ class DiggerCore(
) {
private fun tagRefs() = gitWrapper.listTags().filter { tagRegex.matches(it.name) }

fun currentContributionData() =
with(gitWrapper) {
val (currentTag, previousTag) = currentRelevantTags(
headCommitId = headCommitId(),
lastTwoTags = tagRefs().take(2),
fun currentContributionData() = with(gitWrapper) {
val (currentTag, previousTag) = currentRelevantTags(
headCommitId = headCommitId(),
lastTwoTags = tagRefs().take(2),
)
messageDigger.contribution(currentContributionCommits(previousTag))
.copy(
label = label,
tagName = currentTag?.name,
tagDateTime = currentTag?.dateTime,
)
messageDigger.contribution(currentContributionCommits(previousTag))
.copy(
label = label,
tagName = currentTag?.name,
tagDateTime = currentTag?.dateTime,
)
}
}

private fun currentRelevantTags(
headCommitId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,22 @@ private fun Regex.cleaned(): String {
}
}

private fun Sequence<MatchResult>.messageDigResult() =
MessageDigResult(
coauthors = mapNotNull { it.groups["coAuthors"]?.value }.toList(),
semver = mapNotNull { getSemver(it) }.maxOrNull(),
storyId = firstNotNullOfOrNull { it.groups["storyId"] }?.value,
ease = firstNotNullOfOrNull { it.groups["ease"] }?.value?.toIntOrNull(),
)
private fun Sequence<MatchResult>.messageDigResult() = MessageDigResult(
coauthors = mapNotNull { it.groups["coAuthors"]?.value }.toList(),
semver = mapNotNull { getSemver(it) }.maxOrNull(),
storyId = firstNotNullOfOrNull { it.groups["storyId"] }?.value,
ease = firstNotNullOfOrNull { it.groups["ease"] }?.value?.toIntOrNull(),
)

private fun getSemver(it: MatchResult) =
when {
it.groupMatches("major") -> SemverType.Major
it.groupMatches("minor") -> SemverType.Minor
it.groupMatches("patch") -> SemverType.Patch
it.groupMatches("none") -> SemverType.None
else -> null
}
private fun getSemver(it: MatchResult) = when {
it.groupMatches("major") -> SemverType.Major
it.groupMatches("minor") -> SemverType.Minor
it.groupMatches("patch") -> SemverType.Patch
it.groupMatches("none") -> SemverType.None
else -> null
}

private fun MatchResult.groupMatches(groupName: String) =
runCatching { this@groupMatches.groups[groupName] }
.getOrNull() != null
private fun MatchResult.groupMatches(groupName: String) = runCatching { this@groupMatches.groups[groupName] }
.getOrNull() != null

fun List<SemverType>.highestPrioritySemver() = maxOrNull()
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,27 @@ object ContributionParser {
ignoreUnknownKeys = true
}

fun parseContributions(jsonString: String) =
json.decodeFromString<Array<ContributionJson>>(jsonString)
.map(ContributionJson::toModel)
fun parseContributions(jsonString: String) = json.decodeFromString<Array<ContributionJson>>(jsonString)
.map(ContributionJson::toModel)

fun parseContribution(jsonString: String) =
json.decodeFromString<ContributionJson?>(jsonString)
?.toModel()
fun parseContribution(jsonString: String) = json.decodeFromString<ContributionJson?>(jsonString)
?.toModel()
}

private fun Contribution.toJsonModel() =
ContributionJson(
lastCommit = lastCommit,
firstCommit = firstCommit,
authors = authors,
dateTime = dateTime,
firstCommitDateTime = firstCommitDateTime,
ease = ease,
storyId = storyId,
semver = semver,
label = label,
tagName = tagName,
tagDateTime = tagDateTime,
commitCount = commitCount,
)
private fun Contribution.toJsonModel() = ContributionJson(
lastCommit = lastCommit,
firstCommit = firstCommit,
authors = authors,
dateTime = dateTime,
firstCommitDateTime = firstCommitDateTime,
ease = ease,
storyId = storyId,
semver = semver,
label = label,
tagName = tagName,
tagDateTime = tagDateTime,
commitCount = commitCount,
)

private fun ContributionJson.toModel() = Contribution(
lastCommit = lastCommit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,22 @@ class ContributionParserTest {
assertEquals(contributions, loaded)
}

private fun stubContribution() =
Contribution(
lastCommit = "${uuid4()}",
firstCommit = "${uuid4()}",
authors = listOf("${uuid4()}"),
dateTime =
randomInstant(),
firstCommitDateTime =
randomInstant(),
ease = Random.nextInt(),
storyId = "${uuid4()}",
semver = "${uuid4()}",
label = "${uuid4()}",
tagDateTime = randomInstant(),
tagName = "${uuid4()}",
commitCount = Random.nextInt(10),
)
private fun stubContribution() = Contribution(
lastCommit = "${uuid4()}",
firstCommit = "${uuid4()}",
authors = listOf("${uuid4()}"),
dateTime =
randomInstant(),
firstCommitDateTime =
randomInstant(),
ease = Random.nextInt(),
storyId = "${uuid4()}",
semver = "${uuid4()}",
label = "${uuid4()}",
tagDateTime = randomInstant(),
tagName = "${uuid4()}",
commitCount = Random.nextInt(10),
)

private fun randomInstant() = Instant.fromEpochMilliseconds(
epochMilliseconds =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class GitAdapter(private val workingDirectory: String, private val env: Map<Stri
),
).trim()

private fun runProcess(args: List<String>, env: Map<String, String> = emptyMap()) =
runProcess(args, workingDirectory, env.plus(this.env))
private fun runProcess(args: List<String>, env: Map<String, String> = emptyMap()) = runProcess(args, workingDirectory, env.plus(this.env))

fun newAnnotatedTag(name: String, ref: String, userName: String?, userEmail: String?) {
runProcess(
Expand Down Expand Up @@ -44,8 +43,7 @@ class GitAdapter(private val workingDirectory: String, private val env: Map<Stri
)
}

private fun inlineConfig(property: String, value: String?) =
if (value != null) listOf("-c", "$property=$value") else emptyList()
private fun inlineConfig(property: String, value: String?) = if (value != null) listOf("-c", "$property=$value") else emptyList()

fun pushTags() {
runProcess(listOf("git", "push", "--tags"))
Expand Down Expand Up @@ -174,8 +172,7 @@ class GitAdapter(private val workingDirectory: String, private val env: Map<Stri
)
}

private fun List<String>.findByPrefix(prefix: String) =
find { it.startsWith(prefix) }?.substring(prefix.length)
private fun List<String>.findByPrefix(prefix: String) = find { it.startsWith(prefix) }?.substring(prefix.length)

fun describe(abbrev: Int): String? = runCatching {
runProcess(listOf("git", "describe", "--abbrev=$abbrev"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ private fun findAppropriateIncrement(
previousTag: String,
implicitPatch: Boolean,
minorRegex: VersionRegex,
): ChangeType? =
gitAdapter.logWithRange("HEAD", "^$previousTag")
.also { if (it.isEmpty()) return null }
.map { it.changeType(minorRegex) ?: if (implicitPatch) ChangeType.Patch else null }
.fold(null, ::highestPriority)
?: if (implicitPatch) ChangeType.Patch else ChangeType.None
): ChangeType? = gitAdapter.logWithRange("HEAD", "^$previousTag")
.also { if (it.isEmpty()) return null }
.map { it.changeType(minorRegex) ?: if (implicitPatch) ChangeType.Patch else null }
.fold(null, ::highestPriority)
?: if (implicitPatch) ChangeType.Patch else ChangeType.None

private fun highestPriority(
left: ChangeType?,
Expand Down Expand Up @@ -127,25 +126,23 @@ enum class SnapshotReason {
fun reasonIsValid(check: StatusCheck): Boolean = check.exists()
}

fun TaggerCore.tagReport() =
adapter.listTags()
.groupBy { tag ->
"${tag.dateTime.toLocalDateTime(TimeZone.currentSystemDefault()).year} Week ${tag.weekNumber()}"
}
.toList()
.sortedBy { (key) -> key }
.joinToString("\n") { (key, value) ->
"$key has ${value.size} tags [${value.joinToString { tag -> tag.name }}]"
}
fun TaggerCore.tagReport() = adapter.listTags()
.groupBy { tag ->
"${tag.dateTime.toLocalDateTime(TimeZone.currentSystemDefault()).year} Week ${tag.weekNumber()}"
}
.toList()
.sortedBy { (key) -> key }
.joinToString("\n") { (key, value) ->
"$key has ${value.size} tags [${value.joinToString { tag -> tag.name }}]"
}

private fun TagRef.weekNumber() =
"${dateTime.toLocalDateTime(TimeZone.currentSystemDefault()).dayOfYear / 7}".let {
if (it.length == 1) {
"0$it"
} else {
it
}
private fun TagRef.weekNumber() = "${dateTime.toLocalDateTime(TimeZone.currentSystemDefault()).dayOfYear / 7}".let {
if (it.length == 1) {
"0$it"
} else {
it
}
}

fun VersionRegex.changeType(message: String): ChangeType? = when {
unified?.containsMatchIn(message) == true -> findMatchType(message, unified)
Expand All @@ -170,6 +167,5 @@ private fun findMatchType(
}
}

private fun MatchGroupCollection?.groupExists(groupName: String): Boolean =
runCatching { this?.get(groupName) != null }
.getOrDefault(false)
private fun MatchGroupCollection?.groupExists(groupName: String): Boolean = runCatching { this?.get(groupName) != null }
.getOrDefault(false)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ object TagErrors {

const val BEING_SNAPSHOT = "being snapshot"
fun alreadyTagged(headTag: String?) = "already tagged $headTag"
fun skipMessageNotOnReleaseBranch(releaseBranch: String?, headBranch: String) =
"not on release branch $releaseBranch - branch was $headBranch"
fun skipMessageNotOnReleaseBranch(releaseBranch: String?, headBranch: String) = "not on release branch $releaseBranch - branch was $headBranch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ open class TaggerExtension(
).version
}

private fun versionRegex() =
VersionRegex(
none = noneRegex.get(),
patch = patchRegex.get(),
minor = minorRegex.get(),
major = majorRegex.get(),
unified = versionRegex.orNull?.also { it.validateVersionRegex() },
)
private fun versionRegex() = VersionRegex(
none = noneRegex.get(),
patch = patchRegex.get(),
minor = minorRegex.get(),
major = majorRegex.get(),
unified = versionRegex.orNull?.also { it.validateVersionRegex() },
)
}

private fun Regex.validateVersionRegex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ interface CalculateVersionTestSpec {
}

fun execute(): TestResult
fun runCalculateVersionSuccessfully(): String =
when (val result = execute()) {
is TestResult.Success -> result.message
is TestResult.Failure -> fail("Expected success but got ${result.reason}")
}
fun runCalculateVersionSuccessfully(): String = when (val result = execute()) {
is TestResult.Success -> result.message
is TestResult.Failure -> fail("Expected success but got ${result.reason}")
}

@Test
fun calculatingVersionWithNoTagsProducesZeroVersion() {
Expand Down

0 comments on commit 1f473cb

Please sign in to comment.