-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
49 lines (39 loc) · 1.42 KB
/
Rakefile
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
require 'octokit'
Octokit.auto_paginate = true
client = Octokit::Client.new :access_token => ENV['GITHUB_ACCESS_TOKEN']
# Array[members_to_add Array, members_to_remove Array, unchanged Array]
def reconcile_members(source, target)
[
source - target,
target - source,
source & target
]
end
namespace :team do
task :sync do
source_org, source_team = ENV['SOURCE'].split('/')
target_org, target_team = ENV['TARGET'].split('/')
source_org_teams = client.organization_teams(source_org)
target_org_teams =
if source_org == target_org
source_org_teams
else
client.organization_teams(target_org)
end
source_id = source_org_teams.detect{ |t| t.slug == source_team }[:id]
target_id = source_org_teams.detect{ |t| t.slug == target_team }[:id]
# https://developer.github.com/v3/orgs/teams/#list-team-members
source_members = Set.new client.team_members(source_id).map(&:login)
target_members = Set.new client.team_members(target_id).map(&:login)
to_add, to_remove, rest = reconcile_members(source_members, target_members)
to_add.each do |login|
# https://developer.github.com/v3/orgs/teams/#add-team-member
client.add_team_member(target_id, login)
end
to_remove.each do |login|
# https://developer.github.com/v3/orgs/teams/#remove-team-member
client.remove_team_member(target_id, login)
end
end
end
task :default => "team:sync"