【问题标题】:Getting Github individual file contributors获取 Github 个人文件贡献者
【发布时间】:2012-11-06 00:22:10
【问题描述】:

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

Github 内部有这个功能

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

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

【问题讨论】:

    标签: python github python-sphinx github-api


    【解决方案1】:

    首先,您可以show the commits for a given file

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

    例如:

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

    其次,该 JSON 响应在作者部分包含一个名为“html_url”的 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"
        },
    

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


    这里有一个very crude 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);
            }
        });
    

    【讨论】:

    • 一如既往,很高兴阅读您的回答,冯。全面且切中要害。
    • 谢谢。这正是我一直在寻找的。在发放赏金之前,我会看看是否有其他答案。
    • 好的结果可以在这里看到:github.com/miohtama/sphinxcontrib.contributors :)
    • @MikkoOhtamaa 那。看起来。太棒了!
    • 小心点。 Github 仅返回最后 30 次提交。更多内容需要使用分页。
    【解决方案2】:

    使用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
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Try it in the explorer

    使用 获得此文件的前 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'
    

    【讨论】:

    • 使用更现代的 GraphQL 的有趣更新。 +1
    【解决方案3】:

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

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

    【讨论】:

    • 请注意,提交者的电子邮件是不够的,必须能够将它们映射到 Github 用户配置文件链接。
    • 添加另一个映射电子邮件的层(类似于.mailmap)并不难 -> github 用户。
    • @MikkoOhtamaa 你可以在 GitHub 中搜索邮箱地址。
    • 你将如何从 Github 获取这些信息?这就是问题
    • 电子邮件搜索不可行:添加此 API 调用仅出于兼容性原因。无法保证始终提供完整的电子邮件搜索。地址中的@ 字符必须保持未编码。仅搜索公共电子邮件地址(在用户的 GitHub 个人资料中配置)。
    【解决方案4】:

    除非没有必要直接与 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 2018-09-24
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      相关资源
      最近更新 更多