获得Github个人文件贡献者

Mik*_*maa 10 python github github-api python-sphinx

我打算为Sphinx文档系统插件构建一个插件,该插件显示了对文档页面做出贡献的人员的姓名和Github配置文件链接.

Github内部有这个功能

贡献者

  • 是否可以通过Github API获取文件贡献者的Github配置文件链接?请注意,提交者电子邮件是不够的,必须能够将它们映射到Github用户配置文件链接.另请注意,我不希望所有存储库贡献者 - 只是单个文件贡献者.

  • 如果这是不可能的,那么您可以建议从Github提取这些信息的替代方法(私有API,抓取)?

Von*_*onC 12

首先,您可以显示给定文件的提交:

https://api.github.com/repos/:owner/:repo/commits?path=PATH_TO_FILE
Run Code Online (Sandbox Code Playgroud)

例如:

https://api.github.com/repos/git/git/commits?path=README

其次,JSON响应在作者部分中包含一个名为' html_url'到GitHub配置文件的url 文件:

"author": {
      "login": "gitster",
      "id": 54884,
      "avatar_url": "https://0.gravatar.com/avatar/750680c9dcc7d0be3ca83464a0da49d8?d=https%3A%2F%2Fidenticons.github.com%2Ff8e73a1fe6b3a5565851969c2cb234a7.png",
      "gravatar_id": "750680c9dcc7d0be3ca83464a0da49d8",
      "url": "https://api.github.com/users/gitster",   
      "html_url": "https://github.com/gitster",       <==========
      "followers_url": "https://api.github.com/users/gitster/followers",
      "following_url": "https://api.github.com/users/gitster/following{/other_user}",
      "gists_url": "https://api.github.com/users/gitster/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/gitster/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/gitster/subscriptions",
      "organizations_url": "https://api.github.com/users/gitster/orgs",
      "repos_url": "https://api.github.com/users/gitster/repos",
      "events_url": "https://api.github.com/users/gitster/events{/privacy}",
      "received_events_url": "https://api.github.com/users/gitster/received_events",
      "type": "User"
    },
Run Code Online (Sandbox Code Playgroud)

所以你不需要在这里刮掉任何网页.


这是一个非常粗略的jsfiddle来说明,基于javascript提取:

var url = "https://api.github.com/repos/git/git/commits?path=" + filename
$.getJSON(url, function(data) {
    var twitterList = $("<ul />");
    $.each(data, function(index, item) {
        if(item.author) {
            $("<li />", {
                "text": item.author.html_url
            }).appendTo(twitterList);
        }
    });
Run Code Online (Sandbox Code Playgroud)

从GiHub文件中获取贡献者


Ber*_*tel 6

使用GraphQL API v4,您可以使用:

{
  repository(owner: "torvalds", name: "linux") {
    object(expression: "master") {
      ... on Commit {
        history(first: 100, path: "MAINTAINERS") {
          nodes {
            author {
              email
              name
              user {
                email
                name
                avatarUrl
                login
                url
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在资源管理器中尝试

使用来列出这个文件的前 100 名贡献者,没有重复:

TOKEN=<YOUR_TOKEN>
OWNER=torvalds
REPO=linux
BRANCH=master
FILEPATH=MAINTAINERS
curl -s -H "Authorization: token $TOKEN" \
     -H  "Content-Type:application/json" \
     -d '{ 
          "query": "{repository(owner: \"'"$OWNER"'\", name: \"'"$REPO"'\") {object(expression: \"'"$BRANCH"'\") { ... on Commit { history(first: 100, path: \"'"$FILEPATH"'\") { nodes { author { email name user { email name avatarUrl login url}}}}}}}}"
      }' https://api.github.com/graphql | \
      jq '[.data.repository.object.history.nodes[].author| {name,email}]|unique'
Run Code Online (Sandbox Code Playgroud)