11
votes

I am planning to build a plug-in for Sphinx documentation system plug-in which shows the names and Github profile links of the persons who have contributed to the documentation page.

Github has this feature internally

Contributors

  • Is it possible to get Github profile links of the file contributors through Github API? Note that commiter emails are not enough, one must be able to map them to a Github user profile link. Also note that I don't want all repository contributors - just individual file contributors.

  • If this is not possible then what kind of alternative methods (private API, scraping) you could suggest to extract this information from Github?

4

4 Answers

5
votes

Using GraphQL API v4, you can use :

{
  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

Using & to have a list of the first 100 contributors of this file without duplicates :

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'
15
votes

First, you can show the commits for a given file:

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

For instance:

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

Second, that JSON response does, in the author section, contain an url filed named 'html_url' to the GitHub profile:

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

So you shouldn't need to scrape any web page here.


Here is a very crude jsfiddle to illustrate that, based on the javascript extract:

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
votes

Why do you need to use Github API for that? You can just clone the package and use git log:

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

0
votes

Until and unless it is not necessary to interact with GITHUB API directly one can get the list of contributors by cloning the repo down and then getting into the cloned directory and then getting the list from the github log file using shortlog command

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")

There is one more way to list contributors in case one wants to use GITHUB API, we can use pytgithub3 wrapper to interact with GITHUB API and get list of contributors as follows using 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