From 8da169d2a8cb14b6818897664afb11f32b8e5b98 Mon Sep 17 00:00:00 2001 From: Sven Merk Date: Fri, 22 Mar 2019 11:55:50 +0100 Subject: [PATCH] Enable golang --- resources/default_pipeline_environment.yml | 6 +- src/com/sap/piper/DescriptorUtils.groovy | 35 ++++++++++-- test/groovy/WhitesourceExecuteScanTest.groovy | 57 +++++++++++++++++++ .../com/sap/piper/DescriptorUtilsTest.groovy | 28 +++++++-- .../WhiteSourceConfigurationHelperTest.groovy | 2 +- test/resources/DescriptorUtils/go/VERSION | 1 + vars/whitesourceExecuteScan.groovy | 7 +++ 7 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 test/resources/DescriptorUtils/go/VERSION diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index cccd858d1..3652adfd4 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -305,9 +305,9 @@ steps: - 'buildDescriptor' - 'opensourceConfiguration' golang: - buildDescriptorFile: './setup.py' - dockerImage: 'golang:1.12.1-stretch' - dockerWorkspace: '/home/golang' + buildDescriptorFile: './glide.yaml' + dockerImage: 'instrumentisto/glide:0.13.1-go1.10' + dockerWorkspace: '/home/glide' stashContent: - 'buildDescriptor' - 'opensourceConfiguration' diff --git a/src/com/sap/piper/DescriptorUtils.groovy b/src/com/sap/piper/DescriptorUtils.groovy index c6976353a..979a72f41 100644 --- a/src/com/sap/piper/DescriptorUtils.groovy +++ b/src/com/sap/piper/DescriptorUtils.groovy @@ -72,7 +72,7 @@ def getSbtGAV(file = 'sbtDescriptor.json') { def getPipGAV(file = 'setup.py') { def result = [:] - def descriptor = sh(returnStdout: true, script: "cat ${file}") + def descriptor = readFile(file: file) result['group'] = '' result['packaging'] = '' @@ -81,16 +81,41 @@ def getPipGAV(file = 'setup.py') { if (result['version'] == '' || matches(method, result['version'])) { file = file.replace('setup.py', 'version.txt') - def versionString = sh(returnStdout: true, script: "cat ${file}") - if (versionString) { - result['version'] = versionString.trim() - } + result['version'] = getVersionFromFile(file) } echo "loaded ${result} from ${file}" return result } +def getGoGAV(file = './') { + def f = new File(file) + def path = f.getAbsoluteFile().getParentFile() + def result = [:] + + result['group'] = '' + result['packaging'] = '' + result['artifact'] = path.getName() + file = new File(path, 'version.txt').getAbsolutePath() + result['version'] = getVersionFromFile(file) + + if (!result['version']) { + file = new File(path, 'VERSION').getAbsolutePath() + result['version'] = getVersionFromFile(file) + } + + echo "loaded ${result} from ${file}" + return result +} + +private getVersionFromFile(file) { + def versionString = readFile(file: file) + if (versionString) { + return versionString.trim() + } + return '' +} + @NonCPS private def matches(regex, input) { def m = new Matcher(regex, input) diff --git a/test/groovy/WhitesourceExecuteScanTest.groovy b/test/groovy/WhitesourceExecuteScanTest.groovy index 174c1c4b1..48959ee67 100644 --- a/test/groovy/WhitesourceExecuteScanTest.groovy +++ b/test/groovy/WhitesourceExecuteScanTest.groovy @@ -452,6 +452,63 @@ class WhitesourceExecuteScanTest extends BasePiperTest { assertThat(writeFileRule.files['./wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('projectName=com.sap.sbt.test-scala')) } + @Test + void testGo() { + helper.registerAllowedMethod("readFile", [Map.class], { + map -> + def path = 'test/resources/DescriptorUtils/go/' + map.file.substring(map.file.lastIndexOf(File.separator) + 1, map.file.length()) + def descriptorFile = new File(path) + if(descriptorFile.exists()) + return descriptorFile.text + else + return null + }) + + helper.registerAllowedMethod("readProperties", [Map], { + def result = new Properties() + result.putAll([ + "apiKey": "b39d1328-52e2-42e3-98f0-932709daf3f0", + "productName": "SHC - Piper", + "checkPolicies": "true", + "projectName": "python-test", + "projectVersion": "1.0.0" + ]) + return result + }) + + stepRule.step.whitesourceExecuteScan([ + script : nullScript, + whitesourceRepositoryStub : whitesourceStub, + whitesourceOrgAdminRepositoryStub : whitesourceOrgAdminRepositoryStub, + descriptorUtilsStub : descriptorUtilsStub, + scanType : 'golang', + juStabUtils : utils, + productName : 'testProductName', + orgToken : 'testOrgToken', + reporting : false, + buildDescriptorFile : './myProject/glide.yaml' + ]) + + assertThat(loggingRule.log, containsString('Unstash content: buildDescriptor')) + assertThat(loggingRule.log, containsString('Unstash content: opensourceConfiguration')) + + assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerImage', 'instrumentisto/glide:0.13.1-go1.10')) + assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerWorkspace', '/home/glide')) + assertThat(dockerExecuteRule.dockerParams, hasEntry('stashContent', ['buildDescriptor', 'opensourceConfiguration', 'modified whitesource config d3aa80454919391024374ba46b4df082d15ab9a3'])) + + assertThat(shellRule.shell, Matchers.hasItems( + is('curl --location --output wss-unified-agent.jar https://github.com/whitesource/unified-agent-distribution/raw/master/standAlone/wss-unified-agent.jar'), + is('./bin/java -jar wss-unified-agent.jar -c \'./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3\' -apiKey \'testOrgToken\' -userKey \'token-0815\' -product \'testProductName\'') + )) + + assertThat(writeFileRule.files['./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('apiKey=testOrgToken')) + assertThat(writeFileRule.files['./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('productName=testProductName')) + assertThat(writeFileRule.files['./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('userKey=token-0815')) + assertThat(writeFileRule.files['./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('productVersion=1.2.3')) + assertThat(writeFileRule.files['./myProject/wss-unified-agent.config.d3aa80454919391024374ba46b4df082d15ab9a3'], containsString('projectName=myProject')) + } + + @Test void testAgentNoDownloads() { helper.registerAllowedMethod("readProperties", [Map], { diff --git a/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy b/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy index 1b376cb35..01a9b814a 100644 --- a/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy +++ b/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy @@ -131,9 +131,9 @@ class DescriptorUtilsTest extends BasePiperTest { @Test void testGetPipGAV() { - helper.registerAllowedMethod("sh", [Map.class], { + helper.registerAllowedMethod("readFile", [Map.class], { map -> - def descriptorFile = new File("test/resources/utilsTest/${map.script.substring(4, map.script.size())}") + def descriptorFile = new File("test/resources/utilsTest/${map.file}") return descriptorFile.text }) @@ -147,9 +147,9 @@ class DescriptorUtilsTest extends BasePiperTest { @Test void testGetPipGAVFromVersionTxt() { - helper.registerAllowedMethod("sh", [Map.class], { + helper.registerAllowedMethod("readFile", [Map.class], { map -> - def descriptorFile = new File("test/resources/DescriptorUtils/pip/${map.script.substring(4, map.script.size())}") + def descriptorFile = new File("test/resources/DescriptorUtils/pip/${map.file}") return descriptorFile.text }) @@ -208,4 +208,24 @@ class DescriptorUtilsTest extends BasePiperTest { assertEquals(gav.version, '1.2.4') assertEquals(gav.packaging, 'jar') } + + @Test + void testGetGoGAV() { + + helper.registerAllowedMethod("readFile", [Map.class], { + map -> + def path = 'test/resources/DescriptorUtils/go/' + map.file.substring(map.file.lastIndexOf(File.separator) + 1, map.file.length()) + def descriptorFile = new File(path) + if(descriptorFile.exists()) + return descriptorFile.text + else + return null + }) + + def gav = descriptorUtils.getGoGAV('./myProject/glide.yaml') + + assertEquals('', gav.group) + assertEquals('myProject', gav.artifact) + assertEquals('1.2.3', gav.version) + } } diff --git a/test/groovy/com/sap/piper/WhiteSourceConfigurationHelperTest.groovy b/test/groovy/com/sap/piper/WhiteSourceConfigurationHelperTest.groovy index 2ebd5e630..251998ccc 100644 --- a/test/groovy/com/sap/piper/WhiteSourceConfigurationHelperTest.groovy +++ b/test/groovy/com/sap/piper/WhiteSourceConfigurationHelperTest.groovy @@ -90,7 +90,7 @@ class WhiteSourceConfigurationHelperTest extends BasePiperTest { } @Test - void testExtendConfigurationFileUnifiedAgentVerbose() { + void testExtendConfigurationFileUnifiedAgentGolangVerbose() { def config = [scanType: 'golang', whitesource: [configFilePath: './config', serviceUrl: "http://some.host.whitesource.com/api/", orgToken: 'abcd', productName: 'SHC - name2', productToken: '1234', userKey: '0000'], stashContent: ['some', 'stashes'], verbose: true] WhitesourceConfigurationHelper.extendUAConfigurationFile(nullScript, utils, config, "./") assertThat(jwfr.files['./config.847f9aec2f93de9000d5fa4e6eaace2283ae6377'], containsString("apiKey=abcd")) diff --git a/test/resources/DescriptorUtils/go/VERSION b/test/resources/DescriptorUtils/go/VERSION new file mode 100644 index 000000000..0495c4a88 --- /dev/null +++ b/test/resources/DescriptorUtils/go/VERSION @@ -0,0 +1 @@ +1.2.3 diff --git a/vars/whitesourceExecuteScan.groovy b/vars/whitesourceExecuteScan.groovy index 63a58ef5a..16a4eca78 100644 --- a/vars/whitesourceExecuteScan.groovy +++ b/vars/whitesourceExecuteScan.groovy @@ -353,6 +353,13 @@ private def triggerWhitesourceScanWithUserKey(script, config, utils, descriptorU config.whitesource.projectName = gav.artifact config.whitesource.productVersion = gav.version break + case 'golang': + gav = descriptorUtils.getGoGAV(config.buildDescriptorFile) + config.whitesource.projectName = gav.artifact + config.whitesource.productVersion = gav.version + break + case 'dlang': + break case 'maven': gav = descriptorUtils.getMavenGAV(config.buildDescriptorFile) config.whitesource.projectName = gav.group + "." + gav.artifact