-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscope.go
110 lines (88 loc) · 3.5 KB
/
scope.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"reflect"
"strings"
"github.com/google/go-github/v53/github"
)
type Scope struct {
Repositories []string `json:"repositories,omitempty"`
Permissions github.InstallationPermissions `json:"permissions,omitempty"`
}
func NewScope() *Scope {
return &Scope{
Repositories: []string{},
Permissions: github.InstallationPermissions{},
}
}
func (scope *Scope) isEmpty() bool {
noPermissionSet := true
// Get the list of fields from the struct github.InstallationPermissions
fields := reflect.VisibleFields(reflect.TypeOf(struct{ github.InstallationPermissions }{}))
reflectScope := reflect.ValueOf(&scope.Permissions).Elem()
for _, field := range fields {
value := reflectScope.FieldByName(field.Name)
// Has this filed been set?
if value.IsValid() && !value.IsZero() {
// Get the value of the field as a string
valueString := value.Elem().String()
if valueString != "" {
// We have at least one permission set
noPermissionSet = false
break
}
}
}
return scope == nil || (len(scope.Repositories) == 0 && noPermissionSet)
}
func (scope *Scope) String() string {
kvPairs := []string{}
// Get the list of fields from the struct github.InstallationPermissions
fields := reflect.VisibleFields(reflect.TypeOf(struct{ github.InstallationPermissions }{}))
reflectScope := reflect.ValueOf(&scope.Permissions).Elem()
for _, field := range fields {
value := reflectScope.FieldByName(field.Name)
// Has this filed been set?
if value.IsValid() && !value.IsZero() {
// Get the value of the field as a string
valueString := value.Elem().String()
if valueString != "" {
kvPairs = append(kvPairs, fmt.Sprintf("%s: %s", field.Name, valueString))
}
}
}
return fmt.Sprintf("{repositories: [%s], permissions: {%s}}", strings.Join(scope.Repositories, ", "), strings.Join(kvPairs, ", "))
}
func (cumulativeScope *Scope) merge(additionalScope Scope) {
cumulativeScope.Repositories = append(cumulativeScope.Repositories, additionalScope.Repositories...)
permissionRank := map[string]int{
"read": 0,
"write": 1,
"admin": 2,
}
// Get the list of fields from the struct github.InstallationPermissions
fields := reflect.VisibleFields(reflect.TypeOf(struct{ github.InstallationPermissions }{}))
reflectCumulativeScope := reflect.ValueOf(&cumulativeScope.Permissions).Elem()
reflectadditionalScope := reflect.ValueOf(&additionalScope.Permissions).Elem()
for _, field := range fields {
newValue := reflectadditionalScope.FieldByName(field.Name)
// Has this filed been set in the additional scope?
if newValue.IsValid() && !newValue.IsZero() {
// Get the value of the field as a string
newValueString := newValue.Elem().String()
// Get the same field within the cumulative scope
cumulativeValue := reflectCumulativeScope.FieldByName(field.Name)
// If the cumulative scope has not been set, set it to the new value
if !cumulativeValue.IsValid() || cumulativeValue.IsZero() {
cumulativeValue.Set(reflect.New(newValue.Type().Elem()))
cumulativeValue.Elem().SetString(newValueString)
} else { // Otherwise, compare the current value with the new value and update the cumulative value if the new one provides higher permissions
cumulativeValueString := cumulativeValue.Elem().String()
// If the current permission is lower than the new permission, update the permission
if permissionRank[cumulativeValueString] < permissionRank[newValueString] {
cumulativeValue.Elem().SetString(newValueString)
}
}
}
}
}