GithubHelp home page GithubHelp logo

github-codeowners's Introduction

github-codeowners

Maintainability Test Coverage CircleCI Known Vulnerabilities

A CLI tool for working with GitHub CODEOWNERS.

Things it does:

  • Calculate ownership stats
  • Find out who owns each and every file (ignoring files listed in .gitignore)
  • Find out who owns a single file
  • Find out who owns your staged files
  • Outputs in a bunch of script friendly handy formats for integrations (CSV and JSONL)
  • Validates that your CODEOWNERS file is valid

Installation

Install via npm globally then run

$ npm i -g github-codeowners
$ github-codeowners --help 
Usage: github-codeowners [options] [command]

Commands

Audit

Compares every file in your current (or specified) directory against your CODEOWNERS rules and outputs the result of who owns each file.

$ cd <your awesome project> 
$ github-codeowners audit
README.md
package.json
src/cli.ts      @jjmschofield
...

Ownership stats:

$ github-codeowners audit -s
--- Counts ---
Total: 24 files (1378 lines)
Loved: 10 files (494 lines)
Unloved: 14 files (884 lines)
--- Owners ---
@jjmschofield: 10 files (494 lines)

Only files in a specific directory:

$ github-codeowners audit -r src/
src/cli.ts      @jjmschofield
src/commands/audit.ts   @jjmschofield
...

Only unowned files:

$ github-codeowners audit -u
.github/CODEOWNERS
.gitignore

Output in JSONL:

$ github-codeowners audit -o jsonl
{"path":"src/commands/audit.ts","owners":["@jjmschofield"],"lines":48}
...

Output in CSV:

$ github-codeowners audit -o csv
src/commands/audit.ts,@jjmschofield

Full usage information:

$ github-codeowners audit --help
Usage: github-codeowners audit [options]

list the owners for all files

Options:
  -d, --dir <dirPath>          path to VCS directory (default: "<current working directory>")
  -c, --codeowners <filePath>  path to codeowners file (default: "<dir>/.github/CODEOWNERS")
  -o, --output <outputFormat>  how to output format eg: simple, jsonl, csv (default: "simple")
  -u, --unloved                unowned files only (default: false)
  -g, --only-git               consider only files tracked by git (default: false)
  -s, --stats                  output stats (default: true)
  -r, --root <rootPath>        the root path to filter files by (default: "")
  -h, --help                   output usage information

Who

Tells you who owns a given file or files:

$ cd <your awesome project> 
$ github-codeowners who <file> <file>
<file> @some/team
<file> @some/team

Full usage:

$ github-codeowners who --help                   
Usage: github-codeowners who [options] <file...>

lists owners of a specific file or files

Options:
  -d, --dir <dirPath>          path to VCS directory (default: "/Users/jjmschofield/projects/github/snyk/registry")
  -c, --codeowners <filePath>  path to codeowners file (default: "<dir>/.github/CODEOWNERS")
  -o, --output <outputFormat>  how to output format eg: simple, jsonl, csv (default: "simple")
  -h, --help                   output usage information

Git

Provides a list of files with their owners between commits (against the current version of CODEOWNERS).

Ownership of all files staged for commit:

$ cd <your awesome project>
$ github-codeowners git

Ownership of files existing at a specific commit:

$ github-codeowners git <commit sha>

Ownership of files changed between two commits:

$ github-codeowners git <commit sha> <commit sha>

Output stats:

$ github-codeowners git -s

Full usage:

$ github-codeowners git --help                                                                                       
Usage: github-codeowners git [options] [shaA] [shaB]

lists owners of files changed between commits, a commit against head or staged against head.

Options:
  -d, --dir <dirPath>          path to VCS directory (default: "/Users/jjmschofield/projects/github/snyk/registry")
  -c, --codeowners <filePath>  path to codeowners file (default: "<dir>/.github/CODEOWNERS")
  -o, --output <outputFormat>  how to output format eg: simple, jsonl, csv (default: "simple")
  -s, --stats                  output stats, note line counts are not available for this command (default: false)
  -h, --help                   output usage information

Validate

Validates your CODEOWNERS file to find common mistakes, will throw on errors (such as malformed owners).

$ cd <your awesome project> 
$ github-codeowners validate
Found duplicate rules [ 'some/duplicate/rule @octocat' ]
Found rules which did not match any files [ 'some/non-existent/path @octocat' ]
...

