github.com/SAP/jenkins-library@v1.362.0/cmd/mavenExecuteStaticCodeChecks_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 mavenExecuteStaticCodeChecksOptions struct { 19 SpotBugs bool `json:"spotBugs,omitempty"` 20 Pmd bool `json:"pmd,omitempty"` 21 MavenModulesExcludes []string `json:"mavenModulesExcludes,omitempty"` 22 SpotBugsExcludeFilterFile string `json:"spotBugsExcludeFilterFile,omitempty"` 23 SpotBugsIncludeFilterFile string `json:"spotBugsIncludeFilterFile,omitempty"` 24 SpotBugsMaxAllowedViolations int `json:"spotBugsMaxAllowedViolations,omitempty"` 25 PmdFailurePriority int `json:"pmdFailurePriority,omitempty"` 26 PmdMaxAllowedViolations int `json:"pmdMaxAllowedViolations,omitempty"` 27 ProjectSettingsFile string `json:"projectSettingsFile,omitempty"` 28 GlobalSettingsFile string `json:"globalSettingsFile,omitempty"` 29 M2Path string `json:"m2Path,omitempty"` 30 LogSuccessfulMavenTransfers bool `json:"logSuccessfulMavenTransfers,omitempty"` 31 InstallArtifacts bool `json:"installArtifacts,omitempty"` 32 } 33 34 // MavenExecuteStaticCodeChecksCommand Execute static code checks for Maven based projects. The plugins SpotBugs and PMD are used. 35 func MavenExecuteStaticCodeChecksCommand() *cobra.Command { 36 const STEP_NAME = "mavenExecuteStaticCodeChecks" 37 38 metadata := mavenExecuteStaticCodeChecksMetadata() 39 var stepConfig mavenExecuteStaticCodeChecksOptions 40 var startTime time.Time 41 var logCollector *log.CollectorHook 42 var splunkClient *splunk.Splunk 43 telemetryClient := &telemetry.Telemetry{} 44 45 var createMavenExecuteStaticCodeChecksCmd = &cobra.Command{ 46 Use: STEP_NAME, 47 Short: "Execute static code checks for Maven based projects. The plugins SpotBugs and PMD are used.", 48 Long: `Executes Spotbugs Maven plugin as well as Pmd Maven plugin for static code checks. 49 SpotBugs is a program to find bugs in Java programs. It looks for instances of “bug patterns” — code instances that are likely to be errors. 50 For more information please visit https://spotbugs.readthedocs.io/en/latest/maven.html 51 PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It supports Java, JavaScript, Salesforce.com Apex and Visualforce, PLSQL, Apache Velocity, XML, XSL. 52 For more information please visit https://pmd.github.io/. 53 The plugins should be configured in the respective pom.xml. 54 For SpotBugs include- and exclude filters as well as maximum allowed violations are conifgurable via .pipeline/config.yml. 55 For PMD the failure priority and the max allowed violations are configurable via .pipeline/config.yml.`, 56 PreRunE: func(cmd *cobra.Command, _ []string) error { 57 startTime = time.Now() 58 log.SetStepName(STEP_NAME) 59 log.SetVerbose(GeneralConfig.Verbose) 60 61 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 62 63 path, _ := os.Getwd() 64 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 65 log.RegisterHook(fatalHook) 66 67 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 68 if err != nil { 69 log.SetErrorCategory(log.ErrorConfiguration) 70 return err 71 } 72 73 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 74 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 75 log.RegisterHook(&sentryHook) 76 } 77 78 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 79 splunkClient = &splunk.Splunk{} 80 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 81 log.RegisterHook(logCollector) 82 } 83 84 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 85 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 86 } 87 88 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 89 if err != nil { 90 return err 91 } 92 if err = validation.ValidateStruct(stepConfig); err != nil { 93 log.SetErrorCategory(log.ErrorConfiguration) 94 return err 95 } 96 97 return nil 98 }, 99 Run: func(_ *cobra.Command, _ []string) { 100 stepTelemetryData := telemetry.CustomData{} 101 stepTelemetryData.ErrorCode = "1" 102 handler := func() { 103 config.RemoveVaultSecretFiles() 104 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 105 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 106 stepTelemetryData.PiperCommitHash = GitCommit 107 telemetryClient.SetData(&stepTelemetryData) 108 telemetryClient.Send() 109 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 110 splunkClient.Initialize(GeneralConfig.CorrelationID, 111 GeneralConfig.HookConfig.SplunkConfig.Dsn, 112 GeneralConfig.HookConfig.SplunkConfig.Token, 113 GeneralConfig.HookConfig.SplunkConfig.Index, 114 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 115 splunkClient.Send(telemetryClient.GetData(), logCollector) 116 } 117 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 118 splunkClient.Initialize(GeneralConfig.CorrelationID, 119 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 120 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 121 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 122 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 123 splunkClient.Send(telemetryClient.GetData(), logCollector) 124 } 125 } 126 log.DeferExitHandler(handler) 127 defer handler() 128 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token) 129 mavenExecuteStaticCodeChecks(stepConfig, &stepTelemetryData) 130 stepTelemetryData.ErrorCode = "0" 131 log.Entry().Info("SUCCESS") 132 }, 133 } 134 135 addMavenExecuteStaticCodeChecksFlags(createMavenExecuteStaticCodeChecksCmd, &stepConfig) 136 return createMavenExecuteStaticCodeChecksCmd 137 } 138 139 func addMavenExecuteStaticCodeChecksFlags(cmd *cobra.Command, stepConfig *mavenExecuteStaticCodeChecksOptions) { 140 cmd.Flags().BoolVar(&stepConfig.SpotBugs, "spotBugs", true, "Parameter to turn off SpotBugs.") 141 cmd.Flags().BoolVar(&stepConfig.Pmd, "pmd", true, "Parameter to turn off PMD.") 142 cmd.Flags().StringSliceVar(&stepConfig.MavenModulesExcludes, "mavenModulesExcludes", []string{}, "Maven modules which should be excluded by the static code checks. By default the modules 'unit-tests' and 'integration-tests' will be excluded.") 143 cmd.Flags().StringVar(&stepConfig.SpotBugsExcludeFilterFile, "spotBugsExcludeFilterFile", os.Getenv("PIPER_spotBugsExcludeFilterFile"), "Path to a filter file with bug definitions which should be excluded.") 144 cmd.Flags().StringVar(&stepConfig.SpotBugsIncludeFilterFile, "spotBugsIncludeFilterFile", os.Getenv("PIPER_spotBugsIncludeFilterFile"), "Path to a filter file with bug definitions which should be included.") 145 cmd.Flags().IntVar(&stepConfig.SpotBugsMaxAllowedViolations, "spotBugsMaxAllowedViolations", 0, "The maximum number of failures allowed before execution fails.") 146 cmd.Flags().IntVar(&stepConfig.PmdFailurePriority, "pmdFailurePriority", 0, "What priority level to fail the build on. PMD violations are assigned a priority from 1 (most severe) to 5 (least severe) according the the rule's priority. Violations at or less than this priority level are considered failures and will fail the build if failOnViolation=true and the count exceeds maxAllowedViolations. The other violations will be regarded as warnings and will be displayed in the build output if verbose=true. Setting a value of 5 will treat all violations as failures, which may cause the build to fail. Setting a value of 1 will treat all violations as warnings. Only values from 1 to 5 are valid.") 147 cmd.Flags().IntVar(&stepConfig.PmdMaxAllowedViolations, "pmdMaxAllowedViolations", 0, "The maximum number of failures allowed before execution fails. Used in conjunction with failOnViolation=true and utilizes failurePriority. This value has no meaning if failOnViolation=false. If the number of failures is greater than this number, the build will be failed. If the number of failures is less than or equal to this value, then the build will not be failed.") 148 cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path to the mvn settings file that should be used as project settings file.") 149 cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path to the mvn settings file that should be used as global settings file.") 150 cmd.Flags().StringVar(&stepConfig.M2Path, "m2Path", os.Getenv("PIPER_m2Path"), "Path to the location of the local repository that should be used.") 151 cmd.Flags().BoolVar(&stepConfig.LogSuccessfulMavenTransfers, "logSuccessfulMavenTransfers", false, "Configures maven to log successful downloads. This is set to `false` by default to reduce the noise in build logs.") 152 cmd.Flags().BoolVar(&stepConfig.InstallArtifacts, "installArtifacts", false, "If enabled, it will install all artifacts to the local maven repository to make them available before running the static code checks. This is required if any maven module has dependencies to other modules in the repository and they were not installed before.") 153 154 } 155 156 // retrieve step metadata 157 func mavenExecuteStaticCodeChecksMetadata() config.StepData { 158 var theMetaData = config.StepData{ 159 Metadata: config.StepMetadata{ 160 Name: "mavenExecuteStaticCodeChecks", 161 Aliases: []config.Alias{{Name: "mavenExecute", Deprecated: false}}, 162 Description: "Execute static code checks for Maven based projects. The plugins SpotBugs and PMD are used.", 163 }, 164 Spec: config.StepSpec{ 165 Inputs: config.StepInputs{ 166 Parameters: []config.StepParameters{ 167 { 168 Name: "spotBugs", 169 ResourceRef: []config.ResourceReference{}, 170 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 171 Type: "bool", 172 Mandatory: false, 173 Aliases: []config.Alias{}, 174 Default: true, 175 }, 176 { 177 Name: "pmd", 178 ResourceRef: []config.ResourceReference{}, 179 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 180 Type: "bool", 181 Mandatory: false, 182 Aliases: []config.Alias{}, 183 Default: true, 184 }, 185 { 186 Name: "mavenModulesExcludes", 187 ResourceRef: []config.ResourceReference{}, 188 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 189 Type: "[]string", 190 Mandatory: false, 191 Aliases: []config.Alias{}, 192 Default: []string{}, 193 }, 194 { 195 Name: "spotBugsExcludeFilterFile", 196 ResourceRef: []config.ResourceReference{}, 197 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 198 Type: "string", 199 Mandatory: false, 200 Aliases: []config.Alias{{Name: "spotBugs/excludeFilterFile"}}, 201 Default: os.Getenv("PIPER_spotBugsExcludeFilterFile"), 202 }, 203 { 204 Name: "spotBugsIncludeFilterFile", 205 ResourceRef: []config.ResourceReference{}, 206 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 207 Type: "string", 208 Mandatory: false, 209 Aliases: []config.Alias{{Name: "spotBugs/includeFilterFile"}}, 210 Default: os.Getenv("PIPER_spotBugsIncludeFilterFile"), 211 }, 212 { 213 Name: "spotBugsMaxAllowedViolations", 214 ResourceRef: []config.ResourceReference{}, 215 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 216 Type: "int", 217 Mandatory: false, 218 Aliases: []config.Alias{{Name: "spotBugs/maxAllowedViolations"}}, 219 Default: 0, 220 }, 221 { 222 Name: "pmdFailurePriority", 223 ResourceRef: []config.ResourceReference{}, 224 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 225 Type: "int", 226 Mandatory: false, 227 Aliases: []config.Alias{{Name: "pmd/failurePriority"}}, 228 Default: 0, 229 }, 230 { 231 Name: "pmdMaxAllowedViolations", 232 ResourceRef: []config.ResourceReference{}, 233 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 234 Type: "int", 235 Mandatory: false, 236 Aliases: []config.Alias{{Name: "pmd/maxAllowedViolations"}}, 237 Default: 0, 238 }, 239 { 240 Name: "projectSettingsFile", 241 ResourceRef: []config.ResourceReference{}, 242 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 243 Type: "string", 244 Mandatory: false, 245 Aliases: []config.Alias{{Name: "maven/projectSettingsFile"}}, 246 Default: os.Getenv("PIPER_projectSettingsFile"), 247 }, 248 { 249 Name: "globalSettingsFile", 250 ResourceRef: []config.ResourceReference{}, 251 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 252 Type: "string", 253 Mandatory: false, 254 Aliases: []config.Alias{{Name: "maven/globalSettingsFile"}}, 255 Default: os.Getenv("PIPER_globalSettingsFile"), 256 }, 257 { 258 Name: "m2Path", 259 ResourceRef: []config.ResourceReference{}, 260 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 261 Type: "string", 262 Mandatory: false, 263 Aliases: []config.Alias{{Name: "maven/m2Path"}}, 264 Default: os.Getenv("PIPER_m2Path"), 265 }, 266 { 267 Name: "logSuccessfulMavenTransfers", 268 ResourceRef: []config.ResourceReference{}, 269 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 270 Type: "bool", 271 Mandatory: false, 272 Aliases: []config.Alias{{Name: "maven/logSuccessfulMavenTransfers"}}, 273 Default: false, 274 }, 275 { 276 Name: "installArtifacts", 277 ResourceRef: []config.ResourceReference{}, 278 Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, 279 Type: "bool", 280 Mandatory: false, 281 Aliases: []config.Alias{}, 282 Default: false, 283 }, 284 }, 285 }, 286 Containers: []config.Container{ 287 {Name: "mvn", Image: "maven:3.6-jdk-8"}, 288 }, 289 }, 290 } 291 return theMetaData 292 }