nya_pr 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 733e769aa9751489e6a8e219de2804dc33d40a1cc7aef589bffd002c83e47f19
4
- data.tar.gz: e9327003b3b327834904206c9b9fd402ef09ee5143afccc846dacad5899ce4e8
3
+ metadata.gz: 90e3eebf9061c21b3c4579fbd29a454c4f8984c30dd39fbd1633bc263a717075
4
+ data.tar.gz: c19a7c957ecd8177c10075455699e41812dd1e5c43d07965d1af642fac84b342
5
5
  SHA512:
6
- metadata.gz: 9f27db9b27067f525f3b50b87c61f849c0cffbb71d2c7f2622dd2aba9bd56d1741559766b8eb3261a97b7d4f6192df082970daf6b0fe3f5103d91fd9b74b5e07
7
- data.tar.gz: 7dc96cd1812762b1b437955536eb1725de85888d2a5a28bfe461cd262afa597237a540e339d9d93a2c5cce7e8495e10ab939cd72d924977c41258861cf38f2d4
6
+ metadata.gz: a0734d5b31fd4e72bf9a8058a90e3215edb6449e45f222d5083b09066b836a2fe9b2a77e100a5ecc0f80f3557c480044b0f0aa0ca28e2a91ba27e547faee8a06
7
+ data.tar.gz: b08e6e60dc6174f204cd018bc8d0b1fa16be47b27566b9889db7af5a484d8ff289e3fcbbfedba9a9a9c297f92f301e48ebde02d6671ab6058df2c0642ef9584c
@@ -11,14 +11,6 @@ module NyaPr
11
11
  'fatal' => Logger::FATAL
12
12
  }.freeze
13
13
 
14
- BOOLEAN_KEYS = %i[
15
- refresh
16
- skip_archived
17
- skip_drafts
18
- progress
19
- save_pull_requests
20
- ].freeze
21
-
22
14
  def self.build(options, file_options: {})
23
15
  new(options, file_options).build
24
16
  end
@@ -29,15 +21,19 @@ module NyaPr
29
21
  end
30
22
 
31
23
  def build
32
- validate_keys!
33
-
34
- Global.new(
35
- **normalize(
24
+ result = Schema.call(
25
+ normalize(
36
26
  Defaults::OPTIONS.
37
27
  merge(file_options).
38
28
  merge(options.compact)
39
29
  )
40
30
  )
31
+
32
+ raise_config_error!(result) unless result.success?
33
+
34
+ Global.new(
35
+ **result.to_h, log_level: LOG_LEVELS.fetch(result[:log_level])
36
+ )
41
37
  end
42
38
 
43
39
  private
@@ -50,20 +46,12 @@ module NyaPr
50
46
  owners: normalize_list(attributes[:owners]),
51
47
  exclude_repositories: normalize_repositories(
52
48
  attributes[:exclude_repositories]
53
- ),
54
- limit: normalize_positive_integer(attributes[:limit], :limit),
55
- workers: normalize_positive_integer(attributes[:workers], :workers),
56
- log_level: normalize_log_level(attributes[:log_level]),
57
- **normalize_booleans(attributes)
49
+ )
58
50
  )
59
51
  end
60
52
 
61
53
  def normalize_username(value)
62
- username = value.to_s.strip
63
-
64
- return username unless username.empty?
65
-
66
- raise NyaPr::Error, 'GitHub username is required'
54
+ value&.to_s&.strip
67
55
  end
68
56
 
69
57
  def normalize_list(values)
@@ -79,41 +67,16 @@ module NyaPr
79
67
  uniq
80
68
  end
81
69
 
