ExpandScopes takes a list of required scopes and returns all accepted scopes including parent scopes from the hierarchy. For example, if "public_repo" is required, "repo" is also accepted since having the "repo" scope grants access to "public_repo". The returned slice is sorted for deterministic out
(required ...Scope)
| 121 | // having the "repo" scope grants access to "public_repo". |
| 122 | // The returned slice is sorted for deterministic output. |
| 123 | func ExpandScopes(required ...Scope) []string { |
| 124 | if len(required) == 0 { |
| 125 | return nil |
| 126 | } |
| 127 | |
| 128 | accepted := make(map[string]bool) |
| 129 | |
| 130 | // Add required scopes |
| 131 | for _, scope := range required { |
| 132 | accepted[string(scope)] = true |
| 133 | } |
| 134 | |
| 135 | // Add parent scopes that grant access to required scopes |
| 136 | for parent, children := range ScopeHierarchy { |
| 137 | for _, child := range children { |
| 138 | if accepted[string(child)] { |
| 139 | accepted[string(parent)] = true |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Convert to slice and sort for deterministic output |
| 145 | result := make([]string, 0, len(accepted)) |
| 146 | for scope := range accepted { |
| 147 | result = append(result, scope) |
| 148 | } |
| 149 | sort.Strings(result) |
| 150 | return result |
| 151 | } |
| 152 | |
| 153 | // expandScopeSet returns a set of all scopes granted by the given scopes, |
| 154 | // including child scopes from the hierarchy. |
no outgoing calls