Skip to content
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

not-owned-checker: Add git-ls-tree implementation with subdirectory support #141

Merged
merged 5 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -131,6 +131,7 @@ Use the following environment variables to configure the application:
| <tt>OWNER_CHECKER_ALLOW_UNOWNED_PATTERNS</tt> | `true` | Specifies whether CODEOWNERS may have unowned files. For example: <br> <br> `/infra/oncall-rotator/ @sre-team` <br> `/infra/oncall-rotator/oncall-config.yml` <br> <br> The `/infra/oncall-rotator/oncall-config.yml` file is not owned by anyone. |
| <tt>OWNER_CHEKER_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_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. |
mszostok marked this conversation as resolved.
Show resolved Hide resolved

<b>*</b> - Required

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ inputs:
default: "false"
required: false

not_owned_checker_subdirectories:
description: "Only check listed subdirectories for CODEOWNERS ownership that don't have owners."
required: false

runs:
using: 'docker'
image: 'docker://ghcr.io/mszostok/codeowners-validator:v0.7.2'
Expand Down
3 changes: 3 additions & 0 deletions docs/gh-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ jobs:

# Specifies whether only teams are allowed as owners of files.
owner_checker_owners_must_be_teams: "false"

# Only check listed subdirectories for CODEOWNERS ownership that don't have owners.
not_owned_checker_subdirectories: ""
```

The best is to run this as a cron job and not only if you applying changes to CODEOWNERS file itself, e.g. the CODEOWNERS file can be invalidate when you removing someone from the organization.
Expand Down
12 changes: 9 additions & 3 deletions internal/check/not_owned_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (

type NotOwnedFileConfig struct {
SkipPatterns []string `envconfig:"optional"`
Subdirectories []string `envconfig:"optional"`
}

type NotOwnedFile struct {
skipPatterns map[string]struct{}
subDirectories []string
}

func NewNotOwnedFile(cfg NotOwnedFileConfig) *NotOwnedFile {
Expand All @@ -29,8 +31,10 @@ func NewNotOwnedFile(cfg NotOwnedFileConfig) *NotOwnedFile {
skip[p] = struct{}{}
}


return &NotOwnedFile{
skipPatterns: skip,
subDirectories: cfg.Subdirectories,
}
}

Expand Down Expand Up @@ -75,7 +79,7 @@ func (c *NotOwnedFile) Check(ctx context.Context, in Input) (output Output, err
return Output{}, err
}

out, err := c.GitListFiles(in.RepoDir)
out, err := c.GitListFileTree(in.RepoDir)
if err != nil {
return Output{}, err
}
Expand Down Expand Up @@ -167,10 +171,12 @@ func (c *NotOwnedFile) GitResetCurrentBranch(repoDir string) error {
return nil
}

func (c *NotOwnedFile) GitListFiles(repoDir string) (string, error) {
func (c *NotOwnedFile) GitListFileTree(repoDir string) (string, error) {
args := append([]string{"ls-tree", "-r", "--name-only", "HEAD"}, c.subDirectories...)
mszostok marked this conversation as resolved.
Show resolved Hide resolved

gitls := pipe.Script(
pipe.ChDir(repoDir),
pipe.Exec("git", "ls-files"),
pipe.Exec("git", args...),
)

stdout, stderr, err := pipe.DividedOutput(gitls)
Expand Down