github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/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 QueryParameters map[string]interface{} `json:"queryParameters,omitempty"` 27 SkipSSLVerification bool `json:"skipSSLVerification,omitempty"` 28 } 29 30 // GctsRollbackCommand Perfoms a rollback of one (default) or several commits 31 func GctsRollbackCommand() *cobra.Command { 32 const STEP_NAME = "gctsRollback" 33 34 metadata := gctsRollbackMetadata() 35 var stepConfig gctsRollbackOptions 36 var startTime time.Time 37 var logCollector *log.CollectorHook 38 var splunkClient *splunk.Splunk 39 telemetryClient := &telemetry.Telemetry{} 40 41 var createGctsRollbackCmd = &cobra.Command{ 42 Use: STEP_NAME, 43 Short: "Perfoms a rollback of one (default) or several commits", 44 Long: `This step performs a rollback of commits in a local ABAP repository. If a ` + "`" + `commit` + "`" + ` parameter is specified, it will be used as the target commit for the rollback. 45 If no ` + "`" + `commit` + "`" + ` parameter is specified and the remote repository domain is 'github.com', the last commit with the status 'success' will be used for the rollback. Otherwise, 46 ` + "`" + `gctsRollback` + "`" + ` will roll back to the previously active commit in the local repository.`, 47 PreRunE: func(cmd *cobra.Command, _ []string) error { 48 startTime = time.Now() 49 log.SetStepName(STEP_NAME) 50 log.SetVerbose(GeneralConfig.Verbose) 51 52 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 53 54 path, _ := os.Getwd() 55 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 56 log.RegisterHook(fatalHook) 57 58 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 59 if err != nil { 60 log.SetErrorCategory(log.ErrorConfiguration) 61 return err 62 } 63 log.RegisterSecret(stepConfig.Username) 64 log.RegisterSecret(stepConfig.Password) 65 log.RegisterSecret(stepConfig.GithubPersonalAccessToken) 66 67 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 68 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 69 log.RegisterHook(&sentryHook) 70 } 71 72 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 73 splunkClient = &splunk.Splunk{} 74 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 75 log.RegisterHook(logCollector) 76 } 77 78 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 79 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 80 } 81 82 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 83 if err != nil { 84 return err 85 } 86 if err = validation.ValidateStruct(stepConfig); err != nil { 87 log.SetErrorCategory(log.ErrorConfiguration) 88 return err 89 } 90 91 return nil 92 }, 93 Run: func(_ *cobra.Command, _ []string) { 94 stepTelemetryData := telemetry.CustomData{} 95 stepTelemetryData.ErrorCode = "1" 96 handler := func() { 97 config.RemoveVaultSecretFiles() 98 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 99 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 100 stepTelemetryData.PiperCommitHash = GitCommit 101 telemetryClient.SetData(&stepTelemetryData) 102 telemetryClient.Send() 103 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 104 splunkClient.Initialize(GeneralConfig.CorrelationID, 105 GeneralConfig.HookConfig.SplunkConfig.Dsn, 106 GeneralConfig.HookConfig.SplunkConfig.Token, 107 GeneralConfig.HookConfig.SplunkConfig.Index, 108 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 109 splunkClient.Send(telemetryClient.GetData(), logCollector) 110 } 111 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 112 splunkClient.Initialize(GeneralConfig.CorrelationID, 113 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 114 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 115 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 116 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 117 splunkClient.Send(telemetryClient.GetData(), logCollector) 118 } 119 } 120 log.DeferExitHandler(handler) 121 defer handler() 122 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 123 gctsRollback(stepConfig, &stepTelemetryData) 124 stepTelemetryData.ErrorCode = "0" 125 log.Entry().Info("SUCCESS") 126 }, 127 } 128 129 addGctsRollbackFlags(createGctsRollbackCmd, &stepConfig) 130 return createGctsRollbackCmd 131 } 132 133 func addGctsRollbackFlags(cmd *cobra.Command, stepConfig *gctsRollbackOptions) { 134 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User that authenticates to the ABAP system. **Note** - Don't provide this parameter directly. Either set it in the environment, or in the Jenkins credentials store, and provide the ID as value of the `abapCredentialsId` parameter.") 135 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password of the ABAP user that authenticates to the ABAP system. **Note** - DonĀ“t provide this parameter directly. Either set it in the environment, or in the Jenkins credentials store, and provide the ID as value of the `abapCredentialsId` parameter.") 136 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repsitory on the ABAP system") 137 cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Protocol and host of the ABAP system, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.") 138 cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Specifies the client of the ABAP system to be addressed") 139 cmd.Flags().StringVar(&stepConfig.Commit, "commit", os.Getenv("PIPER_commit"), "Specifies the target commit for the rollback") 140 cmd.Flags().StringVar(&stepConfig.GithubPersonalAccessToken, "githubPersonalAccessToken", os.Getenv("PIPER_githubPersonalAccessToken"), "GitHub personal access token with at least read permissions for the remote repository") 141 142 cmd.Flags().BoolVar(&stepConfig.SkipSSLVerification, "skipSSLVerification", false, "You can skip the SSL (Secure Socket Layer) verification for the http client") 143 144 cmd.MarkFlagRequired("username") 145 cmd.MarkFlagRequired("password") 146 cmd.MarkFlagRequired("repository") 147 cmd.MarkFlagRequired("host") 148 cmd.MarkFlagRequired("client") 149 } 150 151 // retrieve step metadata 152 func gctsRollbackMetadata() config.StepData { 153 var theMetaData = config.StepData{ 154 Metadata: config.StepMetadata{ 155 Name: "gctsRollback", 156 Aliases: []config.Alias{}, 157 Description: "Perfoms a rollback of one (default) or several commits", 158 }, 159 Spec: config.StepSpec{ 160 Inputs: config.StepInputs{ 161 Secrets: []config.StepSecrets{ 162 {Name: "abapCredentialsId", Description: "ID taken from the Jenkins credentials store containing user name and password of the user that authenticates to the ABAP system on which you want to execute the rollback.", Type: "jenkins"}, 163 {Name: "githubPersonalAccessTokenId", Description: "GitHub personal access token with at least read permissions for the remote repository", Type: "jenkins"}, 164 }, 165 Parameters: []config.StepParameters{ 166 { 167 Name: "username", 168 ResourceRef: []config.ResourceReference{ 169 { 170 Name: "abapCredentialsId", 171 Param: "username", 172 Type: "secret", 173 }, 174 }, 175 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 176 Type: "string", 177 Mandatory: true, 178 Aliases: []config.Alias{}, 179 Default: os.Getenv("PIPER_username"), 180 }, 181 { 182 Name: "password", 183 ResourceRef: []config.ResourceReference{ 184 { 185 Name: "abapCredentialsId", 186 Param: "password", 187 Type: "secret", 188 }, 189 }, 190 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 191 Type: "string", 192 Mandatory: true, 193 Aliases: []config.Alias{}, 194 Default: os.Getenv("PIPER_password"), 195 }, 196 { 197 Name: "repository", 198 ResourceRef: []config.ResourceReference{}, 199 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 200 Type: "string", 201 Mandatory: true, 202 Aliases: []config.Alias{}, 203 Default: os.Getenv("PIPER_repository"), 204 }, 205 { 206 Name: "host", 207 ResourceRef: []config.ResourceReference{}, 208 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 209 Type: "string", 210 Mandatory: true, 211 Aliases: []config.Alias{}, 212 Default: os.Getenv("PIPER_host"), 213 }, 214 { 215 Name: "client", 216 ResourceRef: []config.ResourceReference{}, 217 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 218 Type: "string", 219 Mandatory: true, 220 Aliases: []config.Alias{}, 221 Default: os.Getenv("PIPER_client"), 222 }, 223 { 224 Name: "commit", 225 ResourceRef: []config.ResourceReference{}, 226 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 227 Type: "string", 228 Mandatory: false, 229 Aliases: []config.Alias{}, 230 Default: os.Getenv("PIPER_commit"), 231 }, 232 { 233 Name: "githubPersonalAccessToken", 234 ResourceRef: []config.ResourceReference{ 235 { 236 Name: "githubPersonalAccessTokenId", 237 Type: "secret", 238 }, 239 }, 240 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 241 Type: "string", 242 Mandatory: false, 243 Aliases: []config.Alias{}, 244 Default: os.Getenv("PIPER_githubPersonalAccessToken"), 245 }, 246 { 247 Name: "queryParameters", 248 ResourceRef: []config.ResourceReference{}, 249 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 250 Type: "map[string]interface{}", 251 Mandatory: false, 252 Aliases: []config.Alias{}, 253 }, 254 { 255 Name: "skipSSLVerification", 256 ResourceRef: []config.ResourceReference{}, 257 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 258 Type: "bool", 259 Mandatory: false, 260 Aliases: []config.Alias{}, 261 Default: false, 262 }, 263 }, 264 }, 265 }, 266 } 267 return theMetaData 268 }