-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathfind_inactive_members.rb
More file actions
380 lines (332 loc) · 11.6 KB
/
Copy pathfind_inactive_members.rb
File metadata and controls
380 lines (332 loc) · 11.6 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
require "csv"
require "octokit"
require "optparse"
require "optparse/date"
require "json"
require "fileutils"
class InactiveMemberSearch
attr_accessor :organization, :members, :repositories, :date, :unrecognized_authors
SCOPES=["read:org", "read:user", "repo", "user:email"]
def initialize(options={})
@client = options[:client]
if options[:check]
check_app
check_scopes
check_rate_limit
exit 0
end
raise(OptionParser::MissingArgument) if (
options[:organization].nil? or
options[:date].nil?
)
if options[:checkpoint]
@checkpoint = true
FileUtils.mkdir_p "data/activities"
end
@date = options[:date]
@organization = options[:organization]
@email = options[:email]
@unrecognized_authors = []
organization_members
organization_repositories
member_activity
end
def check_app
info "Application client/secret? #{@client.application_authenticated?}\n"
info "Authentication Token? #{@client.token_authenticated?}\n"
end
def check_scopes
info "Scopes: #{@client.scopes.join ','}\n"
end
def check_rate_limit
info "Rate limit: #{@client.rate_limit.remaining}/#{@client.rate_limit.limit}\n"
end
def env_help
output=<<-EOM
Required Environment variables:
OCTOKIT_ACCESS_TOKEN: A valid personal access token with Organzation admin priviliges
OCTOKIT_API_ENDPOINT: A valid GitHub/GitHub Enterprise API endpoint URL (Defaults to https://api.github.com)
EOM
output
end
# helper to get an auth token for the OAuth application and a user
def get_auth_token(login, password, otp)
temp_client = Octokit::Client.new(login: login, password: password)
res = temp_client.create_authorization(
{
:idempotent => true,
:scopes => SCOPES,
:headers => {'X-GitHub-OTP' => otp}
})
res[:token]
end
private
def debug(message)
$stderr.print message
end
def info(message)
$stdout.print message
end
def member_email(login)
@email ? @client.user(login)[:email] : ""
end
def organization_members
# get all organization members and place into an array of hashes
members = load_file("data/members.json")
if members == nil
info "Collecting organization members from API\n"
members = @client.organization_members(@organization)
info "Organization members collected, saving data...\n"
save_file("data/members.json", members.map(&:to_h))
end
info "Finding #{@organization} members "
@members = members.collect do |m|
email =
{
login: m["login"],
email: member_email(m[:login]),
active: false
}
end
info "#{@members.length} members found.\n"
end
def organization_repositories
repositories = load_file("data/repositories.json")
if repositories == nil
repositories = @client.organization_repositories(@organization)
save_file("data/repositories.json", repositories.map(&:to_h))
end
info "Gathering a list of repositories..."
# get all repos in the organizaton and place into a hash
@repositories = repositories.collect do |repo|
repo["full_name"]
end
info "#{@repositories.length} repositories discovered\n"
end
def add_unrecognized_author(author)
@unrecognized_authors << author
end
# method to switch member status to active
def make_active(login)
hsh = @members.find { |member| member[:login] == login }
hsh[:active] = true
end
def commit_activity(repo)
# get all commits after specified date and iterate
info "...commits"
begin
file_name = "data/activities/" + sanitize_filename(repo) + "-commits_since.json"
commits_since = load_file(file_name, :symbolize_names => true)
if commits_since == nil
commits_since = @client.commits_since(repo, @date).map(&:to_h)
save_file(file_name, commits_since)
end
commits_since.each do |commit|
# if commmitter is a member of the org and not active, make active
if commit[:author].nil?
add_unrecognized_author(commit[:commit][:author])
next
end
if t = @members.find {|member| member[:login] == commit[:author][:login] && member[:active] == false }
make_active(t[:login])
end
end
rescue Octokit::Conflict
info "...no commits"
rescue Octokit::NotFound
#API responds with a 404 (instead of an empty set) when the `commits_since` range is out of bounds of commits.
info "...no commits"
end
end
def issue_activity(repo, date=@date)
# get all issues after specified date and iterate
info "...Issues"
file_name = "data/activities/" + sanitize_filename(repo) + "-list_issues.json"
list_issues = load_file(file_name, :symbolize_names => true)
if list_issues == nil
list_issues = @client.list_issues(repo, { :since => date }).map(&:to_h)
save_file(file_name, list_issues)
end
list_issues.each do |issue|
# if there's no user (ghost user?) then skip this // THIS NEEDS BETTER VALIDATION
if issue[:user].nil?
next
end
# if creator is a member of the org and not active, make active
if t = @members.find {|member| member[:login] == issue[:user][:login] && member[:active] == false }
make_active(t[:login])
end
rescue Octokit::NotFound
#API responds with a 404 (instead of an empty set) when repo is a private fork for security advisories
info "...no access to issues in this repo ..."
end
end
def issue_comment_activity(repo, date=@date)
# get all issue comments after specified date and iterate
info "...Issue comments"
file_name = "data/activities/" + sanitize_filename(repo) + "-issues_comments.json"
issues_comments = load_file(file_name, :symbolize_names => true)
if issues_comments == nil
issues_comments = @client.issues_comments(repo, { :since => date }).map(&:to_h)
save_file(file_name, issues_comments)
end
issues_comments.each do |comment|
# if there's no user (ghost user?) then skip this // THIS NEEDS BETTER VALIDATION
if comment[:user].nil?
next
end
# if commenter is a member of the org and not active, make active
if t = @members.find {|member| member[:login] == comment[:user][:login] && member[:active] == false }
make_active(t[:login])
end
rescue Octokit::NotFound
#API responds with a 404 (instead of an empty set) when repo is a private fork for security advisories
info "...no access to issue comments in this repo ..."
end
end
def pr_activity(repo, date=@date)
# get all pull request comments comments after specified date and iterate
info "...Pull Request comments"
file_name = "data/activities/" + sanitize_filename(repo) + "-pull_requests_comments.json"
pull_requests_comments = load_file(file_name, :symbolize_names => true)
if pull_requests_comments == nil
pull_requests_comments = @client.pull_requests_comments(repo, { :since => date }).map(&:to_h)
save_file(file_name, pull_requests_comments)
end
pull_requests_comments.each do |comment|
# if there's no user (ghost user?) then skip this // THIS NEEDS BETTER VALIDATION
if comment[:user].nil?
next
end
# if commenter is a member of the org and not active, make active
if t = @members.find {|member| member[:login] == comment[:user][:login] && member[:active] == false }
make_active(t[:login])
end
end
end
def member_activity
@repos_completed = 0
# print update to terminal
info "Analyzing activity for #{@members.length} members and #{@repositories.length} repos for #{@organization}\n"
# for each repo
@repositories.each do |repo|
info "rate limit remaining: #{@client.rate_limit.remaining} "
info "analyzing #{repo}"
commit_activity(repo)
issue_activity(repo)
issue_comment_activity(repo)
pr_activity(repo)
# print update to terminal
@repos_completed += 1
info "...#{@repos_completed}/#{@repositories.length} repos completed\n"
end
# open a new csv for output
CSV.open("inactive_users.csv", "wb") do |csv|
csv << ["login", "email"]
# iterate and print inactive members
@members.each do |member|
if member[:active] == false
member_detail = []
member_detail << member[:login]
member_detail << member[:email] unless member[:email].nil?
info "#{member_detail} is inactive\n"
csv << member_detail
end
end
end
CSV.open("unrecognized_authors.csv", "wb") do |csv|
csv << ["name", "email"]
@unrecognized_authors.each do |author|
author_detail = []
author_detail << author[:name]
author_detail << author[:email]
info "#{author_detail} is unrecognized\n"
csv << author_detail
end
end
end
def load_file(filename, parseOptions = {})
if @checkpoint != true
return nil
end
begin
# info "Trying to load #{filename}.\n"
data = JSON.parse(File.read(filename), parseOptions)
# info "File #{filename} has been loaded.\n"
return data
rescue => exception
# info "Cannot load #{filename}\n"
# info "#{exception}\n"
return nil
end
end
def save_file(filename, data)
if @checkpoint != true
return
end
info "Saving data to #{filename}\n"
File.open(filename, 'w') do |f|
f.write(JSON.generate(data))
end
info "File saved to #{filename}\n"
end
def sanitize_filename(filename)
# Split the name when finding a period which is preceded by some
# character, and is followed by some character other than a period,
# if there is no following period that is followed by something
# other than a period (yeah, confusing, I know)
fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m
# We now have one or two parts (depending on whether we could find
# a suitable period). For each of these parts, replace any unwanted
# sequence of characters with an underscore
fn.map! { |s| s.gsub /[^a-z0-9\-]+/i, '_' }
# Finally, join the parts with a period and return the result
return fn.join '.'
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "#{$0} - Find and output inactive members in an organization"
opts.on('-c', '--check', "Check connectivity and scope") do |c|
options[:check] = c
end
opts.on('-d', '--date MANDATORY',Date, "Date from which to start looking for activity") do |d|
options[:date] = d.to_s
end
opts.on('-e', '--email', "Fetch the user email (can make the script take longer") do |e|
options[:email] = e
end
opts.on('-o', '--organization MANDATORY',String, "Organization to scan for inactive users") do |o|
options[:organization] = o
end
opts.on('-v', '--verbose', "More output to STDERR") do |v|
@debug = true
options[:verbose] = v
end
opts.on('--checkpoint', "Enable file checkpoint, to be able to continue from failed request") do |c|
options[:checkpoint] = c
end
opts.on('-h', '--help', "Display this help") do |h|
puts opts
exit 0
end
end.parse!
stack = Faraday::RackBuilder.new do |builder|
builder.use Octokit::Middleware::FollowRedirects
builder.use Octokit::Response::RaiseError
builder.use Octokit::Response::FeedParser
builder.response :logger
builder.adapter Faraday.default_adapter
end
Octokit.configure do |kit|
kit.auto_paginate = true
kit.middleware = stack if @debug
kit.connection_options = {
request: {
open_timeout: 10,
timeout: 10,
}
}
end
options[:client] = Octokit::Client.new
InactiveMemberSearch.new(options)