hub_store 0.9.0 → 0.10.0

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: 4f3ebc4985063758ffd4fc0fafb5f3be9145c378d442c4682f976b345defec57
4
- data.tar.gz: 8aaedc11dc2e93e3f314555af5f6ab0a41e62ce409bfcfa7bef3f0087f7730f6
3
+ metadata.gz: f201ad7494f23bab6f9eb1938dad969bc434e0d5de26da14cab920414eb9ddae
4
+ data.tar.gz: 23ed79127208c4a9db714970302bb9c979cdc225fac2482d8a43e7d10d00c687
5
5
  SHA512:
6
- metadata.gz: 0cbd7373c45054704a77de3e7b2d78f9f576f5462a901bb11649931887957bed64479290d08613821780f10627cf90cad14f24c3436a534cc084b294dffbe6b3
7
- data.tar.gz: 6ad266c08cb76a883f8ac02f472387980cf9923d02504b32f9e5615220f0d7b2e949a29cf86abeb63820e419fb258e41c945c3bf9ec0371b922f08898dcdeb3e
6
+ metadata.gz: e9c54eb576bc9f1739c3be646e200352e3ba77fb73361d4a2f34196c324863fc2f1709212c178273abf2940e3f1956633204441edf85f46610ebe76864a71a24
7
+ data.tar.gz: 7e06dd0634abbcf883c8f1ee064e7b219ab9d3b3edf562638f73b9de6e2053015fe3b80a4a509616f4a9a9b47d9c434ba7137f9a989b091c840476c9b2a36392
data/.gitignore CHANGED
@@ -7,7 +7,8 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  .env
10
- /db
10
+ hub_store_db
11
11
  *.csv
12
12
  Gemfile.lock
13
13
  *.log
14
+ test/fixtures/vcr_cassettes/
data/README.md CHANGED
@@ -16,12 +16,6 @@ gem install hub_store
16
16
  OCTOKIT_ACCESS_TOKEN=<token> hub_store <github_organization/repo_names>
