nya_pr 0.1.0 → 0.3.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.
@@ -3,31 +3,25 @@
3
3
  module NyaPr
4
4
  module PullRequest
5
5
  class Finder
6
- include NyaPr::Request
6
+ include NyaPr::Github::Request
7
7
 
8
- MAX_PULL_REQUESTS = 100
9
8
  MAX_PER_PAGE = 100
10
9
 
11
10
  attr_reader :client,
12
11
  :repositories,
13
- :limit,
14
- :progress_enabled
15
-
16
- def initialize(
17
- client,
18
- repositories,
19
- limit: MAX_PULL_REQUESTS,
20
- progress_enabled: true
21
- )
12
+ :config,
13
+ :filter
14
+
15
+ def initialize(client, repositories, config, filter: nil)
22
16
  @client = client
23
17
  @repositories = repositories
24
- @limit = limit
25
- @progress_enabled = progress_enabled
18
+ @config = config
19
+ @filter = filter || Filter.new(config)
26
20
  end
27
21
 
28
22
  def find
29
23
  NyaPr.logger.info(
30
- "Searching for up to #{limit} open pull requests " \
24
+ "Searching for up to #{config.limit} open pull requests " \
31
25
  "in #{repositories.size} repositories"
32
26
  )
33
27
 
@@ -49,55 +43,84 @@ module NyaPr
49
43
  pull_requests = []
50
44
 
51
45
  repositories.each do |repository|
52
- break if pull_requests.size >= limit
53
-
54
- begin
55
- next unless repository.fetch('has_pull_requests', true)
46
+ break if pull_requests.size >= config.limit
56
47
 
57
- remaining = limit - pull_requests.size
58
-
59
- pull_requests.concat(
60
- pull_requests_for(repository, remaining)
61
- )
62
- ensure
63
- progress_bar.advance
64
- end
48
+ process_repository(
49
+ repository,
50
+ pull_requests,
51
+ progress_bar
52
+ )
65
53
  end
66
54
 
67
55
  pull_requests
68
56
  end
69
57
 
58
+ def process_repository(repository, pull_requests, progress_bar)
59
+ return unless pull_requests_enabled?(repository)
60
+
61
+ remaining = config.limit - pull_requests.size
62
+
63
+ pull_requests.concat(
64
+ pull_requests_for(repository, remaining)
65
+ )
66
+ ensure
67
+ progress_bar.advance
68
+ end
69
+
70
+ def pull_requests_enabled?(repository)
71
+ repository.fetch('has_pull_requests', true)
72
+ end
73
+
70
74
  def pull_requests_for(repository, remaining)
71
75
  NyaPr.logger.debug(
72
76
  "Checking pull requests in #{repository.fetch('full_name')}"
73
77
  )
74
78
 
75
79
  pull_requests = []
80
+ per_page = [remaining, MAX_PER_PAGE].min
81
+
82
+ each_batch(repository, per_page) do |batch|
83
+ append_matches(
84
+ pull_requests,
85
+ repository,
86
+ batch,
87
+ remaining
88
+ )
89
+
90
+ break if pull_requests.size >= remaining
91
+ end
92
+
93
+ pull_requests
94
+ end
95
+
96
+ def each_batch(repository, per_page)
76
97
  page = 1
77
98
 
78
99
  loop do
79
- per_page = page_size(remaining, pull_requests.size)
80
100
  batch = get_batch(repository, per_page, page)
81
101
 
82
102
  break if batch.empty?
83
103
 
84
- pull_requests.concat(
85
- batch.map do |pull_request|
86
- normalize(repository, pull_request)
87
- end
88
- )
104
+ yield batch
89
105
 
90
106
  break if batch.size < per_page
91
- break if pull_requests.size >= remaining
92
107
 
93
108
  page += 1
94
109
  end
110
+ end
95
111
 
96
- pull_requests
112
+ def append_matches(pull_requests, repository, batch, limit)
113
+ needed = limit - pull_requests.size
114
+
115
+ pull_requests.concat(
116
+ filter_batch(repository, batch).first(needed)
117
+ )
97
118
  end
