这个脚本使用 GitLab API 来获取数据。以下是一个 Python 脚本,可以实现拉取 GitLab 所有项目所有分支所有人的代码总数、提交数、删除数:

import requests

# GitLab API endpoint
url = 'https://your-gitlab-instance.com/api/v4'

# Personal access token for GitLab API authentication
token = 'your-personal-access-token'

# Get all projects
response = requests.get(f'{url}/projects', headers={'Private-Token': token})
projects = response.json()

# Loop through all projects and get all branches
for project in projects:
    project_id = project['id']
    response = requests.get(f'{url}/projects/{project_id}/repository/branches',
                            headers={'Private-Token': token})
    branches = response.json()

    # Loop through all branches and get all commits
    for branch in branches:
        branch_name = branch['name']
        response = requests.get(
            f'{url}/projects/{project_id}/repository/commits?ref_name={branch_name}&per_page=100',
            headers={'Private-Token': token})
        commits = response.json()

        # Loop through all commits and get the statistics
        for commit in commits:
            response = requests.get(
                f'{url}/projects/{project_id}/repository/commits/{commit['id']}/stats',
                headers={'Private-Token': token})
            stats = response.json()

            # Print the statistics for each commit
            print(f'Project: {project['name']}, Branch: {branch_name}, '
                  f'Author: {commit['author_name']}, '
                  f'Total: {stats['total']}, Adds: {stats['additions']}, '
                  f'Dels: {stats['deletions']}')

注意,这个脚本需要替换 GitLab API 的 endpoint 和 personal access token,并且需要安装 requests 库。

另外,由于 GitLab API 限制,每个 API 请求最多只能返回 100 条数据。所以,为了获取所有的提交,我们需要使用分页参数 pageper_page。在上面的脚本中,我们把 per_page 设置为 100,以获取每个分支的前 100 个提交。如果你的项目的提交数超过 100,你需要通过多次请求来获取所有的提交。

Python 脚本:获取 GitLab 项目所有分支所有人的代码统计数据

原文地址: https://www.cveoy.top/t/topic/loEf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录