X Tutup
Skip to content

Commit 79b77b4

Browse files
committed
Merge branch 'trunk' of https://github.com/cli/cli into bug/gist-deletion
2 parents 61eb7ee + 98df059 commit 79b77b4

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

cmd/gh/main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,19 @@ func main() {
163163

164164
newRelease := <-updateMessageChan
165165
if newRelease != nil {
166-
ghExe, _ := os.Executable()
166+
isHomebrew := false
167+
if ghExe, err := os.Executable(); err == nil {
168+
isHomebrew = isUnderHomebrew(ghExe)
169+
}
170+
if isHomebrew && isRecentRelease(newRelease.PublishedAt) {
171+
// do not notify Homebrew users before the version bump had a chance to get merged into homebrew-core
172+
return
173+
}
167174
fmt.Fprintf(stderr, "\n\n%s %s → %s\n",
168175
ansi.Color("A new release of gh is available:", "yellow"),
169176
ansi.Color(buildVersion, "cyan"),
170177
ansi.Color(newRelease.Version, "cyan"))
171-
if suggestBrewUpgrade(newRelease, ghExe) {
178+
if isHomebrew {
172179
fmt.Fprintf(stderr, "To upgrade, run: %s\n", "brew update && brew upgrade gh")
173180
}
174181
fmt.Fprintf(stderr, "%s\n\n",
@@ -265,13 +272,12 @@ func apiVerboseLog() api.ClientOption {
265272
return api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize)
266273
}
267274

268-
// Suggest to `brew upgrade gh` only if gh was found under homebrew prefix and when the release was
269-
// published over 24h ago, allowing homebrew-core ample time to merge the formula bump.
270-
func suggestBrewUpgrade(rel *update.ReleaseInfo, ghBinary string) bool {
271-
if rel.PublishedAt.IsZero() || time.Since(rel.PublishedAt) < time.Duration(time.Hour*24) {
272-
return false
273-
}
275+
func isRecentRelease(publishedAt time.Time) bool {
276+
return !publishedAt.IsZero() && time.Since(publishedAt) < time.Hour*24
277+
}
274278

279+
// Check whether the gh binary was found under the Homebrew prefix
280+
func isUnderHomebrew(ghBinary string) bool {
275281
brewExe, err := safeexec.LookPath("brew")
276282
if err != nil {
277283
return false

pkg/cmd/auth/gitcredential/helper.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14+
const tokenUser = "x-access-token"
15+
1416
type config interface {
15-
Get(string, string) (string, error)
17+
GetWithSource(string, string) (string, string, error)
1618
}
1719

1820
type CredentialOptions struct {
@@ -98,13 +100,19 @@ func helperRun(opts *CredentialOptions) error {
98100
return err
99101
}
100102

101-
gotUser, _ := cfg.Get(wants["host"], "user")
102-
gotToken, _ := cfg.Get(wants["host"], "oauth_token")
103+
var gotUser string
104+
gotToken, source, _ := cfg.GetWithSource(wants["host"], "oauth_token")
105+
if strings.HasSuffix(source, "_TOKEN") {
106+
gotUser = tokenUser
107+
} else {
108+
gotUser, _, _ = cfg.GetWithSource(wants["host"], "user")
109+
}
110+
103111
if gotUser == "" || gotToken == "" {
104112
return cmdutil.SilentError
105113
}
106114

107-
if wants["username"] != "" && !strings.EqualFold(wants["username"], gotUser) {
115+
if wants["username"] != "" && gotUser != tokenUser && !strings.EqualFold(wants["username"], gotUser) {
108116
return cmdutil.SilentError
109117
}
110118

pkg/cmd/auth/gitcredential/helper_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
type tinyConfig map[string]string
1212

13-
func (c tinyConfig) Get(host, key string) (string, error) {
14-
return c[fmt.Sprintf("%s:%s", host, key)], nil
13+
func (c tinyConfig) GetWithSource(host, key string) (string, string, error) {
14+
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
1515
}
1616

1717
func Test_helperRun(t *testing.T) {
@@ -29,6 +29,7 @@ func Test_helperRun(t *testing.T) {
2929
Operation: "get",
3030
Config: func() (config, error) {
3131
return tinyConfig{
32+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
3233
"example.com:user": "monalisa",
3334
"example.com:oauth_token": "OTOKEN",
3435
}, nil
@@ -53,6 +54,7 @@ func Test_helperRun(t *testing.T) {
5354
Operation: "get",
5455
Config: func() (config, error) {
5556
return tinyConfig{
57+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
5658
"example.com:user": "monalisa",
5759
"example.com:oauth_token": "OTOKEN",
5860
}, nil
@@ -78,6 +80,7 @@ func Test_helperRun(t *testing.T) {
7880
Operation: "get",
7981
Config: func() (config, error) {
8082
return tinyConfig{
83+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
8184
"example.com:user": "monalisa",
8285
"example.com:oauth_token": "OTOKEN",
8386
}, nil
@@ -101,6 +104,7 @@ func Test_helperRun(t *testing.T) {
101104
Operation: "get",
102105
Config: func() (config, error) {
103106
return tinyConfig{
107+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
104108
"example.com:user": "monalisa",
105109
}, nil
106110
},
@@ -119,6 +123,7 @@ func Test_helperRun(t *testing.T) {
119123
Operation: "get",
120124
Config: func() (config, error) {
121125
return tinyConfig{
126+
"_source": "/Users/monalisa/.config/gh/hosts.yml",
122127
"example.com:user": "monalisa",
123128
"example.com:oauth_token": "OTOKEN",
124129
}, nil
@@ -133,6 +138,31 @@ func Test_helperRun(t *testing.T) {
133138
wantStdout: "",
134139
wantStderr: "",
135140
},
141+
{
142+
name: "token from env",
143+
opts: CredentialOptions{
144+
Operation: "get",
145+
Config: func() (config, error) {
146+
return tinyConfig{
147+
"_source": "GITHUB_ENTERPRISE_TOKEN",
148+
"example.com:oauth_token": "OTOKEN",
149+
}, nil
150+
},
151+
},
152+
input: heredoc.Doc(`
153+
protocol=https
154+
host=example.com
155+
username=hubot
156+
`),
157+
wantErr: false,
158+
wantStdout: heredoc.Doc(`
159+
protocol=https
160+
host=example.com
161+
username=x-access-token
162+
password=OTOKEN
163+
`),
164+
wantStderr: "",
165+
},
136166
}
137167
for _, tt := range tests {
138168
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)
X Tutup