Skip to content

Commit 82b5796

Browse files
committed
quality rules: support enabling/disabling of rules based on conditions like repo origin
1 parent 768243f commit 82b5796

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

cmd/bob/quality.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func qualityCheckBuilderUsesExpect(rules map[string]string, bobfile *Bobfile) er
2424

2525
func qualityCheckFiles(rules []FileQualityRule) error {
2626
for _, rule := range rules {
27+
if !conditionsPass(rule.Conditions) { // rule not in use because didn't pass all conditions
28+
continue
29+
}
30+
2731
if err := qualityCheckFile(rule); err != nil {
2832
return fmt.Errorf("quality: file %s: %w", rule.Path, err)
2933
}
@@ -63,3 +67,48 @@ func qualityCheckFile(rule FileQualityRule) error {
6367

6468
return nil
6569
}
70+
71+
func conditionsPass(conditions []QualityRuleCondition) bool {
72+
for _, condition := range conditions {
73+
conditionPasses := func(matches bool) bool {
74+
if condition.Enable && !matches { // condition disqualifies if NOT match
75+
return false
76+
} else if !condition.Enable && matches { // condition disqualifies if DOES match
77+
return false
78+
}
79+
80+
return true
81+
}
82+
83+
if origin := condition.RepoOrigin; origin != "" {
84+
// "*foo*" => "foo"
85+
originWildcard := strings.Trim(origin, "*")
86+
if len(originWildcard) != len(origin)-2 { // stupidest way to check it had * at start AND end
87+
panic("repo_origin needs to be in form '*foobar*'")
88+
}
89+
90+
originMatches := strings.Contains(githubURL(getGithubRepoRef()), originWildcard)
91+
92+
if !conditionPasses(originMatches) {
93+
return false
94+
}
95+
}
96+
}
97+
98+
return true
99+
}
100+
101+
// lazily read gitHubRepoRefFromGit() just once
102+
func getGithubRepoRef() githubRepoRef {
103+
if githubRepoRefSingleton == nil {
104+
var err error
105+
githubRepoRefSingleton, err = gitHubRepoRefFromGit()
106+
if err != nil {
107+
panic(err)
108+
}
109+
}
110+
111+
return *githubRepoRefSingleton
112+
}
113+
114+
var githubRepoRefSingleton *githubRepoRef

cmd/bob/userconfig.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ type UserconfigFile struct {
3232
}
3333

3434
type FileQualityRule struct {
35-
Path string `json:"path"` // file path relative to repo root, e.g. "docs/security.md"
36-
MustExist *bool `json:"must_exist"` // true => must exist, false => must not exist, nil => ok if not exists
37-
MustContain []string `json:"must_contain"` // strings the file must contain
38-
MustNotContain []string `json:"must_not_contain"` // strings the file must not contain
35+
Path string `json:"path"` // file path relative to repo root, e.g. "docs/security.md"
36+
MustExist *bool `json:"must_exist"` // true => must exist, false => must not exist, nil => ok if not exists
37+
MustContain []string `json:"must_contain"` // strings the file must contain
38+
MustNotContain []string `json:"must_not_contain"` // strings the file must not contain
39+
Conditions []QualityRuleCondition `json:"conditions"` // rule may be run conditionally
40+
}
41+
42+
type QualityRuleCondition struct {
43+
RepoOrigin string `json:"repo_origin"`
44+
Enable bool `json:"enable"`
3945
}
4046

4147
func (u *UserconfigFile) CodeEditorCmd(projectRoot string) ([]string, error) {

0 commit comments

Comments
 (0)