Skip to content

Commit a05abf5

Browse files
committed
Simplify mirroring all repos for user/org
[Octokit](http://octokit.github.io) allows simple navigation of Github's APIs, so use that to convert catch-all user/org mirror attributes to a list of all repos. Auto pagination isn't the most optimal way of talking to the APIs (and could result in rate-limiting), but we're not likely to be hitting it heavy enough for that to matter.
1 parent b019ddc commit a05abf5

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

recipes/default.rb

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1-
include_recipe "git"
2-
3-
require "net/http"
4-
require 'json'
1+
include_recipe 'git'
2+
chef_gem 'octokit'
3+
require 'octokit'
54

65
# Replace all * instances with names of all the user's repos from GitHub's API
7-
node.source_mirror.repos.each do | user, repo |
6+
Octokit.auto_paginate = true
7+
node['source_mirror']['repos'].each do | user, repo |
88
if '*' == repo
9-
uri = URI("https://api.github.com/users/#{user}/repos")
10-
user_repos = []
11-
response = Net::HTTP.start(uri.host, uri.port,
12-
:use_ssl => uri.scheme == 'https') do |http|
13-
http.get(uri.path)
14-
end
15-
json = JSON.load(response.body)
16-
17-
case response
18-
when Net::HTTPSuccess
19-
json.each { |r|
20-
user_repos << r['name']
21-
}
22-
node.override['source_mirror']['repos'][user] = user_repos
23-
else
24-
log "GitHub error: #{json.message}" do
25-
level :error
26-
end
27-
return
9+
repos = Octokit.repos user
10+
user_repos = repos.map do |repo_data|
11+
repo_data[:name]
2812
end
13+
node.override['source_mirror']['repos'][user] = user_repos
2914
end
3015
end
3116

0 commit comments

Comments
 (0)