Skip to content

Commit 68376e1

Browse files
committed
validate-rules: use base-branch local staging checks
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
1 parent 8245f93 commit 68376e1

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

cmd/validate-rules/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
if err := config.Validate(rules); err != nil {
4343
glog.Fatalf("Invalid rules file %q: %v", f, err)
4444
}
45-
errors := staging.EnsureStagingDirectoriesExist(rules, os.Getenv(baseRefEnvKey))
45+
errors := staging.EnsureStagingDirectoriesExist(rules, os.Getenv(baseRefEnvKey), f)
4646
if errors != nil {
4747
for _, err := range errors {
4848
glog.Errorf("Error: %s", err)

cmd/validate-rules/staging/validate.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package staging
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"path/filepath"
2223

2324
"github.com/golang/glog"
@@ -34,18 +35,14 @@ const defaultBranch = "master"
3435

3536
// EnsureStagingDirectoriesExist walks through the repository rules and checks
3637
// if the specified directories are present in the specific kubernetes branch.
37-
func EnsureStagingDirectoriesExist(rules *config.RepositoryRules, baseBranch string) []error {
38+
func EnsureStagingDirectoriesExist(rules *config.RepositoryRules, baseBranch, rulesFile string) []error {
3839
glog.Infof("Validating directories exist in the Kubernetes branches")
3940

4041
if baseBranch == "" {
4142
baseBranch = defaultBranch
4243
}
43-
44-
if baseBranch == defaultBranch {
45-
glog.Infof("Testing all rules because the base branch is: %s", baseBranch)
46-
} else {
47-
glog.Infof("Testing only rules matching the base branch: %s", baseBranch)
48-
}
44+
glog.Infof("Testing only rules matching the base branch: %s", baseBranch)
45+
kubeRoot := filepath.Clean(filepath.Join(filepath.Dir(rulesFile), "..", ".."))
4946

5047
var errors []error
5148
for _, rule := range rules.Rules {
@@ -54,22 +51,22 @@ func EnsureStagingDirectoriesExist(rules *config.RepositoryRules, baseBranch str
5451
// ensure all the mentioned directories exist
5552
for _, dir := range branchRule.Source.Dirs {
5653
_, directory := filepath.Split(dir)
57-
if baseBranch != defaultBranch && baseBranch != branchRule.Source.Branch {
54+
if baseBranch != branchRule.Source.Branch {
5855
glog.Infof("Skipping branch %q for repository %q", branchRule.Source.Branch, directory)
5956
continue
6057
}
61-
err := checkDirectoryExistsInBranch(directory, branchRule.Source.Branch)
58+
err := checkDirectoryExists(directory, branchRule.Source.Branch, baseBranch, kubeRoot)
6259
if err != nil {
6360
errors = append(errors, err)
6461
}
6562
}
6663

6764
for _, dependency := range branchRule.Dependencies {
68-
if baseBranch != defaultBranch && baseBranch != dependency.Branch {
65+
if baseBranch != dependency.Branch {
6966
glog.Infof("Skipping branch %q for dependency %q", dependency.Branch, dependency.Repository)
7067
continue
7168
}
72-
err := checkDirectoryExistsInBranch(dependency.Repository, dependency.Branch)
69+
err := checkDirectoryExists(dependency.Repository, dependency.Branch, baseBranch, kubeRoot)
7370
if err != nil {
7471
errors = append(errors, err)
7572
}
@@ -79,6 +76,16 @@ func EnsureStagingDirectoriesExist(rules *config.RepositoryRules, baseBranch str
7976
return errors
8077
}
8178

79+
func checkDirectoryExists(directory, branch, baseBranch, kubeRoot string) error {
80+
if branch == baseBranch {
81+
localPath := filepath.Join(kubeRoot, "staging", "src", "k8s.io", directory)
82+
if info, err := os.Stat(localPath); err == nil && info.IsDir() {
83+
return nil
84+
}
85+
}
86+
return checkDirectoryExistsInBranch(directory, branch)
87+
}
88+
8289
func checkDirectoryExistsInBranch(directory, branch string) error {
8390
glog.Infof("Check if directory %q exists in branch %q", directory, branch)
8491

0 commit comments

Comments
 (0)