codeowner_parser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e3d800c48813130b197b5d40a14826d2c71db2bd094a993476a4377013ecdf0
4
+ data.tar.gz: 51800ac4ca1a4c7af5f24b340bcc5aaddbe1b2bdd2c79dba4e105cc87623f49c
5
+ SHA512:
6
+ metadata.gz: 820cbc3bd94c9e9fd4eb7310783ce55849cf77b667751b877c07eb7f316bb0953d84c05d57ef47c7f49d71abd3c97380ef1cee4c4806183ab32d627e050738b4
7
+ data.tar.gz: 4449255307def71780dbb9382af0df992d6e5c5e0d2d2ebb25f2214f1c850df7346a0e9a0c6509b4559cac8cf75ed99bb0c9e5e220c5c7f7c4e3c95fbe7b96b7
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /spec/.examples.txt
10
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,30 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-performance
4
+
5
+ # Line length is more trouble than it's worth, IMO. Be reasonable.
6
+ Layout/LineLength:
7
+ Enabled: false
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ # Specs are generally long.
12
+ - 'spec/**/*'
13
+
14
+ RSpec/NestedGroups:
15
+ Enabled: false
16
+
17
+ Style/TrailingCommaInHashLiteral:
18
+ EnforcedStyleForMultiline: consistent_comma
19
+
20
+ # Enable newly released cops:
21
+ Lint/RaiseException:
22
+ Enabled: true
23
+ Lint/StructNewOverride:
24
+ Enabled: true
25
+ Style/HashEachMethods:
26
+ Enabled: true
27
+ Style/HashTransformKeys:
28
+ Enabled: true
29
+ Style/HashTransformValues:
30
+ Enabled: true
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4
4
+ - 2.5
5
+ - 2.6
6
+ - 2.7
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in codeowner_parser.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+
10
+ gem 'rspec', '~> 3.9'
11
+ gem 'rubocop', '~> 0.81.0'
12
+ gem 'rubocop-performance', '1.5', require: false
13
+ gem 'rubocop-rspec', '~> 1.38', require: false
14
+
15
+ gem 'byebug'
16
+ gem 'pry-byebug'
@@ -0,0 +1,7 @@
1
+ Copyright 2020 William Johnston
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # CodeownerParser
2
+
3
+ The goal of this gem is primarily to parse a CODEOWNER file and return the team associated with a file given the file path.
4
+
5
+ An ancillary goal is to allow you to route errors to a different team by file.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'codeowner_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codeowner_parser
22
+
23
+ ## Usage
24
+
25
+ See [GitHub's documentation](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#example-of-a-codeowners-file) for examples of codeowner files.
26
+
27
+ ```ruby
28
+ require 'codeowner_parser'
29
+
30
+ str = <<~CODEOWNERS
31
+ * @global-owner1 @global-owner2
32
+ *.js @js-owner
33
+ *.go docs@example.com
34
+ /build/logs/ @doctocat
35
+ docs/* docs@example.com
36
+ apps/ @octocat
37
+ /docs/ @doctocat
38
+ CODEOWNERS
39
+
40
+ parser = CodeownerParser.parse(str)
41
+ parser.owner('/build/docs/blah.json')
42
+ # => docs@example.com
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/will-in-wi/codeowner_parser.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rubocop/rake_task'
6
+ desc 'Run Rubocop'
7
+ RuboCop::RakeTask.new(:rubocop)
8
+
9
+ require 'rspec/core/rake_task'
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task default: %i[rubocop spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'codeowner_parser'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/codeowner_parser/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'codeowner_parser'
7
+ spec.version = CodeownerParser::VERSION
8
+ spec.authors = ['William Johnston']
9
+ spec.email = ['william@johnstonhaus.us']
10
+
11
+ spec.summary = 'Parse a CODEOWNER file and return the team associated with a file given the file path'
12
+ # spec.description = 'Parse a CODEOWNER file and return the team associated with a file given the file path'
13
+ spec.homepage = 'https://github.com/will-in-wi/codeowner_parser'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+ spec.metadata['changelog_uri'] = 'https://github.com/will-in-wi/codeowner_parser/blob/master/CHANGELOG.md'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'codeowner_parser/version'
4
+ require 'codeowner_parser/file'
5
+
6
+ # A parser for CODEOWNER files.
7
+ module CodeownerParser
8
+ class Error < StandardError; end
9
+
10
+ def self.parse(file)
11
+ CodeownerParser::File.new(file)
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'codeowner_parser/rule'
4
+
5
+ module CodeownerParser
6
+ # Parses an entire codeowners file.
7
+ class File
8
+ def initialize(file_string)
9
+ @file_string = file_string
10
+
11
+ raise ArgumentError, 'CODEOWNER file cannot be nil' if @file_string.nil?
12
+
13
+ # Precompute for repeated access. Reverse because of reverse precedence.
14
+ @rules = rules.reverse
15
+ end
16
+
17
+ def owner(path)
18
+ applicative_rule = @rules.find { |rule| rule.applies?(path) }
19
+ return [] if applicative_rule.nil?
20
+
21
+ applicative_rule.owner
22
+ end
23
+
24
+ private
25
+
26
+ def rules
27
+ @file_string.split("\n").inject([]) do |memo, line|
28
+ # Delete everything after a comment.
29
+ line, = line.split('#')
30
+ line = (line || '').strip
31
+ next memo if line == ''
32
+
33
+ memo << Rule.new(line)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeownerParser
4
+ # Handles an individual rule and determines applicability.
5
+ class Rule
6
+ attr_reader :owner
7
+
8
+ def initialize(rule_string)
9
+ @rule_path, *@owner = rule_string.split(/\s+/)
10
+ # Precompute this so that we aren't doing so repeatedly for additional checks.
11
+ @rule_regex = path_regex
12
+ end
13
+
14
+ def applies?(path)
15
+ @rule_regex =~ path
16
+ end
17
+
18
+ private
19
+
20
+ def path_regex
21
+ literals = @rule_path.split('*', -1)
22
+ regex = ''
23
+ # If path started with a slash, this is rooted.
24
+ regex += '\A' if literals.first.start_with?('/')
25
+ # Join with a wildcard for anything other than a slash.
26
+ regex += literals.map { |literal| Regexp.escape(literal) }.join('[^\/]+')
27
+ # If path did not end with a slash, do not search into subdirectories.
28
+ regex += '\Z' unless literals.last.end_with?('/')
29
+
30
+ Regexp.compile(regex)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeownerParser
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeowner_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - William Johnston
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - william@johnstonhaus.us
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - ".travis.yml"
24
+ - CHANGELOG.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - codeowner_parser.gemspec
32
+ - lib/codeowner_parser.rb
33
+ - lib/codeowner_parser/file.rb
34
+ - lib/codeowner_parser/rule.rb
35
+ - lib/codeowner_parser/version.rb
36
+ homepage: https://github.com/will-in-wi/codeowner_parser
37
+ licenses: []
38
+ metadata:
39
+ homepage_uri: https://github.com/will-in-wi/codeowner_parser
40
+ source_code_uri: https://github.com/will-in-wi/codeowner_parser
41
+ changelog_uri: https://github.com/will-in-wi/codeowner_parser/blob/master/CHANGELOG.md
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.3.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Parse a CODEOWNER file and return the team associated with a file given the
61
+ file path
62
+ test_files: []