github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/githubSetCommitStatus_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 githubSetCommitStatusOptions struct { 19 APIURL string `json:"apiUrl,omitempty"` 20 CommitID string `json:"commitId,omitempty"` 21 Context string `json:"context,omitempty"` 22 Description string `json:"description,omitempty"` 23 Owner string `json:"owner,omitempty"` 24 Repository string `json:"repository,omitempty"` 25 Status string `json:"status,omitempty" validate:"possible-values=failure pending success"` 26 TargetURL string `json:"targetUrl,omitempty"` 27 Token string `json:"token,omitempty"` 28 } 29 30 // GithubSetCommitStatusCommand Set a status of a certain commit. 31 func GithubSetCommitStatusCommand() *cobra.Command { 32 const STEP_NAME = "githubSetCommitStatus" 33 34 metadata := githubSetCommitStatusMetadata() 35 var stepConfig githubSetCommitStatusOptions 36 var startTime time.Time 37 var logCollector *log.CollectorHook 38 var splunkClient *splunk.Splunk 39 telemetryClient := &telemetry.Telemetry{} 40 41 var createGithubSetCommitStatusCmd = &cobra.Command{ 42 Use: STEP_NAME, 43 Short: "Set a status of a certain commit.", 44 Long: `This step allows you to set a status for a certain commit. 45 Details can be found here: https://developer.github.com/v3/repos/statuses/. 46 47 Typically, following information is set: 48 49 * state (pending, failure, success) 50 * context 51 * target URL (link to details) 52 53 It can for example be used to create additional check indicators for a pull request which can be evaluated and also be enforced by GitHub configuration.`, 54 PreRunE: func(cmd *cobra.Command, _ []string) error { 55 startTime = time.Now() 56 log.SetStepName(STEP_NAME) 57 log.SetVerbose(GeneralConfig.Verbose) 58 59 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 60 61 path, _ := os.Getwd() 62 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 63 log.RegisterHook(fatalHook) 64 65 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 66 if err != nil { 67 log.SetErrorCategory(log.ErrorConfiguration) 68 return err 69 } 70 log.RegisterSecret(stepConfig.Token) 71 72 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 73 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 74 log.RegisterHook(&sentryHook) 75 } 76 77 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 78 splunkClient = &splunk.Splunk{} 79 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 80 log.RegisterHook(logCollector) 81 } 82 83 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 84 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 85 } 86 87 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 88 if err != nil { 89 return err 90 } 91 if err = validation.ValidateStruct(stepConfig); err != nil { 92 log.SetErrorCategory(log.ErrorConfiguration) 93 return err 94 } 95 96 return nil 97 }, 98 Run: func(_ *cobra.Command, _ []string) { 99 stepTelemetryData := telemetry.CustomData{} 100 stepTelemetryData.ErrorCode = "1" 101 handler := func() { 102 config.RemoveVaultSecretFiles() 103 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 104 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 105 stepTelemetryData.PiperCommitHash = GitCommit 106 telemetryClient.SetData(&stepTelemetryData) 107 telemetryClient.Send() 108 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 109 splunkClient.Initialize(GeneralConfig.CorrelationID, 110 GeneralConfig.HookConfig.SplunkConfig.Dsn, 111 GeneralConfig.HookConfig.SplunkConfig.Token, 112 GeneralConfig.HookConfig.SplunkConfig.Index, 113 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 114 splunkClient.Send(telemetryClient.GetData(), logCollector) 115 } 116 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 117 splunkClient.Initialize(GeneralConfig.CorrelationID, 118 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 119 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 120 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 121 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 122 splunkClient.Send(telemetryClient.GetData(), logCollector) 123 } 124 } 125 log.DeferExitHandler(handler) 126 defer handler() 127 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 128 githubSetCommitStatus(stepConfig, &stepTelemetryData) 129 stepTelemetryData.ErrorCode = "0" 130 log.Entry().Info("SUCCESS") 131 }, 132 } 133 134 addGithubSetCommitStatusFlags(createGithubSetCommitStatusCmd, &stepConfig) 135 return createGithubSetCommitStatusCmd 136 } 137 138 func addGithubSetCommitStatusFlags(cmd *cobra.Command, stepConfig *githubSetCommitStatusOptions) { 139 cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API URL.") 140 cmd.Flags().StringVar(&stepConfig.CommitID, "commitId", os.Getenv("PIPER_commitId"), "The commitId for which the status should be set.") 141 cmd.Flags().StringVar(&stepConfig.Context, "context", os.Getenv("PIPER_context"), "Label for the status which will for example show up in a pull request.") 142 cmd.Flags().StringVar(&stepConfig.Description, "description", os.Getenv("PIPER_description"), "Short description of the status.") 143 cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.") 144 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.") 145 cmd.Flags().StringVar(&stepConfig.Status, "status", os.Getenv("PIPER_status"), "Status which should be set on the commitId.") 146 cmd.Flags().StringVar(&stepConfig.TargetURL, "targetUrl", os.Getenv("PIPER_targetUrl"), "Target URL to associate the status with.") 147 cmd.Flags().StringVar(&stepConfig.Token, "token", os.Getenv("PIPER_token"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.") 148 149 cmd.MarkFlagRequired("apiUrl") 150 cmd.MarkFlagRequired("commitId") 151 cmd.MarkFlagRequired("context") 152 cmd.MarkFlagRequired("owner") 153 cmd.MarkFlagRequired("repository") 154 cmd.MarkFlagRequired("status") 155 cmd.MarkFlagRequired("token") 156 } 157 158 // retrieve step metadata 159 func githubSetCommitStatusMetadata() config.StepData { 160 var theMetaData = config.StepData{ 161 Metadata: config.StepMetadata{ 162 Name: "githubSetCommitStatus", 163 Aliases: []config.Alias{}, 164 Description: "Set a status of a certain commit.", 165 }, 166 Spec: config.StepSpec{ 167 Inputs: config.StepInputs{ 168 Secrets: []config.StepSecrets{ 169 {Name: "githubTokenCredentialsId", Description: "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.", Type: "jenkins"}, 170 }, 171 Parameters: []config.StepParameters{ 172 { 173 Name: "apiUrl", 174 ResourceRef: []config.ResourceReference{}, 175 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 176 Type: "string", 177 Mandatory: true, 178 Aliases: []config.Alias{{Name: "githubApiUrl"}}, 179 Default: `https://api.github.com`, 180 }, 181 { 182 Name: "commitId", 183 ResourceRef: []config.ResourceReference{ 184 { 185 Name: "commonPipelineEnvironment", 186 Param: "git/commitId", 187 }, 188 }, 189 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 190 Type: "string", 191 Mandatory: true, 192 Aliases: []config.Alias{}, 193 Default: os.Getenv("PIPER_commitId"), 194 }, 195 { 196 Name: "context", 197 ResourceRef: []config.ResourceReference{}, 198 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 199 Type: "string", 200 Mandatory: true, 201 Aliases: []config.Alias{}, 202 Default: os.Getenv("PIPER_context"), 203 }, 204 { 205 Name: "description", 206 ResourceRef: []config.ResourceReference{}, 207 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 208 Type: "string", 209 Mandatory: false, 210 Aliases: []config.Alias{}, 211 Default: os.Getenv("PIPER_description"), 212 }, 213 { 214 Name: "owner", 215 ResourceRef: []config.ResourceReference{ 216 { 217 Name: "commonPipelineEnvironment", 218 Param: "github/owner", 219 }, 220 }, 221 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 222 Type: "string", 223 Mandatory: true, 224 Aliases: []config.Alias{{Name: "githubOrg"}}, 225 Default: os.Getenv("PIPER_owner"), 226 }, 227 { 228 Name: "repository", 229 ResourceRef: []config.ResourceReference{ 230 { 231 Name: "commonPipelineEnvironment", 232 Param: "github/repository", 233 }, 234 }, 235 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 236 Type: "string", 237 Mandatory: true, 238 Aliases: []config.Alias{{Name: "githubRepo"}}, 239 Default: os.Getenv("PIPER_repository"), 240 }, 241 { 242 Name: "status", 243 ResourceRef: []config.ResourceReference{}, 244 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 245 Type: "string", 246 Mandatory: true, 247 Aliases: []config.Alias{}, 248 Default: os.Getenv("PIPER_status"), 249 }, 250 { 251 Name: "targetUrl", 252 ResourceRef: []config.ResourceReference{}, 253 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 254 Type: "string", 255 Mandatory: false, 256 Aliases: []config.Alias{}, 257 Default: os.Getenv("PIPER_targetUrl"), 258 }, 259 { 260 Name: "token", 261 ResourceRef: []config.ResourceReference{ 262 { 263 Name: "githubTokenCredentialsId", 264 Type: "secret", 265 }, 266 267 { 268 Name: "githubVaultSecretName", 269 Type: "vaultSecret", 270 Default: "github", 271 }, 272 }, 273 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 274 Type: "string", 275 Mandatory: true, 276 Aliases: []config.Alias{{Name: "githubToken"}, {Name: "access_token"}}, 277 Default: os.Getenv("PIPER_token"), 278 }, 279 }, 280 }, 281 }, 282 } 283 return theMetaData 284 }