Skip to content

Commit 3b484ea

Browse files
authored
Merge pull request #24 from mdb/improve-repo-flag
Improve repo flag and ensure it's properly set.
2 parents 7dfe0a5 + 2c3a6ff commit 3b484ea

File tree

9 files changed

+141
-56
lines changed

9 files changed

+141
-56
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.2
3+
VERSION?=0.1.3
44

55
default: build
66

cmd/main.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,16 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

7-
"github.com/MakeNowJust/heredoc"
8-
"github.com/cli/go-gh"
96
"github.com/mdb/gh-dispatch/internal/dispatch"
10-
"github.com/spf13/cobra"
117
)
128

139
// version's value is passed in at build time.
1410
var version string
1511

1612
func main() {
17-
rootCmd := &cobra.Command{
18-
Use: "gh dispatch",
19-
Short: "Send a GitHub dispatch event and watch the resulting GitHub Actions run",
20-
Long: heredoc.Doc(`
21-
Send a workflow_dispatch or repository_dispatch event and watch the resulting
22-
GitHub Actions run.
23-
`),
24-
SilenceUsage: true,
25-
Version: version,
26-
}
27-
28-
defaultRepo := ""
29-
currentRepo, _ := gh.CurrentRepository()
30-
if currentRepo != nil {
31-
defaultRepo = fmt.Sprintf("%s/%s", currentRepo.Owner(), currentRepo.Name())
32-
}
33-
34-
// TODO: how to make this required?
35-
rootCmd.PersistentFlags().StringP("repo", "R", defaultRepo, "The targeted repository's full name (in 'owner/repo' format)")
36-
37-
repositoryCmd := dispatch.NewCmdRepository()
38-
rootCmd.AddCommand(repositoryCmd)
39-
40-
workflowCmd := dispatch.NewCmdWorkflow()
41-
rootCmd.AddCommand(workflowCmd)
13+
rootCmd := dispatch.NewCmdRoot(version)
4214

4315
if err := rootCmd.Execute(); err != nil {
4416
os.Exit(1)

cmd/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Available Commands:
4141
4242
Flags:
4343
-h, --help help for gh
44-
-R, --repo string The targeted repository's full name (in 'owner/repo' format) (default "mdb/gh-dispatch")
44+
-R, --repo string The targeted repository's full name (default "github.com/mdb/gh-dispatch")
4545
-v, --version version for gh
4646
4747
Use "gh [command] --help" for more information about a command.

internal/dispatch/ghrepo.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
11
package dispatch
22

33
import (
4+
"errors"
45
"fmt"
6+
"strings"
57

68
"github.com/cli/go-gh/pkg/auth"
9+
"github.com/spf13/cobra"
710
)
811

12+
func getRepoOption(cmd *cobra.Command) (*ghRepo, error) {
13+
r, _ := cmd.Flags().GetString("repo")
14+
if r == "" {
15+
return nil, errors.New("A --repo must be specified in the [HOST/]OWNER/REPO format")
16+
}
17+
18+
repo, err := newGHRepo(r)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
return repo, nil
24+
}
25+
926
// ghRepo satisfies the ghrepo interface.
1027
// In the context of gh-dispatch, it enables the reuse of
1128
// functions packaged in the upstream github.com/cli/cli
1229
// codebase for rendering GH Actions run output.
1330
// See github.com/cli/cli/v2/internal/ghrepo.
1431
type ghRepo struct {
15-
Name string
1632
Owner string
33+
Name string
34+
Host string
35+
}
36+
37+
func newGHRepo(name string) (*ghRepo, error) {
38+
defaultHost, _ := auth.DefaultHost()
39+
nameParts := strings.Split(name, "/")
40+
41+
switch len(nameParts) {
42+
case 2:
43+
return &ghRepo{
44+
Owner: nameParts[0],
45+
Name: nameParts[1],
46+
Host: defaultHost,
47+
}, nil
48+
case 3:
49+
return &ghRepo{
50+
Owner: nameParts[1],
51+
Name: nameParts[2],
52+
Host: nameParts[0],
53+
}, nil
54+
default:
55+
return nil, errors.New("invalid repository name")
56+
}
1757
}
1858

1959
func (r ghRepo) RepoName() string {
@@ -25,9 +65,7 @@ func (r ghRepo) RepoOwner() string {
2565
}
2666

2767
func (r ghRepo) RepoHost() string {
28-
host, _ := auth.DefaultHost()
29-
30-
return host
68+
return r.Host
3169
}
3270

3371
func (r ghRepo) RepoFullName() string {

internal/dispatch/ghrepo_test.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,53 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func TestGHRepo(t *testing.T) {
10-
ghRepo := &ghRepo{
11-
Name: "REPO",
12-
Owner: "OWNER",
13-
}
9+
func TestNewGHRepo(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
wantName string
13+
wantOwner string
14+
wantHost string
15+
wantFullName string
16+
wantErr bool
17+
errMsg string
18+
}{
19+
{
20+
name: "foo/bar",
21+
wantOwner: "foo",
22+
wantName: "bar",
23+
wantFullName: "foo/bar",
24+
wantHost: "github.com",
25+
wantErr: false,
26+
}, {
27+
name: "other-github.com/foo/bar",
28+
wantOwner: "foo",
29+
wantName: "bar",
30+
wantFullName: "foo/bar",
31+
wantHost: "other-github.com",
32+
wantErr: false,
33+
}, {
34+
name: "bar",
35+
wantErr: true,
36+
errMsg: "invalid repository name",
37+
}, {
38+
name: "",
39+
wantErr: true,
40+
errMsg: "invalid repository name",
41+
}}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
ghr, err := newGHRepo(tt.name)
1446

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())
47+
if tt.wantErr {
48+
assert.EqualError(t, err, tt.errMsg)
49+
} else {
50+
assert.NoError(t, err)
51+
assert.Equal(t, tt.wantFullName, ghr.RepoFullName())
52+
assert.Equal(t, tt.wantOwner, ghr.RepoOwner())
53+
assert.Equal(t, tt.wantName, ghr.RepoName())
54+
assert.Equal(t, tt.wantHost, ghr.RepoHost())
55+
}
56+
})
57+
}
1958
}

