1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Merge branch 'master' into sv-docuGen

This commit is contained in:
redehnroV 2019-11-22 07:49:40 +01:00 committed by GitHub
commit 9559832a19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 100 additions and 395 deletions

View File

@ -59,7 +59,7 @@ func runGithubPublishRelease(ctx context.Context, myGithubPublishReleaseOptions
log.Entry().Debugf("Previous GitHub release published: '%v'", publishedAt)
//updating assets only supported on latest release
if myGithubPublishReleaseOptions.UpdateAsset && myGithubPublishReleaseOptions.Version == "latest" {
if len(myGithubPublishReleaseOptions.AssetPath) > 0 && myGithubPublishReleaseOptions.Version == "latest" {
return uploadReleaseAsset(ctx, lastRelease.GetID(), myGithubPublishReleaseOptions, ghRepoClient)
}

View File

@ -22,7 +22,6 @@ type githubPublishReleaseOptions struct {
UploadURL string `json:"uploadUrl,omitempty"`
Labels []string `json:"labels,omitempty"`
ReleaseBodyHeader string `json:"releaseBodyHeader,omitempty"`
UpdateAsset bool `json:"updateAsset,omitempty"`
Version string `json:"version,omitempty"`
}
@ -73,7 +72,6 @@ func addGithubPublishReleaseFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&myGithubPublishReleaseOptions.UploadURL, "uploadUrl", "https://uploads.github.com", "Set the GitHub API url.")
cmd.Flags().StringSliceVar(&myGithubPublishReleaseOptions.Labels, "labels", []string{}, "Labels to include in issue search.")
cmd.Flags().StringVar(&myGithubPublishReleaseOptions.ReleaseBodyHeader, "releaseBodyHeader", os.Getenv("PIPER_releaseBodyHeader"), "Content which will appear for the release.")
cmd.Flags().BoolVar(&myGithubPublishReleaseOptions.UpdateAsset, "updateAsset", false, "Specify if a release asset should be updated only.")
cmd.Flags().StringVar(&myGithubPublishReleaseOptions.Version, "version", os.Getenv("PIPER_version"), "Define the version number which will be written as tag as well as release name.")
cmd.MarkFlagRequired("apiUrl")
@ -96,90 +94,98 @@ func githubPublishReleaseMetadata() config.StepData {
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "bool",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "addDeltaToLastRelease",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "bool",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "assetPath",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "commitish",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "excludeLabels",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "[]string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "apiUrl",
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubApiUrl"}},
},
{
Name: "owner",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubOrg"}},
},
{
Name: "repository",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubRepo"}},
},
{
Name: "serverUrl",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubServerUrl"}},
},
{
Name: "token",
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubToken"}},
},
{
Name: "uploadUrl",
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "githubUploadUrl"}},
},
{
Name: "labels",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "[]string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "releaseBodyHeader",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
},
{
Name: "updateAsset",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "bool",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "version",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
},
},

View File

@ -169,9 +169,8 @@ func TestRunGithubPublishRelease(t *testing.T) {
}
myGithubPublishReleaseOptions := githubPublishReleaseOptions{
UpdateAsset: true,
AssetPath: filepath.Join("testdata", t.Name()+"_test.txt"),
Version: "latest",
AssetPath: filepath.Join("testdata", t.Name()+"_test.txt"),
Version: "latest",
}
err := runGithubPublishRelease(ctx, &myGithubPublishReleaseOptions, &ghRepoClient, &ghIssueClient)

View File

@ -67,18 +67,21 @@ func karmaExecuteTestsMetadata() config.StepData {
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "modulePath",
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "runCommand",
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
},
},

3
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.4.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/tools/gopls v0.2.1 // indirect
)

View File

@ -87,6 +87,7 @@ func {{ .StepName }}Metadata() config.StepData {
Scope: []string{{ "{" }}{{ range $notused, $scope := $value.Scope }}"{{ $scope }}",{{ end }}{{ "}" }},
Type: "{{ $value.Type }}",
Mandatory: {{ $value.Mandatory }},
Aliases: []config.Alias{{ "{" }}{{ range $notused, $alias := $value.Aliases }}{{ "{" }}Name: "{{ $alias.Name }}"{{ "}" }},{{ end }}{{ "}" }},
},{{ end }}
},
},

View File

@ -59,18 +59,21 @@ func testStepMetadata() config.StepData {
Scope: []string{"GENERAL","PARAMETERS",},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "param1",
Scope: []string{"PARAMETERS",},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "param2",
Scope: []string{"PARAMETERS",},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
},
},

View File

@ -137,13 +137,6 @@ spec:
- STAGES
- STEPS
type: string
- name: updateAsset
description: Specify if a release asset should be updated only.
scope:
- PARAMETERS
- STAGES
- STEPS
type: bool
- name: version
description: 'Define the version number which will be written as tag as well as release name.'
scope:

View File

