-
Notifications
You must be signed in to change notification settings - Fork 23
/
outside-collaborators-handler.rb
executable file
·245 lines (209 loc) · 7.97 KB
/
outside-collaborators-handler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env ruby
# Copyright: (C) 2020 iCub Tech Facility - Istituto Italiano di Tecnologia
# Authors: Ugo Pattacini <ugo.pattacini@iit.it>
#########################################################################################
# deps
require 'octokit'
require 'yaml'
require './helpers'
#########################################################################################
# global vars
$org = ENV['OUTSIDE_COLLABORATORS_GITHUB_ORG']
$client = Octokit::Client.new :access_token => ENV['OUTSIDE_COLLABORATORS_GITHUB_TOKEN']
#########################################################################################
# traps
Signal.trap("INT") {
exit 2
}
Signal.trap("TERM") {
exit 2
}
#########################################################################################
def get_repo_invitations(repo)
loop do
check_and_wait_until_reset
$client.repository_invitations(repo)
rate_limit = $client.rate_limit
if rate_limit.remaining > 0 then
break
end
end
invitations = []
last_response = $client.last_response
data = last_response.data
data.each { |i| invitations << {"id" => i.id,
"invitee" => i.invitee.login,
"permissions" => i.permissions} }
until last_response.rels[:next].nil?
last_response = last_response.rels[:next].get
data = last_response.data
data.each { |i| invitations << {"id" => i.id,
"invitee" => i.invitee.login,
"permissions" => i.permissions} }
end
return invitations
end
#########################################################################################
def get_repo_collaborators(repo)
loop do
check_and_wait_until_reset
$client.collaborators(repo)
rate_limit = $client.rate_limit
if rate_limit.remaining > 0 then
break
end
end
collaborators = []
last_response = $client.last_response
data = last_response.data
data.each { |c| collaborators << "#{c.login}" }
until last_response.rels[:next].nil?
last_response = last_response.rels[:next].get
data = last_response.data
data.each { |c| collaborators << "#{c.login}" }
end
return collaborators
end
#########################################################################################
def add_repo_collaborator(repo, user, auth)
check_and_wait_until_reset
begin
$client.user(user)
rescue
puts "- Requested action for not existing user \"#{user}\" ❌"
else
check_and_wait_until_reset
if $client.org_member?($org, user) then
puts "- Requested action for organization member \"#{user}\" ❌"
else
if auth.nil? then
auth = ""
end
# bind authorization within available options
auth_ = auth
if !auth_.casecmp?("admin") && !auth_.casecmp?("maintain") &&
!auth_.casecmp?("write") && !auth_.casecmp?("triage") &&
!auth_.casecmp?("read") then
auth_ = "read"
end
# update pending invitation
get_repo_invitations(repo).each { |invitation|
if invitation["invitee"].casecmp?(user) &&
!invitation["permissions"].casecmp?(auth_) then
print "- Updating invitee \"#{user}\" with permissions \"#{auth_}\""
if !auth_.casecmp?(auth) then
print " (\"#{auth}\" is not available ⚠)"
end
print "\n"
check_and_wait_until_reset
$client.update_repository_invitation(repo, invitation["id"], permission: auth_)
return
end
}
# remap permissions to comply w/ REST API
auth__ = auth_
if auth__.casecmp?("read") then
auth__ = "pull"
elsif auth__.casecmp?("write") then
auth__ = "push"
end
# handle: invitation, update
check_and_wait_until_reset
if $client.collaborator?(repo, user) then
print "- Updating collaborator \"#{user}\" with permissions \"#{auth_}\""
else
print "- Inviting collaborator \"#{user}\" with permissions \"#{auth_}\""
end
if !auth_.casecmp?(auth) then
print " (\"#{auth}\" is not available ⚠)"
end
check_and_wait_until_reset
begin
$client.add_collaborator(repo, user, permission: auth__)
rescue
puts " - problem detected ❌"
end
print "\n"
end
end
end
#########################################################################################
def repo_member(repo_metadata, groups, user)
if repo_metadata then
if repo_metadata.key?(user) then
return true
else
repo_metadata.each { |item, props|
if (props["type"].casecmp?("group")) then
if groups.key?(item) then
if groups[item].include?(user)
return true
end
end
end
}
end
end
return false
end
#########################################################################################
# main
# retrieve information from files
groups = get_entries("../groups")
repos = get_entries("../repos")
# cycle over repos
repos.each { |repo_name, repo_metadata|
repo_full_name = $org + "/" + repo_name
puts "Processing automated repository \"#{repo_full_name}\"..."
check_and_wait_until_reset
if $client.repository?(repo_full_name) then
# clean up all pending invitations
# so that we can revive those stale
get_repo_invitations(repo_full_name).each { |invitation|
invitee = invitation["invitee"]
puts "- Removing invitee \"#{invitee}\""
check_and_wait_until_reset
$client.delete_repository_invitation(repo_full_name, invitation["id"])
}
# add collaborators
if repo_metadata then
repo_metadata.each { |user, props|
type = props["type"]
permissions = props["permissions"]
if (type.casecmp?("user")) then
add_repo_collaborator(repo_full_name, user, permissions)
elsif (type.casecmp?("group")) then
if groups.key?(user) then
puts "- Handling group \"#{user}\" 👥"
groups[user].each { |subuser|
if repo_metadata.key?(subuser) then
puts "- Detected group user \"#{subuser}\" handled individually"
else
add_repo_collaborator(repo_full_name, subuser, permissions)
end
}
else
puts "- Unrecognized group \"#{user}\" ❌"
end
else
puts "- Unrecognized type \"#{type}\" ❌"
end
}
end
# remove collaborators no longer requested
get_repo_collaborators(repo_full_name).each { |user|
check_and_wait_until_reset
if !$client.org_member?($org, user) then
if !repo_member(repo_metadata, groups, user) then
puts "- Removing collaborator \"#{user}\""
check_and_wait_until_reset
$client.remove_collaborator(repo_full_name, user)
end
end
}
puts "...done with \"#{repo_full_name}\" ✔"
else
puts "Repository \"#{repo_full_name}\" does not exist ❌"
end
puts ""
}