gitgithubgithub-apigithub-api-v3

Github api to get all the commits done to given repository branch


I am trying to get all the commits given to develop branch of repository test using github api but I am always getting first page no matter how i pass the query params. Not sure what's wrong in the request I am sending.

curl -u username:$token -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/ORGANIZATION_NAME/test/commits/develop

I am always getting the first page. I have tried using the query params given here


Solution

  • It looks like you are accessing the wrong endpoint. The one you are sending to is this one, where you are only getting one commit (in your case the latest develop commit).

    You need to implement some pagination logic to your app and access this endpoint: /repos/{owner}/{repo}/commits.

    So for instance, to get the commits of your development branch, you would send such a request:

    curl -H "Authorization: XYZ" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/ACCNAME/REPO/commits\?sha\=develop
    

    (if you are using the octokit, it would be prettier, but this is just an example :))

    Now, in order for you to paginate, you would need to also query for the per_page and page parameters and check if you've hit the limit or not.

    Basically, with the following query:

    curl -H "Authorization: XYZ" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/ACCNAME/REPO/commits\?sha\=develop\&per_page\=X\&page\=Y
    

    Where X and Y are parameters you pass to the curl request. Once you get a response, you check if the length of the array (since you'll get an array of commits) is equal to X. If it is, do another request, but this time, increment Y+1, that way you'll get the next page. Loop through this process until the response you get is less than the required X, since that way you will know you would have gotten the last commits from your repo.


    An example in node (disclaimer, not properly written code)

    let allCommits = [];
    
    let call = function(n, y) {
        return new Promise((resolve, reject) => {
            getCommits(function(statusCode, result) {
                allCommits.push(result);
                if (result.length === y) {
                   call(n + 1, y);
                }
            }, n, y);
        });
    };
    

    If getCommits is your function to get a range of commits from a branch, with a path defined as path: '/repos/ACC_NAME/REPO/commits?sha=develop&page=' + n + '&per_page=' + y, then in the end you would have all the pages of commits in the allCommits array. You'll just have to flatten it out in the end, but someone with better js skill can provide a more elegant way to do this.