2020-09-14 18:08:24 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-10 17:44:28 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-09-14 18:08:24 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
"github.com/google/go-github/v32/github"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
piperGithub "github.com/SAP/jenkins-library/pkg/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
type gitHubCommitStatusRepositoriesService interface {
|
|
|
|
CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (*github.RepoStatus, *github.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func githubSetCommitStatus(config githubSetCommitStatusOptions, telemetryData *telemetry.CustomData) {
|
2022-02-23 10:30:19 +02:00
|
|
|
//TODO provide parameter for trusted certs
|
|
|
|
ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "", []string{})
|
2020-09-14 18:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Failed to get GitHub client")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = runGithubSetCommitStatus(ctx, &config, telemetryData, client.Repositories)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("GitHub status update failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGithubSetCommitStatus(ctx context.Context, config *githubSetCommitStatusOptions, telemetryData *telemetry.CustomData, ghRepositoriesService gitHubCommitStatusRepositoriesService) error {
|
2020-09-15 17:50:55 +02:00
|
|
|
status := github.RepoStatus{Context: &config.Context, Description: &config.Description, State: &config.Status, TargetURL: &config.TargetURL}
|
2020-09-14 18:08:24 +02:00
|
|
|
_, _, err := ghRepositoriesService.CreateStatus(ctx, config.Owner, config.Repository, config.CommitID, &status)
|
|
|
|
if err != nil {
|
2021-05-10 17:44:28 +02:00
|
|
|
if strings.Contains(fmt.Sprint(err), "No commit found for SHA") {
|
|
|
|
log.SetErrorCategory(log.ErrorCustom)
|
|
|
|
}
|
2020-09-14 18:08:24 +02:00
|
|
|
return errors.Wrapf(err, "failed to set status '%v' on commitId '%v'", config.Status, config.CommitID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|