Salil Surendran
Salil Surendran

Reputation: 2335

Find first and last name of a user in a github repo

I have an organization in Github.com and there are several users making contributions to my organization's repo. I would like to know the first and last name of the users in my organization. Any github api that does this?

Upvotes: 1

Views: 2323

Answers (1)

Ionică Bizău
Ionică Bizău

Reputation: 113365

You should combine the Contributors endpoint with the Users endpoint from GitHub API, since the contributors endpoint does not expose the name field in the response.

List contributors

List contributors to the specified repository, sorted by the number of commits per contributor in descending order.

So, let's suppose your repository is located at owner/repo, you will make a GET request to this url:

https://api.github.com/repos/owner/repo/contributors

For example, this will fetch the contributors in my git-stats project:

https://api.github.com/repos/IonicaBizau/git-stats/contributors

This will give you a reponse like this:

[
  {
    "login": "IonicaBizau",
    "id": 2864371,
    "avatar_url": "https://avatars.githubusercontent.com/u/2864371?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/IonicaBizau",
    "html_url": "https://github.com/IonicaBizau",
    "followers_url": "https://api.github.com/users/IonicaBizau/followers",
    "following_url": "https://api.github.com/users/IonicaBizau/following{/other_user}",
    "gists_url": "https://api.github.com/users/IonicaBizau/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/IonicaBizau/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/IonicaBizau/subscriptions",
    "organizations_url": "https://api.github.com/users/IonicaBizau/orgs",
    "repos_url": "https://api.github.com/users/IonicaBizau/repos",
    "events_url": "https://api.github.com/users/IonicaBizau/events{/privacy}",
    "received_events_url": "https://api.github.com/users/IonicaBizau/received_events",
    "type": "User",
    "site_admin": false,
    "contributions": 238
  },
  ...
]

As you can see, the full name is not exposed here. To get the user full name, you have to make additional requests for each contributor, using the Users endpoint. For example, for IonicaBizau you will make a request to:

https://api.github.com/users/IonicaBizau

The response object has a name field which contains the full name of the user.

Example:

{
  "login": "IonicaBizau",
  "id": 2864371,
  "avatar_url": "https://avatars.githubusercontent.com/u/2864371?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/IonicaBizau",
  "html_url": "https://github.com/IonicaBizau",
  "followers_url": "https://api.github.com/users/IonicaBizau/followers",
  "following_url": "https://api.github.com/users/IonicaBizau/following{/other_user}",
  "gists_url": "https://api.github.com/users/IonicaBizau/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/IonicaBizau/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/IonicaBizau/subscriptions",
  "organizations_url": "https://api.github.com/users/IonicaBizau/orgs",
  "repos_url": "https://api.github.com/users/IonicaBizau/repos",
  "events_url": "https://api.github.com/users/IonicaBizau/events{/privacy}",
  "received_events_url": "https://api.github.com/users/IonicaBizau/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Ionică Bizău",
  "company": "jillix",
  "blog": "http://ionicabizau.net",
  "location": "Romania",
  "email": "[email protected]",
  "hireable": true,
  "bio": null,
  "public_repos": 223,
  "public_gists": 1,
  "followers": 619,
  "following": 77,
  "created_at": "2012-11-22T15:51:02Z",
  "updated_at": "2015-10-14T09:43:43Z"
}

So, summarizing, to get the users' full names for a given repository like owner/repo you have to:

 GET https://api.github.com/repos/owner/repo/contributors
 foreach contributor in contributors
    GET https://api.github.com/users/<contributor.login>
      // Do something with <user.name>

Upvotes: 3

Related Questions