7

For now I am trying this API (just randomly picked an id from a project)

https://gitlab.com/api/v4/projects/3199253/repository/contributors

And I find that the commit number is always 1 while it is more than 1 on the contributor page. Meanwhile, the list is not completed and a lot of people are not on the returned result.

I checked the documentation and it doesn't seem like I need to paging it or I have the option to do so.

https://docs.gitlab.com/ee/api/repositories.html#contributors

Please let me know if there's any better way to count all commits of a user on GitLab. Thanks in advance!

UPDATE: Just found a related issue on Gitlab here:

https://gitlab.com/gitlab-org/gitlab/-/issues/233119

Looks like the commit count will always be 1 due to a bug at the moment?

UPDATE: For now I am scanning the list of commits and matching them with the current user with a for loop (they said it has better performance than map()). Guess that would consume unnecessary API call usage.

Terry Windwalker
  • 1,343
  • 2
  • 16
  • 36

1 Answers1

1

Here's how I've managed to get it :

  1. Download the python-gitlab package

    python3 -m pip install python-gitlab
    
  2. Update the following script for the targetusername and a valid private token. Then run it :

    import gitlab
    
    username = "targetusername"
    git_url = "https://gitlab.com/"
    gl = gitlab.Gitlab(git_url, "***********")
    
    projects = gl.projects.list()
    all_projects = gl.projects.list(all=True)
    nb_projects = len(all_projects)
    
    # Get the user ID by username
    user = gl.users.list(username=username)[0]
    user_name = user.name
    user_id = user.id
    print(f"Checking for user {username} (#{user_id})")
    
    total_commits = 0
    i = 0
    while i < nb_projects:
        project = gl.projects.get(all_projects[i].id)
        print(f"Checking project {i}/{nb_projects} : {project.name}...")
        # Filter commits by author ID
        default_branch = project.default_branch
        project_branches = project.branches.list(get_all=True)
    
        for branch in project_branches:
            branch_name = branch.name
            branch_commits = project.commits.list(ref_name=branch.name, get_all=True)
            for commit in branch_commits:
                total_commits += (
                    1
                    if commit.author_name == user_name or commit.author_name == username
                    else 0
                )
    
        i += 1
    
    print(f"Total number of commits for {username} : {total_commits}")
    
reepy
  • 18
  • 3
secavfr
  • 628
  • 1
  • 9
  • 24