github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/githubCreatePullRequest_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 githubCreatePullRequestOptions struct { 19 Assignees []string `json:"assignees,omitempty"` 20 Base string `json:"base,omitempty"` 21 Body string `json:"body,omitempty"` 22 APIURL string `json:"apiUrl,omitempty"` 23 Head string `json:"head,omitempty"` 24 Owner string `json:"owner,omitempty"` 25 Repository string `json:"repository,omitempty"` 26 ServerURL string `json:"serverUrl,omitempty"` 27 Title string `json:"title,omitempty"` 28 Token string `json:"token,omitempty"` 29 Labels []string `json:"labels,omitempty"` 30 } 31 32 // GithubCreatePullRequestCommand Create a pull request on GitHub 33 func GithubCreatePullRequestCommand() *cobra.Command { 34 const STEP_NAME = "githubCreatePullRequest" 35 36 metadata := githubCreatePullRequestMetadata() 37 var stepConfig githubCreatePullRequestOptions 38 var startTime time.Time 39 var logCollector *log.CollectorHook 40 var splunkClient *splunk.Splunk 41 telemetryClient := &telemetry.Telemetry{} 42 43 var createGithubCreatePullRequestCmd = &cobra.Command{ 44 Use: STEP_NAME, 45 Short: "Create a pull request on GitHub", 46 Long: `This step allows you to create a pull request on Github. 47 48 It can for example be used for GitOps scenarios or for scenarios where you want to have a manual confirmation step which is delegated to a GitHub pull request.`, 49 PreRunE: func(cmd *cobra.Command, _ []string) error { 50 startTime = time.Now() 51 log.SetStepName(STEP_NAME) 52 log.SetVerbose(GeneralConfig.Verbose) 53 54 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 55 56 path, _ := os.Getwd() 57 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 58 log.RegisterHook(fatalHook) 59 60 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 61 if err != nil { 62 log.SetErrorCategory(log.ErrorConfiguration) 63 return err 64 } 65 log.RegisterSecret(stepConfig.Token) 66 67 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 68 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 69 log.RegisterHook(&sentryHook) 70 } 71 72 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 73 splunkClient = &splunk.Splunk{} 74 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 75 log.RegisterHook(logCollector) 76 } 77 78 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 79 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 80 } 81 82 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 83 if err != nil { 84 return err 85 } 86 if err = validation.ValidateStruct(stepConfig); err != nil { 87 log.SetErrorCategory(log.ErrorConfiguration) 88 return err 89 } 90 91 return nil 92 }, 93 Run: func(_ *cobra.Command, _ []string) { 94 stepTelemetryData := telemetry.CustomData{} 95 stepTelemetryData.ErrorCode = "1" 96 handler := func() { 97 config.RemoveVaultSecretFiles() 98 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 99 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 100 stepTelemetryData.PiperCommitHash = GitCommit 101 telemetryClient.SetData(&stepTelemetryData) 102 telemetryClient.Send() 103 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 104 splunkClient.Initialize(GeneralConfig.CorrelationID, 105 GeneralConfig.HookConfig.SplunkConfig.Dsn, 106 GeneralConfig.HookConfig.SplunkConfig.Token, 107 GeneralConfig.HookConfig.SplunkConfig.Index, 108 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 109 splunkClient.Send(telemetryClient.GetData(), logCollector) 110 } 111 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 112 splunkClient.Initialize(GeneralConfig.CorrelationID, 113 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 114 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 115 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 116 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 117 splunkClient.Send(telemetryClient.GetData(), logCollector) 118 } 119 } 120 log.DeferExitHandler(handler) 121 defer handler() 122 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 123 githubCreatePullRequest(stepConfig, &stepTelemetryData) 124 stepTelemetryData.ErrorCode = "0" 125 log.Entry().Info("SUCCESS") 126 }, 127 } 128 129 addGithubCreatePullRequestFlags(createGithubCreatePullRequestCmd, &stepConfig) 130 return createGithubCreatePullRequestCmd 131 } 132 133 func addGithubCreatePullRequestFlags(cmd *cobra.Command, stepConfig *githubCreatePullRequestOptions) { 134 cmd.Flags().StringSliceVar(&stepConfig.Assignees, "assignees", []string{}, "Login names of users to which the PR should be assigned to.") 135 cmd.Flags().StringVar(&stepConfig.Base, "base", os.Getenv("PIPER_base"), "The name of the branch you want the changes pulled into.") 136 cmd.Flags().StringVar(&stepConfig.Body, "body", os.Getenv("PIPER_body"), "The description text of the pull request in markdown format.") 137 cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API url.") 138 cmd.Flags().StringVar(&stepConfig.Head, "head", os.Getenv("PIPER_head"), "The name of the branch where your changes are implemented.") 139 cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.") 140 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.") 141 cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", `https://github.com`, "GitHub server url for end-user access.") 142 cmd.Flags().StringVar(&stepConfig.Title, "title", os.Getenv("PIPER_title"), "Title of the pull request.") 143 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") 144 cmd.Flags().StringSliceVar(&stepConfig.Labels, "labels", []string{}, "Labels to be added to the pull request.") 145 146 cmd.MarkFlagRequired("base") 147 cmd.MarkFlagRequired("body") 148 cmd.MarkFlagRequired("apiUrl") 149 cmd.MarkFlagRequired("head") 150 cmd.MarkFlagRequired("owner") 151 cmd.MarkFlagRequired("repository") 152 cmd.MarkFlagRequired("serverUrl") 153 cmd.MarkFlagRequired("title") 154 cmd.MarkFlagRequired("token") 155 } 156 157 // retrieve step metadata 158 func githubCreatePullRequestMetadata() config.StepData { 159 var theMetaData = config.StepData{ 160 Metadata: config.StepMetadata{ 161 Name: "githubCreatePullRequest", 162 Aliases: []config.Alias{}, 163 Description: "Create a pull request on GitHub", 164 }, 165 Spec: config.StepSpec{ 166 Inputs: config.StepInputs{ 167 Secrets: []config.StepSecrets{ 168 {Name: "githubTokenCredentialsId", Description: "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.", Type: "jenkins"}, 169 }, 170 Parameters: []config.StepParameters{ 171 { 172 Name: "assignees", 173 ResourceRef: []config.ResourceReference{}, 174 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 175 Type: "[]string", 176 Mandatory: false, 177 Aliases: []config.Alias{}, 178 Default: []string{}, 179 }, 180 { 181 Name: "base", 182 ResourceRef: []config.ResourceReference{}, 183 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 184 Type: "string", 185 Mandatory: true, 186 Aliases: []config.Alias{}, 187 Default: os.Getenv("PIPER_base"), 188 }, 189 { 190 Name: "body", 191 ResourceRef: []config.ResourceReference{}, 192 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 193 Type: "string", 194 Mandatory: true, 195 Aliases: []config.Alias{}, 196 Default: os.Getenv("PIPER_body"), 197 }, 198 { 199 Name: "apiUrl", 200 ResourceRef: []config.ResourceReference{}, 201 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 202 Type: "string", 203 Mandatory: true, 204 Aliases: []config.Alias{{Name: "githubApiUrl"}}, 205 Default: `https://api.github.com`, 206 }, 207 { 208 Name: "head", 209 ResourceRef: []config.ResourceReference{}, 210 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 211 Type: "string", 212 Mandatory: true, 213 Aliases: []config.Alias{}, 214 Default: os.Getenv("PIPER_head"), 215 }, 216 { 217 Name: "owner", 218 ResourceRef: []config.ResourceReference{ 219 { 220 Name: "commonPipelineEnvironment", 221 Param: "github/owner", 222 }, 223 }, 224 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 225 Type: "string", 226 Mandatory: true, 227 Aliases: []config.Alias{{Name: "githubOrg"}}, 228 Default: os.Getenv("PIPER_owner"), 229 }, 230 { 231 Name: "repository", 232 ResourceRef: []config.ResourceReference{ 233 { 234 Name: "commonPipelineEnvironment", 235 Param: "github/repository", 236 }, 237 }, 238 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 239 Type: "string", 240 Mandatory: true, 241 Aliases: []config.Alias{{Name: "githubRepo"}}, 242 Default: os.Getenv("PIPER_repository"), 243 }, 244 { 245 Name: "serverUrl", 246 ResourceRef: []config.ResourceReference{}, 247 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 248 Type: "string", 249 Mandatory: true, 250 Aliases: []config.Alias{{Name: "githubServerUrl"}}, 251 Default: `https://github.com`, 252 }, 253 { 254 Name: "title", 255 ResourceRef: []config.ResourceReference{}, 256 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 257 Type: "string", 258 Mandatory: true, 259 Aliases: []config.Alias{}, 260 Default: os.Getenv("PIPER_title"), 261 }, 262 { 263 Name: "token", 264 ResourceRef: []config.ResourceReference{ 265 { 266 Name: "githubTokenCredentialsId", 267 Type: "secret", 268 }, 269 270 { 271 Name: "githubVaultSecretName", 272 Type: "vaultSecret", 273 Default: "github", 274 }, 275 }, 276 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 277 Type: "string", 278 Mandatory: true, 279 Aliases: []config.Alias{{Name: "githubToken"}, {Name: "access_token"}}, 280 Default: os.Getenv("PIPER_token"), 281 }, 282 { 283 Name: "labels", 284 ResourceRef: []config.ResourceReference{}, 285 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 286 Type: "[]string", 287 Mandatory: false, 288 Aliases: []config.Alias{}, 289 Default: []string{}, 290 }, 291 }, 292 }, 293 }, 294 } 295 return theMetaData 296 }