-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathgithubapiutil.go
More file actions
41 lines (36 loc) · 982 Bytes
/
githubapiutil.go
File metadata and controls
41 lines (36 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package githubapiutil
import (
"strings"
"github.com/google/go-github/v32/github"
"github.com/pkg/errors"
)
const xOAuthScopesHeader = "X-OAuth-Scopes"
const xGitHubRequestIDHeader = "X-GitHub-Request-ID"
func HasAnyScope(response *github.Response, scopes ...string) bool {
if response == nil {
return false
}
if len(response.Header.Values(xOAuthScopesHeader)) == 0 {
return false
}
actualScopes := strings.Split(response.Header.Get(xOAuthScopesHeader), ",")
for _, actualScope := range actualScopes {
actualScope = strings.Trim(actualScope, " ")
for _, requiredScope := range scopes {
if actualScope == requiredScope {
return true
}
}
}
return false
}
func EnrichResponseError(response *github.Response, err error, message string) error {
requestID := ""
if response != nil {
requestID = response.Header.Get(xGitHubRequestIDHeader)
}
if requestID != "" {
message = message + " (" + requestID + ")"
}
return errors.Wrap(err, message)
}