github.com/jaylevin/jenkins-library@v1.230.4/cmd/githubCreateIssue.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/telemetry"
     9  	"github.com/pkg/errors"
    10  
    11  	piperGithub "github.com/SAP/jenkins-library/pkg/github"
    12  )
    13  
    14  func githubCreateIssue(config githubCreateIssueOptions, telemetryData *telemetry.CustomData) {
    15  	err := runGithubCreateIssue(&config, telemetryData)
    16  	if err != nil {
    17  		log.Entry().WithError(err).Fatal("Failed to comment on issue")
    18  	}
    19  }
    20  
    21  func runGithubCreateIssue(config *githubCreateIssueOptions, _ *telemetry.CustomData) error {
    22  
    23  	options := piperGithub.CreateIssueOptions{}
    24  	err := transformConfig(config, &options, ioutil.ReadFile)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	return piperGithub.CreateIssue(&options)
    30  }
    31  
    32  func transformConfig(config *githubCreateIssueOptions, options *piperGithub.CreateIssueOptions, readFile func(string) ([]byte, error)) error {
    33  	options.Token = config.Token
    34  	options.APIURL = config.APIURL
    35  	options.Owner = config.Owner
    36  	options.Repository = config.Repository
    37  	options.Title = config.Title
    38  	options.Body = []byte(config.Body)
    39  	options.Assignees = config.Assignees
    40  	options.UpdateExisting = config.UpdateExisting
    41  
    42  	if len(config.Body)+len(config.BodyFilePath) == 0 {
    43  		return fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required")
    44  	}
    45  	if len(config.Body) == 0 {
    46  		issueContent, err := readFile(config.BodyFilePath)
    47  		if err != nil {
    48  			return errors.Wrapf(err, "failed to read file '%v'", config.BodyFilePath)
    49  		}
    50  		options.Body = issueContent
    51  	}
    52  	return nil
    53  }