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"
2020-01-29 14:17:54 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
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-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" `
Token string ` json:"token,omitempty" `
UploadURL string ` json:"uploadUrl,omitempty" `
2019-11-04 17:07:30 +02:00
Version string ` json:"version,omitempty" `
}
var myGithubPublishReleaseOptions githubPublishReleaseOptions
// GithubPublishReleaseCommand Publish a release in GitHub
func GithubPublishReleaseCommand ( ) * cobra . Command {
metadata := githubPublishReleaseMetadata ( )
2020-01-31 15:06:08 +02:00
var startTime time . Time
2020-01-15 13:16:25 +02:00
2019-11-04 17:07:30 +02:00
var createGithubPublishReleaseCmd = & cobra . Command {
Use : "githubPublishRelease" ,
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 ) ` ,
PreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-01-31 15:06:08 +02:00
startTime = time . Now ( )
2019-11-05 15:37:44 +02:00
log . SetStepName ( "githubPublishRelease" )
2019-11-06 17:22:50 +02:00
log . SetVerbose ( GeneralConfig . Verbose )
2019-11-21 17:09:57 +02:00
return PrepareConfig ( cmd , & metadata , "githubPublishRelease" , & myGithubPublishReleaseOptions , config . OpenPiperFile )
2019-11-04 17:07:30 +02:00
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-01-31 15:06:08 +02:00
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
telemetry . Send ( & telemetryData )
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-01-29 14:17:54 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , "githubPublishRelease" )
2020-01-31 15:06:08 +02:00
// ToDo: pass telemetryData to step
err := githubPublishRelease ( myGithubPublishReleaseOptions )
telemetryData . ErrorCode = "0"
return err
2019-11-04 17:07:30 +02:00
} ,
}
addGithubPublishReleaseFlags ( createGithubPublishReleaseCmd )
return createGithubPublishReleaseCmd
}
func addGithubPublishReleaseFlags ( cmd * cobra . Command ) {
cmd . Flags ( ) . BoolVar ( & myGithubPublishReleaseOptions . AddClosedIssues , "addClosedIssues" , false , "If set to `true`, closed issues and merged pull-requests since the last release will added below the `releaseBodyHeader`" )
cmd . Flags ( ) . BoolVar ( & myGithubPublishReleaseOptions . AddDeltaToLastRelease , "addDeltaToLastRelease" , false , "If set to `true`, a link will be added to the relese information that brings up all commits since the last release." )
2020-01-15 13:16:25 +02:00
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . APIURL , "apiUrl" , "https://api.github.com" , "Set the GitHub API url." )
2019-11-04 17:07:30 +02:00
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . AssetPath , "assetPath" , os . Getenv ( "PIPER_assetPath" ) , "Path to a release asset which should be uploaded to the list of release assets." )
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . Commitish , "commitish" , "master" , "Target git commitish for the release" )
cmd . Flags ( ) . StringSliceVar ( & myGithubPublishReleaseOptions . ExcludeLabels , "excludeLabels" , [ ] string { } , "Allows to exclude issues with dedicated list of labels." )
2020-01-15 13:16:25 +02:00
cmd . Flags ( ) . StringSliceVar ( & myGithubPublishReleaseOptions . Labels , "labels" , [ ] string { } , "Labels to include in issue search." )
2019-11-05 16:13:04 +02:00
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . Owner , "owner" , os . Getenv ( "PIPER_owner" ) , "Set the GitHub organization." )
2020-01-15 13:16:25 +02:00
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . ReleaseBodyHeader , "releaseBodyHeader" , os . Getenv ( "PIPER_releaseBodyHeader" ) , "Content which will appear for the release." )
2019-11-05 16:13:04 +02:00
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . Repository , "repository" , os . Getenv ( "PIPER_repository" ) , "Set the GitHub repository." )
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . ServerURL , "serverUrl" , "https://github.com" , "GitHub server url for end-user access." )
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . 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" )
cmd . Flags ( ) . StringVar ( & myGithubPublishReleaseOptions . UploadURL , "uploadUrl" , "https://uploads.github.com" , "Set the GitHub API url." )
2019-11-04 17:07:30 +02:00
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." )
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 {
Spec : config . StepSpec {
Inputs : config . StepInputs {
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 { } ,
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 { } ,
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" } } ,
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 { } ,
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 { } ,
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 { } ,
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 { } ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +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" } } ,
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 { } ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +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" } } ,
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" } } ,
2019-11-05 15:37:44 +02:00
} ,
2019-11-04 17:07:30 +02:00
{
2020-01-15 13:16:25 +02:00
Name : "token" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { { Name : "githubToken" } } ,
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" } } ,
2019-11-04 17:07:30 +02:00
} ,
{
2020-01-15 13:16:25 +02:00
Name : "version" ,
ResourceRef : [ ] config . ResourceReference { { Name : "commonPipelineEnvironment" , Param : "artifactVersion" } } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2019-11-04 17:07:30 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}