X Tutup
Skip to content

Commit 3a7f564

Browse files
committed
tweak reauth code and request a new scope
1 parent ed1a3a6 commit 3a7f564

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

api/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func ReplaceTripper(tr http.RoundTripper) ClientOption {
7676
var issuedScopesWarning bool
7777

7878
// CheckScopes checks whether an OAuth scope is present in a response
79-
func CheckScopes(wantedScope string, cb func(string) error) ClientOption {
79+
func CheckScopes(wantedScope string, cb func(string, string) error) ClientOption {
8080
return func(tr http.RoundTripper) http.RoundTripper {
8181
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
8282
res, err := tr.RoundTrip(req)
83-
if err != nil || res.StatusCode > 299 || issuedScopesWarning {
83+
if err != nil || (res.StatusCode > 299 && req.URL.Path == "/graphql") || issuedScopesWarning {
8484
return res, err
8585
}
8686

@@ -96,9 +96,10 @@ func CheckScopes(wantedScope string, cb func(string) error) ClientOption {
9696
}
9797

9898
if !hasWanted {
99-
if err := cb(appID); err != nil {
99+
if err := cb(wantedScope, appID); err != nil {
100100
return res, err
101101
}
102+
// TODO seems like we should call RoundTrip again? I tried but it didn't work
102103
issuedScopesWarning = true
103104
}
104105

command/gist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package command
1+
package command
22

33
import (
44
"fmt"
@@ -69,4 +69,4 @@ func gistCreate(cmd *cobra.Command, args []string) error {
6969
fmt.Fprintln(cmd.OutOrStdout(), gist.HTMLURL)
7070

7171
return nil
72-
}
72+
}

command/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
141141
}
142142

143143
getAuthValue := func() string {
144+
fmt.Println("GITTIN MY AUTH", token)
144145
return fmt.Sprintf("token %s", token)
145146
}
146147

147-
checkScopesFunc := func(appID string) error {
148+
checkScopesFunc := func(wantedScope, appID string) error {
148149
if config.IsGitHubApp(appID) && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
149150
newToken, loginHandle, err := config.AuthFlow("Notice: additional authorization required")
150151
if err != nil {
@@ -165,8 +166,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
165166
token = newToken
166167
config.AuthFlowComplete()
167168
} else {
168-
// TODO for gist
169-
fmt.Fprintln(os.Stderr, "Warning: gh now requires the `read:org` OAuth scope.")
169+
fmt.Fprintln(os.Stderr, fmt.Sprintf("Warning: gh now requires the `%s` OAuth scope.", wantedScope))
170170
fmt.Fprintln(os.Stderr, "Visit https://github.com/settings/tokens and edit your token to enable `read:org`")
171171
fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`")
172172
}
@@ -175,6 +175,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
175175

176176
opts = append(opts,
177177
api.CheckScopes("read:org", checkScopesFunc),
178+
api.CheckScopes("gist", checkScopesFunc),
178179
api.AddHeaderFunc("Authorization", getAuthValue),
179180
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
180181
// antiope-preview: Checks

0 commit comments

Comments
 (0)
X Tutup