98
119
 
99
- def page_size(remaining, collected)
100
- [remaining - collected, MAX_PER_PAGE].min
120
+ def filter_batch(repository, batch)
121
+ batch.
122
+ map { |pull_request| normalize(repository, pull_request) }.
123
+ grep(filter)
101
124
  end
102
125
 
103
126
  def get_batch(repository, per_page, page)
@@ -115,7 +138,7 @@ module NyaPr
115
138
  Progress.new(
116
139
  '🐾 Checking pull requests',
117
140
  total: repositories.size,
118
- enabled: progress_enabled && $stderr.tty?
141
+ enabled: config.progress_enabled && $stderr.tty?
119
142
  )
120
143
  end
121
144
 
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module NyaPr
6
+ module PullRequest
7
+ class Paths
8
+ DEFAULT_DIR = 'data'
9
+ FILENAME = 'pull_requests.csv'
10
+
11
+ attr_reader :config
12
+
13
+ def initialize(config, timestamp: Time.now)
14
+ @config = config
15
+ @timestamp = timestamp
16
+ end
17
+
18
+ def csv
19
+ path = run_dir.join(FILENAME)
20
+
21
+ FileUtils.mkdir_p(path.dirname)
22
+
23
+ path.to_s
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :timestamp
29
+
30
+ def run_dir
31
+ @run_dir ||= Pathname.
32
+ new(config.pull_requests_dir || DEFAULT_DIR).
33
+ join("pull_requests_#{formatted_timestamp}")
34
+ end
35
+
36
+ def formatted_timestamp
37
+ timestamp.strftime('%Y%m%d_%H%M%S')
38
+ end
39
+ end
40
+ end
41
+ end
@@ -3,14 +3,12 @@
3
3
  module NyaPr
4
4
  module Repository
5
5
  class Collector
6
- attr_reader :client, :username, :owners, :store, :options
6
+ attr_reader :client, :store, :config
7
7
 
8
- def initialize(client, username, owners, store, **options)
8
+ def initialize(client, store, config)
9
9
  @client = client
10
- @username = username
11
- @owners = owners
12
10
  @store = store
13
- @options = options
11
+ @config = config
14
12
  end
15
13
 
16
14
  def collect
@@ -37,10 +35,12 @@ module NyaPr
37
35
  )
38
36
 
39
37
  repositories = Finder.
40
- new(client, username).
41
- repositories_for(owners)
38
+ new(client, config).
39
+ repositories_for
42
40
 
43
- repositories = filter_contributed(reject_archived(repositories))
41
+ repositories = filter_contributed(
42
+ reject_archived(repositories)
43
+ )
44
44
 
45
45
  store.write(repositories)
46
46
 
@@ -53,14 +53,14 @@ module NyaPr
53
53
 
54
54
  def filter_contributed(repositories)
55
55
  own, external = repositories.partition do |repository|
56
- repository_owner(repository).casecmp?(username)
56
+ repository_owner(repository).casecmp?(config.username)
57
57
  end
58
58
 
59
- own + Filter.new(client, username, progress_enabled: options[:progress]).contributed(external)
59
+ own + Filter.new(client, config).contributed(external)
60
60
  end
61
61
 
62
62
  def reject_archived(repositories)
63
- return repositories unless options[:skip_archived]
63
+ return repositories unless config.skip_archived
64
64
 
65
65
  filtered = repositories.reject do |repository|
66
66
  repository['archived']
@@ -78,7 +78,7 @@ module NyaPr
78
78
  end
79
79
 
80
80
  def use_cache?
81
- store.available? && !options[:refresh]
81
+ store.available? && !config.refresh
82
82
  end
83
83
  end
