github.com/jaylevin/jenkins-library@v1.230.4/cmd/gctsExecuteABAPUnitTests_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 gctsExecuteABAPUnitTestsOptions struct { 19 Username string `json:"username,omitempty"` 20 Password string `json:"password,omitempty"` 21 Host string `json:"host,omitempty"` 22 Repository string `json:"repository,omitempty"` 23 Client string `json:"client,omitempty"` 24 AUnitTest bool `json:"aUnitTest,omitempty"` 25 AtcCheck bool `json:"atcCheck,omitempty"` 26 AtcVariant string `json:"atcVariant,omitempty"` 27 Scope string `json:"scope,omitempty"` 28 Commit string `json:"commit,omitempty"` 29 Workspace string `json:"workspace,omitempty"` 30 AtcResultsFileName string `json:"atcResultsFileName,omitempty"` 31 AUnitResultsFileName string `json:"aUnitResultsFileName,omitempty"` 32 } 33 34 // GctsExecuteABAPUnitTestsCommand Runs ABAP unit tests and ATC (ABAP Test Cockpit) checks for a specified object scope. 35 func GctsExecuteABAPUnitTestsCommand() *cobra.Command { 36 const STEP_NAME = "gctsExecuteABAPUnitTests" 37 38 metadata := gctsExecuteABAPUnitTestsMetadata() 39 var stepConfig gctsExecuteABAPUnitTestsOptions 40 var startTime time.Time 41 var logCollector *log.CollectorHook 42 var splunkClient *splunk.Splunk 43 telemetryClient := &telemetry.Telemetry{} 44 45 var createGctsExecuteABAPUnitTestsCmd = &cobra.Command{ 46 Use: STEP_NAME, 47 Short: "Runs ABAP unit tests and ATC (ABAP Test Cockpit) checks for a specified object scope.", 48 Long: `This step executes ABAP unit test and ATC checks for a specified scope of objects that exist in a local Git repository on an ABAP system.`, 49 PreRunE: func(cmd *cobra.Command, _ []string) error { 50 startTime = time.Now() 51 log.SetStepName(STEP_NAME) 52 log.SetVerbose(GeneralConfig.Verbose) 53 54 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 55 56 path, _ := os.Getwd() 57 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 58 log.RegisterHook(fatalHook) 59 60 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 61 if err != nil { 62 log.SetErrorCategory(log.ErrorConfiguration) 63 return err 64 } 65 log.RegisterSecret(stepConfig.Username) 66 log.RegisterSecret(stepConfig.Password) 67 68 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 69 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 70 log.RegisterHook(&sentryHook) 71 } 72 73 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 74 splunkClient = &splunk.Splunk{} 75 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 76 log.RegisterHook(logCollector) 77 } 78 79 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 80 if err != nil { 81 return err 82 } 83 if err = validation.ValidateStruct(stepConfig); err != nil { 84 log.SetErrorCategory(log.ErrorConfiguration) 85 return err 86 } 87 88 return nil 89 }, 90 Run: func(_ *cobra.Command, _ []string) { 91 stepTelemetryData := telemetry.CustomData{} 92 stepTelemetryData.ErrorCode = "1" 93 handler := func() { 94 config.RemoveVaultSecretFiles() 95 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 96 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 97 stepTelemetryData.PiperCommitHash = GitCommit 98 telemetryClient.SetData(&stepTelemetryData) 99 telemetryClient.Send() 100 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 101 splunkClient.Send(telemetryClient.GetData(), logCollector) 102 } 103 } 104 log.DeferExitHandler(handler) 105 defer handler() 106 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 107 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 108 splunkClient.Initialize(GeneralConfig.CorrelationID, 109 GeneralConfig.HookConfig.SplunkConfig.Dsn, 110 GeneralConfig.HookConfig.SplunkConfig.Token, 111 GeneralConfig.HookConfig.SplunkConfig.Index, 112 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 113 } 114 gctsExecuteABAPUnitTests(stepConfig, &stepTelemetryData) 115 stepTelemetryData.ErrorCode = "0" 116 log.Entry().Info("SUCCESS") 117 }, 118 } 119 120 addGctsExecuteABAPUnitTestsFlags(createGctsExecuteABAPUnitTestsCmd, &stepConfig) 121 return createGctsExecuteABAPUnitTestsCmd 122 } 123 124 func addGctsExecuteABAPUnitTestsFlags(cmd *cobra.Command, stepConfig *gctsExecuteABAPUnitTestsOptions) { 125 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.") 126 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.") 127 cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Protocol and host of the ABAP system, including the port. Please provide it in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.") 128 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name (ID) of the local repository on the ABAP system") 129 cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Client of the ABAP system in which you want to execute the checks") 130 cmd.Flags().BoolVar(&stepConfig.AUnitTest, "aUnitTest", true, "Indication whether you want to execute the ABAP Unit tests. If the ABAP Unit tests fail, the results are mapped as follows to the statuses available in the Jenkins plugin. (Successful ABAP unit test results are not displayed.)\n<br/>\n- Failed with severity `fatal` is displayed as an `Error`.\n<br/>\n- Failed with severity `critical` is displayed as an `Error`.\n<br/>\n- Failed with severity `warning` is displayed as a `Warning`.\n") 131 cmd.Flags().BoolVar(&stepConfig.AtcCheck, "atcCheck", true, "Indication whether you want to execute the ATC checks. If the ATC checks result in errors with priorities 1 and 2, they are considered as failed. The results are mapped as follows to the statuses available in the Jenkins plugin. (Successful ATC check results are not displayed.)\n<br/>\n- Priorities 1 and 2 are displayed as an `Error`.\n<br/>\n- Priority 3 is displayed as a `Warning`.\n") 132 cmd.Flags().StringVar(&stepConfig.AtcVariant, "atcVariant", `DEFAULT`, "Variant for ATC checks") 133 cmd.Flags().StringVar(&stepConfig.Scope, "scope", `repository`, "Scope of objects for which you want to execute the checks:\n<br/>\n- `localChangedObjects`: The object scope is derived from the last activity in the local repository. The checks are executed for the individual objects.\n<br/>\n- `remoteChangedObjects`: The object scope is the delta between the commit that triggered the pipeline and the current commit in the remote repository. The checks are executed for the individual objects.\n<br/>\n- `localChangedPackages`: The object scope is derived from the last activity in the local repository. All objects are resolved into packages. The checks are executed for the packages.\n<br/>\n- `remoteChangedPackages`: The object scope is the delta between the commit that triggered the pipeline and the current commit in the remote repository. All objects are resolved into packages. The checks are executed for the packages.\n<br/>\n- `repository`: The object scope comprises all objects that are part of the local repository. The checks are executed for the individual objects. Packages (DEVC) are excluded. This is the default scope.\n<br/>\n- `packages`: The object scope comprises all packages that are part of the local repository. The checks are executed for the packages.\n") 134 cmd.Flags().StringVar(&stepConfig.Commit, "commit", os.Getenv("PIPER_commit"), "ID of the commit that triggered the pipeline or any other commit used to calculate the object scope. Specifying a commit is mandatory for the `remoteChangedObjects` and `remoteChangedPackages` scopes.") 135 cmd.Flags().StringVar(&stepConfig.Workspace, "workspace", os.Getenv("PIPER_workspace"), "Absolute path to the directory that contains the source code that your CI/CD tool checks out. For example, in Jenkins, the workspace parameter is `/var/jenkins_home/workspace/<jobName>/`. As an alternative, you can use Jenkins's predefined environmental variable `WORKSPACE`.") 136 cmd.Flags().StringVar(&stepConfig.AtcResultsFileName, "atcResultsFileName", `ATCResults.xml`, "Specifies an output file name for the results of the ATC checks.") 137 cmd.Flags().StringVar(&stepConfig.AUnitResultsFileName, "aUnitResultsFileName", `AUnitResults.xml`, "Specifies an output file name for the results of the ABAP Unit tests.") 138 139 cmd.MarkFlagRequired("username") 140 cmd.MarkFlagRequired("password") 141 cmd.MarkFlagRequired("host") 142 cmd.MarkFlagRequired("repository") 143 cmd.MarkFlagRequired("client") 144 cmd.MarkFlagRequired("workspace") 145 } 146 147 // retrieve step metadata 148 func gctsExecuteABAPUnitTestsMetadata() config.StepData { 149 var theMetaData = config.StepData{ 150 Metadata: config.StepMetadata{ 151 Name: "gctsExecuteABAPUnitTests", 152 Aliases: []config.Alias{}, 153 Description: "Runs ABAP unit tests and ATC (ABAP Test Cockpit) checks for a specified object scope.", 154 }, 155 Spec: config.StepSpec{ 156 Inputs: config.StepInputs{ 157 Secrets: []config.StepSecrets{ 158 {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 checks.", Type: "jenkins"}, 159 }, 160 Parameters: []config.StepParameters{ 161 { 162 Name: "username", 163 ResourceRef: []config.ResourceReference{ 164 { 165 Name: "abapCredentialsId", 166 Param: "username", 167 Type: "secret", 168 }, 169 }, 170 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 171 Type: "string", 172 Mandatory: true, 173 Aliases: []config.Alias{}, 174 Default: os.Getenv("PIPER_username"), 175 }, 176 { 177 Name: "password", 178 ResourceRef: []config.ResourceReference{ 179 { 180 Name: "abapCredentialsId", 181 Param: "password", 182 Type: "secret", 183 }, 184 }, 185 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 186 Type: "string", 187 Mandatory: true, 188 Aliases: []config.Alias{}, 189 Default: os.Getenv("PIPER_password"), 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: "repository", 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_repository"), 208 }, 209 { 210 Name: "client", 211 ResourceRef: []config.ResourceReference{}, 212 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 213 Type: "string", 214 Mandatory: true, 215 Aliases: []config.Alias{}, 216 Default: os.Getenv("PIPER_client"), 217 }, 218 { 219 Name: "aUnitTest", 220 ResourceRef: []config.ResourceReference{}, 221 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 222 Type: "bool", 223 Mandatory: false, 224 Aliases: []config.Alias{}, 225 Default: true, 226 }, 227 { 228 Name: "atcCheck", 229 ResourceRef: []config.ResourceReference{}, 230 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 231 Type: "bool", 232 Mandatory: false, 233 Aliases: []config.Alias{}, 234 Default: true, 235 }, 236 { 237 Name: "atcVariant", 238 ResourceRef: []config.ResourceReference{}, 239 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 240 Type: "string", 241 Mandatory: false, 242 Aliases: []config.Alias{}, 243 Default: `DEFAULT`, 244 }, 245 { 246 Name: "scope", 247 ResourceRef: []config.ResourceReference{}, 248 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 249 Type: "string", 250 Mandatory: false, 251 Aliases: []config.Alias{}, 252 Default: `repository`, 253 }, 254 { 255 Name: "commit", 256 ResourceRef: []config.ResourceReference{}, 257 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 258 Type: "string", 259 Mandatory: false, 260 Aliases: []config.Alias{}, 261 Default: os.Getenv("PIPER_commit"), 262 }, 263 { 264 Name: "workspace", 265 ResourceRef: []config.ResourceReference{}, 266 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 267 Type: "string", 268 Mandatory: true, 269 Aliases: []config.Alias{}, 270 Default: os.Getenv("PIPER_workspace"), 271 }, 272 { 273 Name: "atcResultsFileName", 274 ResourceRef: []config.ResourceReference{}, 275 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 276 Type: "string", 277 Mandatory: false, 278 Aliases: []config.Alias{}, 279 Default: `ATCResults.xml`, 280 }, 281 { 282 Name: "aUnitResultsFileName", 283 ResourceRef: []config.ResourceReference{}, 284 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 285 Type: "string", 286 Mandatory: false, 287 Aliases: []config.Alias{}, 288 Default: `AUnitResults.xml`, 289 }, 290 }, 291 }, 292 }, 293 } 294 return theMetaData 295 }