From 206dc9e5e742f8ee37887518088eb50b96a28bcb Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 30 Aug 2018 14:08:05 -0700 Subject: [PATCH] tools/populate-owners: Also populate ci-operator/config/{org}/{repo}/OWNERS The old shell script used to do this, but I'd accidentally dropped the functionality in e1f993fb (populate-owners: Also slurp OWNERS_ALIASES, 2018-08-25, #1285). --- tools/populate-owners/README.md | 4 +- tools/populate-owners/main.go | 22 ++++++++++- tools/populate-owners/main_test.go | 62 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/tools/populate-owners/README.md b/tools/populate-owners/README.md index d3128c2ab161..7bbbc94801ce 100644 --- a/tools/populate-owners/README.md +++ b/tools/populate-owners/README.md @@ -23,8 +23,8 @@ For example, if [openshift/origin][] and [openshift/installer][] both defined an After namespacing aliases, the utility writes `OWNERS_ALIASES` to the root of this repository. If no upstreams define aliases, then the utility removes `OWNER_ALIASES` from the root of this repository. -The utility also iterates through the `ci-operator/jobs/{organization}/{repository}` directories, writing `OWNERS` to reflect the upstream configuration. -If the upstream did not have an `OWNERS` file, the utility removes the associated `ci-operator/jobs/{organization}/{repository}/OWNERS`. +The utility also iterates through the `ci-operator/jobs/{organization}/{repository}` and `ci-operator/config/{organization}/{repository}` directories, writing `OWNERS` to reflect the upstream configuration. +If the upstream did not have an `OWNERS` file, the utility removes the associated `ci-operator/*/{organization}/{repository}/OWNERS`. [openshift/origin]: https://github.com/openshift/origin [openshift/installer]: https://github.com/openshift/installer diff --git a/tools/populate-owners/main.go b/tools/populate-owners/main.go index b3adc39f4921..fcde596f0fc7 100644 --- a/tools/populate-owners/main.go +++ b/tools/populate-owners/main.go @@ -93,6 +93,20 @@ func orgRepos(dir string) (orgRepos []*orgRepo, err error) { return orgRepos, err } +func (orgRepo *orgRepo) getConfig(dir string) (err error) { + path := filepath.Join(dir, orgRepo.Organization, orgRepo.Repository) + info, err := os.Stat(path) + if err != nil { + return err + } + + if info.IsDir() { + orgRepo.Directories = append(orgRepo.Directories, path) + } + + return nil +} + func (orgRepo *orgRepo) getOwners() (err error) { dir, err := ioutil.TempDir("", "populate-owners-") if err != nil { @@ -296,8 +310,14 @@ func pullOwners(directory string) (err error) { return err } + config := filepath.Join(repoRoot, "ci-operator", "config") for _, orgRepo := range orgRepos { - err := orgRepo.getOwners() + err = orgRepo.getConfig(config) + if err != nil && !os.IsNotExist(err) { + return err + } + + err = orgRepo.getOwners() if err != nil && !os.IsNotExist(err) { return err } diff --git a/tools/populate-owners/main_test.go b/tools/populate-owners/main_test.go index a084aef8e0e5..333a46a432f2 100644 --- a/tools/populate-owners/main_test.go +++ b/tools/populate-owners/main_test.go @@ -92,6 +92,68 @@ func TestOrgRepos(t *testing.T) { assertEqual(t, orgRepos, expected) } +func TestGetConfig(t *testing.T) { + dir, err := ioutil.TempDir("", "populate-owners-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + repoAB := filepath.Join(dir, "a", "b") + err = os.MkdirAll(repoAB, 0777) + if err != nil { + t.Fatal(err) + } + + for _, test := range []struct{ + name string + input *orgRepo + expected *orgRepo + error *regexp.Regexp + }{ + { + name: "config exists", + input: &orgRepo{ + Directories: []string{"some/directory"}, + Organization: "a", + Repository: "b", + }, + expected: &orgRepo{ + Directories: []string{"some/directory", filepath.Join(dir, "a", "b")}, + Organization: "a", + Repository: "b", + }, + }, + { + name: "config does not exist", + input: &orgRepo{ + Directories: []string{"some/directory"}, + Organization: "c", + Repository: "d", + }, + expected: &orgRepo{ + Directories: []string{"some/directory"}, + Organization: "c", + Repository: "d", + }, + error: regexp.MustCompile("^stat .*/c/d: no such file or directory"), + }, + } { + t.Run(test.name, func (t *testing.T) { + err := test.input.getConfig(dir) + if test.error == nil { + if err != nil { + t.Fatal(err) + } + } else if !test.error.MatchString(err.Error()) { + t.Fatalf("unexpected error: %v does not match %v", err, test.error) + } + + assertEqual(t, test.input, test.expected) + }) + } +} + func TestExtractOwners(t *testing.T) { dir, err := ioutil.TempDir("", "populate-owners-") if err != nil {