github.com/SAP/jenkins-library@v1.362.0/cmd/githubCreateIssue_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 githubCreateIssueOptions struct { 19 APIURL string `json:"apiUrl,omitempty"` 20 Assignees []string `json:"assignees,omitempty"` 21 ChunkSize int `json:"chunkSize,omitempty"` 22 Body string `json:"body,omitempty"` 23 BodyFilePath string `json:"bodyFilePath,omitempty"` 24 Owner string `json:"owner,omitempty"` 25 Repository string `json:"repository,omitempty"` 26 Title string `json:"title,omitempty"` 27 UpdateExisting bool `json:"updateExisting,omitempty"` 28 Token string `json:"token,omitempty"` 29 } 30 31 // GithubCreateIssueCommand Create a new GitHub issue. 32 func GithubCreateIssueCommand() *cobra.Command { 33 const STEP_NAME = "githubCreateIssue" 34 35 metadata := githubCreateIssueMetadata() 36 var stepConfig githubCreateIssueOptions 37 var startTime time.Time 38 var logCollector *log.CollectorHook 39 var splunkClient *splunk.Splunk 40 telemetryClient := &telemetry.Telemetry{} 41 42 var createGithubCreateIssueCmd = &cobra.Command{ 43 Use: STEP_NAME, 44 Short: "Create a new GitHub issue.", 45 Long: `This step allows you to create a new GitHub issue. 46 47 You will be able to use this step for example for regular jobs to report into your repository in case of new security findings.`, 48 PreRunE: func(cmd *cobra.Command, _ []string) error { 49 startTime = time.Now() 50 log.SetStepName(STEP_NAME) 51 log.SetVerbose(GeneralConfig.Verbose) 52 53 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 54 55 path, _ := os.Getwd() 56 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 57 log.RegisterHook(fatalHook) 58 59 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 60 if err != nil { 61 log.SetErrorCategory(log.ErrorConfiguration) 62 return err 63 } 64 log.RegisterSecret(stepConfig.Token) 65 66 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 67 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 68 log.RegisterHook(&sentryHook) 69 } 70 71 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 72 splunkClient = &splunk.Splunk{} 73 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 74 log.RegisterHook(logCollector) 75 } 76 77 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 78 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 79 } 80 81 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 82 if err != nil { 83 return err 84 } 85 if err = validation.ValidateStruct(stepConfig); err != nil { 86 log.SetErrorCategory(log.ErrorConfiguration) 87 return err 88 } 89 90 return nil 91 }, 92 Run: func(_ *cobra.Command, _ []string) { 93 stepTelemetryData := telemetry.CustomData{} 94 stepTelemetryData.ErrorCode = "1" 95 handler := func() { 96 config.RemoveVaultSecretFiles() 97 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 98 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 99 stepTelemetryData.PiperCommitHash = GitCommit 100 telemetryClient.SetData(&stepTelemetryData) 101 telemetryClient.Send() 102 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 103 splunkClient.Initialize(GeneralConfig.CorrelationID, 104 GeneralConfig.HookConfig.SplunkConfig.Dsn, 105 GeneralConfig.HookConfig.SplunkConfig.Token, 106 GeneralConfig.HookConfig.SplunkConfig.Index, 107 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 108 splunkClient.Send(telemetryClient.GetData(), logCollector) 109 } 110 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 111 splunkClient.Initialize(GeneralConfig.CorrelationID, 112 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 113 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 114 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 115 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 116 splunkClient.Send(telemetryClient.GetData(), logCollector) 117 } 118 } 119 log.DeferExitHandler(handler) 120 defer handler() 121 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token) 122 githubCreateIssue(stepConfig, &stepTelemetryData) 123 stepTelemetryData.ErrorCode = "0" 124 log.Entry().Info("SUCCESS") 125 }, 126 } 127 128 addGithubCreateIssueFlags(createGithubCreateIssueCmd, &stepConfig) 129 return createGithubCreateIssueCmd 130 } 131 132 func addGithubCreateIssueFlags(cmd *cobra.Command, stepConfig *githubCreateIssueOptions) { 133 cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API url.") 134 cmd.Flags().StringSliceVar(&stepConfig.Assignees, "assignees", []string{``}, "Defines the assignees for the Issue.") 135 cmd.Flags().IntVar(&stepConfig.ChunkSize, "chunkSize", 65500, "Defines size of the chunk. If content exceed chunk size it'll be sliced into chunks and stored in comments") 136 cmd.Flags().StringVar(&stepConfig.Body, "body", os.Getenv("PIPER_body"), "Defines the content of the issue, e.g. using markdown syntax.") 137 cmd.Flags().StringVar(&stepConfig.BodyFilePath, "bodyFilePath", os.Getenv("PIPER_bodyFilePath"), "Defines the path to a file containing the markdown content for the issue. This can be used instead of [`body`](#body)") 138 cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.") 139 cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.") 140 cmd.Flags().StringVar(&stepConfig.Title, "title", os.Getenv("PIPER_title"), "Defines the title for the Issue.") 141 cmd.Flags().BoolVar(&stepConfig.UpdateExisting, "updateExisting", false, "Whether to update an existing open issue with the same title by adding a comment instead of creating a new one.") 142 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.") 143 144 cmd.MarkFlagRequired("apiUrl") 145 cmd.MarkFlagRequired("owner") 146 cmd.MarkFlagRequired("repository") 147 cmd.MarkFlagRequired("title") 148 cmd.MarkFlagRequired("token") 149 } 150 151 // retrieve step metadata 152 func githubCreateIssueMetadata() config.StepData { 153 var theMetaData = config.StepData{ 154 Metadata: config.StepMetadata{ 155 Name: "githubCreateIssue", 156 Aliases: []config.Alias{}, 157 Description: "Create a new GitHub issue.", 158 }, 159 Spec: config.StepSpec{ 160 Inputs: config.StepInputs{ 161 Secrets: []config.StepSecrets{ 162 {Name: "githubTokenCredentialsId", Description: "Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.", Type: "jenkins"}, 163 }, 164 Parameters: []config.StepParameters{ 165 { 166 Name: "apiUrl", 167 ResourceRef: []config.ResourceReference{}, 168 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 169 Type: "string", 170 Mandatory: true, 171 Aliases: []config.Alias{{Name: "githubApiUrl"}}, 172 Default: `https://api.github.com`, 173 }, 174 { 175 Name: "assignees", 176 ResourceRef: []config.ResourceReference{}, 177 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 178 Type: "[]string", 179 Mandatory: false, 180 Aliases: []config.Alias{}, 181 Default: []string{``}, 182 }, 183 { 184 Name: "chunkSize", 185 ResourceRef: []config.ResourceReference{}, 186 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 187 Type: "int", 188 Mandatory: false, 189 Aliases: []config.Alias{}, 190 Default: 65500, 191 }, 192 { 193 Name: "body", 194 ResourceRef: []config.ResourceReference{}, 195 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 196 Type: "string", 197 Mandatory: false, 198 Aliases: []config.Alias{}, 199 Default: os.Getenv("PIPER_body"), 200 }, 201 { 202 Name: "bodyFilePath", 203 ResourceRef: []config.ResourceReference{}, 204 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 205 Type: "string", 206 Mandatory: false, 207 Aliases: []config.Alias{}, 208 Default: os.Getenv("PIPER_bodyFilePath"), 209 }, 210 { 211 Name: "owner", 212 ResourceRef: []config.ResourceReference{ 213 { 214 Name: "commonPipelineEnvironment", 215 Param: "github/owner", 216 }, 217 }, 218 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 219 Type: "string", 220 Mandatory: true, 221 Aliases: []config.Alias{{Name: "githubOrg"}}, 222 Default: os.Getenv("PIPER_owner"), 223 }, 224 { 225 Name: "repository", 226 ResourceRef: []config.ResourceReference{ 227 { 228 Name: "commonPipelineEnvironment", 229 Param: "github/repository", 230 }, 231 }, 232 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 233 Type: "string", 234 Mandatory: true, 235 Aliases: []config.Alias{{Name: "githubRepo"}}, 236 Default: os.Getenv("PIPER_repository"), 237 }, 238 { 239 Name: "title", 240 ResourceRef: []config.ResourceReference{}, 241 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 242 Type: "string", 243 Mandatory: true, 244 Aliases: []config.Alias{}, 245 Default: os.Getenv("PIPER_title"), 246 }, 247 { 248 Name: "updateExisting", 249 ResourceRef: []config.ResourceReference{}, 250 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 251 Type: "bool", 252 Mandatory: false, 253 Aliases: []config.Alias{}, 254 Default: false, 255 }, 256 { 257 Name: "token", 258 ResourceRef: []config.ResourceReference{ 259 { 260 Name: "githubTokenCredentialsId", 261 Type: "secret", 262 }, 263 264 { 265 Name: "githubVaultSecretName", 266 Type: "vaultSecret", 267 Default: "github", 268 }, 269 }, 270 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 271 Type: "string", 272 Mandatory: true, 273 Aliases: []config.Alias{{Name: "githubToken"}, {Name: "access_token"}}, 274 Default: os.Getenv("PIPER_token"), 275 }, 276 }, 277 }, 278 }, 279 } 280 return theMetaData 281 }