17
17
  ```
18
18
 
19
- ## Development
20
-
21
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
-
23
- 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).
24
-
25
19
  ## Contributing
26
20
 
27
21
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hub_store. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/exe/hub_store CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "hub_store"
4
- require "hub_store/cli"
5
4
 
6
5
  HubStore::Cli.run(ARGV)
data/hub_store.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  spec.add_dependency "activerecord"
33
33
  spec.add_dependency "dotenv"
34
- spec.add_dependency "hub_link", ">= 0.8"
34
+ spec.add_dependency "hub_link", ">= 0.12.0"
35
35
  spec.add_dependency "sqlite3"
36
36
  spec.add_dependency "tty-spinner"
37
37
  end
data/lib/hub_store/cli.rb CHANGED
@@ -1,7 +1,18 @@
1
+ require "hub_store/exporter"
2
+
3
+ require "hub_store/storage/database"
4
+ require "hub_store/storage/import"
5
+
1
6
  require "hub_store/ui"
2
7
 
3
8
  module HubStore
4
9
  class Cli
10
+ RESOURCES = {
11
+ review_requests: Storage::ReviewRequest,
12
+ reviews: Storage::Review,
13
+ pull_requests: Storage::PullRequest
14
+ }.freeze
15
+
5
16
  def self.run(*args)
6
17
  new(*args).run
7
18
  end
@@ -11,6 +22,7 @@ module HubStore
11
22
  end
12
23
 
13
24
  def run
25
+ setup_database
14
26
  import_data
15
27
  export_csv
16
28
  end
@@ -19,18 +31,42 @@ module HubStore
19
31
 
20
32
  attr_reader :argv
21
33
 
34
+ def setup_database
35
+ ui.start("Preparing database")
36
+ Storage::Database.new.setup
37
+ ui.stop("Done.")
38
+ end
39
+
22
40
  def import_data
23
41
  repos.each do |repo|
24
- Importer.new(repo: repo).run
42
+ Storage::Import.new(repo: repo, resources: RESOURCES, start_date: start_date).run do |on|
43
+ on.init do |query|
44
+ ui.log "\n-- #{query} --"
45
+ end
46
+
47
+ on.start do |resource|
48
+ ui.start("Importing #{resource}")
49
+ end
50
+
51
+ on.finish do |count|
52
+ ui.stop("Total: #{count}")
53
+ end
54
+ end
25
55
  end
26
56
  end
27
57
 
28
58
  def export_csv
29
- [PullRequest, Review, ReviewRequest].each do |resource|
59
+ RESOURCES.each do |name, resource|
60
+ ui.start("Exporting #{name}")
30
61
  Exporter.new(resource: resource).run
62
+ ui.stop("Done.")
31
63
  end
32
64
  end
33
65
 
66
+ def ui
67
+ @_ui ||= Ui.new
68
+ end
69
+
34
70
  def repos
35
71
  repo_names.split(",")
36
72
  end
@@ -39,6 +75,10 @@ module HubStore
39
75
  argv[0].presence || stop
40
76
  end
41
77
 
78
+ def start_date
79
+ ENV["START_DATE"]
80
+ end
81
+
42
82
  def stop
43
83
  puts "\nUsage: OCTOKIT_ACCESS_TOKEN=<token> #{$0} <github_org/repo_name>"
44
84
  exit
@@ -1,24 +1,18 @@
1
1
  require "csv"
2
- require "hub_store/insert"
3
- require "hub_store/pull_request"
4
- require "hub_store/review"
5
2
 
6
3
  module HubStore
7
4
  class Exporter
8
- def initialize(resource:, ui: Ui.new)
5
+ def initialize(resource:)
9
6
  @resource = resource
10
- @ui = ui
11
7
  end
12
8
 
13
9
  def run
14
- ui.start("Exporting #{csv_file_name}")
15
10
  export_csv
16
- ui.stop("Done.")
17
11
  end
18
12
 
19
13
  private
20
14
 
21
- attr_reader :resource, :ui
15
+ attr_reader :resource
22
16
 
23
17
  def export_csv
24
18
  CSV.open(csv_file_name, "w", write_headers: true, headers: columns) do |csv|
@@ -28,10 +22,6 @@ module HubStore
28
22
  end
29
23
  end
30
24
 
31
- private
32
-
33
- attr_accessor :records, :columns
34
-
35
25
  def csv_file_name
36
26
  resource.to_s.demodulize.pluralize.underscore + ".csv"
37
27
  end
@@ -0,0 +1,64 @@
1
+ require "active_record"
2
+
3
+ module HubStore::Storage
4
+ class Database
5
+ def initialize(options = { adapter: "sqlite3", database: "hub_store_db" })
6
+ @options = options
7
+ end
8
+
9
+ def setup
10
+ connect
11
+ create_tables
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :options
17
+
18
+ def connect
19
+ ActiveRecord::Base.establish_connection(options)
20
+ end
21
+
22
+ def create_tables
23
+ ActiveRecord::Schema.define do
24
+ unless ActiveRecord::Base.connection.table_exists?("pull_requests")
25
+ create_table "pull_requests", force: :cascade do |t|
26
+ t.string "submitter"
27
+ t.string "number"
28
+ t.string "labels"
29
+ t.string "repo"
30
+ t.integer "approval_time"
31
+ t.integer "time_to_first_review"
32
+ t.integer "merge_time"
33
+ t.integer "additions"
34
+ t.integer "review_count"
35
+ t.boolean "straight_approval"
36
+ t.datetime "closed_at"
37
+ t.datetime "created_at", null: false
38
+ t.datetime "updated_at", null: false
39
+ end
40
+ end
41
+
42
+ unless ActiveRecord::Base.connection.table_exists?("reviews")
43
+ create_table "reviews", force: :cascade do |t|
44
+ t.bigint "pull_request_id"
45
+ t.datetime "submitted_at"
46
+ t.string "reviewer"
47
+ t.string "state"
48
+ t.boolean "approval"
49
+ end
50
+ end
51
+
52
+ unless ActiveRecord::Base.connection.table_exists?("review_requests")
53
+ create_table "review_requests", force: :cascade, id: false do |t|
54
+ t.string "digest", limit: 40, primary: true
55
+ t.bigint "pull_request_id"
56
+ t.string "requester"
57
+ t.string "reviewer"
58
+ t.datetime "created_at", null: false
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ require "hub_link/importer"
2
+ require "hub_store/storage/pull_request"
3
+ require "hub_store/storage/review"
4
+ require "hub_store/storage/review_request"
5
+
6
+ module HubStore::Storage
7
+ class Import
8
+ def initialize(repo:, resources:, start_date: nil)
9
+ @repo = repo
10
+ @resources = resources
11
+ @start_date = start_date
12
+ end
13
+
14
+ def run(&block)
15
+ HubLink::Importer.run(repo: repo, start_date: start_date, resources: resources, &block)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :repo, :resources
21
+
22
+ def start_date
23
+ @start_date.presence || PullRequest.for(repo).latest_update
24
+ end
25
+ end
26
+ end
@@ -1,12 +1,16 @@
1
1
  require "active_record"
2
2
 
3
- module HubStore
3
+ module HubStore::Storage
4
4
  class PullRequest < ActiveRecord::Base
5
5
  scope :recently_updated_first, -> { order(updated_at: :desc) }
6
6
  validates :repo, presence: true
7
7
 
8
8
  def self.for(repo)
9
- where(repo: repo)
9
+ where(repo: repo.to_s)
10
+ end
11
+
12
+ def self.latest_update
13
+ recently_updated_first.first&.updated_at
10
14
  end
11
15
  end
12
16
  end
@@ -1,6 +1,6 @@
1
1
  require "active_record"
2
2
 
3
- module HubStore
3
+ module HubStore::Storage
4
4
  class Review < ActiveRecord::Base
5
5
  end
6
6
  end
@@ -1,6 +1,7 @@
1
1
  require "active_record"
2
2
 
3
- module HubStore
3
+ module HubStore::Storage
4
4
  class ReviewRequest < ActiveRecord::Base
5
+ self.primary_key = :digest
5
6
  end
6
7
  end
@@ -1,3 +1,3 @@
1
1
  module HubStore
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
data/lib/hub_store.rb CHANGED
@@ -1,51 +1,6 @@
1
1
  require "dotenv/load"
2
2
  require "hub_store/version"
3
- require "hub_store/importer"
4
- require "hub_store/exporter"
3
+ require "hub_store/cli"
5
4
 
6
5
  module HubStore
7
- class Error < StandardError; end
8
- # Set up database
9
- ActiveRecord::Base.establish_connection(
10
- adapter: "sqlite3",
11
- database: "db/hub_store"
12
- )
13
-
14
- # Set up database tables and columns
15
- ActiveRecord::Schema.define do
16
- unless ActiveRecord::Base.connection.table_exists?("pull_requests")
17
- create_table "pull_requests", force: :cascade do |t|
18
- t.string "submitter"
19
- t.string "number"
20
- t.string "labels"
21
- t.string "repo"
22
- t.integer "approval_time"
23
- t.integer "time_to_first_review"
24
- t.integer "merge_time"
25
- t.integer "additions"
26
- t.integer "review_count"
27
- t.boolean "straight_approval"
28
- t.datetime "closed_at"
29
- t.datetime "created_at", null: false
30
- t.datetime "updated_at", null: false
31
- end
32
- end
33
-
34
- unless ActiveRecord::Base.connection.table_exists?("reviews")
35
- create_table "reviews", force: :cascade do |t|
36
- t.bigint "pull_request_id"
37
- t.datetime "submitted_at", null: false
38
- t.string "reviewer"
39
- t.boolean "approval"
40
- end
41
- end
42
-
43
- unless ActiveRecord::Base.connection.table_exists?("review_requests")
44
- create_table "review_requests", force: :cascade do |t|
45
- t.string "requester"
46
- t.string "reviewer"
47
- t.datetime "created_at", null: false
48
- end
49
- end
50
- end
51
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hub_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Balvig
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '0.8'
131
+ version: 0.12.0
132
132
  type: :runtime
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: '0.8'
138
+ version: 0.12.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: sqlite3
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -187,11 +187,11 @@ files:
187
187
  - lib/hub_store.rb
188
188
  - lib/hub_store/cli.rb
189
189
  - lib/hub_store/exporter.rb
190
- - lib/hub_store/importer.rb
191
- - lib/hub_store/insert.rb
192
- - lib/hub_store/pull_request.rb
193
- - lib/hub_store/review.rb
194
- - lib/hub_store/review_request.rb
190
+ - lib/hub_store/storage/database.rb
191
+ - lib/hub_store/storage/import.rb
192
+ - lib/hub_store/storage/pull_request.rb
193
+ - lib/hub_store/storage/review.rb
194
+ - lib/hub_store/storage/review_request.rb
195
195
  - lib/hub_store/ui.rb
196
196
  - lib/hub_store/version.rb
197
197
  homepage: https://balvig.com
@@ -1,70 +0,0 @@
1
- require "hub_link"
2
- require "hub_store/insert"
3
- require "hub_store/pull_request"
4
- require "hub_store/review"
5
- require "hub_store/review_request"
6
-
7
- module HubStore
8
- class Importer
9
- def initialize(repo:, start_date: nil, ui: Ui.new)
10
- @repo = repo
11
- @start_date = start_date
12
- @ui = ui
13
- end
14
-
15
- def run
16
- stream.in_batches do |batch|
17
- ui.log "\n-- #{batch.query} --"
18
- import_prs(batch)
19
- import_reviews(batch)
20
- import_review_requests(batch)
21
- end
22
- end
23
-
24
- private
25
-
26
- attr_reader :repo, :start_date, :ui
27
-
28
- def start_date
29
- @start_date || latest_local_update
30
- end
31
-
32
- def latest_local_update
33
- PullRequest.for(repo).recently_updated_first.first&.updated_at
34
- end
35
-
36
- def import_prs(batch)
37
- ui.start("Importing PRs")
38
-
39
- batch.pull_requests.each do |row|
40
- Insert.new(row: row, target: PullRequest).run
41
- end
42
-
43
- ui.stop("Total: #{PullRequest.count}")
44
- end
45
-
46
- def import_reviews(batch)
47
- ui.start("Importing reviews")
48
-
49
- batch.reviews.each do |row|
50
- Insert.new(row: row, target: Review).run
51
- end
52
-
53
- ui.stop("Total: #{Review.count}")
54
- end
55
-
56
- def import_review_requests(batch)
57
- ui.start("Importing review requests")
58
-
59
- batch.review_requests.each do |row|
60
- Insert.new(row: row, target: ReviewRequest).run
61
- end
62
-
63
- ui.stop("Total: #{ReviewRequest.count}")
64
- end
65
-
66
- def stream
67
- @_stream ||= HubLink::Stream.new(repo, start_date: start_date)
68
- end
69
- end
70
- end
@@ -1,32 +0,0 @@
1
- module HubStore
2
- class Insert
3
- def initialize(row:, target:)
4
- @row = row
5
- @target = target
6
- end
7
-
8
- def run
9
- record.update! importable_attributes
10
- end
11
-
12
- private
13
-
14
- def record
15
- target.find_or_initialize_by(id: id)
16
- end
17
-
18
- def id
19
- row[:id]
20
- end
21
-
22
- def importable_attributes
23
- row.slice(*target_columns)
24
- end
25
-
26
- def target_columns
27
- target.columns.map(&:name).map(&:to_sym)
28
- end
29
-
30
- attr_reader :row, :target
31
- end
32
- end