Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Last active August 29, 2015 14:06
Show Gist options
  • Save fancyremarker/2d476067b69d12a8c78a to your computer and use it in GitHub Desktop.
Save fancyremarker/2d476067b69d12a8c78a to your computer and use it in GitHub Desktop.
Sync a (non-Owners) GitHub team with all org repos
#!/usr/bin/env ruby
# Usage: ruby team-sync.rb aptible team
require 'octokit'
org_login = ARGV[0]
team_slug = ARGV[1]
print 'Username: '
username = $stdin.gets.chomp
print 'Password: '
password = $stdin.gets.chomp
Octokit.configure do |c|
c.login = username
c.password = password
end
Octokit.auto_paginate = true
all_repos = Octokit.org_repos(org_login).map do |repo|
[repo] + Octokit.forks(repo.full_name)
end.flatten
team = Octokit.org_teams(org_login).find { |t| t.slug == team_slug }
current_team_repos = Octokit.team_repos(team.id)
diff = all_repos.map(&:full_name) - current_team_repos.map(&:full_name)
diff.each do |name|
begin
Octokit.add_team_repo(team.id, name)
puts "Added authorization on repo: #{name}"
rescue Octokit::UnprocessableEntity => e
puts "Repo skipped: #{name}" if e.errors.first[:code] == 'not_owned'
rescue Octokit::Forbidden
puts "Repo skipped: #{name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment