-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciinfo.go
107 lines (85 loc) · 1.72 KB
/
ciinfo.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
package ciinfo
import (
"os"
)
var (
Name string
IsPr bool
IsCI bool
vendorsIsCI map[string]bool
)
func init() {
initialize()
}
func initialize() {
vendorsIsCI = make(map[string]bool)
IsPr = false
IsCI = false
Name = ""
for _, vendor := range vendors {
envKeys := vendor.env
vendorsIsCI[vendor.constant] = false
for _, env := range envKeys {
if verifyCI(env) {
vendorsIsCI[vendor.constant] = true
break
}
}
if !vendorsIsCI[vendor.constant] {
continue
}
Name = vendor.name
for _, pr := range vendor.pr {
IsPr = verifyPr(pr)
if IsPr {
break
}
}
}
IsCI = isCI()
}
func isCI() bool {
envKeys := []string{
"CI", // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
"CONTINUOUS_INTEGRATION", // Travis CI, Cirrus CI
"BUILD_NUMBER", // Jenkins, TeamCity
"RUN_ID", // TaskCluster, dsari
"CI_APP_ID", // Applfow
"CI_BUILD_ID", // Applfow
"CI_BUILD_NUMBER", // Applfow
}
for _, key := range envKeys {
if _, exists := os.LookupEnv(key); exists {
return true
}
}
return Name != ""
}
func IsVendor(vendor string) bool {
return vendorsIsCI[vendor]
}
func verifyCI(e env) bool {
value, exists := os.LookupEnv(e.key)
if !exists {
return false
}
if e.eq != "" && value != e.eq {
return false
}
return true
}
func verifyPr(p pr) bool {
value, exists := os.LookupEnv(p.key)
if !exists {
return false
}
// if we have eq constraint then env value must be == with the eq
if p.eq != "" {
return value == p.eq
}
// if we have ne constraint then env value must be != with the ne
if p.ne != "" {
return value != p.ne
}
return true
}