84
84
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NyaPr
4
+ module Repository
5
+ class Config < Data.define(
6
+ :username,
7
+ :owners,
8
+ :refresh,
9
+ :skip_archived,
10
+ :progress_enabled,
11
+ :workers,
12
+ :repositories_csv
13
+ )
14
+ DEFAULT_WORKERS = 10
15
+
16
+ def self.from_app(config)
17
+ new(
18
+ username: config.username,
19
+ owners: ([config.username] + config.owners).uniq,
20
+ refresh: config.refresh,
21
+ skip_archived: config.skip_archived,
22
+ progress_enabled: config.progress,
23
+ workers: config.workers,
24
+ repositories_csv: config.repositories_csv
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -3,17 +3,13 @@
3
3
  module NyaPr
4
4
  module Repository
5
5
  class Filter
6
- include NyaPr::Request
6
+ include NyaPr::Github::Request
7
7
 
8
- DEFAULT_WORKERS = 10
8
+ attr_reader :client, :config
9
9
 
10
- attr_reader :client, :username, :workers, :progress_enabled
11
-
12
- def initialize(client, username, workers: DEFAULT_WORKERS, progress_enabled: true)
10
+ def initialize(client, config)
13
11
  @client = client
14
- @username = username
15
- @workers = workers
16
- @progress_enabled = progress_enabled
12
+ @config = config
17
13
  end
18
14
 
19
15
  def contributed(repositories)
@@ -30,6 +26,18 @@ module NyaPr
30
26
 
31
27
  private
32
28
 
29
+ def username
30
+ config.username
31
+ end
32
+
33
+ def workers
34
+ config.workers
35
+ end
36
+
37
+ def progress_enabled
38
+ config.progress_enabled
39
+ end
40
+
33
41
  def log_start(repositories)
34
42
  NyaPr.logger.info(
35
43
  "Checking contributions in #{repositories.size} repositories " \
@@ -58,6 +66,8 @@ module NyaPr
58
66
  def build_workers(queue, result, progress_bar)
59
67
  Array.new(workers) do
60
68
  Thread.new do
69
+ Thread.current.report_on_exception = false
70
+
61
71
  process_queue(queue, result, progress_bar)
62
72
  end
63
73
  end
@@ -81,20 +91,23 @@ module NyaPr
81
91
  loop do
82
92
  index, repository = queue.pop(true)
83
93
 
84
- if contributed_to?(repository)
85
- result[index] = repository
86
-
87
- NyaPr.logger.debug(
88
- "Found contributions in #{repository.fetch('full_name')}"
89
- )
90
- end
91
-
94
+ process_repository(repository, index, result)
92
95
  progress_bar.advance
93
96
  rescue ThreadError
94
97
  break
95
98
  end
96
99
  end
97
100
 
101
+ def process_repository(repository, index, result)
102
+ return unless contributed_to?(repository)
103
+
104
+ result[index] = repository
105
+
106
+ NyaPr.logger.debug(
107
+ "Found contributions in #{repository.fetch('full_name')}"
108
+ )
109
+ end
110
+
98
111
  def contributed_to?(repository)
99
112
  commits = get(
100
113
  "/repos/#{repository.fetch('full_name')}/commits",
@@ -3,18 +3,16 @@
3
3
  module NyaPr
4
4
  module Repository
5
5
  class Finder
6
- include NyaPr::Request
6
+ include NyaPr::Github::Request
7
7
 
8
- attr_reader :client, :username
8
+ attr_reader :client, :config
9
9
 
10
- def initialize(client, username)
10
+ def initialize(client, config)
11
11
  @client = client
12
- @username = username
12
+ @config = config
13
13
  end
14
14
 
15
- def repositories_for(owners)
16
- owners = Array(owners)
17
-
15
+ def repositories_for
18
16
  NyaPr.logger.info(
19
17
  "Starting repository collection for #{owners.size} owners"
20
18
  )
@@ -32,6 +30,14 @@ module NyaPr
32
30
 
33
31
  private
34
32
 
33
+ def owners
34
+ config.owners
35
+ end
36
+
37
+ def username
38
+ config.username
39
+ end
40
+
35
41
  def collect_repositories_for(owner)
36
42
  NyaPr.logger.info("Collecting repositories for #{owner}")
37
43
 
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module NyaPr
6
+ module Repository
7
+ class Paths
8
+ DEFAULT_PATH = 'data/repositories.csv'
9
+
10
+ attr_reader :config
11
+
12
+ def initialize(config)
13
+ @config = config
14
+ end
15
+
16
+ def csv
17
+ path = Pathname.new(
18
+ config.repositories_csv || DEFAULT_PATH
19
+ )
20
+
21
+ FileUtils.mkdir_p(path.dirname)
22
+
23
+ path.to_s
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/nya_pr/runner.rb CHANGED
@@ -4,10 +4,10 @@ require 'dotenv'
4
4
 
5
5
  module NyaPr
6
6
  class Runner
7
- attr_reader :options
7
+ attr_reader :config
8
8
 
9
- def initialize(options)
10
- @options = options
9
+ def initialize(config)
10
+ @config = config
11
11
  end
12
12
 
13
13
  def run
@@ -22,38 +22,51 @@ module NyaPr
22
22
 
23
23
  private
24
24
 
25
- def paths
26
- @paths ||= Paths.new(
27
- repositories_path: options[:repositories_csv],
28
- pull_requests_dir: options[:pull_requests_dir]
29
- )
30
- end
31
-
32
25
  def configure_logger
33
- NyaPr.logger.level = options[:log_level]
26
+ NyaPr.logger.level = config.log_level
34
27
  end
35
28
 
36
29
  def target_repositories
37
30
  repository_collector.collect
38
31
  end
39
32
 
40
- def repository_collector
41
- @repository_collector ||= Repository::Collector.new(
42
- client,
43
- username,
44
- owners,
45
- repository_store,
46
- refresh: options[:refresh],
47
- skip_archived: options[:skip_archived],
48
- progress: options[:progress]
33
+ def repository_config
34
+ @repository_config ||= Repository::Config.from_app(config)
35
+ end
36
+
37
+ def pull_request_config
38
+ @pull_request_config ||= PullRequest::Config.from_app(config)
39
+ end
40
+
41
+ def repository_paths
42
+ @repository_paths ||= Repository::Paths.new(
43
+ repository_config
49
44
  )
50
45
  end
51
46
 
52
- def save_pull_requests(pull_requests)
53
- pull_request_store.write(pull_requests)
47
+ def pull_request_paths
48
+ @pull_request_paths ||= PullRequest::Paths.new(
49
+ pull_request_config
50
+ )
51
+ end
54
52
 
55
- NyaPr.logger.info(
56
- "Saved #{pull_requests.size} pull requests to #{pull_request_store.path}"
53
+ def repository_store
54
+ @repository_store ||= Repository::CsvStore.new(
55
+ repository_paths.csv
56
+ )
57
+ end
58
+
59
+ def pull_request_store
60
+ @pull_request_store ||= PullRequest::CsvStore.new(
61
+ pull_request_paths.csv
62
+ )
63
+ end
64
+
65
+ def repository_collector
66
+ @repository_collector ||= Repository::Collector.new(
67
+ client,
68
+ repository_store,
69
+ repository_config
57
70
  )
58
71
  end
59
72
 
@@ -62,18 +75,17 @@ module NyaPr
62
75
  new(
63
76
  client,
64
77
  target_repositories,
65
- limit: options[:limit],
66
- progress_enabled: options[:progress]
78
+ pull_request_config
67
79
  ).
68
80
  find
69
81
  end
70
82
 
71
- def repository_store
72
- @repository_store ||= Repository::CsvStore.new(paths.repositories_csv)
73
- end
83
+ def save_pull_requests(pull_requests)
84
+ pull_request_store.write(pull_requests)
74
85
 
75
- def pull_request_store
76
- @pull_request_store ||= PullRequest::CsvStore.new(paths.pull_requests_csv)
86
+ NyaPr.logger.info(
87
+ "Saved #{pull_requests.size} pull requests to #{pull_request_store.path}"
88
+ )
77
89
  end
78
90
 
79
91
  def pull_request_printer
@@ -81,21 +93,14 @@ module NyaPr
81
93
  end
82
94
 
83
95
  def client
84
- @client ||= Client.new(token)
96
+ @client ||= Github::Client.new(token)
85
97
  end
86
98
 
87
99
  def token
88
- options[:token] || ENV.fetch('GITHUB_TOKEN') do
89
- raise NyaPr::Error, 'GitHub token is required (--token or GITHUB_TOKEN)'
100
+ config.token || ENV.fetch('GITHUB_TOKEN') do
101
+ raise NyaPr::Error,
102
+ 'GitHub token is required (--token or GITHUB_TOKEN)'
90
103
  end
91
104
  end
92
-
93
- def username
94
- options[:username]
95
- end
96
-
97
- def owners
98
- ([username] + options[:owners]).uniq
99
- end
100
105
  end
101
106
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NyaPr
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elijah S. Krukowski
@@ -65,6 +65,20 @@ dependencies:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: '2.14'
68
+ - !ruby/object:Gem::Dependency
69
+ name: faraday-gzip
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3'
68
82
  - !ruby/object:Gem::Dependency
69
83
  name: oj
70
84
  requirement: !ruby/object:Gem::Requirement
@@ -108,19 +122,25 @@ files:
108
122
  - exe/nya-pr
109
123
  - lib/nya_pr.rb
110
124
  - lib/nya_pr/cli/command.rb
111
- - lib/nya_pr/client.rb
112
- - lib/nya_pr/connection.rb
125
+ - lib/nya_pr/config/global.rb
126
+ - lib/nya_pr/config/loader.rb
113
127
  - lib/nya_pr/error.rb
114
- - lib/nya_pr/paths.rb
128
+ - lib/nya_pr/github/client.rb
129
+ - lib/nya_pr/github/connection.rb
130
+ - lib/nya_pr/github/request.rb
115
131
  - lib/nya_pr/progress.rb
132
+ - lib/nya_pr/pull_request/config.rb
116
133
  - lib/nya_pr/pull_request/csv_store.rb
134
+ - lib/nya_pr/pull_request/filter.rb
117
135
  - lib/nya_pr/pull_request/finder.rb
136
+ - lib/nya_pr/pull_request/paths.rb
118
137
  - lib/nya_pr/pull_request/printer.rb
119
138
  - lib/nya_pr/repository/collector.rb
139
+ - lib/nya_pr/repository/config.rb
120
140
  - lib/nya_pr/repository/csv_store.rb
121
141
  - lib/nya_pr/repository/filter.rb
122
142
  - lib/nya_pr/repository/finder.rb
123
- - lib/nya_pr/request.rb
143
+ - lib/nya_pr/repository/paths.rb
124
144
  - lib/nya_pr/runner.rb
125
145
  - lib/nya_pr/version.rb
126
146
  homepage: https://github.com/bodrovis/nya_pr
data/lib/nya_pr/client.rb DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NyaPr
4
- class Client
5
- attr_reader :token, :options
6
-
7
- def initialize(token, **options)
8
- @token = token
9
- @options = options
10
- end
11
- end
12
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'faraday'
4
-
5
- module NyaPr
6
- module Connection
7
- API_URL = 'https://api.github.com'
8
- API_VERSION = '2026-03-10'
9
-
10
- def connection(client)
11
- Faraday.new(connection_options(client)) do |faraday|
12
- faraday.adapter Faraday.default_adapter
13
- end
14
- end
15
-
16
- private
17
-
18
- def connection_options(client)
19
- {
20
- url: API_URL,
21
- headers: {
22
- accept: 'application/vnd.github+json',
23
- user_agent: "nya_pr gem/#{NyaPr::VERSION}",
24
- authorization: "Bearer #{client.token}",
25
- 'X-GitHub-Api-Version': client.options.fetch(:api_version, API_VERSION)
26
- },
27
- request: {
28
- timeout: client.options.fetch(:timeout, 60),
29
- open_timeout: client.options.fetch(:open_timeout, 10)
30
- }
31
- }
32
- end
33
- end
34
- end