Full usage information:

$ github-codeowners validate --help
Usage: github-codeowners validate [options]

Validates a CODOWNER file and files in dir

Options:
  -d, --dir <dirPath>          path to VCS directory (default: "<current working directory>")
  -c, --codeowners <filePath>  path to codeowners file (default: "<dir>/.github/CODEOWNERS")
  -r, --root <rootPath>        the root path to filter files by (default: "")
  -h, --help                   output usage information

Output Formats

Check github-codeowners <command> --help for support for a given command, however generally the following outputs are supported:

  • simple - tab delimited - terminal friendly output
  • jsonl - line separated json - useful for streaming data to another command
  • csv - csv delimited fields - useful to import into a spreadsheet tool of your choice

Limits and Things to Improve

  • It requires node
  • It is not optimized
  • The output interface might change
  • Command syntax might change

Shout outs

Inspired by codeowners but implemented in Typescript with extra bells and whistles.

github-codeowners's People

Contributors

fauxfaux avatar jjmschofield avatar snyk-bot avatar t0rat0ra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

github-codeowners's Issues

git: option to exclude deleted files

I'm using this as a CI step to require explicit owners for a monolith owned by multiple teams. Currently the CI step will fail if someone deletes a file that didn't have an explicit owner. This is obviously not desirable.

It would be nice to have an option like:

github-codeowners git --exclude-deleted-files develop HEAD

This way the CI can enforce that all new or modified files have owners.

library interface

Offer up a dependable interface so that other apps / scripts can consume the core functionality

files with multiple dots in the file name not matched

I'm told I have a validation error:

$ github-codeowners validate
Found rules which did not match any files [
  '/test.en.yml @org/team',
]

But in reality the file exists:

$ ls test.en.yml
test.en.yml

Had a browse around the codebase and didn't spot the bug, sorry.

Bug: Currently owners are augmented

If multiple lines define ownership for a file then the owners are augmented.

The expected result would be that the later definitions overwrites earlier ones as stated here: codeowners-syntax

This is especially wrong for * ownerships.

Error when using -s and -g flags and a submodule is present

Steps to reproduce

  • In a git repo, add a submodule
  • run github-codeowners audit -s -g
  • See the error message:
failed to read lines from file github-codeowners [Error: EISDIR: illegal operation on a directory, read]

Output from terminal

image

Expected behavior

Ownership stats should be shown as expected without any error.
Submodules should be excluded, or included via a new parameter.

feat: audit unloved should support explicitly unowned

Hey @jjmschofield, thanks much for this awesome library! ๐Ÿ’ฏ Are you currently accepting contributions? And if so, would you possibly have time to review, merge, and publish? If not, I understand - I'm happy to fork.

I'd love for github-codeowners audit --unloved to support the use case of explicitly marking some files as unowned. My use case is basically to enforce 100% CODEOWNERS coverage by ensuring this command has no output, but then allowing some known exceptions to be explicitly listed in the CODEOWNERS file.

GitHub appears to support this: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners#:~:text=owners%20are%20left%20empty

# In this example, @octocat owns any file in the /apps
# directory in the root of your repository except for the /apps/github
# subdirectory, as its owners are left empty.
/apps/ @octocat
/apps/github

Historically I'd been using @ghost entries, but GitHub's recent feature to present a UI for validation of files shows errors because that is not a real team.

Validate: Can't use unix pipes or return code

$ github-codeowners validate
Found rules which did not match any files [
  '/some_file.yml @team'
]
$ echo $?
0

I would have expected this to return an error.

Further I tried to work around this by using wc -l but no dice:

$ github-codeowners validate | wc -l
Found rules which did not match any files [
  '/some_file.yml @team'
]
       0

Incorrect handling of wildcard and subdirectories

It is possible to "fool" this library into thinking a file is covered by codeowners, whilst GitHub will not apply code ownership.

Steps to reproduce

  • Create a file in some nested subdirectory, e.g. /foo/bar/baz.txt
  • Add a rule involving a wildcard to CODEOWNERS e.g. /foo/* or /foo/b*
  • Run github-codeowners audit

Expected

  • The file is considered unloved

Actual

  • The file is considered loved

Notes

I've tested this scenario by raising a PR (in a private repo), and GitHub did not assign any code owners.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.