github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsRollback_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type gctsRollbackOptions struct {
    19  	Username                  string `json:"username,omitempty"`
    20  	Password                  string `json:"password,omitempty"`
    21  	Repository                string `json:"repository,omitempty"`
    22  	Host                      string `json:"host,omitempty"`
    23  	Client                    string `json:"client,omitempty"`
    24  	Commit                    string `json:"commit,omitempty"`
    25  	GithubPersonalAccessToken string `json:"githubPersonalAccessToken,omitempty"`
    26  }
    27  
    28  // GctsRollbackCommand Perfoms roll back of one (default) or several commit(s)
    29  func GctsRollbackCommand() *cobra.Command {
    30  	const STEP_NAME = "gctsRollback"
    31  
    32  	metadata := gctsRollbackMetadata()
    33  	var stepConfig gctsRollbackOptions
    34  	var startTime time.Time
    35  	var logCollector *log.CollectorHook
    36  	var splunkClient *splunk.Splunk
    37  	telemetryClient := &telemetry.Telemetry{}
    38  
    39  	var createGctsRollbackCmd = &cobra.Command{
    40  		Use:   STEP_NAME,
    41  		Short: "Perfoms roll back of one (default) or several commit(s)",
    42  		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.
    43  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,
    44  gctsRollback will rollback to the previously active commit in the local repository.`,
    45  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    46  			startTime = time.Now()
    47  			log.SetStepName(STEP_NAME)
    48  			log.SetVerbose(GeneralConfig.Verbose)
    49  
    50  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    51  
    52  			path, _ := os.Getwd()
    53  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    54  			log.RegisterHook(fatalHook)
    55  
    56  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    57  			if err != nil {
    58  				log.SetErrorCategory(log.ErrorConfiguration)
    59  				return err
    60  			}
    61  			log.RegisterSecret(stepConfig.Username)
    62  			log.RegisterSecret(stepConfig.Password)
    63  			log.RegisterSecret(stepConfig.GithubPersonalAccessToken)
    64  
    65  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    66  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    67  				log.RegisterHook(&sentryHook)
    68  			}
    69  
    70  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    71  				splunkClient = &splunk.Splunk{}
    72  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    73  				log.RegisterHook(logCollector)
    74  			}
    75  
    76  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    77  			if err != nil {
    78  				return err
    79  			}
    80  			if err = validation.ValidateStruct(stepConfig); err != nil {
    81  				log.SetErrorCategory(log.ErrorConfiguration)
    82  				return err
    83  			}
    84  
    85  			return nil
    86  		},
    87  		Run: func(_ *cobra.Command, _ []string) {
    88  			stepTelemetryData := telemetry.CustomData{}
    89  			stepTelemetryData.ErrorCode = "1"
    90  			handler := func() {
    91  				config.RemoveVaultSecretFiles()
    92  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    93  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    94  				stepTelemetryData.PiperCommitHash = GitCommit
    95  				telemetryClient.SetData(&stepTelemetryData)
    96  				telemetryClient.Send()
    97  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    98  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    99  				}
   100  			}
   101  			log.DeferExitHandler(handler)
   102  			defer handler()
   103  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   104  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   105  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   106  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   107  					GeneralConfig.HookConfig.SplunkConfig.Token,
   108  					GeneralConfig.HookConfig.SplunkConfig.Index,
   109  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   110  			}
   111  			gctsRollback(stepConfig, &stepTelemetryData)
   112  			stepTelemetryData.ErrorCode = "0"
   113  			log.Entry().Info("SUCCESS")
   114  		},
   115  	}
   116  
   117  	addGctsRollbackFlags(createGctsRollbackCmd, &stepConfig)
   118  	return createGctsRollbackCmd
   119  }
   120  
   121  func addGctsRollbackFlags(cmd *cobra.Command, stepConfig *gctsRollbackOptions) {
   122  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User to authenticate to the ABAP system")
   123  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password to authenticate to the ABAP system")
   124  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repsitory on the ABAP system")
   125  	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>'")
   126  	cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Specifies the client of the ABAP system to be addressed")
   127  	cmd.Flags().StringVar(&stepConfig.Commit, "commit", os.Getenv("PIPER_commit"), "Specifies the commit to deploy")
   128  	cmd.Flags().StringVar(&stepConfig.GithubPersonalAccessToken, "githubPersonalAccessToken", os.Getenv("PIPER_githubPersonalAccessToken"), "GitHub personal access token with at least read permissions for the remote repository")
   129  
   130  	cmd.MarkFlagRequired("username")
   131  	cmd.MarkFlagRequired("password")
   132  	cmd.MarkFlagRequired("repository")
   133  	cmd.MarkFlagRequired("host")
   134  	cmd.MarkFlagRequired("client")
   135  }
   136  
   137  // retrieve step metadata
   138  func gctsRollbackMetadata() config.StepData {
   139  	var theMetaData = config.StepData{
   140  		Metadata: config.StepMetadata{
   141  			Name:        "gctsRollback",
   142  			Aliases:     []config.Alias{},
   143  			Description: "Perfoms roll back of one (default) or several commit(s)",
   144  		},
   145  		Spec: config.StepSpec{
   146  			Inputs: config.StepInputs{
   147  				Secrets: []config.StepSecrets{
   148  					{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"},
   149  					{Name: "githubPersonalAccessTokenId", Description: "GitHub personal access token with at least read permissions for the remote repository", Type: "jenkins"},
   150  				},
   151  				Parameters: []config.StepParameters{
   152  					{
   153  						Name: "username",
   154  						ResourceRef: []config.ResourceReference{
   155  							{
   156  								Name:  "abapCredentialsId",
   157  								Param: "username",
   158  								Type:  "secret",
   159  							},
   160  						},
   161  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   162  						Type:      "string",
   163  						Mandatory: true,
   164  						Aliases:   []config.Alias{},
   165  						Default:   os.Getenv("PIPER_username"),
   166  					},
   167  					{
   168  						Name: "password",
   169  						ResourceRef: []config.ResourceReference{
   170  							{
   171  								Name:  "abapCredentialsId",
   172  								Param: "password",
   173  								Type:  "secret",
   174  							},
   175  						},
   176  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   177  						Type:      "string",
   178  						Mandatory: true,
   179  						Aliases:   []config.Alias{},
   180  						Default:   os.Getenv("PIPER_password"),
   181  					},
   182  					{
   183  						Name:        "repository",
   184  						ResourceRef: []config.ResourceReference{},
   185  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   186  						Type:        "string",
   187  						Mandatory:   true,
   188  						Aliases:     []config.Alias{},
   189  						Default:     os.Getenv("PIPER_repository"),
   190  					},
   191  					{
   192  						Name:        "host",
   193  						ResourceRef: []config.ResourceReference{},
   194  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   195  						Type:        "string",
   196  						Mandatory:   true,
   197  						Aliases:     []config.Alias{},
   198  						Default:     os.Getenv("PIPER_host"),
   199  					},
   200  					{
   201  						Name:        "client",
   202  						ResourceRef: []config.ResourceReference{},
   203  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   204  						Type:        "string",
   205  						Mandatory:   true,
   206  						Aliases:     []config.Alias{},
   207  						Default:     os.Getenv("PIPER_client"),
   208  					},
   209  					{
   210  						Name:        "commit",
   211  						ResourceRef: []config.ResourceReference{},
   212  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   213  						Type:        "string",
   214  						Mandatory:   false,
   215  						Aliases:     []config.Alias{},
   216  						Default:     os.Getenv("PIPER_commit"),
   217  					},
   218  					{
   219  						Name: "githubPersonalAccessToken",
   220  						ResourceRef: []config.ResourceReference{
   221  							{
   222  								Name: "githubPersonalAccessTokenId",
   223  								Type: "secret",
   224  							},
   225  						},
   226  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   227  						Type:      "string",
   228  						Mandatory: false,
   229  						Aliases:   []config.Alias{},
   230  						Default:   os.Getenv("PIPER_githubPersonalAccessToken"),
   231  					},
   232  				},
   233  			},
   234  		},
   235  	}
   236  	return theMetaData
   237  }