This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 280
/
utils.go
109 lines (97 loc) · 2.75 KB
/
utils.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
package scm_provider
import (
"context"
"fmt"
"regexp"
argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1"
)
func compileFilters(filters []argoprojiov1alpha1.SCMProviderGeneratorFilter) ([]*Filter, error) {
outFilters := make([]*Filter, 0, len(filters))
for _, filter := range filters {
outFilter := &Filter{}
var err error
if filter.RepositoryMatch != nil {
outFilter.RepositoryMatch, err = regexp.Compile(*filter.RepositoryMatch)
if err != nil {
return nil, fmt.Errorf("error compiling RepositoryMatch regexp %q: %v", *filter.RepositoryMatch, err)
}
}
if filter.LabelMatch != nil {
outFilter.LabelMatch, err = regexp.Compile(*filter.LabelMatch)
if err != nil {
return nil, fmt.Errorf("error compiling LabelMatch regexp %q: %v", *filter.LabelMatch, err)
}
}
if filter.PathsExist != nil {
outFilter.PathsExist = filter.PathsExist
}
if filter.BranchMatch != nil {
outFilter.BranchMatch, err = regexp.Compile(*filter.BranchMatch)
if err != nil {
return nil, fmt.Errorf("error compiling BranchMatch regexp %q: %v", *filter.LabelMatch, err)
}
}
outFilters = append(outFilters, outFilter)
}
return outFilters, nil
}
func matchFilter(ctx context.Context, provider SCMProviderService, repo *Repository, filter *Filter) (bool, error) {
if filter.RepositoryMatch != nil && !filter.RepositoryMatch.MatchString(repo.Repository) {
return false, nil
}
if filter.BranchMatch != nil && !filter.BranchMatch.MatchString(repo.Branch) {
return false, nil
}
if filter.LabelMatch != nil {
found := false
for _, label := range repo.Labels {
if filter.LabelMatch.MatchString(label) {
found = true
break
}
}
if !found {
return false, nil
}
}
if len(filter.PathsExist) != 0 {
for _, path := range filter.PathsExist {
hasPath, err := provider.RepoHasPath(ctx, repo, path)
if err != nil {
return false, err
}
if !hasPath {
return false, nil
}
}
}
return true, nil
}
func ListRepos(ctx context.Context, provider SCMProviderService, filters []argoprojiov1alpha1.SCMProviderGeneratorFilter, cloneProtocol string) ([]*Repository, error) {
compiledFilters, err := compileFilters(filters)
if err != nil {
return nil, err
}
repos, err := provider.ListRepos(ctx, cloneProtocol)
if err != nil {
return nil, err
}
// Special case, if we have no filters, allow everything.
if len(compiledFilters) == 0 {
return repos, nil
}
filteredRepos := make([]*Repository, 0, len(repos))
for _, repo := range repos {
for _, filter := range compiledFilters {
matches, err := matchFilter(ctx, provider, repo, filter)
if err != nil {
return nil, err
}
if matches {
filteredRepos = append(filteredRepos, repo)
break
}
}
}
return filteredRepos, nil
}