hub_store 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97df8ac363de159068fcaed6d97775965aac3b7a62e0d306df1d2535b3dd5e7d
4
+ data.tar.gz: b89c4e5ebe4c0658f9fcfdf0f0de61bf585b070560f4e709817180ffc72b6fef
5
+ SHA512:
6
+ metadata.gz: a087117ba86485c70612ac5bf22a6c383976fb51c360b817803b063fa398673df1879e924b4cbadb24118d971e372d4fd26c308228ac729d907093b512124391
7
+ data.tar.gz: b15235ea2df9efc3c7f4f445662fe6e6d03073a35e71ce8830dd0a2a606957dbaddc7121769ee57ec2e57ec887b42d7bcac366cec33b1381c68eb1e9e93e0426
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .env
10
+ /db
11
+ *.csv
12
+ Gemfile.lock
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 1.17.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jens@balvig.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hub_store.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jens Balvig
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Hub Store
2
+
3
+ Save raw pull request and review metadata from GitHub for metrics
4
+ analysis etc.
5
+
6
+
7
+ ## Usage
8
+
9
+ ### Using inside an app
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "hub_store"
14
+ ```
15
+
16
+ Then:
17
+
18
+ ```ruby
19
+ REPOS = "balvig/hub_store,balvig/hub_link"
20
+
21
+ HubStore::Importer.new(repos: REPOS, start_date: 4.weeks.ago).run
22
+ # => Imports pull request and review data to a local sqlite DB
23
+
24
+ HubStore::Exporter.new(resource: PullRequest).run
25
+ # => Exports pull request data from sqlite DB to `pull_requests.csv`
26
+
27
+ HubStore::Exporter.new(resource: Review).run
28
+ # => Exports review data from sqlite DB to `reviews.csv`
29
+ ```
30
+
31
+ ### Using from command line
32
+
33
+ ```bash
34
+ gem install hub_store
35
+ OCTOKIT_ACCESS_TOKEN=<token> hub_store <github_organization/repo_name>
36
+ # Exports 2 years worth of PRs/Reviews to csv files.
37
+ # Subsequent runs resume from last updated_at
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ 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.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
53
+
54
+ ## Code of Conduct
55
+
56
+ Everyone interacting in the HubStore project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hub_store/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hub_store"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/exe/hub_store ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "hub_store"
4
+ require "hub_store/cli"
5
+
6
+ HubStore::Cli.run(ARGV)
data/hub_store.gemspec ADDED
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hub_store/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hub_store"
8
+ spec.version = HubStore::VERSION
9
+ spec.authors = ["Jens Balvig"]
10
+ spec.email = ["jens@balvig.com"]
11
+
12
+ spec.summary = %q{Downloads PR/Review info CSV files}
13
+ spec.homepage = "https://balvig.com"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+
29
+ spec.add_dependency "activerecord"
30
+ spec.add_dependency "dotenv"
31
+ spec.add_dependency "hub_link"
32
+ spec.add_dependency "sqlite3"
33
+ end
@@ -0,0 +1,43 @@
1
+ module HubStore
2
+ class Cli
3
+ def self.run(*args)
4
+ new(*args).run
5
+ end
6
+
7
+ def initialize(argv)
8
+ @argv = argv
9
+ end
10
+
11
+ def run
12
+ import_data
13
+ export_csv
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :argv
19
+
20
+ def start_date
21
+ PullRequest.order(updated_at: :desc).limit(1).pluck(:updated_at).first
22
+ end
23
+
24
+ def import_data
25
+ Importer.new(repos: repo_names, start_date: start_date).run
26
+ end
27
+
28
+ def export_csv
29
+ [PullRequest, Review].each do |resource|
30
+ Exporter.new(resource: resource).run
31
+ end
32
+ end
33
+
34
+ def repo_names
35
+ argv[0].presence || stop
36
+ end
37
+
38
+ def stop
39
+ puts "\nUsage: OCTOKIT_ACCESS_TOKEN=<token> #{$0} <github_org/repo_name>"
40
+ exit
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ require "csv"
2
+ require "hub_store/insert"
3
+ require "hub_store/pull_request"
4
+ require "hub_store/review"
5
+
6
+ module HubStore
7
+ class Exporter
8
+ def initialize(resource:)
9
+ @resource = resource
10
+ end
11
+
12
+ def run
13
+ export_csv
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :resource
19
+
20
+ def export_csv
21
+ CSV.open(csv_file_name, "w", write_headers: true, headers: columns) do |csv|
22
+ resource.find_each do |record|
23
+ csv << record.attributes.values
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ attr_accessor :records, :columns
31
+
32
+ def csv_file_name
33
+ resource.to_s.demodulize.pluralize.underscore + ".csv"
34
+ end
35
+
36
+ def columns
37
+ @_columns ||= resource.columns.map(&:name)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ require "hub_link"
2
+ require "hub_store/insert"
3
+ require "hub_store/pull_request"
4
+ require "hub_store/review"
5
+
6
+ module HubStore
7
+ class Importer
8
+ def initialize(repos:, start_date: nil)
9
+ @repos = repos
10
+ @start_date = start_date || 2.years.ago
11
+ end
12
+
13
+ def run
14
+ stream.in_batches do |batch|
15
+ import_prs(batch.pull_requests)
16
+ import_reviews(batch.reviews)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :repos, :start_date
23
+
24
+ def import_prs(rows)
25
+ rows.each do |row|
26
+ Insert.new(row: row, target: PullRequest).run
27
+ end
28
+
29
+ puts "Imported #{PullRequest.count} PRs so far"
30
+ end
31
+
32
+ def import_reviews(rows)
33
+ rows.each do |row|
34
+ Insert.new(row: row, target: Review).run
35
+ end
36
+
37
+ puts "Imported #{Review.count} reviews so far"
38
+ end
39
+
40
+ def stream
41
+ @_stream ||= HubLink::Stream.new(repos, start_date: start_date)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,38 @@
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
+ rescue ActiveRecord::RecordNotUnique
11
+ Rails.logger.warn "Skipping duplicate record ##{row[:id]}"
12
+ end
13
+
14
+ private
15
+
16
+ def record
17
+ target.find_or_initialize_by(id: id)
18
+ end
19
+
20
+ def id
21
+ row[:id]
22
+ end
23
+
24
+ def importable_attributes
25
+ normalized_attributes.slice(*target_columns)
26
+ end
27
+
28
+ def normalized_attributes
29
+ row.transform_keys { |key| key.to_s.chomp("?") }
30
+ end
31
+
32
+ def target_columns
33
+ target.columns.map(&:name)
34
+ end
35
+
36
+ attr_reader :row, :target
37
+ end
38
+ end
@@ -0,0 +1,6 @@
1
+ require "active_record"
2
+
3
+ module HubStore
4
+ class PullRequest < ActiveRecord::Base
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "active_record"
2
+
3
+ module HubStore
4
+ class Review < ActiveRecord::Base
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module HubStore
2
+ VERSION = "0.2.0"
3
+ end
data/lib/hub_store.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "dotenv/load"
2
+ require "hub_store/version"
3
+ require "hub_store/importer"
4
+ require "hub_store/exporter"
5
+
6
+ module HubStore
7
+ class Error < StandardError; end
8
+
9
+ # Set up a database that resides in RAM
10
+ ActiveRecord::Base.establish_connection(
11
+ adapter: "sqlite3",
12
+ database: "db/hub_store"
13
+ )
14
+
15
+ # Set up database tables and columns
16
+ ActiveRecord::Schema.define do
17
+ unless ActiveRecord::Base.connection.table_exists?("pull_requests")
18
+ create_table "pull_requests", force: :cascade do |t|
19
+ t.string "submitter"
20
+ t.string "labels"
21
+ t.datetime "closed_at"
22
+ t.datetime "created_at", null: false
23
+ t.datetime "updated_at", null: false
24
+ end
25
+ end
26
+
27
+ unless ActiveRecord::Base.connection.table_exists?("reviews")
28
+ create_table "reviews", force: :cascade do |t|
29
+ t.bigint "pull_request_id"
30
+ t.datetime "submitted_at"
31
+ t.string "reviewer"
32
+ t.boolean "approval"
33
+ t.datetime "created_at", null: false
34
+ t.datetime "updated_at", null: false
35
+ end
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hub_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jens Balvig
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: hub_link
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - jens@balvig.com
114
+ executables:
115
+ - hub_store
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".ruby-version"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - exe/hub_store
130
+ - hub_store.gemspec
131
+ - lib/hub_store.rb
132
+ - lib/hub_store/cli.rb
133
+ - lib/hub_store/exporter.rb
134
+ - lib/hub_store/importer.rb
135
+ - lib/hub_store/insert.rb
136
+ - lib/hub_store/pull_request.rb
137
+ - lib/hub_store/review.rb
138
+ - lib/hub_store/version.rb
139
+ homepage: https://balvig.com
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.7.6
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Downloads PR/Review info CSV files
163
+ test_files: []