Skip to content

Commit

Permalink
Merge pull request #22 from mdb/improve-tests
Browse files Browse the repository at this point in the history
improve unit tests!
  • Loading branch information
mdb committed Nov 18, 2022
2 parents b493ec1 + d43f325 commit 712cdb9
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 329 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
SOURCE=./...
GOFMT_FILES?=$$(find . -type f -name '*.go')
VERSION?=0.1.0
VERSION?=0.1.1

default: build

Expand Down
21 changes: 21 additions & 0 deletions internal/dispatch/conf.go
@@ -0,0 +1,21 @@
package dispatch

import (
"github.com/cli/go-gh/pkg/auth"
"github.com/cli/go-gh/pkg/config"
)

// Conf implements the cliapi tokenGetter interface.
// In the context of gh-dispatch, its only purpose is to provide
// a configuration to the GH HTTP client configuration that
// enables the retrieval of an auth token in the same standard way
// that gh itself retrieves the auth token.
type Conf struct {
*config.Config
}

// AuthToken implements the cliapi tokenGetter interface
// by providing a method for retrieving the auth token.
func (c *Conf) AuthToken(hostname string) (string, string) {
return auth.TokenForHost(hostname)
}
35 changes: 35 additions & 0 deletions internal/dispatch/ghrepo.go
@@ -0,0 +1,35 @@
package dispatch

import (
"fmt"

"github.com/cli/go-gh/pkg/auth"
)

// ghRepo satisfies the ghrepo interface.
// In the context of gh-dispatch, it enables the reuse of
// functions packaged in the upstream github.com/cli/cli
// codebase for rendering GH Actions run output.
// See github.com/cli/cli/v2/internal/ghrepo.
type ghRepo struct {
Name string
Owner string
}

func (r ghRepo) RepoName() string {
return r.Name
}

func (r ghRepo) RepoOwner() string {
return r.Owner
}

func (r ghRepo) RepoHost() string {
host, _ := auth.DefaultHost()

return host
}

func (r ghRepo) RepoFullName() string {
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
}
19 changes: 19 additions & 0 deletions internal/dispatch/ghrepo_test.go
@@ -0,0 +1,19 @@
package dispatch

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGHRepo(t *testing.T) {
ghRepo := &ghRepo{
Name: "REPO",
Owner: "OWNER",
}

assert.Equal(t, "OWNER/REPO", ghRepo.RepoFullName())
assert.Equal(t, "OWNER", ghRepo.RepoOwner())
assert.Equal(t, "REPO", ghRepo.RepoName())
assert.Equal(t, "github.com", ghRepo.RepoHost())
}
57 changes: 0 additions & 57 deletions internal/dispatch/shared.go → internal/dispatch/renderutils.go
Expand Up @@ -4,71 +4,14 @@ import (
"bytes"
"fmt"
"io"
"net/http"
"time"

cliapi "github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/go-gh/pkg/auth"
"github.com/cli/go-gh/pkg/config"
)

// Conf implements the cliapi tokenGetter interface
type Conf struct {
*config.Config
}

// AuthToken implements the cliapi tokenGetter interface
// by providing a method for retrieving the auth token.
func (c *Conf) AuthToken(hostname string) (string, string) {
return auth.TokenForHost(hostname)
}

type workflowRun struct {
ID int64 `json:"id"`
WorkflowID int `json:"workflow_id"`
Name string `json:"name"`
Status shared.Status `json:"status"`
Conclusion string `json:"conclusion"`
}

type workflowRunsResponse struct {
WorkflowRuns []workflowRun `json:"workflow_runs"`
}

type dispatchOptions struct {
repo *ghRepo
httpClient *http.Client
io *iostreams.IOStreams
}

// ghRepo satisfies the ghrepo interface...
// See github.com/cli/cli/v2/internal/ghrepo.
type ghRepo struct {
Name string
Owner string
}

func (r ghRepo) RepoName() string {
return r.Name
}

func (r ghRepo) RepoOwner() string {
return r.Owner
}

func (r ghRepo) RepoHost() string {
host, _ := auth.DefaultHost()

return host
}

func (r ghRepo) RepoFullName() string {
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
}

func render(ios *iostreams.IOStreams, client *cliapi.Client, repo *ghRepo, run *shared.Run) error {
cs := ios.ColorScheme()
annotationCache := map[int64][]shared.Annotation{}
Expand Down
5 changes: 3 additions & 2 deletions internal/dispatch/repository.go
Expand Up @@ -26,6 +26,7 @@ type repositoryDispatchOptions struct {
dispatchOptions
}

// NewCmdRepository returns a new repository command.
func NewCmdRepository() *cobra.Command {
var (
repositoryEventType string
Expand Down Expand Up @@ -100,6 +101,8 @@ func NewCmdRepository() *cobra.Command {
}

func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
ghClient := cliapi.NewClientFromHTTP(opts.httpClient)

var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(repositoryDispatchRequest{
EventType: opts.eventType,
Expand All @@ -109,8 +112,6 @@ func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
return err
}

ghClient := cliapi.NewClientFromHTTP(opts.httpClient)

var in interface{}
err = ghClient.REST(opts.repo.RepoHost(), "POST", fmt.Sprintf("repos/%s/dispatches", opts.repo.RepoFullName()), &buf, &in)
if err != nil {
Expand Down

0 comments on commit 712cdb9

Please sign in to comment.