Skip to content

Commit

Permalink
not-owned-checker: Add git-ls-tree implementation with subdirectory s…
Browse files Browse the repository at this point in the history
…upport (#141)
  • Loading branch information
Jeremy Cohen committed Apr 12, 2022
1 parent f555ba6 commit a16e4b9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
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. |

<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
16 changes: 11 additions & 5 deletions internal/check/not_owned_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
)

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

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

func NewNotOwnedFile(cfg NotOwnedFileConfig) *NotOwnedFile {
Expand All @@ -30,7 +32,8 @@ func NewNotOwnedFile(cfg NotOwnedFileConfig) *NotOwnedFile {
}

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

Expand Down Expand Up @@ -130,7 +133,7 @@ func (c *NotOwnedFile) GitRemoveIgnoredFiles(repoDir string) error {
pipe.ChDir(repoDir),
pipe.Line(
pipe.Exec("git", "ls-files", "-ci", "--exclude-standard", "-z"),
pipe.Exec("xargs", "-0", "git", "rm", "--cached"),
pipe.Exec("xargs", "-0", "-r", "git", "rm", "--cached"),
),
)

Expand Down Expand Up @@ -168,9 +171,12 @@ func (c *NotOwnedFile) GitResetCurrentBranch(repoDir string) error {
}

func (c *NotOwnedFile) GitListFiles(repoDir string) (string, error) {
args := []string{"ls-files"}
args = append(args, c.subDirectories...)

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

stdout, stderr, err := pipe.DividedOutput(gitls)
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ func TestCheckFailures(t *testing.T) {
"NOT_OWNED_CHECKER_SKIP_PATTERNS": "*",
},
},
{
name: "notowned_sub_dirs",
envs: Envs{
"PATH": os.Getenv("PATH"), // need to be set to find the `git` binary
"CHECKS": "disable-all",
"EXPERIMENTAL_CHECKS": "notowned",
"NOT_OWNED_CHECKER_SKIP_PATTERNS": "*",
"NOT_OWNED_CHECKER_SUBDIRECTORIES": "notowned/dir",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
==> Executing [Experimental] Not Owned File Checker (<duration>)
[err] Found 3 not owned files (skipped patterns: "*"):
[err] Found 4 not owned files (skipped patterns: "*"):
* .gitignore
* CODEOWNERS
* action.yml
* notowned/dir/example/sample.txt

1 check(s) executed, 1 failure(s)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
==> Executing [Experimental] Not Owned File Checker (<duration>)
[err] Found 1 not owned files (skipped patterns: "*"):
* notowned/dir/example/sample.txt

1 check(s) executed, 1 failure(s)

0 comments on commit a16e4b9

Please sign in to comment.