Skip to content

Instantly share code, notes, and snippets.

@jdennes

jdennes/Gemfile

Last active Jul 4, 2021
Embed
What would you like to do?
Export a list of members from a GitHub organisation

Usage:

Get set up:

$ git clone https://gist.github.com/11404512.git export-members; cd export-members 

Bundle (we're going to use Octokit):

$ bundle install

Replace <org-username> with the username of your organization in export-all-members.rb and/or export-all-members-with-2fa-disabled.rb.

Then export all members:

$ OCTOKIT_ACCESS_TOKEN=<yourtoken> bundle exec ruby export-all-members.rb
$ cat export-all.csv

or, export members with 2FA disabled:

$ OCTOKIT_ACCESS_TOKEN=<yourtoken> bundle exec ruby export-all-members-with-2fa-disabled.rb
$ cat export-2fa-disabled.csv
# Set OCTOKIT_ACCESS_TOKEN to authenticate
require "octokit"
Octokit.auto_paginate = true
members = Octokit.org_members "<org-username>", :filter => "2fa_disabled"
# Then, for example:
require "csv"
CSV.open("export-2fa-disabled.csv", "wb") do |csv|
members.each do |m|
csv << [m[:id], m[:login]] # etc
end
end
# Set OCTOKIT_ACCESS_TOKEN to authenticate
require "octokit"
Octokit.auto_paginate = true
members = Octokit.org_members "<org-username>"
# Then, for example:
require "csv"
CSV.open("export-all.csv", "wb") do |csv|
members.each do |m|
csv << [m[:id], m[:login]] # etc
end
end
source "https://rubygems.org"
gem "octokit"
@rjmoral

This comment has been minimized.

Copy link

@rjmoral rjmoral commented Jun 18, 2018

Hi, good contribution, but i can't bring the field "name", with m [: name], do you have any other option? Thank you.

@robwoo

This comment has been minimized.

Copy link

@robwoo robwoo commented Sep 6, 2018

Thanks for adding this. Do you know of a way to include the email addresses of users in the list?

@pkolyvas

This comment has been minimized.

Copy link

@pkolyvas pkolyvas commented Oct 16, 2018

Hi, good contribution, but i can't bring the field "name", with m [: name], do you have any other option? Thank you.

This will use the login id pulled from the member array to retrieve the full name, if available:

CSV.open("export-all.csv", "wb") do |csv|
  members.each do |m|
    user = Octokit.user m[:login]
    csv << [m[:id], m[:login], m[:html_url], user.name] # etc
  end
end
@pkolyvas

This comment has been minimized.

Copy link

@pkolyvas pkolyvas commented Oct 17, 2018

Thanks for adding this. Do you know of a way to include the email addresses of users in the list?

If you see my answer about including the name, once you retrieve the user array you can access the email address in the same way:

CSV.open("export-all.csv", "wb") do |csv|
  members.each do |m|
    user = Octokit.user m[:login]
    csv << [m[:id], m[:login], m[:html_url], user.name, user.email] # etc
  end
end

You can access anything in the user array using this method: https://developer.github.com/v3/users/

@J-cortes

This comment has been minimized.

Copy link

@J-cortes J-cortes commented Nov 27, 2018

just a quick question to follow up on this thread- is it possible to include repos and events arrays to this export?

-something like
[m[:id], m[:login], user.fields, repo.fields, events.fields] such that it's centric on the user.

@rtlettcrownpeak

This comment has been minimized.

Copy link

@rtlettcrownpeak rtlettcrownpeak commented Jan 21, 2019

Was there ever an update on the last request by J-cortes for adding in repo information?

@J-cortes

This comment has been minimized.

Copy link

@J-cortes J-cortes commented May 31, 2019

@rtlettcrownpeak

just saw this, it would be something like this-

CSV.open("github_users.csv", "wb") do |csv|
members.each do |m|
user = Octokit.user m[:login]
csv << [m[:id], m[:login], m[:html_url], user.name, user.email, user.public_repos, user.private_repos, user.collaborators] # etc
end
end

@RootTJNII

This comment has been minimized.

Copy link

@RootTJNII RootTJNII commented Dec 11, 2019

Note that the token must have read:org rights to work properly, without it the Octokit members call will only return public users.

@rollax

This comment has been minimized.

Copy link

@rollax rollax commented Jul 4, 2021

@jdennes That is great. I'd like to ask if there were any way of using it for multiple organizations at once? or any automation way?

Thank you sir!

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