2020-02-21 13:56:09 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
2019-11-04 17:07:30 +02:00
package cmd
import (
2020-01-31 15:06:08 +02:00
"fmt"
2019-11-04 17:07:30 +02:00
"os"
2020-01-31 15:06:08 +02:00
"time"
2019-11-04 17:07:30 +02:00
"github.com/SAP/jenkins-library/pkg/config"
2019-11-05 15:37:44 +02:00
"github.com/SAP/jenkins-library/pkg/log"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-01-29 14:17:54 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-10-01 12:49:05 +02:00
"github.com/SAP/jenkins-library/pkg/validation"
2019-11-04 17:07:30 +02:00
"github.com/spf13/cobra"
)
type githubPublishReleaseOptions struct {
AddClosedIssues bool ` json:"addClosedIssues,omitempty" `
AddDeltaToLastRelease bool ` json:"addDeltaToLastRelease,omitempty" `
2020-01-15 13:16:25 +02:00
APIURL string ` json:"apiUrl,omitempty" `
2019-11-04 17:07:30 +02:00
AssetPath string ` json:"assetPath,omitempty" `
Commitish string ` json:"commitish,omitempty" `
ExcludeLabels [ ] string ` json:"excludeLabels,omitempty" `
2020-01-15 13:16:25 +02:00
Labels [ ] string ` json:"labels,omitempty" `
2019-11-05 16:13:04 +02:00
Owner string ` json:"owner,omitempty" `
2020-06-15 21:05:08 +02:00
PreRelease bool ` json:"preRelease,omitempty" `
2020-01-15 13:16:25 +02:00
ReleaseBodyHeader string ` json:"releaseBodyHeader,omitempty" `
2019-11-05 16:13:04 +02:00
Repository string ` json:"repository,omitempty" `
ServerURL string ` json:"serverUrl,omitempty" `
2021-10-19 17:36:17 +02:00
TagPrefix string ` json:"tagPrefix,omitempty" `
2019-11-05 16:13:04 +02:00
Token string ` json:"token,omitempty" `
UploadURL string ` json:"uploadUrl,omitempty" `
2019-11-04 17:07:30 +02:00
Version string ` json:"version,omitempty" `
}
// GithubPublishReleaseCommand Publish a release in GitHub
func GithubPublishReleaseCommand ( ) * cobra . Command {
2020-04-28 07:42:02 +02:00
const STEP_NAME = "githubPublishRelease"
2019-11-04 17:07:30 +02:00
metadata := githubPublishReleaseMetadata ( )
2020-02-04 11:46:43 +02:00
var stepConfig githubPublishReleaseOptions
2020-01-31 15:06:08 +02:00
var startTime time . Time
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-01-15 13:16:25 +02:00
2019-11-04 17:07:30 +02:00
var createGithubPublishReleaseCmd = & cobra . Command {
2020-04-28 07:42:02 +02:00
Use : STEP_NAME ,
2019-11-04 17:07:30 +02:00
Short : "Publish a release in GitHub" ,
Long : ` 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 ) ` ,
2020-06-23 18:05:21 +02:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-01-31 15:06:08 +02:00
startTime = time . Now ( )
2020-04-28 07:42:02 +02:00
log . SetStepName ( STEP_NAME )
2019-11-06 17:22:50 +02:00
log . SetVerbose ( GeneralConfig . Verbose )
2020-04-28 07:42:02 +02:00
2021-07-08 15:26:07 +02:00
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
2020-04-28 07:42:02 +02:00
path , _ := os . Getwd ( )
fatalHook := & log . FatalHook { CorrelationID : GeneralConfig . CorrelationID , Path : path }
log . RegisterHook ( fatalHook )
err := PrepareConfig ( cmd , & metadata , STEP_NAME , & stepConfig , config . OpenPiperFile )
2020-04-16 14:37:45 +02:00
if err != nil {
2020-06-24 10:04:05 +02:00
log . SetErrorCategory ( log . ErrorConfiguration )
2020-04-16 14:37:45 +02:00
return err
}
log . RegisterSecret ( stepConfig . Token )
2020-05-05 08:36:24 +02:00
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
2021-10-01 12:49:05 +02:00
validation , err := validation . New ( validation . WithJSONNamesForStructFields ( ) , validation . WithPredefinedErrorMessages ( ) )
if err != nil {
return err
}
if err = validation . ValidateStruct ( stepConfig ) ; err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
2020-04-16 14:37:45 +02:00
return nil
2019-11-04 17:07:30 +02:00
} ,
2020-06-23 18:05:21 +02:00
Run : func ( _ * cobra . Command , _ [ ] string ) {
2020-01-31 15:06:08 +02:00
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-01-31 15:06:08 +02:00
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
2020-09-29 13:49:40 +02:00
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
2020-01-31 15:06:08 +02:00
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-01-31 15:06:08 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-04-28 07:42:02 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
2020-02-04 11:46:43 +02:00
githubPublishRelease ( stepConfig , & telemetryData )
2020-01-31 15:06:08 +02:00
telemetryData . ErrorCode = "0"
2020-05-18 21:52:54 +02:00
log . Entry ( ) . Info ( "SUCCESS" )
2019-11-04 17:07:30 +02:00
} ,
}
2020-02-04 11:46:43 +02:00
addGithubPublishReleaseFlags ( createGithubPublishReleaseCmd , & stepConfig )
2019-11-04 17:07:30 +02:00
return createGithubPublishReleaseCmd
}
2020-02-04 11:46:43 +02:00
func addGithubPublishReleaseFlags ( cmd * cobra . Command , stepConfig * githubPublishReleaseOptions ) {
cmd . Flags ( ) . BoolVar ( & stepConfig . AddClosedIssues , "addClosedIssues" , false , "If set to `true`, closed issues and merged pull-requests since the last release will added below the `releaseBodyHeader`" )
2020-11-25 14:35:37 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . AddDeltaToLastRelease , "addDeltaToLastRelease" , false , "If set to `true`, a link will be added to the release information that brings up all commits since the last release." )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . APIURL , "apiUrl" , ` https://api.github.com ` , "Set the GitHub API url." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . AssetPath , "assetPath" , os . Getenv ( "PIPER_assetPath" ) , "Path to a release asset which should be uploaded to the list of release assets." )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Commitish , "commitish" , ` master ` , "Target git commitish for the release" )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . ExcludeLabels , "excludeLabels" , [ ] string { } , "Allows to exclude issues with dedicated list of labels." )
cmd . Flags ( ) . StringSliceVar ( & stepConfig . Labels , "labels" , [ ] string { } , "Labels to include in issue search." )
2020-09-14 12:05:12 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Owner , "owner" , os . Getenv ( "PIPER_owner" ) , "Name of the GitHub organization." )
2020-06-15 21:05:08 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . PreRelease , "preRelease" , false , "If set to `true` the release will be marked as Pre-release." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ReleaseBodyHeader , "releaseBodyHeader" , os . Getenv ( "PIPER_releaseBodyHeader" ) , "Content which will appear for the release." )
2020-09-14 12:05:12 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Repository , "repository" , os . Getenv ( "PIPER_repository" ) , "Name of the GitHub repository." )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ServerURL , "serverUrl" , ` https://github.com ` , "GitHub server url for end-user access." )
2021-10-19 17:36:17 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . TagPrefix , "tagPrefix" , ` ` , "Defines a prefix to be added to the tag." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Token , "token" , os . Getenv ( "PIPER_token" ) , "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line" )
2020-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . UploadURL , "uploadUrl" , ` https://uploads.github.com ` , "Set the GitHub API url." )
2020-02-04 11:46:43 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Version , "version" , os . Getenv ( "PIPER_version" ) , "Define the version number which will be written as tag as well as release name." )
2019-11-04 17:07:30 +02:00
2019-11-05 16:13:04 +02:00
cmd . MarkFlagRequired ( "apiUrl" )
cmd . MarkFlagRequired ( "owner" )
cmd . MarkFlagRequired ( "repository" )
cmd . MarkFlagRequired ( "serverUrl" )
cmd . MarkFlagRequired ( "token" )
cmd . MarkFlagRequired ( "uploadUrl" )
2019-11-04 17:07:30 +02:00
cmd . MarkFlagRequired ( "version" )
}
// retrieve step metadata
func githubPublishReleaseMetadata ( ) config . StepData {
var theMetaData = config . StepData {
2020-03-19 18:24:35 +02:00
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "githubPublishRelease" ,
Aliases : [ ] config . Alias { } ,
Description : "Publish a release in GitHub" ,
2020-03-19 18:24:35 +02:00
} ,
2019-11-04 17:07:30 +02:00
Spec : config . StepSpec {
Inputs : config . StepInputs {
2021-06-16 08:43:30 +02:00
Secrets : [ ] config . StepSecrets {
{ Name : "githubTokenCredentialsId" , Description : "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub." , Type : "jenkins" } ,
} ,
2019-11-04 17:07:30 +02:00
Parameters : [ ] config . StepParameters {
{
2020-01-15 13:16:25 +02:00
Name : "addClosedIssues" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "addDeltaToLastRelease" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "apiUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubApiUrl" } } ,
2021-06-16 08:43:30 +02:00
Default : ` https://api.github.com ` ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "assetPath" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_assetPath" ) ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "commitish" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : ` master ` ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "excludeLabels" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "labels" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "owner" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "github/owner" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubOrg" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_owner" ) ,
2019-11-04 17:07:30 +02:00
} ,
2020-06-15 21:05:08 +02:00
{
Name : "preRelease" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2020-06-15 21:05:08 +02:00
} ,
2019-11-04 17:07:30 +02:00
{
2020-01-15 13:16:25 +02:00
Name : "releaseBodyHeader" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_releaseBodyHeader" ) ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "repository" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "github/repository" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubRepo" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_repository" ) ,
2019-11-04 17:07:30 +02:00
} ,
2019-11-05 15:37:44 +02:00
{
2020-01-15 13:16:25 +02:00
Name : "serverUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubServerUrl" } } ,
2021-06-16 08:43:30 +02:00
Default : ` https://github.com ` ,
2019-11-05 15:37:44 +02:00
} ,
2021-10-19 17:36:17 +02:00
{
Name : "tagPrefix" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : ` ` ,
} ,
2019-11-04 17:07:30 +02:00
{
2020-09-16 14:50:09 +02:00
Name : "token" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubTokenCredentialsId" ,
Type : "secret" ,
} ,
2020-12-22 18:57:11 +02:00
{
2021-09-21 13:06:32 +02:00
Name : "githubVaultSecretName" ,
Type : "vaultSecret" ,
Default : "github" ,
2020-12-22 18:57:11 +02:00
} ,
2020-09-16 14:50:09 +02:00
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
2021-01-27 13:58:47 +02:00
Aliases : [ ] config . Alias { { Name : "githubToken" } , { Name : "access_token" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_token" ) ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "uploadUrl" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubUploadUrl" } } ,
2021-06-16 08:43:30 +02:00
Default : ` https://uploads.github.com ` ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "version" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "artifactVersion" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_version" ) ,
2019-11-04 17:07:30 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}