github.com/jaylevin/jenkins-library@v1.230.4/cmd/transportRequestDocIDFromGit_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 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type transportRequestDocIDFromGitOptions struct { 21 GitFrom string `json:"gitFrom,omitempty"` 22 GitTo string `json:"gitTo,omitempty"` 23 ChangeDocumentLabel string `json:"changeDocumentLabel,omitempty"` 24 } 25 26 type transportRequestDocIDFromGitCommonPipelineEnvironment struct { 27 custom struct { 28 changeDocumentID string 29 } 30 } 31 32 func (p *transportRequestDocIDFromGitCommonPipelineEnvironment) persist(path, resourceName string) { 33 content := []struct { 34 category string 35 name string 36 value interface{} 37 }{ 38 {category: "custom", name: "changeDocumentId", value: p.custom.changeDocumentID}, 39 } 40 41 errCount := 0 42 for _, param := range content { 43 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 44 if err != nil { 45 log.Entry().WithError(err).Error("Error persisting piper environment.") 46 errCount++ 47 } 48 } 49 if errCount > 0 { 50 log.Entry().Error("failed to persist Piper environment") 51 } 52 } 53 54 // TransportRequestDocIDFromGitCommand Retrieves change document ID from Git repository 55 func TransportRequestDocIDFromGitCommand() *cobra.Command { 56 const STEP_NAME = "transportRequestDocIDFromGit" 57 58 metadata := transportRequestDocIDFromGitMetadata() 59 var stepConfig transportRequestDocIDFromGitOptions 60 var startTime time.Time 61 var commonPipelineEnvironment transportRequestDocIDFromGitCommonPipelineEnvironment 62 var logCollector *log.CollectorHook 63 var splunkClient *splunk.Splunk 64 telemetryClient := &telemetry.Telemetry{} 65 66 var createTransportRequestDocIDFromGitCmd = &cobra.Command{ 67 Use: STEP_NAME, 68 Short: "Retrieves change document ID from Git repository", 69 Long: `This step scans the commit messages of the Git repository for a pattern to retrieve the change document ID. 70 It is primarily made for the transportRequestUploadSOLMAN step to provide the change document ID by Git means.`, 71 PreRunE: func(cmd *cobra.Command, _ []string) error { 72 startTime = time.Now() 73 log.SetStepName(STEP_NAME) 74 log.SetVerbose(GeneralConfig.Verbose) 75 76 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 77 78 path, _ := os.Getwd() 79 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 80 log.RegisterHook(fatalHook) 81 82 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 83 if err != nil { 84 log.SetErrorCategory(log.ErrorConfiguration) 85 return err 86 } 87 88 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 89 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 90 log.RegisterHook(&sentryHook) 91 } 92 93 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 94 splunkClient = &splunk.Splunk{} 95 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 96 log.RegisterHook(logCollector) 97 } 98 99 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 100 if err != nil { 101 return err 102 } 103 if err = validation.ValidateStruct(stepConfig); err != nil { 104 log.SetErrorCategory(log.ErrorConfiguration) 105 return err 106 } 107 108 return nil 109 }, 110 Run: func(_ *cobra.Command, _ []string) { 111 stepTelemetryData := telemetry.CustomData{} 112 stepTelemetryData.ErrorCode = "1" 113 handler := func() { 114 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 115 config.RemoveVaultSecretFiles() 116 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 117 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 118 stepTelemetryData.PiperCommitHash = GitCommit 119 telemetryClient.SetData(&stepTelemetryData) 120 telemetryClient.Send() 121 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 122 splunkClient.Send(telemetryClient.GetData(), logCollector) 123 } 124 } 125 log.DeferExitHandler(handler) 126 defer handler() 127 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 128 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 129 splunkClient.Initialize(GeneralConfig.CorrelationID, 130 GeneralConfig.HookConfig.SplunkConfig.Dsn, 131 GeneralConfig.HookConfig.SplunkConfig.Token, 132 GeneralConfig.HookConfig.SplunkConfig.Index, 133 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 134 } 135 transportRequestDocIDFromGit(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 136 stepTelemetryData.ErrorCode = "0" 137 log.Entry().Info("SUCCESS") 138 }, 139 } 140 141 addTransportRequestDocIDFromGitFlags(createTransportRequestDocIDFromGitCmd, &stepConfig) 142 return createTransportRequestDocIDFromGitCmd 143 } 144 145 func addTransportRequestDocIDFromGitFlags(cmd *cobra.Command, stepConfig *transportRequestDocIDFromGitOptions) { 146 cmd.Flags().StringVar(&stepConfig.GitFrom, "gitFrom", `origin/master`, "GIT starting point for retrieving the change document and transport request ID") 147 cmd.Flags().StringVar(&stepConfig.GitTo, "gitTo", `HEAD`, "GIT ending point for retrieving the change document and transport request ID") 148 cmd.Flags().StringVar(&stepConfig.ChangeDocumentLabel, "changeDocumentLabel", `ChangeDocument`, "Pattern used for identifying lines holding the change document ID. The GIT commit log messages are scanned for this label") 149 150 } 151 152 // retrieve step metadata 153 func transportRequestDocIDFromGitMetadata() config.StepData { 154 var theMetaData = config.StepData{ 155 Metadata: config.StepMetadata{ 156 Name: "transportRequestDocIDFromGit", 157 Aliases: []config.Alias{}, 158 Description: "Retrieves change document ID from Git repository", 159 }, 160 Spec: config.StepSpec{ 161 Inputs: config.StepInputs{ 162 Parameters: []config.StepParameters{ 163 { 164 Name: "gitFrom", 165 ResourceRef: []config.ResourceReference{}, 166 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 167 Type: "string", 168 Mandatory: false, 169 Aliases: []config.Alias{{Name: "changeManagement/git/from"}}, 170 Default: `origin/master`, 171 }, 172 { 173 Name: "gitTo", 174 ResourceRef: []config.ResourceReference{}, 175 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 176 Type: "string", 177 Mandatory: false, 178 Aliases: []config.Alias{{Name: "changeManagement/git/to"}}, 179 Default: `HEAD`, 180 }, 181 { 182 Name: "changeDocumentLabel", 183 ResourceRef: []config.ResourceReference{}, 184 Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, 185 Type: "string", 186 Mandatory: false, 187 Aliases: []config.Alias{{Name: "changeManagement/changeDocumentLabel"}}, 188 Default: `ChangeDocument`, 189 }, 190 }, 191 }, 192 Outputs: config.StepOutputs{ 193 Resources: []config.StepResources{ 194 { 195 Name: "commonPipelineEnvironment", 196 Type: "piperEnvironment", 197 Parameters: []map[string]interface{}{ 198 {"name": "custom/changeDocumentId"}, 199 }, 200 }, 201 }, 202 }, 203 }, 204 } 205 return theMetaData 206 }