82
- def normalize_booleans(attributes)
83
- BOOLEAN_KEYS.to_h do |key|
84
- [key, normalize_boolean(attributes[key], key)]
85
- end
86
- end
87
-
88
- def normalize_positive_integer(value, name)
89
- number = Integer(value)
90
-
91
- return number if number.positive?
92
-
93
- raise NyaPr::Error, "#{name} must be greater than 0"
94
- rescue ArgumentError, TypeError
95
- raise NyaPr::Error, "#{name} must be a positive integer"
96
- end
97
-
98
- def normalize_boolean(value, name)
99
- return value if [true, false].include?(value)
100
-
101
- raise NyaPr::Error, "#{name} must be true or false"
102
- end
103
-
104
- def normalize_log_level(value)
105
- LOG_LEVELS.fetch(value.to_s) do
106
- raise NyaPr::Error, "Invalid log level: #{value}"
107
- end
108
- end
109
-
110
- def validate_keys!
111
- unknown = file_options.keys - Global.members
112
-
113
- return if unknown.empty?
70
+ def raise_config_error!(result)
71
+ message = result.errors.to_h.
72
+ flat_map do |key, messages|
73
+ Array(messages).map do |error|
74
+ "#{key} #{error}"
75
+ end
76
+ end.
77
+ join(', ')
114
78
 
115
- raise NyaPr::Error,
116
- "Unknown config option(s): #{unknown.join(', ')}"
79
+ raise NyaPr::Error, message
117
80
  end
118
81
  end
119
82
  end
@@ -3,15 +3,12 @@
3
3
  module NyaPr
4
4
  module Config
5
5
  module Defaults
6
- LIMIT = 100
7
- WORKERS = 10
8
-
9
6
  OPTIONS = {
10
7
  owners: [],
11
8
  token: nil,
12
9
  repositories_csv: nil,
13
10
  pull_requests_dir: nil,
14
- limit: LIMIT,
11
+ limit: 100,
15
12
  refresh: false,
16
13
  skip_archived: false,
17
14
  skip_drafts: false,
@@ -19,7 +16,7 @@ module NyaPr
19
16
  save_pull_requests: true,
20
17
  progress: true,
21
18
  log_level: 'info',
22
- workers: WORKERS
19
+ workers: 30
23
20
  }.freeze
24
21
  end
25
22
  end
@@ -3,20 +3,7 @@
3
3
  module NyaPr
4
4
  module Config
5
5
  Global = Data.define(
6
- :username,
7
- :owners,
8
- :token,
9
- :repositories_csv,
10
- :pull_requests_dir,
11
- :limit,
12
- :refresh,
13
- :skip_archived,
14
- :skip_drafts,
15
- :exclude_repositories,
16
- :save_pull_requests,
17
- :progress,
18
- :log_level,
19
- :workers
6
+ *Schema.key_map.dump
20
7
  )
21
8
  end
22
9
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/schema'
4
+
5
+ module NyaPr
6
+ module Config
7
+ STRICT_BOOL = Dry::Types['strict.bool']
8
+
9
+ private_constant :STRICT_BOOL
10
+
11
+ Schema = Dry::Schema.Params do
12
+ config.validate_keys = true
13
+
14
+ required(:username).filled(:string)
15
+
16
+ required(:owners).array(:string)
17
+
18
+ required(:token).maybe(:string)
19
+ required(:repositories_csv).maybe(:string)
20
+ required(:pull_requests_dir).maybe(:string)
21
+
22
+ required(:limit).filled(
23
+ :integer,
24
+ gt?: 0
25
+ )
26
+
27
+ required(:workers).filled(
28
+ :integer,
29
+ gt?: 0
30
+ )
31
+
32
+ required(:refresh).value(STRICT_BOOL)
33
+ required(:skip_archived).value(STRICT_BOOL)
34
+ required(:skip_drafts).value(STRICT_BOOL)
35
+ required(:save_pull_requests).value(STRICT_BOOL)
36
+ required(:progress).value(STRICT_BOOL)
37
+
38
+ required(:exclude_repositories).array(:string)
39
+
40
+ required(:log_level).filled(
41
+ :string,
42
+ included_in?: %w[
43
+ debug
44
+ info
45
+ warn
46
+ error
47
+ fatal
48
+ ]
49
+ )
50
+ end
51
+ end
52
+ end
@@ -6,14 +6,16 @@ module NyaPr
6
6
  :limit,
7
7
  :skip_drafts,
8
8
  :progress_enabled,
9
- :pull_requests_dir
9
+ :pull_requests_dir,
10
+ :workers
10
11
  ) do
11
12
  def self.from_app(config)
