Skip to content

Instantly share code, notes, and snippets.

@noahd1
Last active December 16, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahd1/1d8f00bcae8b68d93f15b2ec2d77cff2 to your computer and use it in GitHub Desktop.
Save noahd1/1d8f00bcae8b68d93f15b2ec2d77cff2 to your computer and use it in GitHub Desktop.
Export GitHub Team Memberships

Description

A docker image which can be used to export an organization's team memberships to a CSV file.

The CSV file will be written to a data subfolder, and lists, for each member:

  • The team id
  • The team name
  • The team slug
  • The login id
  • The login username

Setup:

$ git clone https://gist.github.com/1d8f00bcae8b68d93f15b2ec2d77cff2.git export-github-memberships; cd export-github-memberships

Make the docker image

$ make

Copy the example environment file which will hold your GitHub access token:

$ cp env.example env

As a user with administrative privileges in GitHub, go to Settings => Developer settings => Personal access tokens, and create a personal access token with read:org permissions.

Copy the token after the equals sign in your env file.

You're ready.

Running

make run

You'll be prompted for your GitHub organization slug, after which a timestamped CSV will be written to a data subfolder.

FROM ruby:2.1
RUN mkdir /app
RUN mkdir /app/bin
WORKDIR /app
ENV PATH=/app/bin:${PATH}
ADD Gemfile Gemfile.lock /app/
RUN bundle install -j 8
ADD export-team-memberships.rb /app/bin/
OCTOKIT_ACCESS_TOKEN=
#!/usr/local/bin/ruby
require "octokit"
require "csv"
require "time"
print "GitHub.com organization slug: "
organization_slug = STDIN.gets.strip
Octokit.auto_paginate = true
teams = Octokit.org_teams organization_slug
filename = "/data/team-members-#{Time.now.strftime("%m-%d_%H-%M-%S")}.csv"
CSV.open(filename, "wb") do |csv|
puts "Exporting #{teams.size} teams."
teams.each_with_index do |team, index|
puts "Exporting #{index + 1} of #{teams.size}"
members = Octokit.team_members team[:id]
members.each do |m|
if m[:type] == "User"
csv << [team[:id], team[:name], team[:slug], m[:id], m[:login]]
else
raise "Unknown / unhandled member type of #{m[:type]}"
end
end
end
end
puts "Export completed to #{filename}"
source "https://rubygems.org"
gem "octokit"
GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
multipart-post (2.1.1)
octokit (4.14.0)
sawyer (~> 0.8.0, >= 0.5.3)
public_suffix (3.0.3)
sawyer (0.8.2)
addressable (>= 2.3.5)
faraday (> 0.8, < 2.0)
PLATFORMS
ruby
DEPENDENCIES
octokit
BUNDLED WITH
1.16.1
.PHONY: image
IMAGE_NAME = codeclimate/export-team-memberships
image:
docker build --rm -t $(IMAGE_NAME) .
run:
docker run -it -v `pwd`/data:/data --env-file env $(IMAGE_NAME) bin/export-team-memberships.rb
@beaulebens
Copy link

This worked flawlessly, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment