Skip to content

Commit 20112d2

Browse files
author
Kyle Macey
committed
add user audit
1 parent 08386c7 commit 20112d2

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

api/ruby/user-auditing/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# User Audit
2+
3+
Lists total number of active, suspended, and recently suspended users. Gives the option to unsuspend all recently suspended users. This is mostly useful when a configuration change may have caused a large number of users to become suspended.
4+
5+
## Installation
6+
7+
8+
### Clone this repository
9+
10+
```shell
11+
git clone [email protected]:github/platform-samples.git
12+
cd api/ruby/user-auditing
13+
```
14+
15+
16+
### Install dependencies
17+
18+
```shell
19+
gem install octokit
20+
```
21+
22+
23+
## Usage
24+
25+
### Configure Octokit
26+
27+
```shell
28+
export OCTOKIT_API_ENDPOINT="https://github.example.com/api/v3" # Default: "https://api.github.com"
29+
export OCTOKIT_ACCESS_TOKEN=00000000000000000000000
30+
```
31+
32+
### Execute
33+
34+
```shell
35+
ruby user_audit.rb
36+
```

api/ruby/user-auditing/user_audit.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
3+
# User Audit - Generated with Octokitchen https://github.com/kylemacey/octokitchen
4+
5+
# Dependencies
6+
require "octokit"
7+
8+
Octokit.configure do |kit|
9+
kit.auto_paginate = true
10+
end
11+
12+
client = Octokit::Client.new
13+
users = client.all_users
14+
n = 1
15+
puts "Importing users..."
16+
full_users = users.map { |u|
17+
print "\r#{n}/#{users.count}"
18+
n += 1
19+
client.user(u.login) rescue nil;
20+
}
21+
22+
suspended = full_users.select do |u|
23+
next unless u
24+
!u.suspended_at.nil? rescue false;
25+
end
26+
27+
active = full_users.select do |u|
28+
next unless u
29+
u.suspended_at.nil? rescue false;
30+
end
31+
32+
two_days = 172800
33+
recent = suspended.select do |u|
34+
u[:suspended_at] > (Time.now - two_days)
35+
end
36+
37+
puts ""
38+
puts ""
39+
puts "Suspended: #{suspended.count}"
40+
puts "Recently Suspended: #{recent.count}"
41+
puts "Active: #{active.count}"
42+
43+
puts ""
44+
45+
print "Unsuspend recently suspended users? (y/N) "
46+
47+
if gets.rstrip == "y"
48+
ent = Octokit::EnterpriseAdminClient.new
49+
50+
recent.each do |u|
51+
ent.unsuspend u[:login]
52+
end
53+
end

0 commit comments

Comments
 (0)