|
| 1 | + |
| 2 | +# GitHub & GitHub Enterprise Instance auditor |
| 3 | +# ======================================= |
| 4 | +# |
| 5 | +# Usage: ruby instance_audit.rb |
| 6 | +# |
| 7 | +# These environment variables must be set: |
| 8 | +# - GITHUB_TOKEN: A valid personal access token with Organzation admin priviliges |
| 9 | +# - GITHUB_API_ENDPOINT: A valid GitHub/GitHub Enterprise API endpoint URL |
| 10 | +# (use http://api.github.com for GitHub.com auditing) |
| 11 | +# |
| 12 | +# Requires the Octokit Rubygem: https://github.com/octokit/octokit.rb |
| 13 | +# Requires the axlsx Rubygem: https://github.com/randym/axlsx |
| 14 | + |
| 15 | +require 'octokit.rb' |
| 16 | +require 'axlsx' |
| 17 | + |
| 18 | +begin |
| 19 | + ACCESS_TOKEN = ENV.fetch("GITHUB_TOKEN") |
| 20 | + API_ENDPOINT = ENV.fetch("GITHUB_API_ENDPOINT") |
| 21 | +rescue KeyError |
| 22 | + $stderr.puts "To run this script, please set the following environment variables:" |
| 23 | + $stderr.puts "- GITHUB_TOKEN: A valid personal access token with Organzation admin priviliges" |
| 24 | + $stderr.puts "- GITHUB_API_ENDPOINT: A valid GitHub/GitHub Enterprise API endpoint URL" |
| 25 | + $stderr.puts " (use http://api.github.com for GitHub.com auditing)" |
| 26 | + exit 1 |
| 27 | +end |
| 28 | + |
| 29 | +Octokit.configure do |kit| |
| 30 | + kit.api_endpoint = API_ENDPOINT |
| 31 | + kit.access_token = ACCESS_TOKEN |
| 32 | + kit.auto_paginate = true |
| 33 | +end |
| 34 | + |
| 35 | +client = Octokit::Client.new |
| 36 | + |
| 37 | +Axlsx::Package.new do |p| |
| 38 | + client.organizations.each do |org| |
| 39 | + p.workbook.add_worksheet(:name => org[:login]) do |sheet| |
| 40 | + sheet.add_row %w{Organization Team Repo User Access} |
| 41 | + client.organization_teams(org[:login]).each do |team| |
| 42 | + client.team_repos(team[:id]).each do |repo| |
| 43 | + client.team_members(team[:id]).each do |user| |
| 44 | + sheet.add_row [org[:login], team[:name], repo[:name], user[:login], team[:permission]] |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + p.use_shared_strings = true |
| 51 | + p.serialize("#{Time.now.strftime "%Y-%m-%d"}-audit.xlsx") |
| 52 | +end |
0 commit comments