Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Use the following environment variables to configure the application:
| <tt>OWNER_CHECKER_OWNERS_MUST_BE_TEAMS</tt> | `false` | Specifies whether only teams are allowed as owners of files. |
| <tt>NOT_OWNED_CHECKER_SKIP_PATTERNS</tt> | | The comma-separated list of patterns that should be ignored by `not-owned-checker`. For example, you can specify `*` and as a result, the `*` pattern from the **CODEOWNERS** file will be ignored and files owned by this pattern will be reported as unowned unless a later specific pattern will match that path. It's useful because often we have default owners entry at the begging of the CODOEWNERS file, e.g. `* @global-owner1 @global-owner2` |
| <tt>NOT_OWNED_CHECKER_SKIP_PATH_PATTERNS</tt> | | The comma-separated list of path patterns that should be ignored by not-owned-checker. For example `lib/tasks,db/migrations` |
| <tt>NOT_OWNED_CHECKER_ONLY_PATH_PATTERNS</tt> | | The comma-separated list of path patterns that should be checked by not-owned-checker. For example `app` |
| <tt>NOT_OWNED_CHECKER_GIT_DIFF_ARGUMENTS</tt> | | The comma-separated list of git diff options that should be used by `not-owned-checker`. |
| <tt>NOT_OWNED_CHECKER_SUBDIRECTORIES</tt> | | The comma-separated list of subdirectories to check in `not-owned-checker`. When specified, only files in the listed subdirectories will be checked if they do not have specified owners in CODEOWNERS. |
| <tt>NOT_OWNED_CHECKER_TRUST_WORKSPACE</tt> | `false` | Specifies whether the repository path should be marked as safe. See: https://github.com/actions/checkout/issues/766. |
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ inputs:
description: "The comma-separated list of path patterns that should be ignored by not-owned-checker. For example `lib/tasks,db/migrations`"
required: false

not_owned_checker_only_path_patterns:
description: "The comma-separated list of path patterns that should be checked by not-owned-checker. For example `app`"
required: false

owner_checker_repository:
description: "The owner and repository name. For example, gh-codeowners/codeowners-samples. Used to check if GitHub team is in the given organization and has permission to the given repository."
required: false
Expand Down Expand Up @@ -88,7 +92,7 @@ inputs:

runs:
using: 'docker'
image: 'docker://ghcr.io/uchiru/codeowners-validator:v0.8.8'
image: 'docker://ghcr.io/uchiru/codeowners-validator:v0.8.9'
env:
ENVS_PREFIX: "INPUT"

Expand Down
35 changes: 32 additions & 3 deletions internal/check/not_owned_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type NotOwnedFileConfig struct {
TrustWorkspace bool `envconfig:"default=false"`
SkipPatterns []string `envconfig:"optional"`
SkipPathPatterns []string `envconfig:"optional"`
OnlyPathPatterns []string `envconfig:"optional"`
Subdirectories []string `envconfig:"optional"`
GitDiffArguments []string `envconfig:"optional"`
}

type NotOwnedFile struct {
skipPatterns map[string]struct{}
skipPathPatterns []string
onlyPathPatterns []string
subDirectories []string
gitDiffArguments []string
trustWorkspace bool
Expand All @@ -43,6 +45,7 @@ func NewNotOwnedFile(cfg *NotOwnedFileConfig) *NotOwnedFile {
return &NotOwnedFile{
skipPatterns: skip,
skipPathPatterns: cfg.SkipPathPatterns,
onlyPathPatterns: cfg.OnlyPathPatterns,
subDirectories: cfg.Subdirectories,
trustWorkspace: cfg.TrustWorkspace,
gitDiffArguments: cfg.GitDiffArguments,
Expand Down Expand Up @@ -109,9 +112,10 @@ func (c *NotOwnedFile) Check(ctx context.Context, in Input) (output Output, err
if lsOut != "" {
lines := strings.Split(lsOut, "\n")
filteredOwnerLines := c.filterByOwners(patterns, lines)
filteredLines := c.filterByPaths(filteredOwnerLines)
filteredPathLines := c.skipByPaths(filteredOwnerLines)
filteredLines := c.selectByPaths(filteredPathLines)
if len(filteredLines) > 0 {
msg := fmt.Sprintf("Found %d not owned files (skipped patterns: %q, skipped paths: %q):\n%s", len(filteredLines), c.skipPatternsList(), c.skipPathPatterns, c.ListFormatFunc(filteredLines))
msg := fmt.Sprintf("Found %d not owned files (skipped patterns: %q, skipped paths: %q, only paths: %q):\n%s", len(filteredLines), c.skipPatternsList(), c.skipPathPatterns, c.onlyPathPatterns, c.ListFormatFunc(filteredLines))
bldr.ReportIssue(msg)
}
}
Expand Down Expand Up @@ -273,7 +277,7 @@ func (c *NotOwnedFile) filterByOwners(patterns, files []string) []string {
return result
}

func (c *NotOwnedFile) filterByPaths(files []string) []string {
func (c *NotOwnedFile) skipByPaths(files []string) []string {
f := func(search string, patterns []string) bool {
for _, pattern := range patterns {
if pathFound := strings.HasPrefix(search, pattern); pathFound {
Expand All @@ -294,6 +298,31 @@ func (c *NotOwnedFile) filterByPaths(files []string) []string {
return result
}

func (c *NotOwnedFile) selectByPaths(files []string) []string {
if len(c.onlyPathPatterns) == 0 {
return files
}

f := func(search string, patterns []string) bool {
for _, pattern := range patterns {
if pathFound := strings.HasPrefix(search, pattern); pathFound {
return false
}
}
return true
}

var result []string
for _, file := range files {
if filePathfound := f(file, c.onlyPathPatterns); filePathfound {
result = append(result, file)
}
continue
}

return result
}

// ListFormatFunc is a basic formatter that outputs
// a bullet point list of the pattern.
func (c *NotOwnedFile) ListFormatFunc(es []string) string {
Expand Down