Created
March 24, 2015 07:07
-
-
Save krisselden/d0ac00665e1988d14369 to your computer and use it in GitHub Desktop.
simple script to sync teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'io/console' | |
require 'net/https' | |
require 'json' | |
require 'yaml' | |
require 'set' | |
script_name = File.basename($0) | |
$config_file = File.join(Dir.home, ".#{script_name}rc") | |
def write_config(config) | |
File.open($config_file, 'w', 0600) do |io| | |
YAML.dump(config, io) | |
end | |
end | |
def read_config | |
YAML.load_file($config_file) | |
end | |
def open_github(&block) | |
http = Net::HTTP.new('api.github.com', 443) | |
http.use_ssl = true | |
#http.set_debug_output $stdout | |
http.start(&block) | |
end | |
def check_response(res) | |
unless Net::HTTPSuccess === res | |
puts res.code | |
puts res.message | |
puts res.body | |
exit(1) | |
end | |
end | |
case ARGV[0] | |
when 'login' | |
if ARGV.length != 2 | |
puts "#{script_name} team list <org>" | |
exit(1) | |
end | |
username = ARGV[1] | |
puts "Enter github password for user '#{username}'" | |
password = $stdin.noecho(&:gets).chomp | |
open_github do |http| | |
req = Net::HTTP::Post.new('/authorizations') | |
req.basic_auth username, password | |
req.body = JSON.generate(scopes: ['admin:org'], note: script_name) | |
res = http.request(req) | |
check_response(res) | |
authorization = JSON.parse(res.body) | |
write_config('token' => authorization['token']) | |
end | |
when 'team' | |
case ARGV[1] | |
when 'list' | |
if ARGV.length != 3 | |
puts "#{script_name} team list <org>" | |
exit(1) | |
end | |
org = ARGV[2] | |
token = read_config['token'] | |
open_github do |http| | |
res = http.get("/orgs/#{org}/teams", 'Authorization' => "token #{token}") | |
check_response(res) | |
JSON.parse(res.body).each do |org| | |
puts "#{org['id']}\t#{org['name']}" | |
end | |
end | |
when 'sync' | |
if ARGV.length != 4 | |
puts "#{script_name} team sync <src_team_id> <dest_team_id>" | |
end | |
src_team_id = ARGV[2] | |
dest_team_id = ARGV[3] | |
token = read_config['token'] | |
src_set = Set.new | |
dest_set = Set.new | |
open_github do |http| | |
res = http.get("/teams/#{src_team_id}/members", 'Authorization' => "token #{token}") | |
check_response(res) | |
JSON.parse(res.body).each do |member| | |
src_set << member['login'] | |
end | |
res = http.get("/teams/#{dest_team_id}/members", 'Authorization' => "token #{token}") | |
check_response(res) | |
JSON.parse(res.body).each do |member| | |
dest_set << member['login'] | |
end | |
missing_set = src_set - dest_set | |
missing_set.each do |username| | |
puts "adding #{username} to #{dest_team_id}" | |
req = Net::HTTP::Put.new("/teams/#{dest_team_id}/memberships/#{username}", 'Authorization' => "token #{token}") | |
res = http.request(req) | |
check_response(res) | |
end | |
end | |
else | |
puts "#{script_name} team list <org>" | |
puts "#{script_name} team sync <src_team_id> <dest_team_id>" | |
exit(1) | |
end | |
else | |
puts "#{script_name} login <username>" | |
puts "#{script_name} team list <org>" | |
puts "#{script_name} team sync <src_team_id> <dest_team_id>" | |
exit(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment