12

我计划为 Sphinx 文档系统插件构建一个插件,该插件显示为文档页面做出贡献的人员的姓名和 Github 个人资料链接。

Github 内部有这个功能

贡献者

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

  • 如果这不可能,那么您可以建议使用哪种替代方法(私有 API、抓取)从 Github 中提取此信息?

4

4 回答 4

17

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

https://api.github.com/repos/:owner/:repo/commits?path=PATH_TO_FILE

例如:

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

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

"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"
    },

所以你不需要在这里抓取任何网页。


这是一个非常粗略的 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);
        }
    });

从 GiHub 文件中获取贡献者

于 2013-10-05T16:57:01.787 回答
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
              }
            }
          }
        }
      }
    }
  }
}

在资源管理器中尝试

使用 & 获得此文件的前 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'
于 2018-12-28T01:59:53.167 回答
1

为什么你需要为此使用 Github API?您可以克隆包并使用git log

git log --format=format:%an path/to/file ver1..ver2 |sort |uniq

于 2012-11-17T13:13:35.453 回答
0

除非不需要直接与 GITHUB API 交互,否则可以通过将 repo 克隆下来,然后进入克隆目录,然后使用 shortlog 命令从 github 日志文件中获取列表来获取贡献者列表

import os 
import commands 

cmd = "git shortlog -s -n"

os.chdir("C:\Users\DhruvOhri\Documents\COMP 6411\pygithub3-0.3")
os.system("git clone https://github.com/poise/python.git")
os.chdir("/home/d/d_ohri/Desktop/python")
output = commands.getoutput(cmd) 
print(output)
raw_input("press enter to continue")

如果想要使用 GITHUB API,还有另一种列出贡献者的方法,我们可以使用 pytgithub3 包装器与 GITHUB API 交互并使用 list_contributors 获取贡献者列表,如下所示:

from pytgithub3.services.repo import Repo
r = Repo()
r.lis_contributors(user='userid/author',repo='repo name')
for page in r:
    for result in page:
          print result
于 2015-01-30T02:54:18.357 回答