如何获取GitLab上所有存储库的用户总提交数?

Ter*_*ker 7 gitlab gitlab-api

现在我正在尝试这个API(只是从项目中随机选择一个id)

https://gitlab.com/api/v4/projects/3199253/repository/contributors
Run Code Online (Sandbox Code Playgroud)

我发现提交数始终为 1,而贡献者页面上的提交数则大于 1。同时,名单尚未完成,很多人不在返回结果中。

我检查了文档,似乎我不需要对其进行分页,或者我可以选择这样做。

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

如果有更好的方法来统计用户在 GitLab 上的所有提交,请告诉我。提前致谢!

更新:刚刚在 Gitlab 上发现了一个相关问题:

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

看起来由于目前的错误,提交计数将始终为 1?

更新:现在我正在扫描提交列表,并使用 for 循环将它们与当前用户进行匹配(他们说它比 map() 具有更好的性能)。猜测这会消耗不必要的 API 调用使用量。

sec*_*vfr 1

这是我设法得到它的方法:

  1. 下载python-gitlab 包

    python3 -m pip install python-gitlab
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为目标用户名和有效的私有令牌更新以下脚本。然后运行它:

    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}")
    
    Run Code Online (Sandbox Code Playgroud)