获得Github个人文件贡献者

时间:2012-11-17 12:55:06

标签: python github python-sphinx github-api

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

Github内部有此功能

Contributors

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

  • 如果无法做到这一点,那么您可以建议从Github提取哪些替代方法(私有API,抓取)?

4 个答案:

答案 0 :(得分:11)

首先,您可以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);
        }
    });

get Contributors from a GiHub file

答案 1 :(得分: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'

答案 2 :(得分:1)

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

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

答案 3 :(得分: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
相关问题