codeowners

package module
v0.2.3-0...-4def1c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 9 Imported by: 0

README

Attribution, the original work is from
github.com/hairyhenderson/go-codeowners This fork allow to write files and to get non global owners

go-codeowners

A package that finds and parses CODEOWNERS files.

Features:

  • operates on local repos
  • doesn't require a cloned repo (i.e. doesn't need a .git directory to be present at the repo's root)
  • can be called from within a repo (doesn't have to be at the root)
  • will find CODEOWNERS files in all documented locations: the repo's root, docs/, and .github/ (or .gitlab/ for GitLab repos)

Usage

go get -u github.com/noandrea/go-codeowners

To find the owner of the README.md file:

import "github.com/noandrea/go-codeowners"

func main() {
	c, _ := NewCodeowners(cwd())
	owners := c.Owners("README.md")
	for i, o := range owners {
		fmt.Printf("Owner #%d is %s\n", i, o)
	}
}

To generate a new codeowners file :

import "github.com/noandrea/go-codeowners"

func main() {
	c, _ := EmptyCodeowners(cwd())

	err = c.AddPattern("*", []string{"@alice", "@bob"})
	CheckError(err) // this is just to shorten the example
	err = c.AddPattern("/src", []string{"@mark"})
	CheckError(err) // this is just to shorten the example
	// write the result to file
	err = c.ToFile("CODEOWNERS")
}

License

The MIT License

Copyright (c) 2018 Dave Henderson

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codeowner

type Codeowner struct {
	Pattern string

	Owners []string
	// contains filtered or unexported fields
}

Codeowner - owners for a given pattern

func NewCodeowner

func NewCodeowner(pattern string, owners []string) (Codeowner, error)

NewCodeowner -

func (Codeowner) IsGlobal

func (c Codeowner) IsGlobal() bool

IsGlobal - tell whenever the owner is global

func (Codeowner) String

func (c Codeowner) String() string

type Codeowners

type Codeowners struct {
	Patterns []Codeowner
	// contains filtered or unexported fields
}

Codeowners - patterns/owners mappings for the given repo

func EmptyCodeowners

func EmptyCodeowners() *Codeowners

EmptyCodeowners - create an empty codeowners file

func FromFile

func FromFile(path string) (*Codeowners, error)

FromFile creates a Codeowners from the path to a local file.

Example
c, _ := FromFile(cwd())
fmt.Println(c.Patterns[0])
Output:

*	@hairyhenderson

func FromReader

func FromReader(r io.Reader, repoRoot string) (*Codeowners, error)

FromReader creates a Codeowners from a given Reader instance and root path.

Example
reader := strings.NewReader(sample2)
c, _ := FromReader(reader, "")
fmt.Println(c.Patterns[0])
Output:

*	@hairyhenderson

func NewCodeowners deprecated

func NewCodeowners(path string) (*Codeowners, error)

Deprecated: Use FromFile(path) instead.

func (*Codeowners) AddPattern

func (c *Codeowners) AddPattern(pattern string, owners []string) (err error)

AddPattern - add a new pattern to the codeowners file

func (*Codeowners) LocalOwners

func (c *Codeowners) LocalOwners(path string) []string

LocalOwners - return the list of code owners for the given path excluding the global owners (within the repo root)

func (*Codeowners) Owners

func (c *Codeowners) Owners(path string) []string

Owners - return the list of code owners for the given path (within the repo root)

Example
c, _ := FromFile(cwd())
owners := c.Owners("README.md")
for i, o := range owners {
	fmt.Printf("Owner #%d is %s\n", i, o)
}
Output:

Owner #0 is @hairyhenderson

func (*Codeowners) ToFile

func (c *Codeowners) ToFile(path string) (err error)

ToFile - serialize the Codeowners to file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL