Skip to content

Commit

Permalink
Merge pull request github#67 from github/user-audit
Browse files Browse the repository at this point in the history
add user audit
  • Loading branch information
kylemacey committed Aug 27, 2015
2 parents 08386c7 + 54f7fc0 commit c91cc95
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
36 changes: 36 additions & 0 deletions api/ruby/user-auditing/README.md
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
```
56 changes: 56 additions & 0 deletions api/ruby/user-auditing/suspended_user_audit.rb
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

0 comments on commit c91cc95

Please sign in to comment.