2020-09-14 18:08:24 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
|
2022-08-02 08:26:26 +02:00
|
|
|
"github.com/google/go-github/v45/github"
|
2020-09-14 18:08:24 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ghSetCommitRepoService struct {
|
|
|
|
serviceError error
|
|
|
|
owner string
|
|
|
|
ref string
|
|
|
|
repo string
|
|
|
|
status *github.RepoStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ghSetCommitRepoService) CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (*github.RepoStatus, *github.Response, error) {
|
|
|
|
g.owner = owner
|
|
|
|
g.repo = repo
|
|
|
|
g.ref = ref
|
|
|
|
g.status = status
|
|
|
|
|
|
|
|
return nil, nil, g.serviceError
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGithubSetCommitStatus(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
|
|
|
t.Run("success case", func(t *testing.T) {
|
2020-09-15 17:50:55 +02:00
|
|
|
config := githubSetCommitStatusOptions{CommitID: "testSha", Context: "test /context", Description: "testDescription", Owner: "testOrg", Repository: "testRepo", Status: "success", TargetURL: "https://test.url"}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghSetCommitRepoService{}
|
|
|
|
err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo)
|
2020-09-15 17:50:55 +02:00
|
|
|
expectedStatus := github.RepoStatus{Context: &config.Context, Description: &config.Description, State: &config.Status, TargetURL: &config.TargetURL}
|
2020-09-14 18:08:24 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.CommitID, ghRepo.ref)
|
|
|
|
assert.Equal(t, config.Owner, ghRepo.owner)
|
|
|
|
assert.Equal(t, config.Repository, ghRepo.repo)
|
|
|
|
assert.Equal(t, &expectedStatus, ghRepo.status)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error calling GitHub", func(t *testing.T) {
|
|
|
|
config := githubSetCommitStatusOptions{CommitID: "testSha", Owner: "testOrg", Repository: "testRepo", Status: "pending"}
|
|
|
|
ghRepo := ghSetCommitRepoService{serviceError: fmt.Errorf("gh test error")}
|
|
|
|
err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.EqualError(t, err, "failed to set status 'pending' on commitId 'testSha': gh test error")
|
|
|
|
})
|
|
|
|
}
|