github.com/xgoffin/jenkins-library@v1.154.0/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 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 79 if err != nil { 80 return err 81 } 82 if err = validation.ValidateStruct(stepConfig); err != nil { 83 log.SetErrorCategory(log.ErrorConfiguration) 84 return err 85 } 86 87 return nil 88 }, 89 Run: func(_ *cobra.Command, _ []string) { 90 stepTelemetryData := telemetry.CustomData{} 91 stepTelemetryData.ErrorCode = "1" 92 handler := func() { 93 config.RemoveVaultSecretFiles() 94 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 95 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 96 stepTelemetryData.PiperCommitHash = GitCommit 97 telemetryClient.SetData(&stepTelemetryData) 98 telemetryClient.Send() 99 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 100 splunkClient.Send(telemetryClient.GetData(), logCollector) 101 } 102 } 103 log.DeferExitHandler(handler) 104 defer handler() 105 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 106 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 107 splunkClient.Initialize(GeneralConfig.CorrelationID, 108 GeneralConfig.HookConfig.SplunkConfig.Dsn, 109 GeneralConfig.HookConfig.SplunkConfig.Token, 110 GeneralConfig.HookConfig.SplunkConfig.Index, 111 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 112 } 113 githubCreatePullRequest(stepConfig, &stepTelemetryData) 114 stepTelemetryData.ErrorCode = "0" 115 log.Entry().Info("SUCCESS") 116 }, 117 } 118 119 addGithubCreatePullRequestFlags(createGithubCreatePullRequestCmd, &stepConfig) 120 return createGithubCreatePullRequestCmd 121 } 122 123 func addGithubCreatePullRequestFlags(cmd *cobra.Command, stepConfig *githubCreatePullRequestOptions) { 124 cmd.Flags().StringSliceVar(&stepConfig.Assignees, "assignees", []string{}, "Login names of users to which the PR should be assigned to.") 125 cmd.Flags().StringVar(&stepConfig.Base, "base", os.Getenv("PIPER_base"), "The name of the branch you want the changes pulled into.") 126 cmd.Flags().StringVar(&stepConfig.Body, "body", os.Getenv("PIPER_body"), "The description text of the pull request in markdown format.") 127 cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API url.") 128 cmd.Flags().StringVar(&stepConfig.Head, "head", os.Getenv("PIPER_head"), "The name of the branch where your changes are implemented.") 129 cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.") 130 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.") 131 cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", `https://github.com`, "GitHub server url for end-user access.") 132 cmd.Flags().StringVar(&stepConfig.Title, "title", os.Getenv("PIPER_title"), "Title of the pull request.") 133 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") 134 cmd.Flags().StringSliceVar(&stepConfig.Labels, "labels", []string{}, "Labels to be added to the pull request.") 135 136 cmd.MarkFlagRequired("base") 137 cmd.MarkFlagRequired("body") 138 cmd.MarkFlagRequired("apiUrl") 139 cmd.MarkFlagRequired("head") 140 cmd.MarkFlagRequired("owner") 141 cmd.MarkFlagRequired("repository") 142 cmd.MarkFlagRequired("serverUrl") 143 cmd.MarkFlagRequired("title") 144 cmd.MarkFlagRequired("token") 145 } 146 147 // retrieve step metadata 148 func githubCreatePullRequestMetadata() config.StepData { 149 var theMetaData = config.StepData{ 150 Metadata: config.StepMetadata{ 151 Name: "githubCreatePullRequest", 152 Aliases: []config.Alias{}, 153 Description: "Create a pull request on GitHub", 154 }, 155 Spec: config.StepSpec{ 156 Inputs: config.StepInputs{ 157 Secrets: []config.StepSecrets{ 158 {Name: "githubTokenCredentialsId", Description: "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.", Type: "jenkins"}, 159 }, 160 Parameters: []config.StepParameters{ 161 { 162 Name: "assignees", 163 ResourceRef: []config.ResourceReference{}, 164 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 165 Type: "[]string", 166 Mandatory: false, 167 Aliases: []config.Alias{}, 168 Default: []string{}, 169 }, 170 { 171 Name: "base", 172 ResourceRef: []config.ResourceReference{}, 173 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 174 Type: "string", 175 Mandatory: true, 176 Aliases: []config.Alias{}, 177 Default: os.Getenv("PIPER_base"), 178 }, 179 { 180 Name: "body", 181 ResourceRef: []config.ResourceReference{}, 182 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 183 Type: "string", 184 Mandatory: true, 185 Aliases: []config.Alias{}, 186 Default: os.Getenv("PIPER_body"), 187 }, 188 { 189 Name: "apiUrl", 190 ResourceRef: []config.ResourceReference{}, 191 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 192 Type: "string", 193 Mandatory: true, 194 Aliases: []config.Alias{{Name: "githubApiUrl"}}, 195 Default: `https://api.github.com`, 196 }, 197 { 198 Name: "head", 199 ResourceRef: []config.ResourceReference{}, 200 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 201 Type: "string", 202 Mandatory: true, 203 Aliases: []config.Alias{}, 204 Default: os.Getenv("PIPER_head"), 205 }, 206 { 207 Name: "owner", 208 ResourceRef: []config.ResourceReference{ 209 { 210 Name: "commonPipelineEnvironment", 211 Param: "github/owner", 212 }, 213 }, 214 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 215 Type: "string", 216 Mandatory: true, 217 Aliases: []config.Alias{{Name: "githubOrg"}}, 218 Default: os.Getenv("PIPER_owner"), 219 }, 220 { 221 Name: "repository", 222 ResourceRef: []config.ResourceReference{ 223 { 224 Name: "commonPipelineEnvironment", 225 Param: "github/repository", 226 }, 227 }, 228 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 229 Type: "string", 230 Mandatory: true, 231 Aliases: []config.Alias{{Name: "githubRepo"}}, 232 Default: os.Getenv("PIPER_repository"), 233 }, 234 { 235 Name: "serverUrl", 236 ResourceRef: []config.ResourceReference{}, 237 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 238 Type: "string", 239 Mandatory: true, 240 Aliases: []config.Alias{{Name: "githubServerUrl"}}, 241 Default: `https://github.com`, 242 }, 243 { 244 Name: "title", 245 ResourceRef: []config.ResourceReference{}, 246 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 247 Type: "string", 248 Mandatory: true, 249 Aliases: []config.Alias{}, 250 Default: os.Getenv("PIPER_title"), 251 }, 252 { 253 Name: "token", 254 ResourceRef: []config.ResourceReference{ 255 { 256 Name: "githubTokenCredentialsId", 257 Type: "secret", 258 }, 259 260 { 261 Name: "githubVaultSecretName", 262 Type: "vaultSecret", 263 Default: "github", 264 }, 265 }, 266 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 267 Type: "string", 268 Mandatory: true, 269 Aliases: []config.Alias{{Name: "githubToken"}, {Name: "access_token"}}, 270 Default: os.Getenv("PIPER_token"), 271 }, 272 { 273 Name: "labels", 274 ResourceRef: []config.ResourceReference{}, 275 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 276 Type: "[]string", 277 Mandatory: false, 278 Aliases: []config.Alias{}, 279 Default: []string{}, 280 }, 281 }, 282 }, 283 }, 284 } 285 return theMetaData 286 }