1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2024-11-28 09:33:03 +02:00

Рефакторинг VersionParser

This commit is contained in:
Nikita Gryzlov 2020-04-15 13:43:01 +03:00
parent 5762353fe1
commit 6aaad1d6e2
No known key found for this signature in database
GPG Key ID: C1EAE411FEF0BF2F
9 changed files with 144 additions and 26 deletions

View File

@ -10,4 +10,5 @@ interface IStepExecutor {
String libraryResource(String path)
String readFile(String file, String encoding)
}

View File

@ -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
}
}

View File

@ -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>(.*)<\/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] : ""
}
}

View File

@ -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;
}

View File

@ -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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<VERSION>3487</VERSION>", StandardCharsets.UTF_8);
// when
String storage = VersionParser.storage(file.toString());
// then
assertThat(storage).isEqualTo("3487");
}
@Test
void testEmptyConfiguration() throws IOException {
// given
FileUtils.writeStringToFile(file,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<MetaDataObject xmlns=\"http://v8.1c.ru/8.3/MDClasses\" xmlns:app=\"http://v8.1c.ru/8.2/managed-application/core\" xmlns:cfg=\"http://v8.1c.ru/8.1/data/enterprise/current-config\" xmlns:cmi=\"http://v8.1c.ru/8.2/managed-application/cmi\" xmlns:ent=\"http://v8.1c.ru/8.1/data/enterprise\" xmlns:lf=\"http://v8.1c.ru/8.2/managed-application/logform\" xmlns:style=\"http://v8.1c.ru/8.1/data/ui/style\" xmlns:sys=\"http://v8.1c.ru/8.1/data/ui/fonts/system\" xmlns:v8=\"http://v8.1c.ru/8.1/data/core\" xmlns:v8ui=\"http://v8.1c.ru/8.1/data/ui\" xmlns:web=\"http://v8.1c.ru/8.1/data/ui/colors/web\" xmlns:win=\"http://v8.1c.ru/8.1/data/ui/colors/windows\" xmlns:xen=\"http://v8.1c.ru/8.3/xcf/enums\" xmlns:xpr=\"http://v8.1c.ru/8.3/xcf/predef\" xmlns:xr=\"http://v8.1c.ru/8.3/xcf/readable\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.8\">\n" +
"\t<Configuration uuid=\"1e4190e9-76c2-456e-a607-4d817110ffd9\">\n" +
"\t\t<Properties>\n" +
"\t\t\t<Vendor>Some vendor</Vendor>\n" +
"\t\t\t<Version/>\n" +
"\t\t</Properties>\n" +
"\t</Configuration>" +
"</MetaDataObject>"
, StandardCharsets.UTF_8);
// when
String storage = VersionParser.configuration(file.toString());
// then
assertThat(storage).isEmpty();
}
@Test
void testConfiguration() throws IOException {
// given
FileUtils.writeStringToFile(file,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<MetaDataObject xmlns=\"http://v8.1c.ru/8.3/MDClasses\" xmlns:app=\"http://v8.1c.ru/8.2/managed-application/core\" xmlns:cfg=\"http://v8.1c.ru/8.1/data/enterprise/current-config\" xmlns:cmi=\"http://v8.1c.ru/8.2/managed-application/cmi\" xmlns:ent=\"http://v8.1c.ru/8.1/data/enterprise\" xmlns:lf=\"http://v8.1c.ru/8.2/managed-application/logform\" xmlns:style=\"http://v8.1c.ru/8.1/data/ui/style\" xmlns:sys=\"http://v8.1c.ru/8.1/data/ui/fonts/system\" xmlns:v8=\"http://v8.1c.ru/8.1/data/core\" xmlns:v8ui=\"http://v8.1c.ru/8.1/data/ui\" xmlns:web=\"http://v8.1c.ru/8.1/data/ui/colors/web\" xmlns:win=\"http://v8.1c.ru/8.1/data/ui/colors/windows\" xmlns:xen=\"http://v8.1c.ru/8.3/xcf/enums\" xmlns:xpr=\"http://v8.1c.ru/8.3/xcf/predef\" xmlns:xr=\"http://v8.1c.ru/8.3/xcf/readable\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.8\">\n" +
"\t<Configuration uuid=\"1e4190e9-76c2-456e-a607-4d817110ffd9\">\n" +
"\t\t<Properties>\n" +
"\t\t\t<Vendor>Some vendor</Vendor>\n" +
"\t\t\t<Version>1.0.0.1</Version>\n" +
"\t\t</Properties>\n" +
"\t</Configuration>" +
"</MetaDataObject>"
, StandardCharsets.UTF_8);
// when
String storage = VersionParser.configuration(file.toString());
// then
assertThat(storage).isEqualTo("1.0.0.1");
}
}

View File

@ -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\""
}
}

View File

@ -56,12 +56,8 @@ void call() {
dir("build/out") { echo '' }
script {
def storageVersion = versionParser.storage()
// Создание базы загрузкой конфигурации из хранилища
initFromStorage(jobConfiguration, storageVersion)
}
initFromStorage(jobConfiguration)
zipInfobase()

View File

@ -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"
}

View File

@ -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>(.*)<\/Version>/)
}
def storage(versionFile = 'src/cf/VERSION') {
storageVersionText = readFile encoding: 'UTF-8', file: versionFile
return version(versionFile, /<VERSION>(.*)<\/VERSION>/)
}
@NonCPS
private static String version(String text, String regexp) {
def matcher = text =~ regexp
return matcher ? matcher.group(1) : ""
}