-
Notifications
You must be signed in to change notification settings - Fork 57
/
config.go
318 lines (264 loc) · 7.74 KB
/
config.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package governance
import (
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
Contributors map[string]Person
Teams map[string]Team
Repos map[string]Repo
}
type Person struct {
Name string `yaml:"name"`
GitHub string `yaml:"github"`
Discord string `yaml:"discord,omitempty"`
Email string `yaml:"email,omitempty"`
Repos map[string]string `yaml:"repos,omitempty"`
}
type Team struct {
Name string `yaml:"name"`
Purpose string `yaml:"purpose"`
Responsibilities []string `yaml:"responsibilities"`
Discord Discord `yaml:"discord,omitempty"`
AllContributors bool `yaml:"all_contributors"`
RawMembers []string `yaml:"members"`
RequiresEmail bool `yaml:"requires_email,omitempty"`
RawRepoPermission string `yaml:"repo_permission"`
Repos []string `yaml:"repos,omitempty"`
}
func (team Team) Members(cfg *Config) map[string]Person {
if team.AllContributors {
return cfg.Contributors
} else {
members := map[string]Person{}
for _, m := range team.RawMembers {
members[m] = cfg.Contributors[m]
}
return members
}
}
func (team Team) RepoPermission() RepoPermission {
if team.RawRepoPermission == "" {
return RepoPermissionMaintain
}
return permission3to4(team.RawRepoPermission)
}
type Discord struct {
Role string `yaml:"role,omitempty"`
Color int `yaml:"color,omitempty"`
Priority int `yaml:"priority,omitempty"`
AddedPermissions DiscordPermissionSet `yaml:"added_permissions,omitempty"`
// if set, the role will never be removed. this is primarily to
// grandfather in users who have roles which predated the governance
// automation, i.e. the 'contributors' role.
Sticky bool `yaml:"sticky,omitempty"`
}
// 1. copied from https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
// 2. replaced GUILD with SERVER
var DiscordPermissions = map[string]int64{
"CREATE_INSTANT_INVITE": 0x00000001,
"KICK_MEMBERS": 0x00000002,
"BAN_MEMBERS": 0x00000004,
"ADMINISTRATOR": 0x00000008,
"MANAGE_CHANNELS": 0x00000010,
"MANAGE_SERVER": 0x00000020,
"ADD_REACTIONS": 0x00000040,
"VIEW_AUDIT_LOG": 0x00000080,
"PRIORITY_SPEAKER": 0x00000100,
"STREAM": 0x00000200,
"VIEW_CHANNEL": 0x00000400,
"SEND_MESSAGES": 0x00000800,
"SEND_TTS_MESSAGES": 0x00001000,
"MANAGE_MESSAGES": 0x00002000,
"EMBED_LINKS": 0x00004000,
"ATTACH_FILES": 0x00008000,
"READ_MESSAGE_HISTORY": 0x00010000,
"MENTION_EVERYONE": 0x00020000,
"USE_EXTERNAL_EMOJIS": 0x00040000,
"VIEW_SERVER_INSIGHTS": 0x00080000,
"CONNECT": 0x00100000,
"SPEAK": 0x00200000,
"MUTE_MEMBERS": 0x00400000,
"DEAFEN_MEMBERS": 0x00800000,
"MOVE_MEMBERS": 0x01000000,
"USE_VAD": 0x02000000,
"CHANGE_NICKNAME": 0x04000000,
"MANAGE_NICKNAMES": 0x08000000,
"MANAGE_ROLES": 0x10000000,
"MANAGE_WEBHOOKS": 0x20000000,
"MANAGE_EMOJIS": 0x40000000,
}
// defaults copied from newly created role; may be worth tuning later
type DiscordPermissionSet []string
func (set DiscordPermissionSet) Permissions() (int64, error) {
var permissions int64
for _, permission := range set {
bits, found := DiscordPermissions[permission]
if !found {
return 0, fmt.Errorf("unknown permission: %s", permission)
}
permissions |= bits
}
return permissions, nil
}
var TeamRoleBasePermissions = DiscordPermissionSet{
"VIEW_CHANNEL",
"CREATE_INSTANT_INVITE",
"CHANGE_NICKNAME",
"SEND_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"ADD_REACTIONS",
"USE_EXTERNAL_EMOJIS",
"MENTION_EVERYONE",
"READ_MESSAGE_HISTORY",
"SEND_TTS_MESSAGES",
"CONNECT",
"SPEAK",
"STREAM",
"USE_VAD",
}
type Repo struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Private bool `yaml:"private,omitempty"`
Topics []string `yaml:"topics,omitempty"`
HomepageURL string `yaml:"homepage_url,omitempty"`
HasIssues bool `yaml:"has_issues,omitempty"`
HasProjects bool `yaml:"has_projects,omitempty"`
HasWiki bool `yaml:"has_wiki,omitempty"`
HasDiscussions bool `yaml:"has_discussions,omitempty"`
Pages *RepoPages `yaml:"pages,omitempty"`
BranchProtection []RepoBranchProtection `yaml:"branch_protection,omitempty"`
Labels []RepoLabel `yaml:"labels,omitempty"`
DeployKeys []RepoDeployKey `yaml:"deploy_keys"`
}
type RepoPages struct {
CNAME string `yaml:"cname,omitempty"`
Branch string `yaml:"branch"`
Path string `yaml:"path,omitempty"`
}
type RepoBranchProtection struct {
Pattern string `yaml:"pattern"`
AllowsDeletions bool `yaml:"allows_deletions,omitempty"`
RequiredChecks []string `yaml:"required_checks,omitempty"`
StrictChecks bool `yaml:"strict_checks,omitempty"`
RequiredReviews int `yaml:"required_reviews,omitempty"`
DismissStaleReviews bool `yaml:"dismiss_stale_reviews,omitempty"`
RequireCodeOwnerReviews bool `yaml:"require_code_owner_reviews,omitempty"`
}
type RepoLabel struct {
Name string `yaml:"name"`
Color int `yaml:"color"`
}
type RepoDeployKey struct {
Title string `yaml:"title"`
PublicKey string `yaml:"public_key"`
Writable bool `yaml:"writable"`
}
func LoadConfig(tree fs.FS) (*Config, error) {
personFiles, err := fs.ReadDir(tree, "contributors")
if err != nil {
return nil, err
}
contributors := map[string]Person{}
for _, f := range personFiles {
fn := filepath.Join("contributors", f.Name())
file, err := tree.Open(fn)
if err != nil {
return nil, err
}
var person Person
err = decode(file, &person)
if err != nil {
return nil, fmt.Errorf("decode %s: %w", fn, err)
}
contributors[strings.TrimSuffix(f.Name(), ".yml")] = person
}
teamFiles, err := fs.ReadDir(tree, "teams")
if err != nil {
return nil, err
}
teams := map[string]Team{}
for _, f := range teamFiles {
fn := filepath.Join("teams", f.Name())
file, err := tree.Open(fn)
if err != nil {
return nil, err
}
var team Team
err = decode(file, &team)
if err != nil {
return nil, fmt.Errorf("decode %s: %w", fn, err)
}
teams[strings.TrimSuffix(f.Name(), ".yml")] = team
}
repoFiles, err := fs.ReadDir(tree, "repos")
if err != nil {
return nil, err
}
repos := map[string]Repo{}
for _, f := range repoFiles {
fn := filepath.Join("repos", f.Name())
file, err := tree.Open(fn)
if err != nil {
return nil, err
}
var repo Repo
err = decode(file, &repo)
if err != nil {
return nil, fmt.Errorf("decode %s: %w", fn, err)
}
repos[strings.TrimSuffix(f.Name(), ".yml")] = repo
}
return &Config{
Contributors: contributors,
Teams: teams,
Repos: repos,
}, nil
}
// collapse word-wrapped string YAML blocks
func sanitize(str string) string {
return strings.TrimSpace(strings.Join(strings.Split(str, "\n"), " "))
}
func permission3to4(v3permission string) RepoPermission {
switch v3permission {
case "pull":
return RepoPermissionRead
case "push":
return RepoPermissionWrite
case "admin":
return RepoPermissionAdmin
case "maintain":
return RepoPermissionMaintain
case "triage":
return RepoPermissionTriage
default:
return "INVALID"
}
}
func permission4to3(v4permission RepoPermission) string {
switch v4permission {
case RepoPermissionRead:
return "pull"
case RepoPermissionWrite:
return "push"
case RepoPermissionAdmin:
return "admin"
case RepoPermissionMaintain:
return "maintain"
case RepoPermissionTriage:
return "triage"
default:
return "invalid"
}
}
func decode(stream io.Reader, dest interface{}) error {
dec := yaml.NewDecoder(stream)
dec.SetStrict(true)
return dec.Decode(dest)
}