Skip to content

Allow CODEOWNERS declare rules with no owners #7

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 1 commit into from
Mar 9, 2022
Merged
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
2 changes: 1 addition & 1 deletion cmd/codeowners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
return err
}
// If we didn't get a match, the file is unowned
if rule == nil {
if rule == nil || rule.Owners == nil {
// Don't show unowned files if we're filtering by owner
if len(ownerFilters) == 0 {
fmt.Printf("%-70s (unowned)\n", path)
Expand Down
16 changes: 9 additions & 7 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@ func parseRule(ruleStr string) (Rule, error) {
// if the line didn't end with a separator (whitespace)
switch state {
case statePattern:
// We should have at least one owner as well
return r, fmt.Errorf("unexpected end of rule")
if buf.Len() == 0 { // We should have non-empty pattern
return r, fmt.Errorf("unexpected end of rule")
}

pattern, err := newPattern(buf.String())
if err != nil {
return r, err
}
r.pattern = pattern

case stateOwners:
// If there's an owner left in the buffer, don't leave it behind
Expand All @@ -132,11 +139,6 @@ func parseRule(ruleStr string) (Rule, error) {
}
}

// All rules need at least one owner
if len(r.Owners) == 0 {
return r, fmt.Errorf("unexpected end of rule")
}

return r, nil
}

Expand Down
32 changes: 27 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,40 @@ func TestParseRule(t *testing.T) {
Comment: "some comment",
},
},
{
name: "pattern with no owners",
rule: "pattern",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "",
},
},
{
name: "pattern with no owners and comment",
rule: "pattern # but no more",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "but no more",
},
},
{
name: "pattern with no owners with whitespace",
rule: "pattern ",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "",
},
},

// Error cases
{
name: "empty rule",
rule: "",
err: "unexpected end of rule",
},
{
name: "no owners",
rule: "pattern # but no more",
err: "unexpected end of rule",
},
{
name: "malformed patterns",
rule: "file.{txt @user",
Expand Down