codeowners-checker 1.0.5 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fea710f9cf51fa064b3ac9fdd73290673959ebb8f1d3420fc10db85f38d427be
4
- data.tar.gz: 81277d6cfdcb325372374a3ef09ef7d91526672b78c25f5b5e42bb756a150ab0
3
+ metadata.gz: 6d9d7332bd9c20b04f99d88d0d91ba7430e64ea45af0720f3b96b66aab535bd6
4
+ data.tar.gz: 52a7866ebda6380f924c18f80ab689e6cd5416f5ea0da8e96b6ea09e1645eca3
5
5
  SHA512:
6
- metadata.gz: 9170ec3459dcaeabb7cdd6abdde1faab7739db931f5bdb01e9a202cd6b2b794a1561a033272f6fd3c41798f8dc662ceb939e2ea17a121aca26ec8318639db875
7
- data.tar.gz: 481e5e568f458f408e2d260538dfaba02da323f2267cca7969b662ece7ec61b163f7419f04221cac7a784c26aae59bbca48e677d394e5f66c37e218dd5f509bf
6
+ metadata.gz: 89a86ecabad2150b1f1f17f202ef70226d071754bd689d349ef412c23d1423446600648f79d7ec1f8508b1a432b154a156d9e9261008c93168de6633f79109f0
7
+ data.tar.gz: 554ee80bdb5cad8eada8b850dcca17c147511bbbddd114bc03a2ee3d79f28a5e68fd9543a599887e606bf050e519038f9c69a0c5f27eb7e97b10c542ddc0ce49
data/README.md CHANGED
@@ -33,7 +33,11 @@ Example OWNERS:
33
33
  GitHub credentials are taken from the following environment variables. You might want to put them into your .bashrc or equivalent:
34
34
 
35
35
  $ export GITHUB_TOKEN='your GitHub PAT' # your personal access token from GitHub
36
- $ export GITHUB_ORGANIZATION='company' # name of your GitHub organization
36
+
37
+ The Github organization used to retrieve the groups/teams is automatically set according to the git remote URL. You can check and change this value using:
38
+
39
+ $ codeowners-checker config list
40
+ $ codeowners-checker config organization <organization>
37
41
 
38
42
  You can generate your PAT in [Settings -> Developer settings -> Personal access tokens on GitHub](https://github.com/settings/tokens) and `read:org` scope is **required**.
39
43
 
@@ -187,7 +191,7 @@ Or you can filter any owner as a parameter:
187
191
 
188
192
  ## Development
189
193
 
190
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
194
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
191
195
 
192
196
  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).
193
197
 
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'fuzzy_match', '~> 2.1'
24
24
  spec.add_dependency 'git', '~> 1.5'
25
25
  spec.add_dependency 'json', '~> 2.1'
26
+ spec.add_dependency 'pathspec', '~> 0.2.0'
26
27
  spec.add_dependency 'rest-client', '~> 2.1'
27
28
  spec.add_dependency 'thor', '~> 0.20.3'
28
29
  spec.add_development_dependency 'bundler', '~> 1.16'
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'line'
4
4
  require_relative '../owner'
5
+ require 'pathspec'
5
6
 
6
7
  module Codeowners
7
8
  class Checker
@@ -10,7 +11,7 @@ module Codeowners
10
11
  # Parse the line into pattern, owners and whitespaces.
11
12
  class Pattern < Line
12
13
  attr_accessor :owners, :whitespace
13
- attr_reader :pattern
14
+ attr_reader :pattern, :spec
14
15
 
15
16
  def self.match?(line)
16
17
  _pattern, *owners = line.split(/\s+/)
@@ -35,24 +36,18 @@ module Codeowners
35
36
  def parse(line)
36
37
  @pattern, *@owners = line.split(/\s+/)
37
38
  @whitespace = line.split('@').first.count(' ') - 1
39
+ @spec = parse_spec(@pattern)
38
40
  end
39
41
 
40
42
  def match_file?(file)
41
- File.fnmatch(pattern.gsub(%r{^/}, ''), file, fn_options)
42
- end
43
-
44
- def fn_options
45
- if pattern.include?('**')
46
- File::FNM_DOTMATCH
47
- else
48
- File::FNM_DOTMATCH | File::FNM_PATHNAME
49
- end
43
+ spec.match file
50
44
  end
