github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/githubCreateIssue.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/SAP/jenkins-library/pkg/log" 7 "github.com/SAP/jenkins-library/pkg/piperutils" 8 "github.com/SAP/jenkins-library/pkg/telemetry" 9 "github.com/pkg/errors" 10 11 piperGithub "github.com/SAP/jenkins-library/pkg/github" 12 github "github.com/google/go-github/v45/github" 13 ) 14 15 type githubCreateIssueUtils interface { 16 FileRead(string) ([]byte, error) 17 } 18 19 func githubCreateIssue(config githubCreateIssueOptions, telemetryData *telemetry.CustomData) { 20 fileUtils := &piperutils.Files{} 21 options := piperGithub.CreateIssueOptions{} 22 err := runGithubCreateIssue(&config, telemetryData, &options, fileUtils, piperGithub.CreateIssue) 23 if err != nil { 24 log.Entry().WithError(err).Fatal("Failed to comment on issue") 25 } 26 } 27 28 func runGithubCreateIssue(config *githubCreateIssueOptions, _ *telemetry.CustomData, options *piperGithub.CreateIssueOptions, utils githubCreateIssueUtils, createIssue func(*piperGithub.CreateIssueOptions) (*github.Issue, error)) error { 29 chunks, err := getBody(config, utils.FileRead) 30 if err != nil { 31 return err 32 } 33 transformConfig(config, options, chunks[0]) 34 issue, err := createIssue(options) 35 if err != nil { 36 return err 37 } 38 if len(chunks) > 1 { 39 for _, v := range chunks[1:] { 40 options.Body = []byte(v) 41 options.Issue = issue 42 options.UpdateExisting = true 43 _, err = createIssue(options) 44 if err != nil { 45 return err 46 } 47 } 48 } 49 return nil 50 } 51 52 func getBody(config *githubCreateIssueOptions, readFile func(string) ([]byte, error)) ([]string, error) { 53 var bodyString []rune 54 if len(config.Body)+len(config.BodyFilePath) == 0 { 55 return nil, fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required") 56 } 57 if len(config.Body) == 0 { 58 issueContent, err := readFile(config.BodyFilePath) 59 if err != nil { 60 return nil, errors.Wrapf(err, "failed to read file '%v'", config.BodyFilePath) 61 } 62 bodyString = []rune(string(issueContent)) 63 } else { 64 bodyString = []rune(config.Body) 65 } 66 return getChunks(bodyString, config.ChunkSize), nil 67 } 68 69 func transformConfig(config *githubCreateIssueOptions, options *piperGithub.CreateIssueOptions, body string) { 70 options.Token = config.Token 71 options.APIURL = config.APIURL 72 options.Owner = config.Owner 73 options.Repository = config.Repository 74 options.Title = config.Title 75 options.Body = []byte(config.Body) 76 options.Assignees = config.Assignees 77 options.UpdateExisting = config.UpdateExisting 78 options.Body = []byte(body) 79 } 80 81 func getChunks(value []rune, chunkSize int) []string { 82 chunks := []string{} 83 length := len(value) 84 if length == 0 { 85 return []string{""} 86 } 87 for i := 0; i < length; i += chunkSize { 88 to := length 89 if to > i+chunkSize { 90 to = i + chunkSize 91 } 92 chunks = append(chunks, string(value[i:to])) 93 } 94 return chunks 95 }