You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
Move setting git info in cpe from stage init to setupCPE step (#2083)
* Move setting git info in cpe Everything cpe related should be done in setupCommonPipelineEnvironment. Thus a new parameter was introduced to accept the scmInfo object returned by a checkout. * Improve documentation
This commit is contained in:
@@ -15,9 +15,11 @@ import util.JenkinsStepRule
|
||||
import util.JenkinsWriteFileRule
|
||||
import util.Rules
|
||||
|
||||
import static org.hamcrest.Matchers.is
|
||||
import static org.junit.Assert.assertEquals
|
||||
import static org.junit.Assert.assertNotNull
|
||||
import static org.junit.Assert.assertNull
|
||||
import static org.junit.Assert.assertThat
|
||||
|
||||
class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
|
||||
|
||||
@@ -255,5 +257,52 @@ class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
|
||||
assertNull(nullScript.commonPipelineEnvironment.buildTool)
|
||||
}
|
||||
|
||||
@Test
|
||||
void "Set scmInfo parameter sets commit id"() {
|
||||
helper.registerAllowedMethod("fileExists", [String], { String path ->
|
||||
return path.endsWith('.pipeline/config.yml')
|
||||
})
|
||||
|
||||
def dummyScmInfo = [GIT_COMMIT: 'dummy_git_commit_id', GIT_URL: 'https://github.com/testOrg/testRepo.git']
|
||||
|
||||
stepRule.step.setupCommonPipelineEnvironment(script: nullScript, scmInfo: dummyScmInfo)
|
||||
assertThat(nullScript.commonPipelineEnvironment.gitCommitId, is('dummy_git_commit_id'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void "No scmInfo passed as parameter yields empty git info"() {
|
||||
helper.registerAllowedMethod("fileExists", [String], { String path ->
|
||||
return path.endsWith('.pipeline/config.yml')
|
||||
})
|
||||
|
||||
stepRule.step.setupCommonPipelineEnvironment(script: nullScript)
|
||||
assertNull(nullScript.commonPipelineEnvironment.gitCommitId)
|
||||
assertNull(nullScript.commonPipelineEnvironment.getGitSshUrl())
|
||||
assertNull(nullScript.commonPipelineEnvironment.getGitHttpsUrl())
|
||||
assertNull(nullScript.commonPipelineEnvironment.getGithubOrg())
|
||||
assertNull(nullScript.commonPipelineEnvironment.getGithubRepo())
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetScmInfoOnCommonPipelineEnvironment() {
|
||||
//currently supported formats
|
||||
def scmInfoTestList = [
|
||||
[GIT_URL: 'https://github.com/testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'https://github.com:7777/testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com:7777/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'git@github.com:testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com/testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com:7777/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com:7777/testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/path/to/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com/path/to/testOrg/testRepo.git', expectedHttp: 'https://github.com/path/to/testOrg/testRepo.git', expectedOrg: 'path/to/testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/testRepo.git', expectedSsh: 'ssh://git@github.com/testRepo.git', expectedHttp: 'https://github.com/testRepo.git', expectedOrg: 'N/A', expectedRepo: 'testRepo'],
|
||||
]
|
||||
|
||||
scmInfoTestList.each {scmInfoTest ->
|
||||
stepRule.step.setupCommonPipelineEnvironment.setGitUrlsOnCommonPipelineEnvironment(nullScript, scmInfoTest.GIT_URL)
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGitSshUrl(), is(scmInfoTest.expectedSsh))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGitHttpsUrl(), is(scmInfoTest.expectedHttp))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGithubOrg(), is(scmInfoTest.expectedOrg))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGithubRepo(), is(scmInfoTest.expectedRepo))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -143,28 +143,6 @@ class PiperPipelineStageInitTest extends BasePiperTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetScmInfoOnCommonPipelineEnvironment() {
|
||||
//currently supported formats
|
||||
def scmInfoTestList = [
|
||||
[GIT_URL: 'https://github.com/testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'https://github.com:7777/testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com:7777/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'git@github.com:testOrg/testRepo.git', expectedSsh: 'git@github.com:testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com/testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com:7777/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com:7777/testOrg/testRepo.git', expectedHttp: 'https://github.com/testOrg/testRepo.git', expectedOrg: 'testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/path/to/testOrg/testRepo.git', expectedSsh: 'ssh://git@github.com/path/to/testOrg/testRepo.git', expectedHttp: 'https://github.com/path/to/testOrg/testRepo.git', expectedOrg: 'path/to/testOrg', expectedRepo: 'testRepo'],
|
||||
[GIT_URL: 'ssh://git@github.com/testRepo.git', expectedSsh: 'ssh://git@github.com/testRepo.git', expectedHttp: 'https://github.com/testRepo.git', expectedOrg: 'N/A', expectedRepo: 'testRepo'],
|
||||
]
|
||||
|
||||
scmInfoTestList.each {scmInfoTest ->
|
||||
jsr.step.piperPipelineStageInit.setGitUrlsOnCommonPipelineEnvironment(nullScript, scmInfoTest.GIT_URL)
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGitSshUrl(), is(scmInfoTest.expectedSsh))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGitHttpsUrl(), is(scmInfoTest.expectedHttp))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGithubOrg(), is(scmInfoTest.expectedOrg))
|
||||
assertThat(nullScript.commonPipelineEnvironment.getGithubRepo(), is(scmInfoTest.expectedRepo))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPullRequestStageStepActivation() {
|
||||
|
||||
|
@@ -101,7 +101,7 @@ void call(Map parameters = [:]) {
|
||||
piperStageWrapper (script: script, stageName: stageName, stashContent: [], ordinal: 1, telemetryDisabled: true) {
|
||||
def scmInfo = checkout scm
|
||||
|
||||
setupCommonPipelineEnvironment(script: script, customDefaults: parameters.customDefaults,
|
||||
setupCommonPipelineEnvironment(script: script, customDefaults: parameters.customDefaults, scmInfo: scmInfo,
|
||||
configFile: parameters.configFile, customDefaultsFromFiles: parameters.customDefaultsFromFiles)
|
||||
|
||||
Map config = ConfigurationHelper.newInstance(this)
|
||||
@@ -146,9 +146,6 @@ void call(Map parameters = [:]) {
|
||||
initStashConfiguration(script, config.stashSettings, config.verbose?: false)
|
||||
}
|
||||
|
||||
setGitUrlsOnCommonPipelineEnvironment(script, scmInfo.GIT_URL)
|
||||
script.commonPipelineEnvironment.setGitCommitId(scmInfo.GIT_COMMIT)
|
||||
|
||||
if (config.verbose) {
|
||||
echo "piper-lib-os configuration: ${script.commonPipelineEnvironment.configuration}"
|
||||
}
|
||||
@@ -223,62 +220,6 @@ private void initStashConfiguration (script, stashSettings, verbose) {
|
||||
script.commonPipelineEnvironment.configuration.stageStashes = stashConfiguration
|
||||
}
|
||||
|
||||
private void setGitUrlsOnCommonPipelineEnvironment(script, String gitUrl) {
|
||||
|
||||
Map url = parseUrl(gitUrl)
|
||||
|
||||
if (url.protocol in ['http', 'https']) {
|
||||
script.commonPipelineEnvironment.setGitSshUrl("git@${url.host}:${url.path}")
|
||||
script.commonPipelineEnvironment.setGitHttpsUrl(gitUrl)
|
||||
} else if (url.protocol in [ null, 'ssh', 'git']) {
|
||||
script.commonPipelineEnvironment.setGitSshUrl(gitUrl)
|
||||
script.commonPipelineEnvironment.setGitHttpsUrl("https://${url.host}/${url.path}")
|
||||
}
|
||||
|
||||
List gitPathParts = url.path.replaceAll('.git', '').split('/')
|
||||
def gitFolder = 'N/A'
|
||||
def gitRepo = 'N/A'
|
||||
switch (gitPathParts.size()) {
|
||||
case 1:
|
||||
gitRepo = gitPathParts[0]
|
||||
break
|
||||
case 2:
|
||||
gitFolder = gitPathParts[0]
|
||||
gitRepo = gitPathParts[1]
|
||||
break
|
||||
case { it > 3 }:
|
||||
gitRepo = gitPathParts[gitPathParts.size()-1]
|
||||
gitPathParts.remove(gitPathParts.size()-1)
|
||||
gitFolder = gitPathParts.join('/')
|
||||
break
|
||||
}
|
||||
script.commonPipelineEnvironment.setGithubOrg(gitFolder)
|
||||
script.commonPipelineEnvironment.setGithubRepo(gitRepo)
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the parts of an url.
|
||||
* Valid keys for the retured map are:
|
||||
* - protocol
|
||||
* - auth
|
||||
* - host
|
||||
* - port
|
||||
* - path
|
||||
*/
|
||||
@NonCPS
|
||||
/* private */ Map parseUrl(String url) {
|
||||
|
||||
def urlMatcher = url =~ /^((http|https|git|ssh):\/\/)?((.*)@)?([^:\/]+)(:([\d]*))?(\/?(.*))$/
|
||||
|
||||
return [
|
||||
protocol: urlMatcher[0][2],
|
||||
auth: urlMatcher[0][4],
|
||||
host: urlMatcher[0][5],
|
||||
port: urlMatcher[0][7],
|
||||
path: urlMatcher[0][9],
|
||||
]
|
||||
}
|
||||
|
||||
private void setPullRequestStageStepActivation(script, config, List actions) {
|
||||
|
||||
if (script.commonPipelineEnvironment.configuration.runStep == null)
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import com.cloudbees.groovy.cps.NonCPS
|
||||
|
||||
import static com.sap.piper.Prerequisites.checkScript
|
||||
|
||||
import com.sap.piper.GenerateDocumentation
|
||||
@@ -35,7 +37,10 @@ import groovy.transform.Field
|
||||
/** A list of file paths or URLs which must point to YAML content. These work exactly like
|
||||
* `customDefaults`, but from local or remote files instead of library resources. They are merged with and
|
||||
* take precedence over `customDefaults`.*/
|
||||
'customDefaultsFromFiles'
|
||||
'customDefaultsFromFiles',
|
||||
/** The map returned from a Jenkins git checkout. Used to set the git information in the
|
||||
* common pipeline environment */
|
||||
'scmInfo'
|
||||
]
|
||||
|
||||
/**
|
||||
@@ -104,6 +109,12 @@ void call(Map parameters = [:]) {
|
||||
|
||||
InfluxData.addField('step_data', 'build_url', env.BUILD_URL)
|
||||
InfluxData.addField('pipeline_data', 'build_url', env.BUILD_URL)
|
||||
|
||||
def scmInfo = parameters.scmInfo
|
||||
if (scmInfo) {
|
||||
setGitUrlsOnCommonPipelineEnvironment(script, scmInfo.GIT_URL)
|
||||
script.commonPipelineEnvironment.setGitCommitId(scmInfo.GIT_COMMIT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,3 +197,59 @@ private static List copyOrDownloadCustomDefaultsIntoPipelineEnv(script, List cus
|
||||
}
|
||||
return fileList
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the parts of an url.
|
||||
* Valid keys for the retured map are:
|
||||
* - protocol
|
||||
* - auth
|
||||
* - host
|
||||
* - port
|
||||
* - path
|
||||
*/
|
||||
@NonCPS
|
||||
/* private */ Map parseUrl(String url) {
|
||||
|
||||
def urlMatcher = url =~ /^((http|https|git|ssh):\/\/)?((.*)@)?([^:\/]+)(:([\d]*))?(\/?(.*))$/
|
||||
|
||||
return [
|
||||
protocol: urlMatcher[0][2],
|
||||
auth: urlMatcher[0][4],
|
||||
host: urlMatcher[0][5],
|
||||
port: urlMatcher[0][7],
|
||||
path: urlMatcher[0][9],
|
||||
]
|
||||
}
|
||||
|
||||
private void setGitUrlsOnCommonPipelineEnvironment(script, String gitUrl) {
|
||||
|
||||
Map url = parseUrl(gitUrl)
|
||||
|
||||
if (url.protocol in ['http', 'https']) {
|
||||
script.commonPipelineEnvironment.setGitSshUrl("git@${url.host}:${url.path}")
|
||||
script.commonPipelineEnvironment.setGitHttpsUrl(gitUrl)
|
||||
} else if (url.protocol in [ null, 'ssh', 'git']) {
|
||||
script.commonPipelineEnvironment.setGitSshUrl(gitUrl)
|
||||
script.commonPipelineEnvironment.setGitHttpsUrl("https://${url.host}/${url.path}")
|
||||
}
|
||||
|
||||
List gitPathParts = url.path.replaceAll('.git', '').split('/')
|
||||
def gitFolder = 'N/A'
|
||||
def gitRepo = 'N/A'
|
||||
switch (gitPathParts.size()) {
|
||||
case 1:
|
||||
gitRepo = gitPathParts[0]
|
||||
break
|
||||
case 2:
|
||||
gitFolder = gitPathParts[0]
|
||||
gitRepo = gitPathParts[1]
|
||||
break
|
||||
case { it > 3 }:
|
||||
gitRepo = gitPathParts[gitPathParts.size()-1]
|
||||
gitPathParts.remove(gitPathParts.size()-1)
|
||||
gitFolder = gitPathParts.join('/')
|
||||
break
|
||||
}
|
||||
script.commonPipelineEnvironment.setGithubOrg(gitFolder)
|
||||
script.commonPipelineEnvironment.setGithubRepo(gitRepo)
|
||||
}
|
||||
|
Reference in New Issue
Block a user