forked from Bostonhacks/squadra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.rb
66 lines (59 loc) · 2.19 KB
/
github.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
require 'octokit'
require 'yaml'
puts 'Starting github team update'
config = YAML.load_file('team.yml')
members = config['members']
new_permissions = {}
members.each do |member|
new_permissions[member['github']['username'].downcase] = member['github']['role'].downcase
end
client = Octokit::Client.new(
:login => ENV['GITHUB_USERNAME'],
:password => ENV['GITHUB_ACCESS_TOKEN'])
current_members = client.organization_members('bostonhacks')
current_permissions = {}
# The permissions on this repo are the current permissions
current_members.each do |member|
permission = client.permission_level('bostonhacks/squadra', member[:login])[:permission]
permission = 'member' if permission == 'read'
current_permissions[member[:login].downcase] = permission.downcase
end
current_permissions.each do |username, permission|
# If nothing changed, delete from hash
if new_permissions[username] == permission
puts "Nothing changed for user #{username}. Before: #{current_permissions[username]}, After: #{new_permissions[username]}"
# If permission is different, delete and update
elsif !new_permissions[username].nil?
puts "Update permissions for user #{username} from #{current_permissions[username]} to #{new_permissions[username]}"
if (ENV['PROD'])
puts "Calling Github API, changing perms for user: #{username}"
client.remove_organization_member('bostonhacks', username)
client.update_organization_membership(
'bostonhacks',
:user => username,
:role => new_permissions[username]
)
end
# Otherwise just delete
else
puts "Remove user #{username} from bostonhacks"
if (ENV['PROD'])
puts "Calling Github API, removing user: #{username}"
client.remove_organization_member('bostonhacks', username)
end
end
current_permissions.delete(username)
new_permissions.delete(username)
end
# Cleanup: add remaining users
new_permissions.each do |username, permission|
puts "Adding new member to bostonhacks: #{username}"
if (ENV['PROD'])
puts "Calling Github API, adding user: #{username}"
client.update_organization_membership(
'bostonhacks',
:user => username,
:role => new_permissions[username]
)
end
end