github.com/SAP/jenkins-library@v1.362.0/cmd/npmExecuteLint_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 npmExecuteLintOptions struct { 19 Install bool `json:"install,omitempty"` 20 RunScript string `json:"runScript,omitempty"` 21 FailOnError bool `json:"failOnError,omitempty"` 22 DefaultNpmRegistry string `json:"defaultNpmRegistry,omitempty"` 23 OutputFormat string `json:"outputFormat,omitempty"` 24 OutputFileName string `json:"outputFileName,omitempty"` 25 } 26 27 // NpmExecuteLintCommand Execute ci-lint script on all npm packages in a project or execute default linting 28 func NpmExecuteLintCommand() *cobra.Command { 29 const STEP_NAME = "npmExecuteLint" 30 31 metadata := npmExecuteLintMetadata() 32 var stepConfig npmExecuteLintOptions 33 var startTime time.Time 34 var logCollector *log.CollectorHook 35 var splunkClient *splunk.Splunk 36 telemetryClient := &telemetry.Telemetry{} 37 38 var createNpmExecuteLintCmd = &cobra.Command{ 39 Use: STEP_NAME, 40 Short: "Execute ci-lint script on all npm packages in a project or execute default linting", 41 Long: `Execute ci-lint script for all package json files, if they implement the script. If no ci-lint script is defined, 42 either use ESLint configurations present in the project or use the provided general purpose configuration to run ESLint.`, 43 PreRunE: func(cmd *cobra.Command, _ []string) error { 44 startTime = time.Now() 45 log.SetStepName(STEP_NAME) 46 log.SetVerbose(GeneralConfig.Verbose) 47 48 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 49 50 path, _ := os.Getwd() 51 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 52 log.RegisterHook(fatalHook) 53 54 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 55 if err != nil { 56 log.SetErrorCategory(log.ErrorConfiguration) 57 return err 58 } 59 60 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 61 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 62 log.RegisterHook(&sentryHook) 63 } 64 65 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 66 splunkClient = &splunk.Splunk{} 67 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 68 log.RegisterHook(logCollector) 69 } 70 71 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 72 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 73 } 74 75 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 76 if err != nil { 77 return err 78 } 79 if err = validation.ValidateStruct(stepConfig); err != nil { 80 log.SetErrorCategory(log.ErrorConfiguration) 81 return err 82 } 83 84 return nil 85 }, 86 Run: func(_ *cobra.Command, _ []string) { 87 stepTelemetryData := telemetry.CustomData{} 88 stepTelemetryData.ErrorCode = "1" 89 handler := func() { 90 config.RemoveVaultSecretFiles() 91 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 92 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 93 stepTelemetryData.PiperCommitHash = GitCommit 94 telemetryClient.SetData(&stepTelemetryData) 95 telemetryClient.Send() 96 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 97 splunkClient.Initialize(GeneralConfig.CorrelationID, 98 GeneralConfig.HookConfig.SplunkConfig.Dsn, 99 GeneralConfig.HookConfig.SplunkConfig.Token, 100 GeneralConfig.HookConfig.SplunkConfig.Index, 101 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 102 splunkClient.Send(telemetryClient.GetData(), logCollector) 103 } 104 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 105 splunkClient.Initialize(GeneralConfig.CorrelationID, 106 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 107 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 108 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 109 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 110 splunkClient.Send(telemetryClient.GetData(), logCollector) 111 } 112 } 113 log.DeferExitHandler(handler) 114 defer handler() 115 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token) 116 npmExecuteLint(stepConfig, &stepTelemetryData) 117 stepTelemetryData.ErrorCode = "0" 118 log.Entry().Info("SUCCESS") 119 }, 120 } 121 122 addNpmExecuteLintFlags(createNpmExecuteLintCmd, &stepConfig) 123 return createNpmExecuteLintCmd 124 } 125 126 func addNpmExecuteLintFlags(cmd *cobra.Command, stepConfig *npmExecuteLintOptions) { 127 cmd.Flags().BoolVar(&stepConfig.Install, "install", false, "Run npm install or similar commands depending on the project structure.") 128 cmd.Flags().StringVar(&stepConfig.RunScript, "runScript", `ci-lint`, "List of additional run scripts to execute from package.json.") 129 cmd.Flags().BoolVar(&stepConfig.FailOnError, "failOnError", false, "Defines the behavior in case linting errors are found.") 130 cmd.Flags().StringVar(&stepConfig.DefaultNpmRegistry, "defaultNpmRegistry", os.Getenv("PIPER_defaultNpmRegistry"), "URL of the npm registry to use. Defaults to https://registry.npmjs.org/") 131 cmd.Flags().StringVar(&stepConfig.OutputFormat, "outputFormat", `checkstyle`, "eslint output format, e.g. stylish, checkstyle") 132 cmd.Flags().StringVar(&stepConfig.OutputFileName, "outputFileName", `defaultlint.xml`, "name of the output file. There might be a 'N_' prefix where 'N' is a number. When the empty string is provided, we will print to console") 133 134 } 135 136 // retrieve step metadata 137 func npmExecuteLintMetadata() config.StepData { 138 var theMetaData = config.StepData{ 139 Metadata: config.StepMetadata{ 140 Name: "npmExecuteLint", 141 Aliases: []config.Alias{{Name: "executeNpm", Deprecated: false}}, 142 Description: "Execute ci-lint script on all npm packages in a project or execute default linting", 143 }, 144 Spec: config.StepSpec{ 145 Inputs: config.StepInputs{ 146 Parameters: []config.StepParameters{ 147 { 148 Name: "install", 149 ResourceRef: []config.ResourceReference{}, 150 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 151 Type: "bool", 152 Mandatory: false, 153 Aliases: []config.Alias{}, 154 Default: false, 155 }, 156 { 157 Name: "runScript", 158 ResourceRef: []config.ResourceReference{}, 159 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 160 Type: "string", 161 Mandatory: false, 162 Aliases: []config.Alias{}, 163 Default: `ci-lint`, 164 }, 165 { 166 Name: "failOnError", 167 ResourceRef: []config.ResourceReference{}, 168 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 169 Type: "bool", 170 Mandatory: false, 171 Aliases: []config.Alias{}, 172 Default: false, 173 }, 174 { 175 Name: "defaultNpmRegistry", 176 ResourceRef: []config.ResourceReference{}, 177 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 178 Type: "string", 179 Mandatory: false, 180 Aliases: []config.Alias{{Name: "npm/defaultNpmRegistry"}}, 181 Default: os.Getenv("PIPER_defaultNpmRegistry"), 182 }, 183 { 184 Name: "outputFormat", 185 ResourceRef: []config.ResourceReference{}, 186 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 187 Type: "string", 188 Mandatory: false, 189 Aliases: []config.Alias{{Name: "npm/outputFormat"}}, 190 Default: `checkstyle`, 191 }, 192 { 193 Name: "outputFileName", 194 ResourceRef: []config.ResourceReference{}, 195 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 196 Type: "string", 197 Mandatory: false, 198 Aliases: []config.Alias{{Name: "npm/outputFormat"}}, 199 Default: `defaultlint.xml`, 200 }, 201 }, 202 }, 203 Containers: []config.Container{ 204 {Name: "node", Image: "node:lts-buster"}, 205 }, 206 }, 207 } 208 return theMetaData 209 }