Search code examples
restgithubgithub-api

Github API get user code merged into master branch of specific repo


Just had a question regarding GitHub's REST api, I am looking through the documentation but cant seem to find what I am looking for. Is there a way to look up how much code a user has merged into the master branch over a period of time of a specific repo?

essentially I would like to get a return of

user x committed x lines of code in the range of May 2020 to May 2021. Something like that.


Solution

  • GITHUB API

    Unfortunately, the Github API doesn't provide those informations (yet?), you'll only get the amount of contributions the user made to the repository with the List repository contributors service.

    WORKAROUND

    There seems to be a workaround:

    Consulting this URL: https://github.com/<repo_owner>/<repo_name>/graphs/contributors, I observed the following requisition was made to fill contributors datas:

    • Request URL: https://github.com/<repo_owner>/<repo_name>/graphs/contributors-data
    • Request Method: GET
    • Request Headers: Not Sure...

    This endpoint will return something similar to the following object:

    [
      {
         "total":N,
         "author":{
            "id":<user_id>,
            "login":"<user_name>",
            "avatar":"<user_avatar>",
            "path":"/<user_name>",
            "hovercard_url":"/users/<user_name>/hovercard"
         },
         "weeks":[
            {
               "w":1586044800,
               "a":0,
               "d":0,
               "c":0
            },
            {
               "w":1586649600,
               "a":0,
               "d":0,
               "c":0
            },
            ...
         ]
       }
     ]
    

    On this response, you'll have a list of contributors with contributors' details, where:

    • total: represent the amount of contribution this user made to the repository.

    • author: the Github user datas.

    • weeks: a list of weekly contribution where for each week:

      • w: is the week id
      • a: the amount of lines added
      • d: the amount of lines deleted
      • c: the amount of commits made

    Therefore, to get what you want, you'll have to sum all weeks contributions for each user.

    Observation: I understand that to do so, you need to have access to the repository insights (they may not be available to any user).