51
45
 
52
46
  def pattern=(new_pattern)
53
47
  @whitespace += @pattern.size - new_pattern.size
54
48
  @whitespace = 1 if @whitespace < 1
55
49
 
50
+ @spec = parse_spec(new_pattern)
56
51
  @pattern = new_pattern
57
52
  end
58
53
 
@@ -68,6 +63,10 @@ module Codeowners
68
63
  def to_s
69
64
  to_file(preserve_whitespaces: false)
70
65
  end
66
+
67
+ def parse_spec(pattern)
68
+ PathSpec.from_lines(pattern)
69
+ end
71
70
  end
72
71
  end
73
72
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'file_as_array'
4
+ require_relative '../config'
4
5
 
5
6
  module Codeowners
6
7
  class Checker
@@ -9,10 +10,17 @@ module Codeowners
9
10
  attr_accessor :validate_owners, :filename
10
11
  attr_writer :owners
11
12
 
12
- def initialize(repo)
13
+ def initialize(repo, _config = nil)
13
14
  @validate_owners = true
14
15
  # doing gsub here ensures the files are always in the same directory
15
16
  @filename = CodeOwners.filename(repo).gsub('CODEOWNERS', 'OWNERS')
17
+ @config ||= Codeowners::Config.new
18
+ end
19
+
20
+ def self.persist!(repo, owners, config = nil)
21
+ owner_list = new(repo, config)
22
+ owner_list.owners = owners
23
+ owner_list.persist!
16
24
  end
17
25
 
18
26
  def persist!
@@ -30,7 +38,7 @@ module Codeowners
30
38
 
31
39
  @owners ||=
32
40
  if github_credentials_exist?
33
- Codeowners::GithubFetcher.get_owners(ENV['GITHUB_ORGANIZATION'], ENV['GITHUB_TOKEN'])
41
+ Codeowners::GithubFetcher.get_owners(@config.default_organization, ENV['GITHUB_TOKEN'])
34
42
  else
35
43
  FileAsArray.new(@filename).content
36
44
  end
@@ -38,8 +46,8 @@ module Codeowners
38
46
 
39
47
  def github_credentials_exist?
40
48
  token = ENV['GITHUB_TOKEN']
41
- organization = ENV['GITHUB_ORGANIZATION']
42
- token && organization
49
+ organization = @config.default_organization
50
+ token && !organization.empty?
43
51
  end
44
52
 
45
53
  def invalid_owners(codeowners)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Codeowners
4
4
  class Checker
5
- VERSION = '1.0.5'
5
+ VERSION = '1.1.1'
6
6
  end
7
7
  end
@@ -3,6 +3,7 @@
3
3
  require 'thor'
4
4
 
5
5
  require_relative '../config'
6
+ require_relative 'warner'
6
7
 
7
8
  module Codeowners
8
9
  module Cli
@@ -13,6 +14,7 @@ module Codeowners
13
14
  def initialize(args = [], options = {}, config = {})
14
15
  super
15
16
  @config ||= config[:config] || default_config
17
+ Warner.check_warnings
16
18
  end
17
19
 
18
20
  private
@@ -8,10 +8,10 @@ module Codeowners
8
8
  class Config < Base
9
9
  default_task :list
10
10
 
11
- desc 'list', 'List the default owner configured in the config file'
11
+ desc 'list', 'List the default values configured in the config file'
12
12
  def list
13
13
  puts(config.to_h.map { |name, value| "#{name}: #{value.inspect}" })
14
- help_stderr if config.default_owner.empty?
14
+ help_stderr if config.default_owner.empty? || config.default_organization.empty?
15
15
  end
16
16
 
17
17
  desc 'owner <name>', 'Configure a default owner name'
@@ -19,6 +19,12 @@ module Codeowners
19
19
  config.default_owner = name
20
20
  puts "Default owner configured to #{name}"
21
21
  end
22
+
23
+ desc 'organization <name>', 'Configure a default organization name'
24
+ def organization(name)
25
+ config.default_organization = name
26
+ puts "Default organization configured to #{name}"
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -8,22 +8,24 @@ module Codeowners
8
8
  class OwnersListHandler < Base
9
9
  default_task :fetch
10
10
 
