Skip to content

Commit c0cd4c1

Browse files
authored
Merge pull request cli#12845 from yuvrajangadsingh/refactor/deduplicate-scope-error-handling
refactor: deduplicate scope error handling between api/client.go and project queries
2 parents 19d70d1 + fdd6388 commit c0cd4c1

3 files changed

Lines changed: 4 additions & 66 deletions

File tree

api/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ func GenerateScopeErrorForGQL(gqlErr *ghAPI.GraphQLError) error {
203203
}
204204
if missing.Len() > 0 {
205205
s := missing.ToSlice()
206-
// TODO: this duplicates parts of generateScopesSuggestion
207206
return fmt.Errorf(
208207
"error: your authentication token is missing required scopes %v\n"+
209208
"To request it, run: gh auth refresh -s %s",

pkg/cmd/project/shared/queries/queries.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8-
"regexp"
98
"strings"
109

1110
"github.com/cli/cli/v2/api"
1211
"github.com/cli/cli/v2/internal/prompter"
1312
"github.com/cli/cli/v2/pkg/iostreams"
14-
"github.com/cli/cli/v2/pkg/set"
1513
"github.com/shurcooL/githubv4"
1614
)
1715

@@ -1664,42 +1662,15 @@ func (c *Client) UnlinkProjectFromTeam(projectID string, teamID string) error {
16641662
}
16651663

16661664
func handleError(err error) error {
1667-
var gerr api.GraphQLError
1668-
if errors.As(err, &gerr) {
1669-
missing := set.NewStringSet()
1670-
for _, e := range gerr.Errors {
1671-
if e.Type != "INSUFFICIENT_SCOPES" {
1672-
continue
1673-
}
1674-
missing.AddValues(requiredScopesFromServerMessage(e.Message))
1675-
}
1676-
if missing.Len() > 0 {
1677-
s := missing.ToSlice()
1678-
// TODO: this duplicates parts of generateScopesSuggestion
1679-
return fmt.Errorf(
1680-
"error: your authentication token is missing required scopes %v\n"+
1681-
"To request it, run: gh auth refresh -s %s",
1682-
s,
1683-
strings.Join(s, ","))
1665+
var gqlErr api.GraphQLError
1666+
if errors.As(err, &gqlErr) {
1667+
if scopeErr := api.GenerateScopeErrorForGQL(gqlErr.GraphQLError); scopeErr != nil {
1668+
return scopeErr
16841669
}
16851670
}
16861671
return err
16871672
}
16881673

1689-
var scopesRE = regexp.MustCompile(`one of the following scopes: \[(.+?)]`)
1690-
1691-
func requiredScopesFromServerMessage(msg string) []string {
1692-
m := scopesRE.FindStringSubmatch(msg)
1693-
if m == nil {
1694-
return nil
1695-
}
1696-
var scopes []string
1697-
for _, mm := range strings.Split(m[1], ",") {
1698-
scopes = append(scopes, strings.Trim(mm, "' "))
1699-
}
1700-
return scopes
1701-
}
1702-
17031674
func projectFieldValueData(v FieldValueNodes) interface{} {
17041675
switch v.Type {
17051676
case "ProjectV2ItemFieldDateValue":

pkg/cmd/project/shared/queries/queries_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package queries
33
import (
44
"io"
55
"net/http"
6-
"reflect"
76
"strings"
87
"testing"
98

@@ -564,37 +563,6 @@ func TestProjectFields_NoLimit(t *testing.T) {
564563
assert.Len(t, project.Fields.Nodes, 3)
565564
}
566565

567-
func Test_requiredScopesFromServerMessage(t *testing.T) {
568-
tests := []struct {
569-
name string
570-
msg string
571-
want []string
572-
}{
573-
{
574-
name: "no scopes",
575-
msg: "SERVER OOPSIE",
576-
want: []string(nil),
577-
},
578-
{
579-
name: "one scope",
580-
msg: "Your token has not been granted the required scopes to execute this query. The 'dataType' field requires one of the following scopes: ['read:project'], but your token has only been granted the: ['codespace', repo'] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.",
581-
want: []string{"read:project"},
582-
},
583-
{
584-
name: "multiple scopes",
585-
msg: "Your token has not been granted the required scopes to execute this query. The 'dataType' field requires one of the following scopes: ['read:project', 'read:discussion', 'codespace'], but your token has only been granted the: [repo'] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.",
586-
want: []string{"read:project", "read:discussion", "codespace"},
587-
},
588-
}
589-
for _, tt := range tests {
590-
t.Run(tt.name, func(t *testing.T) {
591-
if got := requiredScopesFromServerMessage(tt.msg); !reflect.DeepEqual(got, tt.want) {
592-
t.Errorf("requiredScopesFromServerMessage() = %v, want %v", got, tt.want)
593-
}
594-
})
595-
}
596-
}
597-
598566
func TestNewProject_nonTTY(t *testing.T) {
599567
client := NewTestClient()
600568
_, err := client.NewProject(false, &Owner{}, 0, false)

0 commit comments

Comments
 (0)