diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml index 606327059..7ed0c3fa9 100644 --- a/.github/workflows/releases.yml +++ b/.github/workflows/releases.yml @@ -24,7 +24,7 @@ jobs: - name: Generate changelog id: changelog run: | - echo "::set-output name=tag-name::${GITHUB_REF#refs/tags/}" + echo "tag-name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT gh api repos/$GITHUB_REPOSITORY/releases/generate-notes \ -f tag_name="${GITHUB_REF#refs/tags/}" \ -f target_commitish=trunk \ @@ -143,7 +143,7 @@ jobs: shell: bash run: | hub release download "${GITHUB_REF#refs/tags/}" -i '*windows_amd64*.zip' - printf "::set-output name=zip::%s\n" *.zip + printf "zip=%s\n" *.zip >> $GITHUB_OUTPUT unzip -o *.zip && rm -v *.zip env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} @@ -165,7 +165,7 @@ jobs: shell: bash run: | base64 -d <<<"$CERT_CONTENTS" > ./cert.pfx - printf "::set-output name=cert-file::%s\n" ".\\cert.pfx" + printf "cert-file=%s\n" ".\\cert.pfx" >> $GITHUB_OUTPUT env: CERT_CONTENTS: ${{ secrets.WINDOWS_CERT_PFX }} - name: Sign MSI diff --git a/internal/config/config.go b/internal/config/config.go index ce5796c9e..96d6b5ed0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -140,6 +140,22 @@ func (c *AuthConfig) Token(hostname string) (string, string) { return token, source } +// HasEnvToken checks whether the current env or config contains a token +func (c *AuthConfig) HasEnvToken() bool { + // This will check if there are any environment variable + // authentication tokens set for enterprise hosts. + // Any non-github.com hostname is fine here + hostname := "example.com" + if c.tokenOverride != nil { + token, _ := c.tokenOverride(hostname) + if token != "" { + return true + } + } + token, _ := ghAuth.TokenFromEnvOrConfig(hostname) + return token != "" +} + // SetToken will override any token resolution and return the given // token and source for all calls to Token. Use for testing purposes only. func (c *AuthConfig) SetToken(token, source string) { diff --git a/pkg/cmd/factory/remote_resolver.go b/pkg/cmd/factory/remote_resolver.go index a672ba5e6..08ff297c6 100644 --- a/pkg/cmd/factory/remote_resolver.go +++ b/pkg/cmd/factory/remote_resolver.go @@ -82,11 +82,9 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) { } if len(cachedRemotes) == 0 { - // Any non-github.com hostname is fine here - dummyHostname := "example.com" if isHostEnv(src) { return nil, fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable.", src) - } else if v, _ := cfg.Authentication().Token(dummyHostname); v != "" { + } else if cfg.Authentication().HasEnvToken() { return nil, errors.New("set the GH_HOST environment variable to specify which GitHub host to use") } return nil, errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`") diff --git a/pkg/cmd/repo/archive/archive.go b/pkg/cmd/repo/archive/archive.go index fd2716111..dbf2d209e 100644 --- a/pkg/cmd/repo/archive/archive.go +++ b/pkg/cmd/repo/archive/archive.go @@ -5,13 +5,12 @@ import ( "net/http" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" - "github.com/cli/cli/v2/pkg/prompt" "github.com/cli/cli/v2/pkg/iostreams" "github.com/spf13/cobra" @@ -24,6 +23,7 @@ type ArchiveOptions struct { Confirmed bool IO *iostreams.IOStreams RepoArg string + Prompter prompter.Prompter } func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.Command { @@ -32,6 +32,7 @@ func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra. HttpClient: f.HttpClient, Config: f.Config, BaseRepo: f.BaseRepo, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -115,16 +116,11 @@ func archiveRun(opts *ArchiveOptions) error { } if !opts.Confirmed { - p := &survey.Confirm{ - Message: fmt.Sprintf("Archive %s?", fullName), - Default: false, - } - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(p, &opts.Confirmed) + confirmed, err := opts.Prompter.Confirm(fmt.Sprintf("Archive %s?", fullName), false) if err != nil { return fmt.Errorf("failed to prompt: %w", err) } - if !opts.Confirmed { + if !confirmed { return cmdutil.CancelError } } diff --git a/pkg/cmd/repo/archive/archive_test.go b/pkg/cmd/repo/archive/archive_test.go index 02aab3383..e730a0967 100644 --- a/pkg/cmd/repo/archive/archive_test.go +++ b/pkg/cmd/repo/archive/archive_test.go @@ -7,10 +7,10 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -68,20 +68,21 @@ func TestNewCmdArchive(t *testing.T) { func Test_ArchiveRun(t *testing.T) { queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }` tests := []struct { - name string - opts ArchiveOptions - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - isTTY bool - wantStdout string - wantStderr string + name string + opts ArchiveOptions + httpStubs func(*httpmock.Registry) + prompterStubs func(pm *prompter.MockPrompter) + isTTY bool + wantStdout string + wantStderr string }{ { name: "unarchived repo tty", wantStdout: "✓ Archived repository OWNER/REPO\n", - askStubs: func(q *prompt.AskStubber) { - //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt - q.StubOne(true) + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterConfirm("Archive OWNER/REPO?", func(_ string, _ bool) (bool, error) { + return true, nil + }) }, isTTY: true, opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, @@ -98,9 +99,10 @@ func Test_ArchiveRun(t *testing.T) { name: "infer base repo", wantStdout: "✓ Archived repository OWNER/REPO\n", opts: ArchiveOptions{}, - askStubs: func(q *prompt.AskStubber) { - //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt - q.StubOne(true) + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterConfirm("Archive OWNER/REPO?", func(_ string, _ bool) (bool, error) { + return true, nil + }) }, isTTY: true, httpStubs: func(reg *httpmock.Registry) { @@ -140,12 +142,11 @@ func Test_ArchiveRun(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() tt.opts.IO = ios - //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber - q, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(q) + pm := prompter.NewMockPrompter(t) + if tt.prompterStubs != nil { + tt.prompterStubs(pm) } + tt.opts.Prompter = pm t.Run(tt.name, func(t *testing.T) { defer reg.Verify(t) diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index ea18d1502..56ebb0c4d 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -14,12 +14,7 @@ func DisableAuthCheck(cmd *cobra.Command) { } func CheckAuth(cfg config.Config) bool { - // This will check if there are any environment variable - // authentication tokens set for enterprise hosts. - // Any non-github.com hostname is fine here - dummyHostname := "example.com" - token, _ := cfg.Authentication().Token(dummyHostname) - if token != "" { + if cfg.Authentication().HasEnvToken() { return true } diff --git a/pkg/cmdutil/auth_check_test.go b/pkg/cmdutil/auth_check_test.go index 6bef0d413..fa8bd80e2 100644 --- a/pkg/cmdutil/auth_check_test.go +++ b/pkg/cmdutil/auth_check_test.go @@ -36,6 +36,13 @@ func Test_CheckAuth(t *testing.T) { }, expected: true, }, + { + name: "enterprise token", + cfgStubs: func(c *config.ConfigMock) { + t.Setenv("GH_ENTERPRISE_TOKEN", "token") + }, + expected: true, + }, } for _, tt := range tests {