Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active September 22, 2022 16:31
Show Gist options
  • Save jdennes/11404512 to your computer and use it in GitHub Desktop.
Save jdennes/11404512 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@J-cortes
Copy link

@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
Copy link

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

@rollax
Copy link

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!

@ITsupportAmagi
Copy link

How to get the OCTOKIT_ACCESS_TOKEN ?

@ITsupportAmagi
Copy link

I got the OCTOKIT_ACCESS_TOKEN, by generating it as:

  1. https://github.com/settings/tokens
  2. Generate New Token, follow the screen and grant required permissions, authorize the tokens

@ITsupportAmagi
Copy link

I get error while I execute:
OCTOKIT_ACCESS_TOKEN=<my-token> bundle exec ruby export-all-members.rb

To use retry middleware with Faraday v2.0+, install `faraday-retry` gem
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/response/raise_error.rb:14:in `on_complete': GET https://api.github.com/orgs/myuserid/members?per_page=100: 404 - Not Found // See: https://docs.github.com/rest/reference/orgs#list-organization-members (Octokit::NotFound)
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/middleware.rb:18:in `block in call'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/response.rb:42:in `on_complete'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/middleware.rb:17:in `call'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/middleware/follow_redirects.rb:73:in `perform_with_redirection'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/middleware/follow_redirects.rb:61:in `call'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/rack_builder.rb:153:in `build_response'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/connection.rb:445:in `run_request'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/faraday-2.5.2/lib/faraday/connection.rb:200:in `get'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/sawyer-0.9.2/lib/sawyer/agent.rb:99:in `call'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/connection.rb:156:in `request'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/connection.rb:84:in `paginate'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit/client/organizations.rb:147:in `organization_members'
        from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/octokit-5.6.1/lib/octokit.rb:54:in `method_missing'
        from export-all-members.rb:6:in `<main>'

Any help is appreciated

@ITsupportAmagi
Copy link

BTW our ORG / Enterprise is SAML enabled & Enforced ... is that why its erroring out ?

@ITsupportAmagi
Copy link

Please help me

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