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