forked from github/platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request github#67 from github/user-audit
add user audit
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Suspended User Audit | ||
|
||
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. | ||
|
||
## Installation | ||
|
||
|
||
### Clone this repository | ||
|
||
```shell | ||
git clone [email protected]:github/platform-samples.git | ||
cd api/ruby/user-auditing | ||
``` | ||
|
||
|
||
### Install dependencies | ||
|
||
```shell | ||
gem install octokit | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
### Configure Octokit | ||
|
||
```shell | ||
export OCTOKIT_API_ENDPOINT="https://github.example.com/api/v3" # Default: "https://api.github.com" | ||
export OCTOKIT_ACCESS_TOKEN=00000000000000000000000 | ||
``` | ||
|
||
### Execute | ||
|
||
```shell | ||
ruby suspended_user_audit.rb | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Suspended User Audit - Generated with Octokitchen https://github.com/kylemacey/octokitchen | ||
|
||
# Dependencies | ||
require "octokit" | ||
|
||
Octokit.configure do |kit| | ||
kit.auto_paginate = true | ||
end | ||
|
||
client = Octokit::Client.new | ||
users = client.all_users | ||
n = 1 | ||
puts "Aggregating users..." | ||
full_users = users.map { |u| | ||
print "\r#{n}/#{users.count}" | ||
n += 1 | ||
client.user(u.login) rescue nil; | ||
} | ||
|
||
suspended = full_users.select do |u| | ||
next unless u | ||
!u.suspended_at.nil? rescue false; | ||
end | ||
|
||
active = full_users.select do |u| | ||
next unless u | ||
u.suspended_at.nil? rescue false; | ||
end | ||
|
||
seconds_in_two_days = 60 * # seconds in an minute | ||
60 * # minutes in an hour | ||
48 # hours in 2 days | ||
|
||
recent = suspended.select do |u| | ||
u[:suspended_at] > (Time.now - seconds_in_two_days) | ||
end | ||
|
||
puts "" | ||
puts "" | ||
puts "Suspended: #{suspended.count}" | ||
puts "Recently Suspended: #{recent.count}" | ||
puts "Active: #{active.count}" | ||
|
||
puts "" | ||
|
||
print "Unsuspend recently suspended users? (y/N) " | ||
|
||
if gets.rstrip == "y" | ||
ent = Octokit::EnterpriseAdminClient.new | ||
|
||
recent.each do |u| | ||
ent.unsuspend u[:login] | ||
end | ||
end |