in GitHub api how to get commit done by a particular user in a repo

Viewed 201

I am using github api to get a users commits only those commits which are done by that user not the total commit of the repo only the commits which are done by that user. right now i am using this

code

def total_commit_user_func(repo_name, username):
    commit_by_user = 0
    try:
        url = "https://api.github.com/repos/"+username+"/"+repo_name+"/commits"
        page_no = 1
        while (True):
            response = requests.get(url, headers=headers, params={
                                    "author": username, "per_page": 100})
            response = response.json()
            commit_by_user = len(response)+commit_by_user
            commit_by_user_fetched = len(response)
            if (commit_by_user_fetched == 100):
                page_no = page_no + 1
                url = url + '?page=' + str(page_no)
            else:
                break

        return commit_by_user
    except:
        print("ERROR while getting commits by user")
        return commit_by_user

I am passing params as author but it is not working and in some repos it is giving an empty array and sometime it is giving me right response.

for example int his repo all the commits are done by me and it should be me an array whcih have data but is giving me an empty array https://api.github.com/repos/imvsr-0609/chatbud/commits?author=imvsr-0609

and on this i am getting the data. https://api.github.com/repos/imvsr-0609/Music-Player/commits?author=imvsr-0609

why this is happening and why in some repos i am getting empty arrays. and let me know if anyone has other idea of fetching commits of repo for a particular user only

1 Answers
Related