Merge branch 'trunk' into trunk

This commit is contained in:
S R Gokul Krishnan 2023-03-20 07:58:02 +00:00 committed by GitHub
commit 9c83723a01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 53 additions and 40 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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