-
Notifications
You must be signed in to change notification settings - Fork 4
/
get.rb
71 lines (60 loc) · 2.24 KB
/
get.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
require "json"
require "open-uri"
module Jekyll
module GetFilter
# Check for environment variables
if not ENV["GITHUB_ACTOR"]
raise "GITHUB_ACTOR not set"
elsif not ENV["GITHUB_TOKEN"]
raise "GITHUB_TOKEN not set"
end
# Tuples with owners and respositories
@@tuples = {}
# Get API responses
def get(rows)
# If memoized
if not @@tuples.empty?
return @@tuples.values
end
# For each row in sorted order
rows.sort_by { |row| row["repo"].downcase }.each do |row|
# If a GitHub repo
if row["repo"] =~ /^https?:\/\/(?:www\.)?github\.com\/([^\/]*)\/([^\/]*)\/?/
# Remember login, name
login, name = $1, $2
# If we've not yet seen this login
if not @@tuples.key?(login)
# GET https://api.github.com/users/:login
begin
sleep(1) # To avoid API limits
url = "https://api.github.com/users/#{login}"
print "Fetching #{url}... "
response = JSON.parse(open(url, :http_basic_authentication => [ENV["GITHUB_ACTOR"], ENV["GITHUB_TOKEN"]]).read)
print "200 OK\n"
@@tuples[login] = response, [] # Tuple for this owner and its repos
rescue => e
print "#{e}\n"
puts open("https://api.github.com/rate_limit", :http_basic_authentication => [ENV["GITHUB_ACTOR"], ENV["GITHUB_TOKEN"]]).read
next
end
end
begin
sleep(1) # To avoid API limits
url = "https://api.github.com/repos/#{login}/#{name}"
print "Fetching #{url}... "
response = JSON.parse(open(url, :http_basic_authentication => [ENV["GITHUB_ACTOR"], ENV["GITHUB_TOKEN"]]).read)
print "200 OK\n"
@@tuples[login][1].push(response) # Another repo for this owner
rescue => e
print "#{e}\n"
puts open("https://api.github.com/rate_limit", :http_basic_authentication => [ENV["GITHUB_ACTOR"], ENV["GITHUB_TOKEN"]]).read
end
else
print "Ignoring #{repo}.\n"
end
end
@@tuples.values # Return tuples
end
end
end
Liquid::Template.register_filter(Jekyll::GetFilter)