github.com/jaylevin/jenkins-library@v1.230.4/cmd/isChangeInDevelopment_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 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type isChangeInDevelopmentOptions struct { 21 Endpoint string `json:"endpoint,omitempty"` 22 Username string `json:"username,omitempty"` 23 Password string `json:"password,omitempty"` 24 ChangeDocumentID string `json:"changeDocumentId,omitempty"` 25 FailIfStatusIsNotInDevelopment bool `json:"failIfStatusIsNotInDevelopment,omitempty"` 26 CmClientOpts []string `json:"cmClientOpts,omitempty"` 27 } 28 29 type isChangeInDevelopmentCommonPipelineEnvironment struct { 30 custom struct { 31 isChangeInDevelopment bool 32 } 33 } 34 35 func (p *isChangeInDevelopmentCommonPipelineEnvironment) persist(path, resourceName string) { 36 content := []struct { 37 category string 38 name string 39 value interface{} 40 }{ 41 {category: "custom", name: "isChangeInDevelopment", value: p.custom.isChangeInDevelopment}, 42 } 43 44 errCount := 0 45 for _, param := range content { 46 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 47 if err != nil { 48 log.Entry().WithError(err).Error("Error persisting piper environment.") 49 errCount++ 50 } 51 } 52 if errCount > 0 { 53 log.Entry().Error("failed to persist Piper environment") 54 } 55 } 56 57 // IsChangeInDevelopmentCommand This step checks if a certain change is in status 'in development' 58 func IsChangeInDevelopmentCommand() *cobra.Command { 59 const STEP_NAME = "isChangeInDevelopment" 60 61 metadata := isChangeInDevelopmentMetadata() 62 var stepConfig isChangeInDevelopmentOptions 63 var startTime time.Time 64 var commonPipelineEnvironment isChangeInDevelopmentCommonPipelineEnvironment 65 var logCollector *log.CollectorHook 66 var splunkClient *splunk.Splunk 67 telemetryClient := &telemetry.Telemetry{} 68 69 var createIsChangeInDevelopmentCmd = &cobra.Command{ 70 Use: STEP_NAME, 71 Short: "This step checks if a certain change is in status 'in development'", 72 Long: `This step checks if a certain change is in status 'in development'`, 73 PreRunE: func(cmd *cobra.Command, _ []string) error { 74 startTime = time.Now() 75 log.SetStepName(STEP_NAME) 76 log.SetVerbose(GeneralConfig.Verbose) 77 78 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 79 80 path, _ := os.Getwd() 81 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 82 log.RegisterHook(fatalHook) 83 84 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 85 if err != nil { 86 log.SetErrorCategory(log.ErrorConfiguration) 87 return err 88 } 89 log.RegisterSecret(stepConfig.Username) 90 log.RegisterSecret(stepConfig.Password) 91 92 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 93 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 94 log.RegisterHook(&sentryHook) 95 } 96 97 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 98 splunkClient = &splunk.Splunk{} 99 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 100 log.RegisterHook(logCollector) 101 } 102 103 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 104 if err != nil { 105 return err 106 } 107 if err = validation.ValidateStruct(stepConfig); err != nil { 108 log.SetErrorCategory(log.ErrorConfiguration) 109 return err 110 } 111 112 return nil 113 }, 114 Run: func(_ *cobra.Command, _ []string) { 115 stepTelemetryData := telemetry.CustomData{} 116 stepTelemetryData.ErrorCode = "1" 117 handler := func() { 118 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 119 config.RemoveVaultSecretFiles() 120 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 121 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 122 stepTelemetryData.PiperCommitHash = GitCommit 123 telemetryClient.SetData(&stepTelemetryData) 124 telemetryClient.Send() 125 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 126 splunkClient.Send(telemetryClient.GetData(), logCollector) 127 } 128 } 129 log.DeferExitHandler(handler) 130 defer handler() 131 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 132 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 133 splunkClient.Initialize(GeneralConfig.CorrelationID, 134 GeneralConfig.HookConfig.SplunkConfig.Dsn, 135 GeneralConfig.HookConfig.SplunkConfig.Token, 136 GeneralConfig.HookConfig.SplunkConfig.Index, 137 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 138 } 139 isChangeInDevelopment(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 140 stepTelemetryData.ErrorCode = "0" 141 log.Entry().Info("SUCCESS") 142 }, 143 } 144 145 addIsChangeInDevelopmentFlags(createIsChangeInDevelopmentCmd, &stepConfig) 146 return createIsChangeInDevelopmentCmd 147 } 148 149 func addIsChangeInDevelopmentFlags(cmd *cobra.Command, stepConfig *isChangeInDevelopmentOptions) { 150 cmd.Flags().StringVar(&stepConfig.Endpoint, "endpoint", os.Getenv("PIPER_endpoint"), "The service endpoint") 151 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "Service user to authenticate against the ABAP backend") 152 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Service user password to authenticate against the ABAP backend") 153 cmd.Flags().StringVar(&stepConfig.ChangeDocumentID, "changeDocumentId", os.Getenv("PIPER_changeDocumentId"), "ID of the change document to be checked for the status") 154 cmd.Flags().BoolVar(&stepConfig.FailIfStatusIsNotInDevelopment, "failIfStatusIsNotInDevelopment", true, "lets the build fail in case the change is not in status 'in developent'. Otherwise a warning is emitted to the log") 155 cmd.Flags().StringSliceVar(&stepConfig.CmClientOpts, "cmClientOpts", []string{}, "additional options passed to cm client, e.g. for troubleshooting") 156 157 cmd.MarkFlagRequired("endpoint") 158 cmd.MarkFlagRequired("username") 159 cmd.MarkFlagRequired("password") 160 cmd.MarkFlagRequired("changeDocumentId") 161 } 162 163 // retrieve step metadata 164 func isChangeInDevelopmentMetadata() config.StepData { 165 var theMetaData = config.StepData{ 166 Metadata: config.StepMetadata{ 167 Name: "isChangeInDevelopment", 168 Aliases: []config.Alias{}, 169 Description: "This step checks if a certain change is in status 'in development'", 170 }, 171 Spec: config.StepSpec{ 172 Inputs: config.StepInputs{ 173 Secrets: []config.StepSecrets{ 174 {Name: "credentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate against the ABAP backend", Type: "jenkins", Aliases: []config.Alias{{Name: "changeManagement/credentialsId", Deprecated: false}}}, 175 }, 176 Parameters: []config.StepParameters{ 177 { 178 Name: "endpoint", 179 ResourceRef: []config.ResourceReference{}, 180 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 181 Type: "string", 182 Mandatory: true, 183 Aliases: []config.Alias{{Name: "changeManagement/endpoint"}}, 184 Default: os.Getenv("PIPER_endpoint"), 185 }, 186 { 187 Name: "username", 188 ResourceRef: []config.ResourceReference{ 189 { 190 Name: "credentialsId", 191 Param: "username", 192 Type: "secret", 193 }, 194 }, 195 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 196 Type: "string", 197 Mandatory: true, 198 Aliases: []config.Alias{}, 199 Default: os.Getenv("PIPER_username"), 200 }, 201 { 202 Name: "password", 203 ResourceRef: []config.ResourceReference{ 204 { 205 Name: "credentialsId", 206 Param: "password", 207 Type: "secret", 208 }, 209 }, 210 Scope: []string{"PARAMETERS"}, 211 Type: "string", 212 Mandatory: true, 213 Aliases: []config.Alias{}, 214 Default: os.Getenv("PIPER_password"), 215 }, 216 { 217 Name: "changeDocumentId", 218 ResourceRef: []config.ResourceReference{ 219 { 220 Name: "commonPipelineEnvironment", 221 Param: "custom/changeDocumentId", 222 }, 223 }, 224 Scope: []string{"PARAMETERS"}, 225 Type: "string", 226 Mandatory: true, 227 Aliases: []config.Alias{}, 228 Default: os.Getenv("PIPER_changeDocumentId"), 229 }, 230 { 231 Name: "failIfStatusIsNotInDevelopment", 232 ResourceRef: []config.ResourceReference{}, 233 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 234 Type: "bool", 235 Mandatory: false, 236 Aliases: []config.Alias{}, 237 Default: true, 238 }, 239 { 240 Name: "cmClientOpts", 241 ResourceRef: []config.ResourceReference{}, 242 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 243 Type: "[]string", 244 Mandatory: false, 245 Aliases: []config.Alias{{Name: "clientOpts"}, {Name: "changeManagement/clientOpts"}}, 246 Default: []string{}, 247 }, 248 }, 249 }, 250 Containers: []config.Container{ 251 {Name: "cmclient", Image: "ppiper/cm-client:3.0.0.0"}, 252 }, 253 Outputs: config.StepOutputs{ 254 Resources: []config.StepResources{ 255 { 256 Name: "commonPipelineEnvironment", 257 Type: "piperEnvironment", 258 Parameters: []map[string]interface{}{ 259 {"name": "custom/isChangeInDevelopment", "type": "bool"}, 260 }, 261 }, 262 }, 263 }, 264 }, 265 } 266 return theMetaData 267 }