2020-07-23 20:20:07 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-07-23 20:20:07 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-10-01 12:49:05 +02:00
"github.com/SAP/jenkins-library/pkg/validation"
2020-07-23 20:20:07 +02:00
"github.com/spf13/cobra"
)
type gctsRollbackOptions struct {
Username string ` json:"username,omitempty" `
Password string ` json:"password,omitempty" `
Repository string ` json:"repository,omitempty" `
Host string ` json:"host,omitempty" `
Client string ` json:"client,omitempty" `
Commit string ` json:"commit,omitempty" `
GithubPersonalAccessToken string ` json:"githubPersonalAccessToken,omitempty" `
}
// GctsRollbackCommand Perfoms roll back of one (default) or several commit(s)
func GctsRollbackCommand ( ) * cobra . Command {
const STEP_NAME = "gctsRollback"
metadata := gctsRollbackMetadata ( )
var stepConfig gctsRollbackOptions
var startTime time . Time
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-07-23 20:20:07 +02:00
var createGctsRollbackCmd = & cobra . Command {
Use : STEP_NAME ,
Short : "Perfoms roll back of one (default) or several commit(s)" ,
Long : ` This step performs a rollback of commit ( s ) in a local ABAP system repository . If a < commit > parameter is specified , it will be used as the target commit for the rollback .
If no < commit > parameter is specified and the remote repository domain is ' github . com ' , the last commit with status ' success ' will be used for the rollback . Otherwise ,
gctsRollback will rollback to the previously active commit in the local repository . ` ,
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
startTime = time . Now ( )
log . SetStepName ( STEP_NAME )
log . SetVerbose ( GeneralConfig . Verbose )
2021-07-08 15:26:07 +02:00
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
2020-07-23 20:20:07 +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 )
if err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
log . RegisterSecret ( stepConfig . Username )
log . RegisterSecret ( stepConfig . Password )
log . RegisterSecret ( stepConfig . GithubPersonalAccessToken )
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-07-23 20:20:07 +02:00
return nil
} ,
Run : func ( _ * cobra . Command , _ [ ] string ) {
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-07-23 20:20:07 +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-07-23 20:20:07 +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-07-23 20:20:07 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
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-07-23 20:20:07 +02:00
gctsRollback ( stepConfig , & telemetryData )
telemetryData . ErrorCode = "0"
log . Entry ( ) . Info ( "SUCCESS" )
} ,
}
addGctsRollbackFlags ( createGctsRollbackCmd , & stepConfig )
return createGctsRollbackCmd
}
func addGctsRollbackFlags ( cmd * cobra . Command , stepConfig * gctsRollbackOptions ) {
cmd . Flags ( ) . StringVar ( & stepConfig . Username , "username" , os . Getenv ( "PIPER_username" ) , "User to authenticate to the ABAP system" )
cmd . Flags ( ) . StringVar ( & stepConfig . Password , "password" , os . Getenv ( "PIPER_password" ) , "Password to authenticate to the ABAP system" )
cmd . Flags ( ) . StringVar ( & stepConfig . Repository , "repository" , os . Getenv ( "PIPER_repository" ) , "Specifies the name (ID) of the local repsitory on the ABAP system" )
2020-09-24 07:41:06 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Host , "host" , os . Getenv ( "PIPER_host" ) , "Specifies the protocol and host address, including the port. Please provide in the format '<protocol>://<host>:<port>'" )
cmd . Flags ( ) . StringVar ( & stepConfig . Client , "client" , os . Getenv ( "PIPER_client" ) , "Specifies the client of the ABAP system to be addressed" )
2020-07-23 20:20:07 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Commit , "commit" , os . Getenv ( "PIPER_commit" ) , "Specifies the commit to deploy" )
cmd . Flags ( ) . StringVar ( & stepConfig . GithubPersonalAccessToken , "githubPersonalAccessToken" , os . Getenv ( "PIPER_githubPersonalAccessToken" ) , "GitHub personal access token with at least read permissions for the remote repository" )
cmd . MarkFlagRequired ( "username" )
cmd . MarkFlagRequired ( "password" )
cmd . MarkFlagRequired ( "repository" )
cmd . MarkFlagRequired ( "host" )
cmd . MarkFlagRequired ( "client" )
}
// retrieve step metadata
func gctsRollbackMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "gctsRollback" ,
Aliases : [ ] config . Alias { } ,
Description : "Perfoms roll back of one (default) or several commit(s)" ,
2020-07-23 20:20:07 +02:00
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
2021-06-16 08:43:30 +02:00
Secrets : [ ] config . StepSecrets {
{ Name : "abapCredentialsId" , Description : "Jenkins credentials ID containing username and password for authentication to the ABAP system on which you want to perform the rollback" , Type : "jenkins" } ,
{ Name : "githubPersonalAccessTokenId" , Description : "GitHub personal access token with at least read permissions for the remote repository" , Type : "jenkins" } ,
} ,
2020-07-23 20:20:07 +02:00
Parameters : [ ] config . StepParameters {
{
2020-09-16 14:50:09 +02:00
Name : "username" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "abapCredentialsId" ,
Param : "username" ,
Type : "secret" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_username" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "password" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "abapCredentialsId" ,
Param : "password" ,
Type : "secret" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_password" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
Name : "repository" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_repository" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
Name : "host" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_host" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
Name : "client" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : true ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_client" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
Name : "commit" ,
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_commit" ) ,
2020-07-23 20:20:07 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "githubPersonalAccessToken" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubPersonalAccessTokenId" ,
Type : "secret" ,
} ,
} ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_githubPersonalAccessToken" ) ,
2020-07-23 20:20:07 +02:00
} ,
} ,
} ,
} ,
}
return theMetaData
}