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

Implement basic section support #26

Closed
wants to merge 9 commits into from
Closed
8 changes: 8 additions & 0 deletions codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ type Rule struct {
pattern pattern
}

type Section struct {
Name string
Owners []Owner
Comment string
}

// RawPattern returns the rule's gitignore-style path pattern.
func (r Rule) RawPattern() string {
return r.pattern.pattern
Expand All @@ -139,6 +145,8 @@ const (
TeamOwner string = "team"
// UsernameOwner is the owner type for GitHub usernames.
UsernameOwner string = "username"
// GroupOwner is the owner type for Gitlab groups.
GroupOwner string = "group"
)

// Owner represents an owner found in a rule.
Expand Down
90 changes: 89 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package codeowners_test
import (
"bytes"
"fmt"
"regexp"

"github.com/hmarr/codeowners"
)

func Example() {
f := bytes.NewBufferString("src/**/*.c @acme/c-developers")
f := bytes.NewBufferString(`src/**/*.c @acme/c-developers
# The following line should be ignored; it contains only spaces and tabs` +
" \t\nsrc/**/*.go @acme/go-developers")
ruleset, err := codeowners.ParseFile(f)
if err != nil {
panic(err)
Expand All @@ -19,9 +22,13 @@ func Example() {

match, err = ruleset.Match("src/foo.rs")
fmt.Println(match)

match, err = ruleset.Match("src/go/bar/bar.go")
fmt.Println(match.Owners)
// Output:
// [@acme/c-developers]
// <nil>
// [@acme/go-developers]
}

func ExampleParseFile() {
Expand All @@ -41,6 +48,43 @@ func ExampleParseFile() {
// Go code
}

func ExampleParseFile_customOwnerMatchers() {
validUsernames := []string{"the-a-team", "the-b-team"}
usernameRegexp := regexp.MustCompile(`\A@([a-zA-Z0-9\-]+)\z`)

f := bytes.NewBufferString("src/**/*.go @the-a-team # Go code")
ownerMatchers := []codeowners.OwnerMatcher{
codeowners.OwnerMatchFunc(codeowners.MatchEmailOwner),
codeowners.OwnerMatchFunc(func(s string) (codeowners.Owner, error) {
// Custom owner matcher that only matches valid usernames
match := usernameRegexp.FindStringSubmatch(s)
if match == nil {
return codeowners.Owner{}, codeowners.ErrNoMatch
}

for _, t := range validUsernames {
if t == match[1] {
return codeowners.Owner{Value: match[1], Type: codeowners.TeamOwner}, nil
}
}
return codeowners.Owner{}, codeowners.ErrNoMatch
}),
}
ruleset, err := codeowners.ParseFile(f, codeowners.WithOwnerMatchers(ownerMatchers))
if err != nil {
panic(err)
}
fmt.Println(len(ruleset))
fmt.Println(ruleset[0].RawPattern())
fmt.Println(ruleset[0].Owners[0].String())
fmt.Println(ruleset[0].Comment)
// Output:
// 1
// src/**/*.go
// @the-a-team
// Go code
}

func ExampleRuleset_Match() {
f := bytes.NewBufferString("src/**/*.go @acme/go-developers # Go code")
ruleset, _ := codeowners.ParseFile(f)
Expand All @@ -62,3 +106,47 @@ func ExampleRuleset_Match() {
// src/foo/bar.go true
// src/foo.rs false
}

func ExampleRuleset_Match_section() {
f := bytes.NewBufferString(`[SECTION] @the-a-team
src
src-b @user-b
`)
ruleset, _ := codeowners.ParseFile(f)
match, _ := ruleset.Match("src")
fmt.Println("src", match != nil)
fmt.Println(ruleset[0].Owners[0].String())
match, _ = ruleset.Match("src-b")
fmt.Println("src-b", match != nil)
fmt.Println(ruleset[1].Owners[0].String())
// Output:
// src true
// @the-a-team
// src-b true
// @user-b
}

func ExampleRuleset_Match_section_groups() {
f := bytes.NewBufferString(`[SECTION] @the/a/group
src
src-b @user-b
src-c @the/c/group
`)
ruleset, _ := codeowners.ParseFile(f)
match, _ := ruleset.Match("src")
fmt.Println("src", match != nil)
fmt.Println(ruleset[0].Owners[0].String())
match, _ = ruleset.Match("src-b")
fmt.Println("src-b", match != nil)
fmt.Println(ruleset[1].Owners[0].String())
match, _ = ruleset.Match("src-c")
fmt.Println("src-c", match != nil)
fmt.Println(ruleset[2].Owners[0].String())
// Output:
// src true
// @the/a/group
// src-b true
// @user-b
// src-c true
// @the/c/group
}
Loading