github.com/xgoffin/jenkins-library@v1.154.0/cmd/githubCommentIssue.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/SAP/jenkins-library/pkg/log"
     7  	"github.com/SAP/jenkins-library/pkg/telemetry"
     8  	"github.com/google/go-github/v32/github"
     9  	"github.com/pkg/errors"
    10  
    11  	piperGithub "github.com/SAP/jenkins-library/pkg/github"
    12  )
    13  
    14  type githubIssueCommentService interface {
    15  	CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
    16  }
    17  
    18  func githubCommentIssue(config githubCommentIssueOptions, telemetryData *telemetry.CustomData) {
    19  	//TODO provide parameter for trusted certs
    20  	ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "", []string{})
    21  	if err != nil {
    22  		log.Entry().WithError(err).Fatal("Failed to get GitHub client")
    23  	}
    24  	err = runGithubCommentIssue(ctx, &config, telemetryData, client.Issues)
    25  	if err != nil {
    26  		log.Entry().WithError(err).Fatal("Failed to comment on issue")
    27  	}
    28  }
    29  
    30  func runGithubCommentIssue(ctx context.Context, config *githubCommentIssueOptions, _ *telemetry.CustomData, ghIssueCommentService githubIssueCommentService) error {
    31  	issueComment := github.IssueComment{
    32  		Body: &config.Body,
    33  	}
    34  
    35  	newcomment, resp, err := ghIssueCommentService.CreateComment(ctx, config.Owner, config.Repository, config.Number, &issueComment)
    36  	if err != nil {
    37  		log.Entry().Errorf("GitHub response code %v", resp.Status)
    38  		return errors.Wrapf(err, "Error occurred when creating comment on issue %v", config.Number)
    39  	}
    40  	log.Entry().Debugf("New issue comment created for issue %v: %v", config.Number, newcomment)
    41  
    42  	return nil
    43  }