12
13
  new(
13
14
  limit: config.limit,
14
15
  skip_drafts: config.skip_drafts,
15
16
  progress_enabled: config.progress,
16
- pull_requests_dir: config.pull_requests_dir
17
+ pull_requests_dir: config.pull_requests_dir,
18
+ workers: config.workers
17
19
  )
18
20
  end
19
21
  end
@@ -44,27 +44,56 @@ module NyaPr
44
44
  def collect_pull_requests(repositories, progress_bar)
45
45
  pull_requests = []
46
46
 
47
- repositories.each do |repository|
48
- break if pull_requests.size >= config.limit
47
+ repositories.each_slice(config.workers) do |repositories_batch|
48
+ remaining = config.limit - pull_requests.size
49
+ break unless remaining.positive?
49
50
 
50
- process_repository(
51
- repository,
52
- pull_requests,
51
+ results = process_batch(
52
+ repositories_batch,
53
+ remaining,
53
54
  progress_bar
54
55
  )
56
+
57
+ append_results(
58
+ pull_requests,
59
+ results
60
+ )
55
61
  end
56
62
 
57
63
  pull_requests
58
64
  end
59
65
 
60
- def process_repository(repository, pull_requests, progress_bar)
61
- return unless pull_requests_enabled?(repository)
66
+ def process_batch(repositories, limit, progress_bar)
67
+ threads = repositories.map do |repository|
68
+ Thread.new do
69
+ Thread.current.report_on_exception = false
70
+
71
+ process_repository(
72
+ repository,
73
+ limit,
74
+ progress_bar
75
+ )
76
+ end
77
+ end
62
78
 
63
- remaining = config.limit - pull_requests.size
79
+ threads.map(&:value)
80
+ end
64
81
 
65
- pull_requests.concat(
66
- pull_requests_for(repository, remaining)
67
- )
82
+ def append_results(pull_requests, results)
83
+ results.each do |repository_pull_requests|
84
+ remaining = config.limit - pull_requests.size
85
+ break unless remaining.positive?
86
+
87
+ pull_requests.concat(
88
+ repository_pull_requests.first(remaining)
89
+ )
90
+ end
91
+ end
92
+
93
+ def process_repository(repository, limit, progress_bar)
94
+ return [] unless pull_requests_enabled?(repository)
95
+
96
+ pull_requests_for(repository, limit)
68
97
  ensure
69
98
  progress_bar.advance
70
99
  end
@@ -49,7 +49,7 @@ module NyaPr
49
49
  store.write(repositories)
50
50
 
51
51
  NyaPr.logger.info(
52
- "Saved #{repositories.size} repositories to #{store.path}"
52
+ "Selected #{repositories.size} repositories for pull request scanning"
53
53
  )
54
54
 
55
55
  repositories
@@ -40,7 +40,7 @@ module NyaPr
40
40
 
41
41
  def log_start(repositories)
42
42
  NyaPr.logger.info(
43
- "Checking contributions in #{repositories.size} repositories " \
43
+ "Checking contributions in #{repositories.size} external repositories " \
44
44
  "using #{workers} workers"
45
45
  )
46
46
  end
@@ -75,7 +75,7 @@ module NyaPr
75
75
 
76
76
  def log_result(result)
77
77
  NyaPr.logger.info(
78
- "Finished contribution check: #{result.size} repositories matched"
78
+ "Finished contribution check: contributions found in #{result.size} repositories"
79
79
  )
80
80
  end
81
81
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NyaPr
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nya_pr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elijah S. Krukowski
@@ -51,6 +51,20 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.4'
54
+ - !ruby/object:Gem::Dependency
55
+ name: dry-schema
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.16'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.16'
54
68
  - !ruby/object:Gem::Dependency
55
69
  name: faraday
56
70
  requirement: !ruby/object:Gem::Requirement
@@ -126,6 +140,7 @@ files:
126
140
  - lib/nya_pr/config/defaults.rb
127
141
  - lib/nya_pr/config/global.rb
128
142
  - lib/nya_pr/config/loader.rb
143
+ - lib/nya_pr/config/schema.rb
129
144
  - lib/nya_pr/error.rb
130
145
  - lib/nya_pr/github/client.rb
131
146
  - lib/nya_pr/github/connection.rb