-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixture.go
115 lines (107 loc) · 3 KB
/
fixture.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
package dadsgha
import (
"fmt"
"regexp"
"strings"
"time"
)
// RawEndpoint holds data source endpoint with possible flags how to generate the final endpoints
// flags can be "type: github_org/github_user" which means that we need to get actual repository list from github org/user
type RawEndpoint struct {
Name string `yaml:"name"`
Flags map[string]string `yaml:"flags"`
Skip []string `yaml:"skip"`
Only []string `yaml:"only"`
Project string `yaml:"project"`
SkipREs []*regexp.Regexp `yaml:"-"`
OnlyREs []*regexp.Regexp `yaml:"-"`
}
// EndpointIncluded - checks if given endpoint's origin should be included or excluded based on endpoint's skip/only regular expressions lists
// First return value specifies if endpoint is included or not
// Second value specifies: 1 - included by 'only' condition, 2 - skipped by 'skip' condition
func EndpointIncluded(ctx *Ctx, ep *RawEndpoint, origin string) (bool, int) {
for _, skipRE := range ep.SkipREs {
if skipRE.MatchString(origin) {
if ctx.Debug > 0 {
fmt.Printf("%s: skipped %s (%v)\n", ep.Name, origin, skipRE)
}
return false, 2
}
}
if len(ep.OnlyREs) == 0 {
if ctx.Debug > 0 {
fmt.Printf("%s: included all\n", ep.Name)
}
return true, 0
}
included := false
inc := 0
for _, onlyRE := range ep.OnlyREs {
if onlyRE.MatchString(origin) {
if ctx.Debug > 0 {
fmt.Printf("%s: included %s (%v)\n", ep.Name, origin, onlyRE)
}
included = true
inc = 1
break
}
}
return included, inc
}
// Project holds project data and list of endpoints
type Project struct {
Name string `yaml:"name"`
RawEndpoints []RawEndpoint `yaml:"endpoints"`
HistEndpoints []RawEndpoint `yaml:"historical_endpoints"`
}
// DataSource contains data source spec from dev-analytics-api
type DataSource struct {
Slug string `yaml:"slug"`
Projects []Project `yaml:"projects"`
RawEndpoints []RawEndpoint `yaml:"endpoints"`
HistEndpoints []RawEndpoint `yaml:"historical_endpoints"`
IndexSuffix string `yaml:"index_suffix"`
}
// Native - keeps fixture slug and eventual global affiliation source
type Native struct {
Slug string `yaml:"slug"`
}
// Fixture contains full YAML structure of dev-analytics-api fixture files
type Fixture struct {
Disabled bool `yaml:"disabled"`
Native Native `yaml:"native"`
DataSources []DataSource `yaml:"data_sources"`
}
// GetFixtures - read all fixture files
func GetFixtures(ctx *Ctx, path string) (fixtures []string) {
dtStart := time.Now()
ctx.ExecOutput = true
defer func() {
ctx.ExecOutput = false
}()
if path == "" {
path = "data/"
}
res, err := ExecCommand(
ctx,
[]string{
"find",
path,
"-type",
"f",
"-iname",
"*.y*ml",
},
nil,
nil,
)
dtEnd := time.Now()
if err != nil {
Fatalf("Error finding fixtures (took %v): %+v\n", dtEnd.Sub(dtStart), err)
}
fixtures = strings.Split(res, "\n")
if ctx.Debug > 0 {
Printf("Fixtures to process: %+v\n", fixtures)
}
return
}