X Tutup
Skip to content

Commit d440a95

Browse files
authored
Improved error message when "owner/repo" format not provided (cli#919)
Fixes cli#882
1 parent c115d32 commit d440a95

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

api/queries_issue_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ func TestIssueList(t *testing.T) {
3939
} } }
4040
`))
4141

42-
_, err := IssueList(client, ghrepo.FromFullName("OWNER/REPO"), "open", []string{}, "", 251, "")
42+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
43+
_, err := IssueList(client, repo, "open", []string{}, "", 251, "")
4344
if err != nil {
4445
t.Fatalf("unexpected error: %v", err)
4546
}

api/queries_repo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Test_RepoMetadata(t *testing.T) {
5050
http := &httpmock.Registry{}
5151
client := NewClient(ReplaceTripper(http))
5252

53-
repo := ghrepo.FromFullName("OWNER/REPO")
53+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
5454
input := RepoMetadataInput{
5555
Assignees: true,
5656
Reviewers: true,
@@ -181,7 +181,7 @@ func Test_RepoResolveMetadataIDs(t *testing.T) {
181181
http := &httpmock.Registry{}
182182
client := NewClient(ReplaceTripper(http))
183183

184-
repo := ghrepo.FromFullName("OWNER/REPO")
184+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
185185
input := RepoResolveInput{
186186
Assignees: []string{"monalisa", "hubot"},
187187
Reviewers: []string{"monalisa", "octocat", "OWNER/core", "/robots"},

command/repo.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ func repoCreate(cmd *cobra.Command, args []string) error {
189189
if len(args) > 0 {
190190
name = args[0]
191191
if strings.Contains(name, "/") {
192-
newRepo := ghrepo.FromFullName(name)
192+
newRepo, err := ghrepo.FromFullName(name)
193+
if err != nil {
194+
return fmt.Errorf("argument error: %w", err)
195+
}
193196
orgName = newRepo.RepoOwner()
194197
name = newRepo.RepoName()
195198
}
@@ -366,9 +369,9 @@ func repoFork(cmd *cobra.Command, args []string) error {
366369
return fmt.Errorf("did not understand argument: %w", err)
367370
}
368371
} else {
369-
repoToFork = ghrepo.FromFullName(repoArg)
370-
if repoToFork.RepoName() == "" || repoToFork.RepoOwner() == "" {
371-
return fmt.Errorf("could not parse owner or repo name from %s", repoArg)
372+
repoToFork, err = ghrepo.FromFullName(repoArg)
373+
if err != nil {
374+
return fmt.Errorf("argument error: %w", err)
372375
}
373376
}
374377
}
@@ -508,7 +511,11 @@ func repoView(cmd *cobra.Command, args []string) error {
508511
return fmt.Errorf("did not understand argument: %w", err)
509512
}
510513
} else {
511-
toView = ghrepo.FromFullName(repoArg)
514+
var err error
515+
toView, err = ghrepo.FromFullName(repoArg)
516+
if err != nil {
517+
return fmt.Errorf("argument error: %w", err)
518+
}
512519
}
513520
}
514521

command/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ func changelogURL(version string) string {
219219
func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) {
220220
repo, err := cmd.Flags().GetString("repo")
221221
if err == nil && repo != "" {
222-
return ghrepo.FromFullName(repo), nil
222+
baseRepo, err := ghrepo.FromFullName(repo)
223+
if err != nil {
224+
return nil, fmt.Errorf("argument error: %w", err)
225+
}
226+
return baseRepo, nil
223227
}
224228

225229
apiClient, err := apiClientForContext(ctx)

context/blank_context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@ func (c *blankContext) BaseRepo() (ghrepo.Interface, error) {
9292
}
9393

9494
func (c *blankContext) SetBaseRepo(nwo string) {
95-
c.baseRepo = ghrepo.FromFullName(nwo)
95+
repo, _ := ghrepo.FromFullName(nwo)
96+
c.baseRepo = repo
9697
}

context/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ResolveRemotesToRepos(remotes Remotes, client *api.Client, base string) (Re
3939
}
4040

4141
hasBaseOverride := base != ""
42-
baseOverride := ghrepo.FromFullName(base)
42+
baseOverride, _ := ghrepo.FromFullName(base)
4343
foundBaseOverride := false
4444
repos := make([]ghrepo.Interface, 0, lenRemotesForLookup)
4545
for _, r := range remotes[:lenRemotesForLookup] {
@@ -266,5 +266,5 @@ func (c *fsContext) BaseRepo() (ghrepo.Interface, error) {
266266
}
267267

268268
func (c *fsContext) SetBaseRepo(nwo string) {
269-
c.baseRepo = ghrepo.FromFullName(nwo)
269+
c.baseRepo, _ = ghrepo.FromFullName(nwo)
270270
}

internal/ghrepo/repo.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ func FullName(r Interface) string {
2828
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
2929
}
3030

31-
// FromFullName extracts the GitHub repository inforation from an "OWNER/REPO" string
32-
func FromFullName(nwo string) Interface {
31+
// FromFullName extracts the GitHub repository information from an "OWNER/REPO" string
32+
func FromFullName(nwo string) (Interface, error) {
3333
var r ghRepo
3434
parts := strings.SplitN(nwo, "/", 2)
35-
if len(parts) == 2 {
36-
r.owner, r.name = parts[0], parts[1]
35+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
36+
return &r, fmt.Errorf("expected OWNER/REPO format, got %q", nwo)
3737
}
38-
return &r
38+
r.owner, r.name = parts[0], parts[1]
39+
return &r, nil
3940
}
4041

4142
// FromURL extracts the GitHub repository information from a URL

0 commit comments

Comments
 (0)
X Tutup