nmishin
nmishin

Reputation: 3064

How to get all users in organization in GitHub?

I try to get all users in my organization in GitHub. I can get users, but I have problem with pagination - I don't know how many page I have to get around.

curl -i -s -u "user:pass" 'https://api.github.com/orgs/:org/members?page=1&per_page=100'

Of course I can iterate all the pages until my request will not return "0", but i think this is not very good idea )

Maybe GitHub have standard method for get all users in organization?

Upvotes: 6

Views: 12911

Answers (4)

nmishin
nmishin

Reputation: 3064

You can use the PyGithub Python library implementing the GitHub API v3 https://pygithub.readthedocs.io/en/latest/

g = Github("ghp_your-github-token")
for member in g.get_organization("my-org").get_members():
    print(member.login, member.name, member.email)

Upvotes: 0

Md Abrar
Md Abrar

Reputation: 111

If you are using Python and requests library then get the header response and split it like below to get the last page number

last_page_num = int(r.headers["link"].split(",")[-1].split('&page=')[-1][0])

Upvotes: 0

Dorian
Dorian

Reputation: 23999

Here is my github-users script for instance:

#!/usr/bin/env ruby

require 'octokit'

Octokit.configure do |c|
  c.login = '....'
  c.password = '...'
end

get = Octokit.org(ARGV.first).rels[:public_members].get
members = get.data
urls = members.map(&:url)

while members.size > 0
  next_url = get.rels[:next]
  next members = [] unless next_url
  get = next_url.get
  members = get.data
  urls << members.map(&:url)
end

puts urls

e.g. github-members stackexchange gives:

https://api.github.com/users/JasonPunyon
https://api.github.com/users/JonHMChan
https://api.github.com/users/NickCraver
https://api.github.com/users/NickLarsen
https://api.github.com/users/PeterGrace
https://api.github.com/users/bretcope
https://api.github.com/users/captncraig
https://api.github.com/users/df07
https://api.github.com/users/dixon
https://api.github.com/users/gdalgas
https://api.github.com/users/haneytron
https://api.github.com/users/jc4p
https://api.github.com/users/kevin-montrose
https://api.github.com/users/kirtithorat
https://api.github.com/users/kylebrandt
https://api.github.com/users/momow
https://api.github.com/users/ocoster-se
https://api.github.com/users/robertaarcoverde
https://api.github.com/users/rossipedia
https://api.github.com/users/shanemadden
https://api.github.com/users/sklivvz

Upvotes: 1

Jean Hominal
Jean Hominal

Reputation: 16796

According to Traversing with Pagination, there should be a Link response header, such as:

Link: <https://api.github.com/orgs/:org/members?page=2>; rel="next", <https://api.github.com/orgs/:org/members?page=3>; rel="last"

These headers should give you all the information needed so that you can continue getting pages.

For performance reasons, I do not think that any API exists to bypass pagination.

Upvotes: 4

Related Questions