-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetContributionCounts.rb
83 lines (68 loc) · 2.41 KB
/
GetContributionCounts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'bundler/setup'
require 'httparty'
require './config.rb'
require './database.rb'
require './models/person.rb'
# Basic class for consuming the API
class GetContributionCounts
include HTTParty
base_uri 'https://api.github.com'
headers 'Accept' => 'application/vnd.github.v3+json', 'User-Agent' => 'jakobbuis/womath'
def initialize(options)
@config = options
end
def execute!
entries = Person.where('commits_count IS NULL and company_name IS NOT NULL')
count = entries.count
puts "Processing #{count} entries..."
entries.each do |person|
person.update_attribute :commits_count, getContribution(person)
count = count - 1
puts "#{count} entries to go" if count % 100 === 0
end
puts 'All done'
end
private
def getContribution person
page = 1
results = []
commits = 0
while true
# Grab a page of repositories
response = self.class.get("https://api.github.com/repos/eclipse/#{person.repository}/commits?per_page=100&page=#{page.to_s}&author=#{person.email}", @config)
# Do not capture repositories that return HTTP errors (empty repositories do this)
break if response.code >= 400
# Apply the callback to each elements
commits = commits + response.count
# Stop if we are at the end of the list
break if response.count < 100
# else continue
page += 1
end
return commits
end
# Override get method to take the rate limiter into account
def self.get(*args, &block)
result = super # Execute the call to find the current rate limit
if result.headers['x-ratelimit-remaining'].to_i < 10
raise "Close to the rate limit (#{result.headers['x-ratelimit-remaining']}/#{result.headers['x-ratelimit-limit']})"
end
return result # Return the original call
end
end
# Validate input parameters
instructions = "Usage: ruby GetContributionCounts.rb [-v]\n"
if ARGV[0] == '-h' or ARGV[0] == '--help'
puts instructions
exit 1
end
# Process the options given
options = {
basic_auth: {
username: $config[:github][:user],
password: $config[:github][:password],
},
verbose: (ARGV[1].present? and ARGV[1] == '-v')
}
# Boot the main process
GetContributionCounts.new(options).execute!