diff --git a/pkg/cmd/project/link/link.go b/pkg/cmd/project/link/link.go index 60102587a..2d81d6efc 100644 --- a/pkg/cmd/project/link/link.go +++ b/pkg/cmd/project/link/link.go @@ -61,6 +61,21 @@ func NewCmdLink(f *cmdutil.Factory, runF func(config linkConfig) error) *cobra.C opts.number = int32(num) } + if opts.repo == "" && opts.team == "" { + repo, err := f.BaseRepo() + if err != nil { + return err + } + opts.repo = repo.RepoName() + if opts.owner == "" { + opts.owner = repo.RepoOwner() + } + } + + if err := cmdutil.MutuallyExclusive("specify only one of `--repo` or `--team`", opts.repo != "", opts.team != ""); err != nil { + return err + } + config := linkConfig{ httpClient: f.HttpClient, config: f.Config, @@ -69,13 +84,6 @@ func NewCmdLink(f *cmdutil.Factory, runF func(config linkConfig) error) *cobra.C io: f.IOStreams, } - if err := cmdutil.MutuallyExclusive("specify only one of `--repo` or `--team`", opts.repo != "", opts.team != ""); err != nil { - return err - } - if err := cmdutil.MutuallyExclusive("specify either `--repo` or `--team`", opts.repo == "", opts.team == ""); err != nil { - return err - } - // allow testing of the command without actually running it if runF != nil { return runF(config) @@ -84,6 +92,7 @@ func NewCmdLink(f *cmdutil.Factory, runF func(config linkConfig) error) *cobra.C }, } + cmdutil.EnableRepoOverride(linkCmd, f) linkCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") linkCmd.Flags().StringVarP(&opts.repo, "repo", "R", "", "The repository to be linked to this project") linkCmd.Flags().StringVarP(&opts.team, "team", "T", "", "The team to be linked to this project") diff --git a/pkg/cmd/project/link/link_test.go b/pkg/cmd/project/link/link_test.go index 160a950db..07af37f60 100644 --- a/pkg/cmd/project/link/link_test.go +++ b/pkg/cmd/project/link/link_test.go @@ -2,6 +2,7 @@ package link import ( "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -27,18 +28,20 @@ func TestNewCmdLink(t *testing.T) { wantsErr: true, wantsErrMsg: "invalid number: x", }, - { - name: "specify-nothing", - cli: "", - wantsErr: true, - wantsErrMsg: "specify either `--repo` or `--team`", - }, { name: "specify-repo-and-team", cli: "--repo my-repo --team my-team", wantsErr: true, wantsErrMsg: "specify only one of `--repo` or `--team`", }, + { + name: "specify-nothing", + cli: "", + wants: linkOpts{ + repo: "REPO", + owner: "OWNER", + }, + }, { name: "repo", cli: "--repo my-repo", @@ -62,13 +65,21 @@ func TestNewCmdLink(t *testing.T) { }, }, { - name: "owner", + name: "owner-with-repo-flag", cli: "--repo my-repo --owner monalisa", wants: linkOpts{ owner: "monalisa", repo: "my-repo", }, }, + { + name: "owner-without-repo-flag", + cli: "--owner monalisa", + wants: linkOpts{ + owner: "monalisa", + repo: "REPO", + }, + }, { name: "json", cli: "--repo my-repo --format json", @@ -86,6 +97,9 @@ func TestNewCmdLink(t *testing.T) { ios, _, _, _ := iostreams.Test() f := &cmdutil.Factory{ IOStreams: ios, + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, } argv, err := shlex.Split(tt.cli) diff --git a/pkg/cmd/project/unlink/unlink.go b/pkg/cmd/project/unlink/unlink.go index 88b8fdb94..ad91b8581 100644 --- a/pkg/cmd/project/unlink/unlink.go +++ b/pkg/cmd/project/unlink/unlink.go @@ -61,6 +61,21 @@ func NewCmdUnlink(f *cmdutil.Factory, runF func(config unlinkConfig) error) *cob opts.number = int32(num) } + if opts.repo == "" && opts.team == "" { + repo, err := f.BaseRepo() + if err != nil { + return err + } + opts.repo = repo.RepoName() + if opts.owner == "" { + opts.owner = repo.RepoOwner() + } + } + + if err := cmdutil.MutuallyExclusive("specify only one of `--repo` or `--team`", opts.repo != "", opts.team != ""); err != nil { + return err + } + config := unlinkConfig{ httpClient: f.HttpClient, config: f.Config, @@ -69,13 +84,6 @@ func NewCmdUnlink(f *cmdutil.Factory, runF func(config unlinkConfig) error) *cob io: f.IOStreams, } - if err := cmdutil.MutuallyExclusive("specify only one of `--repo` or `--team`", opts.repo != "", opts.team != ""); err != nil { - return err - } - if err := cmdutil.MutuallyExclusive("specify either `--repo` or `--team`", opts.repo == "", opts.team == ""); err != nil { - return err - } - // allow testing of the command without actually running it if runF != nil { return runF(config) @@ -84,6 +92,7 @@ func NewCmdUnlink(f *cmdutil.Factory, runF func(config unlinkConfig) error) *cob }, } + cmdutil.EnableRepoOverride(linkCmd, f) linkCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") linkCmd.Flags().StringVarP(&opts.repo, "repo", "R", "", "The repository to be unlinked from this project") linkCmd.Flags().StringVarP(&opts.team, "team", "T", "", "The team to be unlinked from this project") diff --git a/pkg/cmd/project/unlink/unlink_test.go b/pkg/cmd/project/unlink/unlink_test.go index 5f0078df9..09ec7fc35 100644 --- a/pkg/cmd/project/unlink/unlink_test.go +++ b/pkg/cmd/project/unlink/unlink_test.go @@ -2,6 +2,7 @@ package unlink import ( "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -27,18 +28,20 @@ func TestNewCmdUnlink(t *testing.T) { wantsErr: true, wantsErrMsg: "invalid number: x", }, - { - name: "specify-nothing", - cli: "", - wantsErr: true, - wantsErrMsg: "specify either `--repo` or `--team`", - }, { name: "specify-repo-and-team", cli: "--repo my-repo --team my-team", wantsErr: true, wantsErrMsg: "specify only one of `--repo` or `--team`", }, + { + name: "specify-nothing", + cli: "", + wants: unlinkOpts{ + repo: "REPO", + owner: "OWNER", + }, + }, { name: "repo", cli: "--repo my-repo", @@ -62,13 +65,21 @@ func TestNewCmdUnlink(t *testing.T) { }, }, { - name: "owner", + name: "owner-with-repo-flag", cli: "--repo my-repo --owner monalisa", wants: unlinkOpts{ owner: "monalisa", repo: "my-repo", }, }, + { + name: "owner-without-repo-flag", + cli: "--owner monalisa", + wants: unlinkOpts{ + owner: "monalisa", + repo: "REPO", + }, + }, { name: "json", cli: "--repo my-repo --format json", @@ -86,6 +97,9 @@ func TestNewCmdUnlink(t *testing.T) { ios, _, _, _ := iostreams.Test() f := &cmdutil.Factory{ IOStreams: ios, + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, } argv, err := shlex.Split(tt.cli)