@ -28,7 +28,7 @@ class PiperGoUtils implements Serializable {
}
def fallbackUrl = 'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master'
def piperBinUrl = (version == 'master') ? fallbackUrl : "https://github.com/SAP/jenkins-library/releases/tag/${version}"
def piperBinUrl = (version == 'master') ? fallbackUrl : "https://github.com/SAP/jenkins-library/releases/download/${version}/piper"
boolean downloaded = downloadGoBinary(piperBinUrl)
if (!downloaded) {

View File

@ -53,7 +53,7 @@ public class CommonStepsTest extends BasePiperTest{
'piperPipeline',
'prepareDefaultValues',
'setupCommonPipelineEnvironment',
'buildSetResult'
'buildSetResult',
]
List steps = getSteps().stream()
@ -114,7 +114,8 @@ public class CommonStepsTest extends BasePiperTest{
'commonPipelineEnvironment', // special step (infrastructure)
'handlePipelineStepErrors', // special step (infrastructure)
'piperStageWrapper', //intended to be called from within stages
'buildSetResult'
'buildSetResult',
'githubPublishRelease' //implementing new golang pattern without fields
]
@Test

File diff suppressed because one or more lines are too long

View File

@ -80,11 +80,11 @@ class PiperGoUtilsTest extends BasePiperTest {
return []
})
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/tag/testTag\'', '200')
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\'', '200')
piperGoUtils.unstashPiperBin()
assertThat(shellCallRule.shell.size(), is(2))
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/tag/testTag\''))
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\''))
assertThat(shellCallRule.shell[1].toString(), is('chmod +x ./piper'))
}
@ -94,7 +94,7 @@ class PiperGoUtilsTest extends BasePiperTest {
def piperGoUtils = new PiperGoUtils(nullScript, utils)
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/tag/notAvailable\'', '404')
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404')
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '200')
// this mocks utils.unstash - mimic stash not existing
@ -104,7 +104,7 @@ class PiperGoUtilsTest extends BasePiperTest {
piperGoUtils.unstashPiperBin()
assertThat(shellCallRule.shell.size(), is(3))
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/tag/notAvailable\''))
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\''))
assertThat(shellCallRule.shell[1].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\''))
assertThat(shellCallRule.shell[2].toString(), is('chmod +x ./piper'))
}
@ -114,7 +114,7 @@ class PiperGoUtilsTest extends BasePiperTest {
def piperGoUtils = new PiperGoUtils(nullScript, utils)
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/tag/notAvailable\'', '404')
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404')
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output ./piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '500')
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->

View File

