github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/pipelineCreateScanSummary_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 pipelineCreateScanSummaryOptions struct {
    19  	FailedOnly     bool   `json:"failedOnly,omitempty"`
    20  	OutputFilePath string `json:"outputFilePath,omitempty"`
    21  	PipelineLink   string `json:"pipelineLink,omitempty"`
    22  }
    23  
    24  // PipelineCreateScanSummaryCommand Collect scan result information anc create a summary report
    25  func PipelineCreateScanSummaryCommand() *cobra.Command {
    26  	const STEP_NAME = "pipelineCreateScanSummary"
    27  
    28  	metadata := pipelineCreateScanSummaryMetadata()
    29  	var stepConfig pipelineCreateScanSummaryOptions
    30  	var startTime time.Time
    31  	var logCollector *log.CollectorHook
    32  	var splunkClient *splunk.Splunk
    33  	telemetryClient := &telemetry.Telemetry{}
    34  
    35  	var createPipelineCreateScanSummaryCmd = &cobra.Command{
    36  		Use:   STEP_NAME,
    37  		Short: "Collect scan result information anc create a summary report",
    38  		Long: `This step allows you to create a summary report of your scan results.
    39  
    40  It is for example used to create a markdown file which can be used to create a GitHub issue.`,
    41  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    42  			startTime = time.Now()
    43  			log.SetStepName(STEP_NAME)
    44  			log.SetVerbose(GeneralConfig.Verbose)
    45  
    46  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    47  
    48  			path, _ := os.Getwd()
    49  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    50  			log.RegisterHook(fatalHook)
    51  
    52  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    53  			if err != nil {
    54  				log.SetErrorCategory(log.ErrorConfiguration)
    55  				return err
    56  			}
    57  
    58  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    59  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    60  				log.RegisterHook(&sentryHook)
    61  			}
    62  
    63  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    64  				splunkClient = &splunk.Splunk{}
    65  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    66  				log.RegisterHook(logCollector)
    67  			}
    68  
    69  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    70  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    71  			}
    72  
    73  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    74  			if err != nil {
    75  				return err
    76  			}
    77  			if err = validation.ValidateStruct(stepConfig); err != nil {
    78  				log.SetErrorCategory(log.ErrorConfiguration)
    79  				return err
    80  			}
    81  
    82  			return nil
    83  		},
    84  		Run: func(_ *cobra.Command, _ []string) {
    85  			stepTelemetryData := telemetry.CustomData{}
    86  			stepTelemetryData.ErrorCode = "1"
    87  			handler := func() {
    88  				config.RemoveVaultSecretFiles()
    89  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    90  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    91  				stepTelemetryData.PiperCommitHash = GitCommit
    92  				telemetryClient.SetData(&stepTelemetryData)
    93  				telemetryClient.Send()
    94  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  					splunkClient.Initialize(GeneralConfig.CorrelationID,
    96  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
    97  						GeneralConfig.HookConfig.SplunkConfig.Token,
    98  						GeneralConfig.HookConfig.SplunkConfig.Index,
    99  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   100  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   101  				}
   102  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   103  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   104  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   106  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   107  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   108  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   109  				}
   110  			}
   111  			log.DeferExitHandler(handler)
   112  			defer handler()
   113  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   114  			pipelineCreateScanSummary(stepConfig, &stepTelemetryData)
   115  			stepTelemetryData.ErrorCode = "0"
   116  			log.Entry().Info("SUCCESS")
   117  		},
   118  	}
   119  
   120  	addPipelineCreateScanSummaryFlags(createPipelineCreateScanSummaryCmd, &stepConfig)
   121  	return createPipelineCreateScanSummaryCmd
   122  }
   123  
   124  func addPipelineCreateScanSummaryFlags(cmd *cobra.Command, stepConfig *pipelineCreateScanSummaryOptions) {
   125  	cmd.Flags().BoolVar(&stepConfig.FailedOnly, "failedOnly", false, "Defines if only failed scans should be included into the summary.")
   126  	cmd.Flags().StringVar(&stepConfig.OutputFilePath, "outputFilePath", `scanSummary.md`, "Defines the filepath to the target file which will be created by the step.")
   127  	cmd.Flags().StringVar(&stepConfig.PipelineLink, "pipelineLink", os.Getenv("PIPER_pipelineLink"), "Link to the pipeline (e.g. Jenkins job url) for reference in the scan summary.")
   128  
   129  }
   130  
   131  // retrieve step metadata
   132  func pipelineCreateScanSummaryMetadata() config.StepData {
   133  	var theMetaData = config.StepData{
   134  		Metadata: config.StepMetadata{
   135  			Name:        "pipelineCreateScanSummary",
   136  			Aliases:     []config.Alias{},
   137  			Description: "Collect scan result information anc create a summary report",
   138  		},
   139  		Spec: config.StepSpec{
   140  			Inputs: config.StepInputs{
   141  				Parameters: []config.StepParameters{
   142  					{
   143  						Name:        "failedOnly",
   144  						ResourceRef: []config.ResourceReference{},
   145  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   146  						Type:        "bool",
   147  						Mandatory:   false,
   148  						Aliases:     []config.Alias{},
   149  						Default:     false,
   150  					},
   151  					{
   152  						Name:        "outputFilePath",
   153  						ResourceRef: []config.ResourceReference{},
   154  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   155  						Type:        "string",
   156  						Mandatory:   false,
   157  						Aliases:     []config.Alias{},
   158  						Default:     `scanSummary.md`,
   159  					},
   160  					{
   161  						Name:        "pipelineLink",
   162  						ResourceRef: []config.ResourceReference{},
   163  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   164  						Type:        "string",
   165  						Mandatory:   false,
   166  						Aliases:     []config.Alias{},
   167  						Default:     os.Getenv("PIPER_pipelineLink"),
   168  					},
   169  				},
   170  			},
   171  		},
   172  	}
   173  	return theMetaData
   174  }