Support for directory based repo and GH_REPO env variable

This commit is contained in:
benebsiny 2024-01-27 11:44:07 +08:00
parent 0ce66347b9
commit adc0abe5da
4 changed files with 74 additions and 28 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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")

View file

@ -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)