@ -1,198 +1,44 @@
import com.sap.piper.JsonUtils
import com.sap.piper.PiperGoUtils
import com.sap.piper.Utils
import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import groovy.text.GStringTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubrelease.yaml'
@Field Set GENERAL_CONFIG_KEYS = [
/** Allows to overwrite the GitHub API url.*/
'githubApiUrl',
/**
* Allows to overwrite the GitHub token credentials id.
* @possibleValues Jenkins credential id
*/
'githubTokenCredentialsId',
/** Allows to overwrite the GitHub url.*/
'githubServerUrl'
]
//Metadata maintained in file project://resources/metadata/githubrelease.yaml
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
/**
* If it is set to `true`, a list of all closed issues and merged pull-requests since the last release will added below the `releaseBodyHeader`.
* @possibleValues `true`, `false`
*/
'addClosedIssues',
/**
* If you set `addDeltaToLastRelease` to `true`, a link will be added to the relese information that brings up all commits since the last release.
* @possibleValues `true`, `false`
*/
'addDeltaToLastRelease',
/** Allows to pass additional filter criteria for retrieving closed issues since the last release. Additional criteria could be for example specific `label`, or `filter` according to [GitHub API documentation](https://developer.github.com/v3/issues/).*/
'customFilterExtension',
/** Allows to exclude issues with dedicated labels. Usage is like `excludeLabels: ['label1', 'label2']`.*/
'excludeLabels',
/** Allows to overwrite the GitHub organitation.*/
'githubOrg',
/** Allows to overwrite the GitHub repository.*/
'githubRepo',
/** Allows to specify the content which will appear for the release.
* It is possible to define it as Groovy template as well in order to bring in dynamic information.
* Following information can be used: everything contained in `config` as well as information from `commonPipelineEnvironment`.
*/
'releaseBodyHeader',
/** Defines the version number which will be written as tag as well as release name.*/
'version'
])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
/**
* This step creates a tag in your GitHub repository together with a release.
*
* The release can be filled with text plus additional information like:
*
* * Closed pull request since last release
* * Closed issues since last release
* * link to delta information showing all commits since last release
*
* The result looks like
*
* ![Example release](../images/githubRelease.png)
*/
@GenerateDocumentation
void call(Map parameters = [:]) {
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
def script = checkScript(this, parameters) ?: this
// load default & individual configuration
Map config = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.addIfEmpty('githubOrg', script.commonPipelineEnvironment.getGithubOrg())
.addIfEmpty('githubRepo', script.commonPipelineEnvironment.getGithubRepo())
.addIfEmpty('version', script.commonPipelineEnvironment.getArtifactVersion())
.withMandatoryProperty('githubOrg')
.withMandatoryProperty('githubRepo')
.withMandatoryProperty('githubTokenCredentialsId')
.withMandatoryProperty('version')
.use()
Map config
def utils = parameters.juStabUtils ?: new Utils()
parameters.juStabUtils = null
new Utils().pushToSWA([step: STEP_NAME], config)
// telemetry reporting
utils.pushToSWA([step: STEP_NAME], config)
withCredentials([string(credentialsId: config.githubTokenCredentialsId, variable: 'TOKEN')]) {
new PiperGoUtils(this, utils).unstashPiperBin()
utils.unstash('pipelineConfigAndTests')
def releaseBodyHeader = ''
if (config.releaseBodyHeader) {
releaseBodyHeader = GStringTemplateEngine.newInstance()
.createTemplate(config.releaseBodyHeader)
.make([
config: config,
commonPipelineEnvironment: script.commonPipelineEnvironment
]).toString()
releaseBodyHeader += '<br />'
}
def releaseBody = releaseBodyHeader
def content = getLastRelease(config, TOKEN)
if (config.addClosedIssues)
releaseBody += addClosedIssue(config, TOKEN, content.published_at)
if (config.addDeltaToLastRelease)
releaseBody += addDeltaToLastRelease(config, content.tag_name)
postNewRelease(config, TOKEN, releaseBody)
}
}
}
writeFile(file: METADATA_FILE, text: libraryResource(METADATA_FILE))
Map getLastRelease(config, TOKEN){
def result = [:]
withEnv([
"PIPER_parametersJSON=${groovy.json.JsonOutput.toJson(parameters)}",
"PIPER_owner=${script.commonPipelineEnvironment.getGithubOrg()?:''}",
"PIPER_repository=${script.commonPipelineEnvironment.getGithubRepo()?:''}",
"PIPER_version=${script.commonPipelineEnvironment.getArtifactVersion()?:''}"
]) {
// get context configuration
config = readJSON (text: sh(returnStdout: true, script: "./piper getConfig --contextConfig --stepMetadata '${METADATA_FILE}' --stepName ${STEP_NAME}"))
def response = httpRequest url: "${config.githubApiUrl}/repos/${config.githubOrg}/${config.githubRepo}/releases/latest?access_token=${TOKEN}", validResponseCodes: '100:500'
if (response.status == 200) {
result = readJSON text: response.content
} else {
echo "[${STEP_NAME}] This is the first release - no previous releases available"
config.addDeltaToLastRelease = false
}
return result
}
String addClosedIssue(config, TOKEN, publishedAt){
if (config.customFilterExtension) {
config.customFilterExtension = "&${config.customFilterExtension}"
}
def publishedAtFilter = publishedAt ? "&since=${publishedAt}": ''
def response = httpRequest "${config.githubApiUrl}/repos/${config.githubOrg}/${config.githubRepo}/issues?access_token=${TOKEN}&per_page=100&state=closed&direction=asc${publishedAtFilter}${config.customFilterExtension}"
def result = ''
content = readJSON text: response.content
//list closed pull-requests
result += '<br />**List of closed pull-requests since last release**<br />'
for (def item : content) {
if (item.pull_request && !isExcluded(item, config.excludeLabels)) {
result += "[#${item.number}](${item.html_url}): ${item.title}<br />"
}
}
//list closed issues
result += '<br />**List of closed issues since last release**<br />'
for (def item : content) {
if (!item.pull_request && !isExcluded(item, config.excludeLabels)) {
result += "[#${item.number}](${item.html_url}): ${item.title}<br />"
}
}
return result
}
String addDeltaToLastRelease(config, latestTag){
def result = ''
//add delta link to previous release
result += '<br />**Changes**<br />'
result += "[${latestTag}...${config.version}](${config.githubServerUrl}/${config.githubOrg}/${config.githubRepo}/compare/${latestTag}...${config.version}) <br />"
return result
}
void postNewRelease(config, TOKEN, releaseBody){
Map messageBody = [
tag_name: "${config.version}",
target_commitish: 'master',
name: "${config.version}",
body: releaseBody,
draft: false,
prerelease: false
]
def data = new JsonUtils().groovyObjectToJsonString(messageBody)
try {
httpRequest httpMode: 'POST', requestBody: data, url: "${config.githubApiUrl}/repos/${config.githubOrg}/${config.githubRepo}/releases?access_token=${TOKEN}"
} catch (e) {
echo """[${STEP_NAME}] Error occured when writing release information
---------------------------------------------------------------------
Request body was:
---------------------------------------------------------------------
${data}
---------------------------------------------------------------------"""
throw e
}
}
boolean isExcluded(item, excludeLabels){
def result = false
excludeLabels.each {labelName ->
item.labels.each { label ->
if (label.name == labelName) {
result = true
// execute step
withCredentials([string(credentialsId: config.githubTokenCredentialsId, variable: 'TOKEN')]) {
sh "./piper githubPublishRelease --token ${TOKEN}"
}
}
}
return result
}