在github仓库中查找用户的名字和姓氏

时间:2015-10-22 23:38:54

标签: json github-api

我在Github.com有一个组织,有几个用户为我的组织回购做出了贡献。我想知道组织中用户的名字和姓氏。这样做的任何github api?

1 个答案:

答案 0 :(得分:0)

您应该将Contributors endpoint与来自GitHub API的the Users endpoint结合使用,因为贡献者端点会在响应中公开name字段。

  

列出贡献者

     

列出指定存储库的贡献者,按降序排列每个贡献者的提交数量。

因此,假设您的存储库位于owner/repo,您将向此网址发出GET个请求:

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

例如,这将获取我git-stats项目中的贡献者:

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

这会给你一个这样的反应:

[
  {
    "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
  },
  ...
]

如您所见,此处的全名。要获取用户的全名,您必须使用the Users endpoint为每个参与者提出其他请求。例如,对于IonicaBizau,您将发出请求:

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

响应对象有一个name字段,其中包含用户的全名。

示例:

{
  "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": "contact@ionicabizau.net",
  "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"
}

总而言之,为了吸引用户'您需要owner/repo给定存储库的全名:

 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>