-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.go
More file actions
294 lines (256 loc) · 7.24 KB
/
Copy pathscope.go
File metadata and controls
294 lines (256 loc) · 7.24 KB
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
package features
import (
"sort"
"strings"
"github.com/cego/gitte/config"
"github.com/cego/gitte/state"
)
// ProjectsInGateScope returns the projects a gate's configured scope applies to,
// keyed by config project name. Projects with an unparseable remote are skipped.
func ProjectsInGateScope(cfg *config.GitteConfig, gate config.FeatureGate) map[string]ScopeProject {
projects := make(map[string]ScopeProject)
for projName, proj := range cfg.Projects {
host, path, _, err := config.ParseRemoteURL(proj.Remote)
if err != nil {
continue
}
if projectMatchesScopeByName(projName, host, path, gate.Scope) {
projects[projName] = ScopeProject{Host: host, Path: path}
}
}
return projects
}
// ProjectMatchesOverrideScope checks if a project is included in an override scope.
// projName is the config key, host and path come from config.ParseRemoteURL.
// Returns false if override is nil (caller should use config scope instead).
func ProjectMatchesOverrideScope(projName, host, path string, override *state.ScopeOverride) bool {
if override == nil {
return false
}
for _, p := range override.Projects {
if p == projName {
return true
}
}
for _, gs := range override.GitlabGroups {
if gs.Host != host {
continue
}
if path == gs.Group || strings.HasPrefix(path, gs.Group+"/") {
if !containsString(gs.ExcludeProjects, projName) {
return true
}
}
}
for _, ghs := range override.GithubOrgs {
if ghs.Host != host {
continue
}
if strings.HasPrefix(path, ghs.Org+"/") {
if !containsString(ghs.ExcludeProjects, projName) {
return true
}
}
}
return false
}
func containsString(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}
// ScopeProject holds parsed remote info for a project within a gate's scope.
type ScopeProject struct {
Host string
Path string // full path from ParseRemoteURL, e.g. "cego/monolith"
}
// ScopeRowKind distinguishes tree node types.
type ScopeRowKind int
const (
ScopeRowHost ScopeRowKind = iota // e.g. gitlab.cego.dk
ScopeRowNamespace // e.g. cego, services
ScopeRowProject // leaf project
)
// ScopeRow is a flat row in the scope tree.
type ScopeRow struct {
Kind ScopeRowKind
Label string
Depth int
ProjName string // config key, only set for ScopeRowProject
Children []string // config keys of all leaf projects under this node (for branches)
}
// BuildScopeTree builds a flat row list grouped by host → namespace segments → project.
func BuildScopeTree(projects map[string]ScopeProject) []ScopeRow {
hostMap := make(map[string]map[string]ScopeProject)
for name, sp := range projects {
if hostMap[sp.Host] == nil {
hostMap[sp.Host] = make(map[string]ScopeProject)
}
hostMap[sp.Host][name] = sp
}
hosts := make([]string, 0, len(hostMap))
for h := range hostMap {
hosts = append(hosts, h)
}
sort.Strings(hosts)
var rows []ScopeRow
for _, host := range hosts {
hostProjs := hostMap[host]
hostChildren := sortedKeys(hostProjs)
rows = append(rows, ScopeRow{Kind: ScopeRowHost, Label: host, Depth: 0, Children: hostChildren})
rows = append(rows, buildNamespaceRows(hostProjs, 1)...)
}
return rows
}
func buildNamespaceRows(projects map[string]ScopeProject, depth int) []ScopeRow {
type nsEntry struct {
name string
sp ScopeProject
}
nsMap := make(map[string][]nsEntry)
var leafs []nsEntry
for name, sp := range projects {
parts := strings.SplitN(sp.Path, "/", 2)
if len(parts) == 1 {
leafs = append(leafs, nsEntry{name: name, sp: sp})
} else {
seg := parts[0]
nsMap[seg] = append(nsMap[seg], nsEntry{
name: name,
sp: ScopeProject{Host: sp.Host, Path: parts[1]},
})
}
}
nsKeys := make([]string, 0, len(nsMap))
for k := range nsMap {
nsKeys = append(nsKeys, k)
}
sort.Strings(nsKeys)
sort.Slice(leafs, func(i, j int) bool { return leafs[i].name < leafs[j].name })
var rows []ScopeRow
for _, l := range leafs {
rows = append(rows, ScopeRow{
Kind: ScopeRowProject, Label: l.name, Depth: depth, ProjName: l.name,
})
}
for _, seg := range nsKeys {
entries := nsMap[seg]
children := make([]string, len(entries))
subProjs := make(map[string]ScopeProject, len(entries))
for i, e := range entries {
children[i] = e.name
subProjs[e.name] = e.sp
}
sort.Strings(children)
rows = append(rows, ScopeRow{
Kind: ScopeRowNamespace, Label: seg, Depth: depth, Children: children,
})
rows = append(rows, buildNamespaceRows(subProjs, depth+1)...)
}
return rows
}
func sortedKeys(m map[string]ScopeProject) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// CheckedStateToOverride converts a map of project checked states to a ScopeOverride.
// Returns nil if all projects are checked (full scope). Returns empty override if none checked.
func CheckedStateToOverride(checked map[string]bool, projects map[string]ScopeProject) *state.ScopeOverride {
allChecked := true
anyChecked := false
for _, v := range checked {
if !v {
allChecked = false
} else {
anyChecked = true
}
}
if allChecked {
return nil
}
if !anyChecked {
return &state.ScopeOverride{}
}
type groupInfo struct {
host string
segment string
projects map[string]bool
}
groups := make(map[string]*groupInfo)
var standaloneProjects []string
for name, sp := range projects {
parts := strings.SplitN(sp.Path, "/", 2)
if len(parts) == 1 {
if checked[name] {
standaloneProjects = append(standaloneProjects, name)
}
continue
}
key := sp.Host + "/" + parts[0]
if groups[key] == nil {
groups[key] = &groupInfo{
host: sp.Host,
segment: parts[0],
projects: make(map[string]bool),
}
}
groups[key].projects[name] = checked[name]
}
override := &state.ScopeOverride{}
for _, gi := range groups {
allGroupChecked := true
anyGroupChecked := false
var excluded []string
for name, isChecked := range gi.projects {
if !isChecked {
allGroupChecked = false
excluded = append(excluded, name)
} else {
anyGroupChecked = true
}
}
if !anyGroupChecked {
continue
}
sort.Strings(excluded)
isGithub := strings.Contains(gi.host, "github")
if isGithub {
entry := state.ScopeOverrideOrg{Host: gi.host, Org: gi.segment}
if !allGroupChecked {
entry.ExcludeProjects = excluded
}
override.GithubOrgs = append(override.GithubOrgs, entry)
} else {
entry := state.ScopeOverrideGroup{Host: gi.host, Group: gi.segment}
if !allGroupChecked {
entry.ExcludeProjects = excluded
}
override.GitlabGroups = append(override.GitlabGroups, entry)
}
}
sort.Strings(standaloneProjects)
override.Projects = standaloneProjects
return override
}
// OverrideToCheckedState converts a ScopeOverride to a per-project checked map.
// If override is nil, all projects are checked (full scope).
func OverrideToCheckedState(override *state.ScopeOverride, projects map[string]ScopeProject) map[string]bool {
checked := make(map[string]bool, len(projects))
if override == nil {
for name := range projects {
checked[name] = true
}
return checked
}
for name, sp := range projects {
checked[name] = ProjectMatchesOverrideScope(name, sp.Host, sp.Path, override)
}
return checked
}