timoschinkel / codeowners
A parser and matcher for the codeowners file as used by Github, Gitlab and Bitbucket.
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^4.0
- vimeo/psalm: ^6.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Code Owners allows you to parse code owners files and apply the outcome to all kinds of different result sets ranging from code coverage to static analysis results.
Code Owners files are supported by Github, Gitlab and Bitbucket.
Installation
Use Composer for installation:
composer require timoschinkel/codeowners
Usage
Parse CODEOWNERS file:
<?php use CodeOwners\Parser; use CodeOwners\PatternMatcher; try { $patterns = (new GitHubParser())->parseFile($filename); $pattern = (new PatternMatcher(...$patterns))->match($filename); } catch (\CodeOwners\Exception\UnableToParseException $exception) { // unable to read or parse file } catch (\CodeOwners\Exception\NoMatchFoundException $exception) { // no match found }
Alternatively, parsing a string directly is also supported:
<?php use CodeOwners\Parser; use CodeOwners\PatternMatcher; try { $patterns = (new GitLabParser())->parseString($contents); $pattern = (new PatternMatcher(...$patterns))->match($filename); } catch (\CodeOwners\Exception\UnableToParseException $exception) { // unable to read or parse file } catch (\CodeOwners\Exception\NoMatchFoundException $exception) { // no match found }
Parsers
GitHub, GitLab and BitBucket all have their own features when it comes to code owner files. That's why there are currently two parsers available:
GitHubParser; this parser does strict parsing of owners, and allows entries without owners. This will result in aPatternwith an empty owners array.GitLabParser; this parser does strict parsing of owners, and has support for sections.
NB. I hope to add a parser for BitBucket in the future.
There's also a "plain" Parser. This parser does not support any of the specifics and is deliberately unchanged - since the introduction of the vendor specific parsers - to guarantee backwards compatibility. This parser might be deprecated, and thus removed, in the future. The recommendation is to use one of the vendor specific parsers.