11
+ FETCH_OWNER_MESSAGE = 'Fetching owners list from GitHub ...'
12
+ ASK_GITHUB_ORGANIZATION = 'GitHub organization (e.g. github): '
13
+ ASK_GITHUB_TOKEN = 'Enter GitHub token: '
14
+
11
15
  desc 'fetch [REPO]', 'Fetches .github/OWNERS based on github organization'
12
16
  def fetch(repo = '.')
13
17
  @repo = repo
14
18
  owners = owners_from_github
15
- owners_list = Checker::OwnersList.new(repo)
16
- owners_list.owners = owners
17
- owners_list.persist!
19
+ Checker::OwnersList.persist!(repo, owners)
18
20
  end
19
21
 
20
22
  no_commands do
21
23
  def owners_from_github
22
- organization = ENV['GITHUB_ORGANIZATION']
23
- organization ||= ask('GitHub organization (e.g. github): ')
24
+ organization = config.default_organization
25
+ organization = ask(ASK_GITHUB_ORGANIZATION) if organization.empty?
24
26
  token = ENV['GITHUB_TOKEN']
25
- token ||= ask('Enter GitHub token: ', echo: false)
26
- puts 'Fetching owners list from GitHub ...'
27
+ token ||= ask(ASK_GITHUB_TOKEN, echo: false)
28
+ puts FETCH_OWNER_MESSAGE
27
29
  Codeowners::GithubFetcher.get_owners(organization, token)
28
30
  end
29
31
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codeowners
4
+ module Cli
5
+ # Checks and prints deprecation warnings.
6
+ module Warner
7
+ class << self
8
+ def check_warnings
9
+ check_github_env
10
+ end
11
+
12
+ def warn(msg)
13
+ puts "[WARNING] #{msg}"
14
+ end
15
+
16
+ protected
17
+
18
+ def check_github_env
19
+ return if ENV['GITHUB_ORGANIZATION'].nil? || ENV['GITHUB_ORGANIZATION'].empty?
20
+
21
+ warn 'Usage of GITHUB_ORGANIZATION ENV variable has been deprecated.'\
22
+ 'Run `codeowners-checker config organization #{organization}` instead.'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -40,10 +40,34 @@ module Codeowners
40
40
  @git.config('user.owner', name)
41
41
  end
42
42
 
43
+ def default_organization
44
+ config_org = @git.config('user.organization')
45
+ return config_org.strip unless config_org.nil? || config_org.strip.empty?
46
+
47
+ parse_organization_from_origin || ''
48
+ end
49
+
50
+ def default_organization=(name)
51
+ @git.config('user.organization', name)
52
+ end
53
+
43
54
  def to_h
44
55
  {
45
- default_owner: default_owner
56
+ default_owner: default_owner,
57
+ default_organization: default_organization
46
58
  }
47
59
  end
60
+
61
+ protected
62
+
63
+ def parse_organization_from_origin
64
+ origin_url = @git.config('remote.origin.url')
65
+ return if origin_url.nil? || origin_url.strip.empty?
66
+
67
+ org_regexp = origin_url.match(%r{^https?://.+?/(?<org>.+?)/|:(?<org>.+?)/})
68
+ return if org_regexp.nil? || org_regexp[:org].strip.empty?
69
+
70
+ org_regexp[:org].strip
71
+ end
48
72
  end
49
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeowners-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jônatas Davi Paganini
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-12-23 00:00:00.000000000 Z
13
+ date: 2020-02-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: fuzzy_match
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
56
  version: '2.1'
57
+ - !ruby/object:Gem::Dependency
58
+ name: pathspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 0.2.0
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 0.2.0
57
71
  - !ruby/object:Gem::Dependency
58
72
  name: rest-client
59
73
  requirement: !ruby/object:Gem::Requirement
@@ -247,6 +261,7 @@ files:
247
261
  - lib/codeowners/cli/main.rb
248
262
  - lib/codeowners/cli/owners_list_handler.rb
249
263
  - lib/codeowners/cli/suggest_file_from_pattern.rb
264
+ - lib/codeowners/cli/warner.rb
250
265
  - lib/codeowners/cli/wizards.rb
251
266
  - lib/codeowners/cli/wizards/new_file_wizard.rb
252
267
  - lib/codeowners/cli/wizards/new_owner_wizard.rb