github.com/xgoffin/jenkins-library@v1.154.0/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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    70  			if err != nil {
    71  				return err
    72  			}
    73  			if err = validation.ValidateStruct(stepConfig); err != nil {
    74  				log.SetErrorCategory(log.ErrorConfiguration)
    75  				return err
    76  			}
    77  
    78  			return nil
    79  		},
    80  		Run: func(_ *cobra.Command, _ []string) {
    81  			stepTelemetryData := telemetry.CustomData{}
    82  			stepTelemetryData.ErrorCode = "1"
    83  			handler := func() {
    84  				config.RemoveVaultSecretFiles()
    85  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    86  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    87  				stepTelemetryData.PiperCommitHash = GitCommit
    88  				telemetryClient.SetData(&stepTelemetryData)
    89  				telemetryClient.Send()
    90  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    91  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    92  				}
    93  			}
    94  			log.DeferExitHandler(handler)
    95  			defer handler()
    96  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    97  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    98  				splunkClient.Initialize(GeneralConfig.CorrelationID,
    99  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   100  					GeneralConfig.HookConfig.SplunkConfig.Token,
   101  					GeneralConfig.HookConfig.SplunkConfig.Index,
   102  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   103  			}
   104  			pipelineCreateScanSummary(stepConfig, &stepTelemetryData)
   105  			stepTelemetryData.ErrorCode = "0"
   106  			log.Entry().Info("SUCCESS")
   107  		},
   108  	}
   109  
   110  	addPipelineCreateScanSummaryFlags(createPipelineCreateScanSummaryCmd, &stepConfig)
   111  	return createPipelineCreateScanSummaryCmd
   112  }
   113  
   114  func addPipelineCreateScanSummaryFlags(cmd *cobra.Command, stepConfig *pipelineCreateScanSummaryOptions) {
   115  	cmd.Flags().BoolVar(&stepConfig.FailedOnly, "failedOnly", false, "Defines if only failed scans should be included into the summary.")
   116  	cmd.Flags().StringVar(&stepConfig.OutputFilePath, "outputFilePath", `scanSummary.md`, "Defines the filepath to the target file which will be created by the step.")
   117  	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.")
   118  
   119  }
   120  
   121  // retrieve step metadata
   122  func pipelineCreateScanSummaryMetadata() config.StepData {
   123  	var theMetaData = config.StepData{
   124  		Metadata: config.StepMetadata{
   125  			Name:        "pipelineCreateScanSummary",
   126  			Aliases:     []config.Alias{},
   127  			Description: "Collect scan result information anc create a summary report",
   128  		},
   129  		Spec: config.StepSpec{
   130  			Inputs: config.StepInputs{
   131  				Parameters: []config.StepParameters{
   132  					{
   133  						Name:        "failedOnly",
   134  						ResourceRef: []config.ResourceReference{},
   135  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   136  						Type:        "bool",
   137  						Mandatory:   false,
   138  						Aliases:     []config.Alias{},
   139  						Default:     false,
   140  					},
   141  					{
   142  						Name:        "outputFilePath",
   143  						ResourceRef: []config.ResourceReference{},
   144  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   145  						Type:        "string",
   146  						Mandatory:   false,
   147  						Aliases:     []config.Alias{},
   148  						Default:     `scanSummary.md`,
   149  					},
   150  					{
   151  						Name:        "pipelineLink",
   152  						ResourceRef: []config.ResourceReference{},
   153  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   154  						Type:        "string",
   155  						Mandatory:   false,
   156  						Aliases:     []config.Alias{},
   157  						Default:     os.Getenv("PIPER_pipelineLink"),
   158  					},
   159  				},
   160  			},
   161  		},
   162  	}
   163  	return theMetaData
   164  }