Skip to content

Commit

Permalink
tools/populate-owners: Also populate ci-operator/config/{org}/{repo}/…
Browse files Browse the repository at this point in the history
…OWNERS

The old shell script used to do this, but I'd accidentally dropped the
functionality in e1f993f (populate-owners: Also slurp OWNERS_ALIASES,
2018-08-25, openshift#1285).
  • Loading branch information
wking committed Aug 30, 2018
1 parent 8f42984 commit 206dc9e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tools/populate-owners/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 21 additions & 1 deletion tools/populate-owners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
62 changes: 62 additions & 0 deletions tools/populate-owners/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 206dc9e

Please sign in to comment.