Skip to content

Commit ad4a06f

Browse files
Rename MissingAllScopes to HasAnyScope and invert the logic.
Co-authored-by: Simon Engledew <[email protected]>
1 parent 0375789 commit ad4a06f

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

internal/githubapiutil/githubapiutil.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
const xOAuthScopesHeader = "X-OAuth-Scopes"
1010

11-
func MissingAllScopes(response *github.Response, requiredAnyScopes ...string) bool {
11+
func HasAnyScope(response *github.Response, scopes ...string) bool {
1212
if response == nil {
1313
return false
1414
}
@@ -18,11 +18,11 @@ func MissingAllScopes(response *github.Response, requiredAnyScopes ...string) bo
1818
actualScopes := strings.Split(response.Header.Get(xOAuthScopesHeader), ",")
1919
for _, actualScope := range actualScopes {
2020
actualScope = strings.Trim(actualScope, " ")
21-
for _, requiredAnyScope := range requiredAnyScopes {
22-
if actualScope == requiredAnyScope {
23-
return false
21+
for _, requiredScope := range scopes {
22+
if actualScope == requiredScope {
23+
return true
2424
}
2525
}
2626
}
27-
return true
27+
return false
2828
}

internal/githubapiutil/githubapiutil_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
"github.com/google/go-github/v32/github"
1010
)
1111

12-
func TestHasAnyScopes(t *testing.T) {
12+
func TestHasAnyScope(t *testing.T) {
1313
response := github.Response{
1414
Response: &http.Response{Header: http.Header{}},
1515
}
1616

1717
response.Header.Set(xOAuthScopesHeader, "gist, notifications, admin:org, repo")
18-
require.False(t, MissingAllScopes(&response, "public_repo", "repo"))
18+
require.True(t, HasAnyScope(&response, "public_repo", "repo"))
1919

2020
response.Header.Set(xOAuthScopesHeader, "gist, notifications, public_repo, admin:org")
21-
require.False(t, MissingAllScopes(&response, "public_repo", "repo"))
21+
require.True(t, HasAnyScope(&response, "public_repo", "repo"))
2222

2323
response.Header.Set(xOAuthScopesHeader, "gist, notifications, admin:org")
24-
require.True(t, MissingAllScopes(&response, "public_repo", "repo"))
24+
require.False(t, HasAnyScope(&response, "public_repo", "repo"))
2525
}

internal/push/push.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (pushService *pushService) createRepository() (*github.Repository, error) {
7676
Name: github.String(pushService.destinationRepositoryOwner),
7777
}, user.GetLogin())
7878
if err != nil {
79-
if response != nil && response.StatusCode == http.StatusNotFound && githubapiutil.MissingAllScopes(response, "site_admin") {
79+
if response != nil && response.StatusCode == http.StatusNotFound && !githubapiutil.HasAnyScope(response, "site_admin") {
8080
return nil, usererrors.New("The destination token you have provided does not have the `site_admin` scope, so the destination organization cannot be created.")
8181
}
8282
return nil, errors.Wrap(err, "Error creating organization.")
@@ -105,15 +105,15 @@ func (pushService *pushService) createRepository() (*github.Repository, error) {
105105
if response.StatusCode == http.StatusNotFound {
106106
repository, response, err = pushService.githubEnterpriseClient.Repositories.Create(pushService.ctx, destinationOrganization, &desiredRepositoryProperties)
107107
if err != nil {
108-
if response.StatusCode == http.StatusNotFound && githubapiutil.MissingAllScopes(response, "public_repo", "repo") {
108+
if response.StatusCode == http.StatusNotFound && !githubapiutil.HasAnyScope(response, "public_repo", "repo") {
109109
return nil, usererrors.New("The destination token you have provided does not have the `public_repo` scope.")
110110
}
111111
return nil, errors.Wrap(err, "Error creating destination repository.")
112112
}
113113
} else {
114114
repository, response, err = pushService.githubEnterpriseClient.Repositories.Edit(pushService.ctx, pushService.destinationRepositoryOwner, pushService.destinationRepositoryName, &desiredRepositoryProperties)
115115
if err != nil {
116-
if response.StatusCode == http.StatusNotFound && githubapiutil.MissingAllScopes(response, "public_repo", "repo") {
116+
if response.StatusCode == http.StatusNotFound && !githubapiutil.HasAnyScope(response, "public_repo", "repo") {
117117
return nil, usererrors.New("The destination token you have provided does not have the `public_repo` scope.")
118118
}
119119
return nil, errors.Wrap(err, "Error updating destination repository.")

0 commit comments

Comments
 (0)