-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathfind_inactive_members.rb
More file actions
285 lines (247 loc) · 8.39 KB
/
find_inactive_members.rb
File metadata and controls
285 lines (247 loc) · 8.39 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
require "csv"
require "octokit"
require 'optparse'
require 'optparse/date'
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?
)
@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
info "Finding #{@organization} members "
@members = @client.organization_members(@organization).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
info "Gathering a list of repositories..."
# get all repos in the organizaton and place into a hash
@repositories = @client.organization_repositories(@organization).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
@client.commits_since(repo, @date).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"
begin
@client.list_issues(repo, { :since => date }).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
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"
begin
@client.issues_comments(repo, { :since => date }).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
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"
@client.pull_requests_comments(repo, { :since => date }).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
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('-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
end
options[:client] = Octokit::Client.new
InactiveMemberSearch.new(options)