2020-12-21 18:13:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-03-19 14:04:30 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-12-21 18:13:16 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
piperGithub "github.com/SAP/jenkins-library/pkg/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
func githubCreateIssue(config githubCreateIssueOptions, telemetryData *telemetry.CustomData) {
|
2022-01-21 11:52:17 +02:00
|
|
|
err := runGithubCreateIssue(&config, telemetryData)
|
2020-12-21 18:13:16 +02:00
|
|
|
if err != nil {
|
2022-01-21 11:52:17 +02:00
|
|
|
log.Entry().WithError(err).Fatal("Failed to comment on issue")
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runGithubCreateIssue(config *githubCreateIssueOptions, _ *telemetry.CustomData) error {
|
|
|
|
|
|
|
|
options := piperGithub.CreateIssueOptions{}
|
|
|
|
err := transformConfig(config, &options, ioutil.ReadFile)
|
2020-12-21 18:13:16 +02:00
|
|
|
if err != nil {
|
2022-01-21 11:52:17 +02:00
|
|
|
return err
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
|
|
|
|
return piperGithub.CreateIssue(&options)
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
func transformConfig(config *githubCreateIssueOptions, options *piperGithub.CreateIssueOptions, readFile func(string) ([]byte, error)) error {
|
|
|
|
options.Token = config.Token
|
|
|
|
options.APIURL = config.APIURL
|
|
|
|
options.Owner = config.Owner
|
|
|
|
options.Repository = config.Repository
|
|
|
|
options.Title = config.Title
|
|
|
|
options.Body = []byte(config.Body)
|
|
|
|
options.Assignees = config.Assignees
|
|
|
|
options.UpdateExisting = config.UpdateExisting
|
2021-03-19 14:04:30 +02:00
|
|
|
|
|
|
|
if len(config.Body)+len(config.BodyFilePath) == 0 {
|
|
|
|
return fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required")
|
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
if len(config.Body) == 0 {
|
2021-03-19 14:04:30 +02:00
|
|
|
issueContent, err := readFile(config.BodyFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read file '%v'", config.BodyFilePath)
|
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
options.Body = issueContent
|
2021-03-19 14:04:30 +02:00
|
|
|
}
|
2020-12-21 18:13:16 +02:00
|
|
|
return nil
|
|
|
|
}
|