Skip to content

Commit

Permalink
Added support for recursive check of referred-to files and fixed bug …
Browse files Browse the repository at this point in the history
…in backward compatibility check
  • Loading branch information
joelrosario committed May 29, 2024
1 parent ef5c73c commit 7c94aab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ open class BackwardCompatibilityCheckCommand(

override fun call() {
val filesChangedInCurrentBranch: Set<String> = getOpenAPISpecFilesChangedInCurrentBranch()
if (filesChangedInCurrentBranch.isEmpty()) exitWithMessage("$newLine OpenAPI spec files were changed, skipping the check.$newLine")
if (filesChangedInCurrentBranch.isEmpty()) exitWithMessage("${newLine}No OpenAPI spec files were changed, skipping the check.$newLine")

val filesReferringToChangedSchemaFiles = filesReferringToChangedSchemaFiles(filesChangedInCurrentBranch)

val filesToCheck: Set<String> = filesChangedInCurrentBranch + filesReferringToChangedSchemaFiles


logFilesToBeCheckedForBackwardCompatibility(
filesChangedInCurrentBranch,
filesReferringToChangedSchemaFiles
Expand All @@ -52,43 +53,54 @@ open class BackwardCompatibilityCheckCommand(
}

private fun runBackwardCompatibilityCheckFor(files: Set<String>): String {
val currentBranch = gitCommand.currentBranch()
val currentTreeish = if (currentBranch == HEAD) gitCommand.detachedHEAD() else currentBranch
val branchWithChanges = gitCommand.currentBranch()
val treeishWithChanges = if (branchWithChanges == HEAD) gitCommand.detachedHEAD() else branchWithChanges

try {
val failures = files.mapIndexed { index, specFilePath ->
println("${index.inc()}. Running the check for $specFilePath:")
try {
println("${index.inc()}. Running the check for $specFilePath:")

// newer => the file with changes on the branch
val newer = OpenApiSpecification.fromFile(specFilePath).toFeature()
// newer => the file with changes on the branch
val newer = OpenApiSpecification.fromFile(specFilePath).toFeature()

val olderFile = gitCommand.getFileInTheDefaultBranch(specFilePath, currentTreeish)
if (olderFile == null) {
println("$specFilePath is a new file.$newLine")
return@mapIndexed SUCCESS
}
// older => the same file on the default (e.g. main) branch
val older = OpenApiSpecification.fromFile(olderFile.path).toFeature()

val backwardCompatibilityResult = testBackwardCompatibility(older, newer)

if (backwardCompatibilityResult.success()) {
println("$newLine The file $specFilePath is backward compatible.$newLine".prependIndent(MARGIN_SPACE))
SUCCESS
} else {
println("$newLine ${backwardCompatibilityResult.report().prependIndent(MARGIN_SPACE)}")
println(
"$newLine *** The file $specFilePath is NOT backward compatible. ***$newLine".prependIndent(
MARGIN_SPACE
val olderFile = gitCommand.getFileInTheDefaultBranch(specFilePath, treeishWithChanges)
if (olderFile == null) {
println("$specFilePath is a new file.$newLine")
return@mapIndexed SUCCESS
}

gitCommand.checkout(gitCommand.defaultBranch())

// older => the same file on the default (e.g. main) branch
val older = OpenApiSpecification.fromFile(olderFile.path).toFeature()

val backwardCompatibilityResult = testBackwardCompatibility(older, newer)

if (backwardCompatibilityResult.success()) {
println(
"$newLine The file $specFilePath is backward compatible.$newLine".prependIndent(
MARGIN_SPACE
)
)
SUCCESS
} else {
println("$newLine ${backwardCompatibilityResult.report().prependIndent(MARGIN_SPACE)}")
println(
"$newLine *** The file $specFilePath is NOT backward compatible. ***$newLine".prependIndent(
MARGIN_SPACE
)
)
)
FAILED
FAILED
}
} finally {
gitCommand.checkout(treeishWithChanges)
}
}.filter { it == FAILED }

return if (failures.isNotEmpty()) FAILED else SUCCESS
} finally {
gitCommand.checkout(currentTreeish)
gitCommand.checkout(treeishWithChanges)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CentralContractRepoReportCommandTestE2E {

@Test
fun `test generates report json file`() {
centralContractRepoReportCommand.baseDir = ""
centralContractRepoReportCommand.call()
val reportJson: CentralContractRepoReportJson = Json.decodeFromString(reportFile.readText())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File

class CentralContractRepoReport {
fun generate(currentWorkingDir: String = ""): CentralContractRepoReportJson {
val searchPath = currentWorkingDir.takeIf { it.isNotEmpty() }.let { File(it).canonicalPath } ?: File("").canonicalPath
val searchPath = File(currentWorkingDir).canonicalPath
logger.log("Searching for specification files at: $searchPath")
val specifications = findSpecifications(searchPath)
return CentralContractRepoReportJson(getSpecificationRows(specifications.sorted(), searchPath))
Expand Down

0 comments on commit 7c94aab

Please sign in to comment.