internal/dispatch/repository.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"strings"
87

98
"github.com/MakeNowJust/heredoc"
109
cliapi "github.com/cli/cli/v2/api"
@@ -60,11 +59,9 @@ func NewCmdRepository() *cobra.Command {
6059
--workflow Hello
6160
`),
6261
RunE: func(cmd *cobra.Command, args []string) error {
63-
r, _ := cmd.Flags().GetString("repo")
64-
repoParts := strings.Split(r, "/")
65-
repo := &ghRepo{
66-
Owner: repoParts[0],
67-
Name: repoParts[1],
62+
repo, err := getRepoOption(cmd)
63+
if err != nil {
64+
return err
6865
}
6966

7067
b := []byte(repositoryClientPayload)

internal/dispatch/root.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dispatch
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/cli/go-gh"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewCmdRoot(version string) *cobra.Command {
12+
rootCmd := &cobra.Command{
13+
Use: "gh dispatch",
14+
Short: "Send a GitHub dispatch event and watch the resulting GitHub Actions run",
15+
Long: heredoc.Doc(`
16+
Send a workflow_dispatch or repository_dispatch event and watch the resulting
17+
GitHub Actions run.
18+
`),
19+
SilenceUsage: true,
20+
Version: version,
21+
}
22+
23+
defaultRepo := ""
24+
currentRepo, _ := gh.CurrentRepository()
25+
if currentRepo != nil {
26+
defaultRepo = fmt.Sprintf("%s/%s/%s", currentRepo.Host(), currentRepo.Owner(), currentRepo.Name())
27+
}
28+
29+
var repo string
30+
rootCmd.PersistentFlags().StringVarP(&repo, "repo", "R", defaultRepo, "The targeted repository's full name")
31+
32+
repositoryCmd := NewCmdRepository()
33+
rootCmd.AddCommand(repositoryCmd)
34+
35+
workflowCmd := NewCmdWorkflow()
36+
rootCmd.AddCommand(workflowCmd)
37+
38+
return rootCmd
39+
}

internal/dispatch/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package dispatch
22

33
import (
4+
"errors"
45
"net/http"
56

67
"github.com/cli/cli/v2/pkg/cmd/run/shared"
78
"github.com/cli/cli/v2/pkg/iostreams"
89
)
910

11+
var errUnspecifiedRepo = errors.New("A --repo must be specified in the [HOST/]OWNER/REPO format")
12+
1013
type workflowRun struct {
1114
ID int64 `json:"id"`
1215
WorkflowID int `json:"workflow_id"`

internal/dispatch/workflow.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"strings"
87

98
"github.com/MakeNowJust/heredoc"
109
cliapi "github.com/cli/cli/v2/api"
@@ -65,11 +64,9 @@ func NewCmdWorkflow() *cobra.Command {
6564
--ref my-feature-branch
6665
`),
6766
RunE: func(cmd *cobra.Command, args []string) error {
68-
r, _ := cmd.Flags().GetString("repo")
69-
repoParts := strings.Split(r, "/")
70-
repo := &ghRepo{
71-
Owner: repoParts[0],
72-
Name: repoParts[1],
67+
repo, err := getRepoOption(cmd)
68+
if err != nil {
69+
return err
7370
}
7471

7572
b := []byte(workflowInputs)

0 commit comments

Comments
 (0)