Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active September 22, 2022 16:31
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • 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"
@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