-
Notifications
You must be signed in to change notification settings - Fork 554
Add check for github codeowners file #2768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5872fa5
Add check for github codeowners file
jsoriano fbe881d
Check scanner error
jsoriano c9084c3
Move codeowners checks to the dev folder
jsoriano 5efe4b8
Add tests for codeowners
jsoriano fdb4e67
Run go test
jsoriano f646cea
Don't use github name for test codeowners files
jsoriano f420be6
Merge remote-tracking branch 'origin/main' into codeowners-check
jsoriano aea4dae
Add missing codeowners entry
jsoriano fab5c67
Hide go test output by default
jsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package codeowners | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "io/fs" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| const ( | ||
| codeownersPath = ".github/CODEOWNERS" | ||
| ) | ||
|
|
||
| func Check() error { | ||
| codeowners, err := readGithubOwners(codeownersPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| const packagesDir = "packages" | ||
| return filepath.WalkDir(packagesDir, func(path string, d fs.DirEntry, err error) error { | ||
| if d.IsDir() { | ||
| if path != packagesDir && filepath.Dir(path) != packagesDir { | ||
| return fs.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
| if d.Name() != "manifest.yml" { | ||
| return nil | ||
| } | ||
|
|
||
| return codeowners.checkManifest(path) | ||
| }) | ||
| } | ||
|
|
||
| type githubOwners struct { | ||
| owners map[string][]string | ||
| path string | ||
| } | ||
|
|
||
| func readGithubOwners(codeownersPath string) (*githubOwners, error) { | ||
| f, err := os.Open(codeownersPath) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "failed to open %q", codeownersPath) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| codeowners := githubOwners{ | ||
| owners: make(map[string][]string), | ||
| path: codeownersPath, | ||
| } | ||
|
|
||
| scanner := bufio.NewScanner(f) | ||
| lineNumber := 0 | ||
| for scanner.Scan() { | ||
| lineNumber++ | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if len(line) == 0 || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| fields := strings.Fields(line) | ||
| if len(fields) < 2 { | ||
| return nil, errors.Errorf("invalid line %d in %q: %q", lineNumber, codeownersPath, line) | ||
| } | ||
| path, owners := fields[0], fields[1:] | ||
|
|
||
| // It is ok to overwrite because latter lines have precedence in these files. | ||
| codeowners.owners[path] = owners | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, errors.Wrapf(err, "scanner error") | ||
| } | ||
|
|
||
| return &codeowners, nil | ||
| } | ||
|
|
||
| func (codeowners *githubOwners) checkManifest(path string) error { | ||
| pkgDir := filepath.Dir(path) | ||
| owners, found := codeowners.owners["/"+pkgDir] | ||
| if !found { | ||
| return errors.Errorf("there is no owner for %q in %q", pkgDir, codeowners.path) | ||
| } | ||
|
|
||
| content, err := ioutil.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var manifest struct { | ||
| Owner struct { | ||
| Github string `yaml:"github"` | ||
| } `yaml:"owner"` | ||
| } | ||
| err = yaml.Unmarshal(content, &manifest) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if manifest.Owner.Github == "" { | ||
| return errors.Errorf("no owner specified in %q", path) | ||
| } | ||
|
|
||
| found = false | ||
| for _, owner := range owners { | ||
| if owner == "@"+manifest.Owner.Github { | ||
| found = true | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| return errors.Errorf("owner %q defined in %q is not in %q", manifest.Owner.Github, path, codeowners.path) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package codeowners | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCheckManifest(t *testing.T) { | ||
| cases := []struct { | ||
| codeownersPath string | ||
| manifestPath string | ||
| valid bool | ||
| }{ | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-valid", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: true, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-valid", | ||
| manifestPath: "testdata/noowner/manifest.yml", | ||
| valid: false, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-multiple-owners", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: true, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-empty", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: false, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-wrong-devexp", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: false, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-precedence", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: true, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-wrong-precedence", | ||
| manifestPath: "testdata/devexp/manifest.yml", | ||
| valid: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.codeownersPath+"_"+c.manifestPath, func(t *testing.T) { | ||
| owners, err := readGithubOwners(c.codeownersPath) | ||
| require.NoError(t, err) | ||
|
|
||
| err = owners.checkManifest(c.manifestPath) | ||
| if c.valid { | ||
| assert.NoError(t, err) | ||
| } else { | ||
| assert.Error(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestReadGithubOwners(t *testing.T) { | ||
| cases := []struct { | ||
| codeownersPath string | ||
| valid bool | ||
| }{ | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-valid", | ||
| valid: true, | ||
| }, | ||
| { | ||
| codeownersPath: "notexsists", | ||
| valid: false, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-no-owner", | ||
| valid: false, | ||
| }, | ||
| { | ||
| codeownersPath: "testdata/CODEOWNERS-multiple-owners", | ||
| valid: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.codeownersPath, func(t *testing.T) { | ||
| _, err := readGithubOwners(c.codeownersPath) | ||
| if c.valid { | ||
| assert.NoError(t, err) | ||
| } else { | ||
| assert.Error(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| owner: ~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # No owners here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # This is a valid test file with multiple owners for a path | ||
|
|
||
| /testdata/devexp @elastic/integrations @elastic/integrations-developer-experience | ||
| /testdata/integration @elastic/integrations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # This is invalid, a path has no owner. | ||
|
|
||
| /testdata/devexp @elastic/integrations-developer-experience | ||
| /testdata/integration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Here the codeowner is correctly overriden. | ||
|
|
||
| /testdata/devexp @jsoriano | ||
| /testdata/devexp @elastic/integrations-developer-experience |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # This is a valid test file | ||
|
|
||
| /testdata/devexp @elastic/integrations-developer-experience | ||
| /testdata/integration @elastic/integrations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Here the codeowner is not what was expected. | ||
|
|
||
| /testdata/devexp @jsoriano |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Here the codeowner is incorrectly overriden. | ||
|
|
||
| /testdata/devexp @elastic/integrations-developer-experience | ||
| /testdata/devexp @jsoriano |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| owner: | ||
| github: elastic/integrations-developer-experience |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,15 @@ | |
| package main | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/magefile/mage/mg" | ||
| "github.com/magefile/mage/sh" | ||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/elastic/integrations/dev/codeowners" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -27,6 +30,8 @@ func Check() error { | |
| mg.Deps(build) | ||
| mg.Deps(format) | ||
| mg.Deps(modTidy) | ||
| mg.Deps(goTest) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find if go tests were executed in CI. As all the Go code is somehow related to the magefile, I am adding it here, but I guess this could be added somewhere in the Jenkinsfile too. |
||
| mg.Deps(codeowners.Check) | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -86,6 +91,20 @@ func goImports() error { | |
| return sh.RunV("goimports", args...) | ||
| } | ||
|
|
||
| func goTest() error { | ||
| args := []string{"test"} | ||
| stdout := io.Discard | ||
| stderr := io.Discard | ||
| if mg.Verbose() { | ||
| args = append(args, "-v") | ||
| stdout = os.Stdout | ||
| stderr = os.Stderr | ||
| } | ||
| args = append(args, "./dev/...") | ||
| _, err := sh.Exec(nil, stdout, stderr, "go", args...) | ||
| return err | ||
| } | ||
|
|
||
| func findFilesRecursive(match func(path string, info os.FileInfo) bool) ([]string, error) { | ||
| var matches []string | ||
| err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.