Skip to content

Commit 37800dd

Browse files
Merge pull request cli#12915 from cli/revert-12596-fix/clarify-scope-error
Revert "fix: clarify scope error while creating issues for projects"
2 parents 4a3f7d9 + 3921788 commit 37800dd

File tree

2 files changed

+0
-116
lines changed

2 files changed

+0
-116
lines changed

api/client.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"regexp"
1111
"strings"
1212

13-
"github.com/cli/cli/v2/pkg/set"
1413
ghAPI "github.com/cli/go-gh/v2/pkg/api"
1514
ghauth "github.com/cli/go-gh/v2/pkg/auth"
1615
)
@@ -181,10 +180,6 @@ func handleResponse(err error) error {
181180

182181
var gqlErr *ghAPI.GraphQLError
183182
if errors.As(err, &gqlErr) {
184-
scopeErr := GenerateScopeErrorForGQL(gqlErr)
185-
if scopeErr != nil {
186-
return scopeErr
187-
}
188183
return GraphQLError{
189184
GraphQLError: gqlErr,
190185
}
@@ -193,40 +188,6 @@ func handleResponse(err error) error {
193188
return err
194189
}
195190

196-
func GenerateScopeErrorForGQL(gqlErr *ghAPI.GraphQLError) error {
197-
missing := set.NewStringSet()
198-
for _, e := range gqlErr.Errors {
199-
if e.Type != "INSUFFICIENT_SCOPES" {
200-
continue
201-
}
202-
missing.AddValues(requiredScopesFromServerMessage(e.Message))
203-
}
204-
if missing.Len() > 0 {
205-
s := missing.ToSlice()
206-
// TODO: this duplicates parts of generateScopesSuggestion
207-
return fmt.Errorf(
208-
"error: your authentication token is missing required scopes %v\n"+
209-
"To request it, run: gh auth refresh -s %s",
210-
s,
211-
strings.Join(s, ","))
212-
}
213-
return nil
214-
}
215-
216-
var scopesRE = regexp.MustCompile(`one of the following scopes: \[(.+?)]`)
217-
218-
func requiredScopesFromServerMessage(msg string) []string {
219-
m := scopesRE.FindStringSubmatch(msg)
220-
if m == nil {
221-
return nil
222-
}
223-
var scopes []string
224-
for _, mm := range strings.Split(m[1], ",") {
225-
scopes = append(scopes, strings.Trim(mm, "' "))
226-
}
227-
return scopes
228-
}
229-
230191
// ScopesSuggestion is an error messaging utility that prints the suggestion to request additional OAuth
231192
// scopes in case a server response indicates that there are missing scopes.
232193
func ScopesSuggestion(resp *http.Response) string {

api/client_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/cli/cli/v2/pkg/httpmock"
1212
"github.com/cli/cli/v2/pkg/iostreams"
13-
"github.com/cli/go-gh/v2/pkg/api"
1413
"github.com/stretchr/testify/assert"
1514
)
1615

@@ -257,79 +256,3 @@ func TestHTTPHeaders(t *testing.T) {
257256
}
258257
assert.Equal(t, "", stderr.String())
259258
}
260-
261-
func TestGenerateScopeErrorForGQL(t *testing.T) {
262-
tests := []struct {
263-
name string
264-
gqlError *api.GraphQLError
265-
wantErr bool
266-
expected string
267-
}{
268-
{
269-
name: "missing scope",
270-
gqlError: &api.GraphQLError{
271-
Errors: []api.GraphQLErrorItem{
272-
{
273-
Type: "INSUFFICIENT_SCOPES",
274-
Message: "The 'addProjectV2ItemById' field requires one of the following scopes: ['project']",
275-
},
276-
},
277-
},
278-
wantErr: true,
279-
expected: "error: your authentication token is missing required scopes [project]\n" +
280-
"To request it, run: gh auth refresh -s project",
281-
},
282-
283-
{
284-
name: "ignore non-scope errors",
285-
gqlError: &api.GraphQLError{
286-
Errors: []api.GraphQLErrorItem{
287-
{
288-
Type: "NOT_FOUND",
289-
Message: "Could not resolve to a Repository",
290-
},
291-
},
292-
},
293-
wantErr: false,
294-
},
295-
}
296-
297-
for _, tt := range tests {
298-
t.Run(tt.name, func(t *testing.T) {
299-
err := GenerateScopeErrorForGQL(tt.gqlError)
300-
if tt.wantErr {
301-
assert.NotNil(t, err)
302-
assert.Equal(t, tt.expected, err.Error())
303-
} else {
304-
assert.Nil(t, err)
305-
}
306-
})
307-
}
308-
}
309-
310-
func TestRequiredScopesFromServerMessage(t *testing.T) {
311-
tests := []struct {
312-
msg string
313-
expected []string
314-
}{
315-
{
316-
msg: "requires one of the following scopes: ['project']",
317-
expected: []string{"project"},
318-
},
319-
{
320-
msg: "requires one of the following scopes: ['repo', 'read:org']",
321-
expected: []string{"repo", "read:org"},
322-
},
323-
{
324-
msg: "no match here",
325-
expected: nil,
326-
},
327-
}
328-
329-
for _, tt := range tests {
330-
t.Run(tt.msg, func(t *testing.T) {
331-
output := requiredScopesFromServerMessage(tt.msg)
332-
assert.Equal(t, tt.expected, output)
333-
})
334-
}
335-
}

0 commit comments

Comments
 (0)