diff --git a/src/ru/pulsar/jenkins/library/IStepExecutor.groovy b/src/ru/pulsar/jenkins/library/IStepExecutor.groovy index 9e3c446..3ffda12 100644 --- a/src/ru/pulsar/jenkins/library/IStepExecutor.groovy +++ b/src/ru/pulsar/jenkins/library/IStepExecutor.groovy @@ -10,4 +10,5 @@ interface IStepExecutor { String libraryResource(String path) + String readFile(String file, String encoding) } \ No newline at end of file diff --git a/src/ru/pulsar/jenkins/library/StepExecutor.groovy b/src/ru/pulsar/jenkins/library/StepExecutor.groovy index 3678798..738a7ac 100644 --- a/src/ru/pulsar/jenkins/library/StepExecutor.groovy +++ b/src/ru/pulsar/jenkins/library/StepExecutor.groovy @@ -27,4 +27,9 @@ class StepExecutor implements IStepExecutor { String libraryResource(String path) { steps.libraryResource path } + + @Override + String readFile(String file, String encoding) { + steps.readFile encoding: encoding, file: file + } } diff --git a/src/ru/pulsar/jenkins/library/utils/VersionParser.groovy b/src/ru/pulsar/jenkins/library/utils/VersionParser.groovy new file mode 100644 index 0000000..092e97f --- /dev/null +++ b/src/ru/pulsar/jenkins/library/utils/VersionParser.groovy @@ -0,0 +1,33 @@ +package ru.pulsar.jenkins.library.utils + +import com.cloudbees.groovy.cps.NonCPS +import ru.pulsar.jenkins.library.IStepExecutor +import ru.pulsar.jenkins.library.ioc.ContextRegistry + +import java.util.regex.Pattern + +class VersionParser implements Serializable { + final static VERSION_REGEXP = ~/(?i)(.*)<\/version>/ + + static String configuration(rootFile = 'src/cf/Configuration.xml') { + + IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() + + def configurationText = steps.readFile(rootFile, 'UTF-8'); + return version(configurationText, VERSION_REGEXP) + } + + static String storage(versionFile = 'src/cf/VERSION') { + IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() + + def storageVersionText = steps.readFile(versionFile, 'UTF-8') + return version(storageVersionText, VERSION_REGEXP) + } + + @NonCPS + private static String version(String text, Pattern regexp) { + def matcher = text =~ regexp + return matcher != null && matcher.getCount() == 1 ? matcher[0][1] : "" + } + +} diff --git a/test/unit/groovy/ru/pulsar/jenkins/library/utils/TestUtils.java b/test/unit/groovy/ru/pulsar/jenkins/library/utils/TestUtils.java index f3f45c3..2531ae6 100644 --- a/test/unit/groovy/ru/pulsar/jenkins/library/utils/TestUtils.java +++ b/test/unit/groovy/ru/pulsar/jenkins/library/utils/TestUtils.java @@ -28,6 +28,12 @@ public class TestUtils { ); }); + when(steps.readFile(anyString(), anyString())).thenAnswer(invocation -> { + String file = invocation.getArgument(0); + String encoding = invocation.getArgument(1); + return FileUtils.readFileToString(new File(file), encoding); + }); + return steps; } diff --git a/test/unit/groovy/ru/pulsar/jenkins/library/utils/VersionParserTest.java b/test/unit/groovy/ru/pulsar/jenkins/library/utils/VersionParserTest.java new file mode 100644 index 0000000..3ef626c --- /dev/null +++ b/test/unit/groovy/ru/pulsar/jenkins/library/utils/VersionParserTest.java @@ -0,0 +1,88 @@ +package ru.pulsar.jenkins.library.utils; + +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import ru.pulsar.jenkins.library.IStepExecutor; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.assertj.core.api.Assertions.assertThat; + +class VersionParserTest { + + private final IStepExecutor steps = TestUtils.getMockedStepExecutor(); + private File file; + + @BeforeEach + void setUp() throws IOException { + TestUtils.setupMockedContext(steps); + file = File.createTempFile("version", ".xml"); + } + + @AfterEach + void tearDown() { + FileUtils.deleteQuietly(file); + } + + @Test + void testStorage() throws IOException { + // given + FileUtils.writeStringToFile(file, "\n" + + "3487", StandardCharsets.UTF_8); + + // when + String storage = VersionParser.storage(file.toString()); + + // then + assertThat(storage).isEqualTo("3487"); + } + + @Test + void testEmptyConfiguration() throws IOException { + // given + FileUtils.writeStringToFile(file, + "\n" + + "\n" + + "\t\n" + + "\t\t\n" + + "\t\t\tSome vendor\n" + + "\t\t\t\n" + + "\t\t\n" + + "\t" + + "" + , StandardCharsets.UTF_8); + + // when + String storage = VersionParser.configuration(file.toString()); + + // then + assertThat(storage).isEmpty(); + } + + @Test + void testConfiguration() throws IOException { + // given + FileUtils.writeStringToFile(file, + "\n" + + "\n" + + "\t\n" + + "\t\t\n" + + "\t\t\tSome vendor\n" + + "\t\t\t1.0.0.1\n" + + "\t\t\n" + + "\t" + + "" + , StandardCharsets.UTF_8); + + // when + String storage = VersionParser.configuration(file.toString()); + + // then + assertThat(storage).isEqualTo("1.0.0.1"); + } + +} \ No newline at end of file diff --git a/vars/initFromStorage.groovy b/vars/initFromStorage.groovy index 34474f1..3937c0c 100644 --- a/vars/initFromStorage.groovy +++ b/vars/initFromStorage.groovy @@ -1,6 +1,11 @@ import ru.pulsar.jenkins.library.configuration.JobConfiguration +import ru.pulsar.jenkins.library.utils.VersionParser + +def call(JobConfiguration jobConfiguration) { + + def storageVersion = VersionParser.storage() + def storageVersionParameter = storageVersion == "" ? "" : "--storage-ver $storageVersion" -def call(JobConfiguration jobConfiguration, String storageVersion) { withCredentials([ usernamePassword( credentialsId: jobConfiguration.secrets.storage, @@ -12,6 +17,6 @@ def call(JobConfiguration jobConfiguration, String storageVersion) { variable: 'STORAGE_PATH' ) ]) { - cmd "oscript_modules/bin/vrunner init-dev --storage --storage-name $STORAGE_PATH --storage-user $STORAGE_USR --storage-pwd $STORAGE_PSW --storage-ver $storageVersion --ibconnection \"/F./build/ib\"" + cmd "oscript_modules/bin/vrunner init-dev --storage --storage-name $STORAGE_PATH --storage-user $STORAGE_USR --storage-pwd $STORAGE_PSW $storageVersionParameter --ibconnection \"/F./build/ib\"" } } \ No newline at end of file diff --git a/vars/pipeline1C.groovy b/vars/pipeline1C.groovy index 25aba12..2f60b42 100644 --- a/vars/pipeline1C.groovy +++ b/vars/pipeline1C.groovy @@ -56,12 +56,8 @@ void call() { dir("build/out") { echo '' } - script { - def storageVersion = versionParser.storage() - - // Создание базы загрузкой конфигурации из хранилища - initFromStorage(jobConfiguration, storageVersion) - } + // Создание базы загрузкой конфигурации из хранилища + initFromStorage(jobConfiguration) zipInfobase() diff --git a/vars/sonarScanner.groovy b/vars/sonarScanner.groovy index 13c3585..9495b71 100644 --- a/vars/sonarScanner.groovy +++ b/vars/sonarScanner.groovy @@ -1,6 +1,7 @@ import com.cloudbees.groovy.cps.NonCPS import groovy.transform.Field import ru.pulsar.jenkins.library.configuration.JobConfiguration +import ru.pulsar.jenkins.library.utils.VersionParser @Field def sonarCommand @@ -11,7 +12,7 @@ def call(String rootFile = 'src/cf/Configuration.xml') { String scannerHome = tool config.sonarScannerToolName sonarCommand = "$scannerHome/bin/sonar-scanner -Dsonar.branch.name=$env.BRANCH_NAME" - String configurationVersion = versionParser.configuration(rootFile) + String configurationVersion = VersionParser.configuration(rootFile) if (configurationVersion) { sonarCommand += " -Dsonar.projectVersion=$configurationVersion" } diff --git a/vars/versionParser.groovy b/vars/versionParser.groovy deleted file mode 100644 index 92f5c65..0000000 --- a/vars/versionParser.groovy +++ /dev/null @@ -1,17 +0,0 @@ -import com.cloudbees.groovy.cps.NonCPS - -def configuration(rootFile = 'src/cf/Configuration.xml') { - def configurationText = readFile encoding: 'UTF-8', file: rootFile - return version(configurationText, /(.*)<\/Version>/) -} - -def storage(versionFile = 'src/cf/VERSION') { - storageVersionText = readFile encoding: 'UTF-8', file: versionFile - return version(versionFile, /(.*)<\/VERSION>/) -} - -@NonCPS -private static String version(String text, String regexp) { - def matcher = text =~ regexp - return matcher ? matcher.group(1) : "" -} \ No newline at end of file