Skip to content

Commit

Permalink
[TH2-5025] update CR converted to support new urlPath structure (conv…
Browse files Browse the repository at this point in the history
…ersion from v2 to v2.2) (#32)

Co-authored-by: Vakhtang Donadze <vakhtang.donadze@exactprosystems.com>
Co-authored-by: andrey.shulika <andrey.shulika@exactprosystems.com>
Co-authored-by: Oleg <oleg.smirnov@exactprosystems.com>
  • Loading branch information
4 people authored Sep 12, 2023
1 parent b87b2a4 commit 5e365cd
Show file tree
Hide file tree
Showing 29 changed files with 1,053 additions and 190 deletions.
31 changes: 28 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import com.github.jk1.license.filter.LicenseBundleNormalizer
import com.github.jk1.license.render.JsonReportRenderer

plugins {
id "org.jetbrains.kotlin.jvm" version "${kotlin_version}"
id "org.jetbrains.kotlin.kapt" version "${kotlin_version}"
Expand All @@ -11,14 +14,16 @@ plugins {
id 'signing'
id 'distribution'
id "org.owasp.dependencycheck" version "${owaspVersion}"
id 'com.github.jk1.dependency-license-report' version '2.5'
id "de.undercouch.download" version "5.4.0"
}

ext {
kotlin_logging_version = '2.0.10'
fabric8_version = '6.6.2'
jackson_databind_version = '2.15.2'
infra_repo_version = "1.2.4"
resource_model_version = '1.3.3'
resource_model_version = '1.4.0'
slf4j_api_version = '2.0.5'
slf4j2_impl_version = '2.19.0'
junit_jupiter_version = '5.9.2'
Expand Down Expand Up @@ -80,7 +85,6 @@ dependencies {
implementation "org.apache.commons:commons-text:$commons_text_version"
implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: jackson_databind_version

testImplementation "org.junit.jupiter:junit-jupiter:$junit_jupiter_version"
testImplementation "org.junit.jupiter:junit-jupiter:$junit_jupiter_version"
testImplementation group: 'org.jetbrains.kotlin', name: 'kotlin-test-junit5', version: kotlin_version

Expand Down Expand Up @@ -253,4 +257,25 @@ configurations {
compileClasspath {
resolutionStrategy.activateDependencyLocking()
}
}
}

licenseReport {
def licenseNormalizerBundlePath = "$buildDir/license-normalizer-bundle.json"

if (!file(licenseNormalizerBundlePath).exists()) {
download.run {
src 'https://raw.githubusercontent.com/th2-net/.github/main/license-compliance/gradle-license-report/license-normalizer-bundle.json'
dest "$buildDir/license-normalizer-bundle.json"
overwrite false
}
}

filters = [
new LicenseBundleNormalizer(licenseNormalizerBundlePath, false)
]
renderers = [
new JsonReportRenderer('licenses.json', false),
]
excludeOwnGroup = false
allowedLicensesFile = new URL("https://raw.githubusercontent.com/th2-net/.github/main/license-compliance/gradle-license-report/allowed-licenses.json")
}
16 changes: 13 additions & 3 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ complexity:
- 'run'
- 'use'
- 'with'
CyclomaticComplexMethod:
active: true
excludes:
- '**/fun/ExtendedSettingsExtensions**'
LabeledExpression:
active: false
ignoredLabels: []
Expand All @@ -114,7 +118,9 @@ complexity:
threshold: 600
LongMethod:
active: true
threshold: 65
threshold: 75
excludes:
- '**/fun/ExtendedSettingsExtensions**'
LongParameterList:
active: true
functionThreshold: 6
Expand Down Expand Up @@ -547,6 +553,8 @@ style:
- '0'
- '1'
- '2'
- '8080'
- '80'
ignoreHashCodeFunction: true
ignorePropertyDeclaration: false
ignoreLocalVariableDeclaration: false
Expand Down Expand Up @@ -599,11 +607,13 @@ style:
active: false
ReturnCount:
active: true
max: 2
max: 3
excludedFunctions: 'equals'
excludeLabeled: false
excludeReturnFromLambda: true
excludeGuardClauses: false
excludes:
- '**/fun/ExtendedSettingsExtensions**'
SafeCast:
active: true
SerialVersionUIDInSerializableClass:
Expand All @@ -612,7 +622,7 @@ style:
active: true
ThrowsCount:
active: true
max: 2
max: 3
excludeGuardClauses: false
TrailingWhitespace:
active: false
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

release_version=1.3.6
release_version=1.4.0
description = 'cr schema converter'

kotlin.code.style=official
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
package com.exactpro.th2.converter

import com.exactpro.th2.converter.conversion.LocalFilesConverter
import com.exactpro.th2.converter.util.SchemaVersion
import io.micronaut.runtime.Micronaut
import mu.KotlinLogging

open class ConverterApplication

private val logger = KotlinLogging.logger {}

private const val LOCAL_USAGE: String =
"Usage: local <branch or directory with schemas> <current version> <target version>"

private const val ARGUMENTS_SIZE = 4
private const val SCHEMA_INDEX = 1
private const val CURRENT_VERSION_INDEX = 2
private const val TARGET_VERSION_INDEX = 3

fun main(args: Array<String>) {
when (val mode = if (args.isNotEmpty()) args[0] else "server") {
"server" -> {
Expand All @@ -40,10 +49,17 @@ fun main(args: Array<String>) {
}
}
"local" -> {
LocalFilesConverter(args[1], args[2]).convert()
require(args.size == ARGUMENTS_SIZE) { LOCAL_USAGE }
val currentVersion: SchemaVersion = getEnumValue(args[CURRENT_VERSION_INDEX])
val targetVersion: SchemaVersion = getEnumValue(args[TARGET_VERSION_INDEX])
LocalFilesConverter(args[SCHEMA_INDEX], currentVersion, targetVersion).convert()
}
else -> {
logger.error("Mode: {} is not supported", mode)
}
}
}

private fun getEnumValue(name: String): SchemaVersion =
SchemaVersion.values().find { it.name.equals(name, ignoreCase = true) }
?: error("unknown schema version $name")
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import com.exactpro.th2.converter.controllers.errors.BadRequestException
import com.exactpro.th2.converter.controllers.errors.ErrorCode
import com.exactpro.th2.converter.conversion.Converter.convertFromGit
import com.exactpro.th2.converter.conversion.Converter.convertFromRequest
import com.exactpro.th2.converter.util.ProjectConstants
import com.exactpro.th2.converter.util.ProjectConstants.PROPAGATION_DENY
import com.exactpro.th2.converter.util.ProjectConstants.PROPAGATION_RULE
import com.exactpro.th2.converter.util.ProjectConstants.SOURCE_BRANCH
import com.exactpro.th2.converter.util.RepositoryUtils.PROPAGATION_DENY
import com.exactpro.th2.converter.util.RepositoryUtils.PROPAGATION_RULE
import com.exactpro.th2.converter.util.RepositoryUtils.SOURCE_BRANCH
import com.exactpro.th2.converter.util.RepositoryUtils.schemaExists
import com.exactpro.th2.converter.util.RepositoryUtils.updateRepository
import com.exactpro.th2.converter.util.RepositoryUtils.updateSchemaK8sPropagation
import com.exactpro.th2.converter.util.SchemaVersion
import com.exactpro.th2.infrarepo.git.GitterContext
import com.exactpro.th2.infrarepo.repo.Repository
import com.exactpro.th2.infrarepo.repo.RepositoryResource
Expand All @@ -43,24 +43,23 @@ import org.eclipse.jgit.errors.EntryExistsException
@Produces(MediaType.APPLICATION_JSON)
class ConverterController {

@Post("convert/{schemaName}/{targetVersion}")
@Post("convert/{schemaName}/{currentVersion}/{targetVersion}")
fun convertInSameBranch(
schemaName: String,
targetVersion: String
currentVersion: SchemaVersion,
targetVersion: SchemaVersion
): ConversionSummary {
checkRequestedVersion(targetVersion)
val gitterContext = GitterContext.getContext(ApplicationConfig.git)
checkSourceSchema(schemaName, gitterContext)
val gitter = gitterContext.getGitter(schemaName)
val conversionResult = convertFromGit(targetVersion, gitter)
val conversionResult = convertFromGit(currentVersion, targetVersion, gitter)
val currentResponse = conversionResult.summary
if (currentResponse.hasErrors()) {
return currentResponse
}

try {
gitter.lock()
Repository.removeLinkResources(gitter)
updateRepository(conversionResult.convertedResources, gitter, Repository::update)
conversionResult.summary.commitRef = gitter.commitAndPush("Schema conversion")
} finally {
Expand All @@ -69,17 +68,18 @@ class ConverterController {
return conversionResult.summary
}

@Post("convert/{sourceSchemaName}/{newSchemaName}/{targetVersion}")
@Post("convert/{sourceSchemaName}/{newSchemaName}/{currentVersion}/{targetVersion}")
fun convertInNewBranch(
sourceSchemaName: String,
newSchemaName: String,
targetVersion: String
currentVersion: SchemaVersion,
targetVersion: SchemaVersion
): ConversionSummary {
checkRequestedVersion(targetVersion)
val gitterContext: GitterContext = GitterContext.getContext(ApplicationConfig.git)
checkSourceSchema(sourceSchemaName, gitterContext)
val sourceBranchGitter = gitterContext.getGitter(sourceSchemaName)
val conversionResult = convertFromGit(targetVersion, sourceBranchGitter)
val newBranchGitter = gitterContext.getGitter(newSchemaName)
val conversionResult = convertFromGit(currentVersion, targetVersion, sourceBranchGitter, newBranchGitter)
val currentResponse = conversionResult.summary
if (currentResponse.hasErrors()) {
return currentResponse
Expand All @@ -94,12 +94,10 @@ class ConverterController {
sourceBranchGitter.unlock()
}

val newBranchGitter = gitterContext.getGitter(newSchemaName)
try {
newBranchGitter.lock()
newBranchGitter.createBranch(sourceSchemaName)
updateSchemaK8sPropagation(PROPAGATION_RULE, newBranchGitter)
Repository.removeLinkResources(newBranchGitter)
updateRepository(conversionResult.convertedResources, newBranchGitter, Repository::update)
conversionResult.summary.commitRef = newBranchGitter.commitAndPush("Schema conversion")
} catch (e: EntryExistsException) {
Expand All @@ -113,29 +111,20 @@ class ConverterController {
return conversionResult.summary
}

@Get("convert/{targetVersion}")
@Get("convert/{currentVersion}/{targetVersion}")
fun convertRequestedResources(
targetVersion: String,
currentVersion: SchemaVersion,
targetVersion: SchemaVersion,
@Body resources: Set<RepositoryResource>
): ConversionResult {
checkRequestedVersion(targetVersion)
return convertFromRequest(targetVersion, resources)
return convertFromRequest(currentVersion, targetVersion, resources)
}

@Get("test")
fun testApi(): String {
return "Conversion API is working !"
}

private fun checkRequestedVersion(version: String) {
if (version !in ProjectConstants.ACCEPTED_VERSIONS) {
throw BadRequestException(
ErrorCode.VERSION_NOT_ALLOWED,
"Conversion to specified version: '$version' is not supported"
)
}
}

private fun checkSourceSchema(schemaName: String, gitterContext: GitterContext) {
if (!schemaExists(schemaName, gitterContext)) {
throw BadRequestException(
Expand Down
Loading

0 comments on commit 5e365cd

Please sign in to comment.