Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,29 @@ stderr 'multiple remotes detected. please specify which repo to use by providing
# Secret delete requires disambiguation
! exec gh secret delete 'TEST_SECRET_NAME'
stderr 'multiple remotes detected. please specify which repo to use by providing the -R, --repo argument'

# Move out of the fork repo to test whether secret commands work without local repository context
cd ..

# Secret set using --repo flag does not require disambiguation
exec gh secret set 'TEST_SECRET_NAME' --body 'TEST_SECRET_VALUE' --repo ${ORG}/${REPO}-fork

# Secret list using --repo flag does not require disambiguation
exec gh secret list --repo ${ORG}/${REPO}-fork
stdout 'TEST_SECRET_NAME'

# Secret delete using --repo flag does not require disambiguation
exec gh secret delete 'TEST_SECRET_NAME' --repo ${ORG}/${REPO}-fork

# Setup GH_REPO for testing environment variable behavior
env GH_REPO=${ORG}/${REPO}-fork

# Secret set using GH_REPO env var does not require disambiguation
exec gh secret set 'TEST_SECRET_NAME2' --body 'TEST_SECRET_VALUE2'

# Secret list using GH_REPO env var does not require disambiguation
exec gh secret list
stdout 'TEST_SECRET_NAME2'

# Secret delete using GH_REPO env var does not require disambiguation
exec gh secret delete 'TEST_SECRET_NAME2'
6 changes: 4 additions & 2 deletions pkg/cmd/secret/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package delete
import (
"fmt"
"net/http"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
Expand Down Expand Up @@ -48,8 +49,9 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
opts.BaseRepo = f.BaseRepo
if !cmd.Flags().Changed("repo") {
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
17 changes: 15 additions & 2 deletions pkg/cmd/secret/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
tests := []struct {
name string
args string
env map[string]string
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
Expand All @@ -154,14 +155,22 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
name: "when GH_REPO env var is provided, the factory base repo func is used",
args: "SECRET_NAME",
env: map[string]string{
"GH_REPO": "owner/repo",
},
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},
{
name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used",
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
Expand Down Expand Up @@ -199,6 +208,10 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
},
}

for k, v := range tt.env {
t.Setenv(k, v)
}

argv, err := shlex.Split(tt.args)
assert.NoError(t, err)

Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/secret/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"fmt"
"net/http"
"os"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -69,8 +70,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
opts.BaseRepo = f.BaseRepo
if !cmd.Flags().Changed("repo") {
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
16 changes: 14 additions & 2 deletions pkg/cmd/secret/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
tests := []struct {
name string
args string
env map[string]string
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
Expand All @@ -134,14 +135,21 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
name: "when GH_REPO env var is provided, the factory base repo func is used",
env: map[string]string{
"GH_REPO": "owner/repo",
},
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "",
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},
{
name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used",
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "",
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
Expand Down Expand Up @@ -179,6 +187,10 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
},
}

for k, v := range tt.env {
t.Setenv(k, v)
}

argv, err := shlex.Split(tt.args)
assert.NoError(t, err)

Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/secret/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
RunE: func(cmd *cobra.Command, args []string) error {
// If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride
// So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation.
opts.BaseRepo = f.BaseRepo
if !cmd.Flags().Changed("repo") {
if cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" {
opts.BaseRepo = f.BaseRepo
} else {
// If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if
// there might be multiple valid remotes.
opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes)
Expand Down
17 changes: 15 additions & 2 deletions pkg/cmd/secret/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
tests := []struct {
name string
args string
env map[string]string
prompterStubs func(*prompter.MockPrompter)
wantRepo ghrepo.Interface
wantErr error
Expand All @@ -253,14 +254,22 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
name: "when GH_REPO env var is provided, the factory base repo func is used",
args: "SECRET_NAME",
env: map[string]string{
"GH_REPO": "owner/repo",
},
wantRepo: ghrepo.New("owner", "repo"),
},
{
name: "when there is no repo flag or GH_REPO env var provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},
{
name: "when there is no repo flag provided, and can prompt, the base func resolving ambiguity is used",
name: "when there is no repo flag or GH_REPO env var provided, and can prompt, the base func resolving ambiguity is used",
args: "SECRET_NAME",
prompterStubs: func(pm *prompter.MockPrompter) {
pm.RegisterSelect(
Expand Down Expand Up @@ -298,6 +307,10 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
},
}

for k, v := range tt.env {
t.Setenv(k, v)
}

argv, err := shlex.Split(tt.args)
assert.NoError(t, err)

Expand Down
Loading