Skip to content

Commit 712cdb9

Browse files
authored
Merge pull request #22 from mdb/improve-tests
improve unit tests!
2 parents b493ec1 + d43f325 commit 712cdb9

10 files changed

+258
-329
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SOURCE=./...
22
GOFMT_FILES?=$$(find . -type f -name '*.go')
3-
VERSION?=0.1.0
3+
VERSION?=0.1.1
44

55
default: build
66

internal/dispatch/conf.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dispatch
2+
3+
import (
4+
"github.com/cli/go-gh/pkg/auth"
5+
"github.com/cli/go-gh/pkg/config"
6+
)
7+
8+
// Conf implements the cliapi tokenGetter interface.
9+
// In the context of gh-dispatch, its only purpose is to provide
10+
// a configuration to the GH HTTP client configuration that
11+
// enables the retrieval of an auth token in the same standard way
12+
// that gh itself retrieves the auth token.
13+
type Conf struct {
14+
*config.Config
15+
}
16+
17+
// AuthToken implements the cliapi tokenGetter interface
18+
// by providing a method for retrieving the auth token.
19+
func (c *Conf) AuthToken(hostname string) (string, string) {
20+
return auth.TokenForHost(hostname)
21+
}

internal/dispatch/ghrepo.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dispatch
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/cli/go-gh/pkg/auth"
7+
)
8+
9+
// ghRepo satisfies the ghrepo interface.
10+
// In the context of gh-dispatch, it enables the reuse of
11+
// functions packaged in the upstream github.com/cli/cli
12+
// codebase for rendering GH Actions run output.
13+
// See github.com/cli/cli/v2/internal/ghrepo.
14+
type ghRepo struct {
15+
Name string
16+
Owner string
17+
}
18+
19+
func (r ghRepo) RepoName() string {
20+
return r.Name
21+
}
22+
23+
func (r ghRepo) RepoOwner() string {
24+
return r.Owner
25+
}
26+
27+
func (r ghRepo) RepoHost() string {
28+
host, _ := auth.DefaultHost()
29+
30+
return host
31+
}
32+
33+
func (r ghRepo) RepoFullName() string {
34+
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
35+
}

internal/dispatch/ghrepo_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dispatch
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGHRepo(t *testing.T) {
10+
ghRepo := &ghRepo{
11+
Name: "REPO",
12+
Owner: "OWNER",
13+
}
14+
15+
assert.Equal(t, "OWNER/REPO", ghRepo.RepoFullName())
16+
assert.Equal(t, "OWNER", ghRepo.RepoOwner())
17+
assert.Equal(t, "REPO", ghRepo.RepoName())
18+
assert.Equal(t, "github.com", ghRepo.RepoHost())
19+
}

internal/dispatch/shared.go renamed to internal/dispatch/renderutils.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,14 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"net/http"
87
"time"
98

109
cliapi "github.com/cli/cli/v2/api"
1110
"github.com/cli/cli/v2/pkg/cmd/run/shared"
1211
"github.com/cli/cli/v2/pkg/cmdutil"
1312
"github.com/cli/cli/v2/pkg/iostreams"
14-
"github.com/cli/go-gh/pkg/auth"
15-
"github.com/cli/go-gh/pkg/config"
1613
)
1714

18-
// Conf implements the cliapi tokenGetter interface
19-
type Conf struct {
20-
*config.Config
21-
}
22-
23-
// AuthToken implements the cliapi tokenGetter interface
24-
// by providing a method for retrieving the auth token.
25-
func (c *Conf) AuthToken(hostname string) (string, string) {
26-
return auth.TokenForHost(hostname)
27-
}
28-
29-
type workflowRun struct {
30-
ID int64 `json:"id"`
31-
WorkflowID int `json:"workflow_id"`
32-
Name string `json:"name"`
33-
Status shared.Status `json:"status"`
34-
Conclusion string `json:"conclusion"`
35-
}
36-
37-
type workflowRunsResponse struct {
38-
WorkflowRuns []workflowRun `json:"workflow_runs"`
39-
}
40-
41-
type dispatchOptions struct {
42-
repo *ghRepo
43-
httpClient *http.Client
44-
io *iostreams.IOStreams
45-
}
46-
47-
// ghRepo satisfies the ghrepo interface...
48-
// See github.com/cli/cli/v2/internal/ghrepo.
49-
type ghRepo struct {
50-
Name string
51-
Owner string
52-
}
53-
54-
func (r ghRepo) RepoName() string {
55-
return r.Name
56-
}
57-
58-
func (r ghRepo) RepoOwner() string {
59-
return r.Owner
60-
}
61-
62-
func (r ghRepo) RepoHost() string {
63-
host, _ := auth.DefaultHost()
64-
65-
return host
66-
}
67-
68-
func (r ghRepo) RepoFullName() string {
69-
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
70-
}
71-
7215
func render(ios *iostreams.IOStreams, client *cliapi.Client, repo *ghRepo, run *shared.Run) error {
7316
cs := ios.ColorScheme()
7417
annotationCache := map[int64][]shared.Annotation{}

internal/dispatch/repository.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type repositoryDispatchOptions struct {
2626
dispatchOptions
2727
}
2828

29+
// NewCmdRepository returns a new repository command.
2930
func NewCmdRepository() *cobra.Command {
3031
var (
3132
repositoryEventType string
@@ -100,6 +101,8 @@ func NewCmdRepository() *cobra.Command {
100101
}
101102

102103
func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
104+
ghClient := cliapi.NewClientFromHTTP(opts.httpClient)
105+
103106
var buf bytes.Buffer
104107
err := json.NewEncoder(&buf).Encode(repositoryDispatchRequest{
105108
EventType: opts.eventType,
@@ -109,8 +112,6 @@ func repositoryDispatchRun(opts *repositoryDispatchOptions) error {
109112
return err
110113
}
111114

112-
ghClient := cliapi.NewClientFromHTTP(opts.httpClient)
113-
114115
var in interface{}
115116
err = ghClient.REST(opts.repo.RepoHost(), "POST", fmt.Sprintf("repos/%s/dispatches", opts.repo.RepoFullName()), &buf, &in)
116117
if err != nil {

0 commit comments

Comments
 (0)