透過 gitlab 介面獲取程式碼提交記錄和改動行數

jc發表於2020-11-16

之前是透過讀取 jenkins 的快取來獲取的,後面發現了個更好的方式,官方有 http 請求提供出來。無開始時間,只獲取前 10 條資料,有開始時間,獲取到最新的資料。不需要把程式碼拉下來,直接請求遠端 git。

get_git_all_url = 'http://git.xxxxxx.com/api/v4/projects/'
get_project_branch_url = get_git_all_url + '{}/repository/branches'
get_branch_commit_url = get_git_all_url + '{}/repository/commits'
get_commit_msg_url = get_git_all_url + '{}/repository/commits/{}'
base_token = {'private_token': 'xxxxx'}


def get_git_project_id(private_token, project_repo_url):
    """
    :param private_token: gitlab個人token,請在gitlab上生產
    :param project_repo_url: http://xxxx.git,需要帶上.git
    :return:
    """
    if all([private_token, project_repo_url]):
        params_dict = {'private_token': private_token, 'per_page': 100}
        for i in range(1, 101):
            params_dict['page'] = i
            response = requests.get(url=get_git_all_url, params=params_dict)
            print(response.text)
            if response.status_code == 200:
                msg_dict = json.loads(response.text)
                for j in range(len(msg_dict)):
                    msg = msg_dict[j]
                    if 'http_url_to_repo' in msg.keys() and msg['http_url_to_repo'] == project_repo_url:
                        if 'id' in msg.keys():
                            project_id = str(msg['id'])
                            print('查詢到該token{}名下對git地址{}的專案id為{}'.format(private_token, project_repo_url, project_id))
                            return project_id

    print('未查詢到該token{}名下git地址{}的專案id'.format(private_token, project_repo_url))
    return


def get_git_project_branchs(private_token, git_project_id):
    """

    :param private_token: gitlab個人token,請在gitlab上生產
    :param git_project_id: 字串型別,專案id
    :return:
    """

    branch_list = []
    if all([private_token, git_project_id]):
        param_dict = {'private_token': private_token, 'per_page': 100}
        for i in range(1, 101):
            param_dict['page'] = i
            response = requests.get(url=get_project_branch_url.format(git_project_id), params=param_dict)
            print(response.text)
            if len(response.text)<=2:
                break
            elif response.status_code == 200:
                msg_dict = json.loads(response.text)
                for j in range(len(msg_dict)):
                    if 'name' in msg_dict[j].keys():
                        if not msg_dict[j]['name']:
                            break
                        branch_list.append(msg_dict[j]['name'])
    print('該專案id:{}對應的所有分支:{}'.format(git_project_id,str(branch_list)))
    return branch_list


def get_branch_commit(private_token, git_project_id, branch_name, begin_time=None):
    """

    :param private_token: gitlab的token
    :param git_project_id: git專案id
    :param branch_name: 分支名稱
    :param begin_time: 從這開始之後的commit時間
    :return:
    """
    commit_list = []
    if all([private_token, git_project_id, branch_name]):
        para_dict = {'private_token': private_token, 'ref_name': branch_name}

        if not begin_time:
            para_dict['per_page'] = 10
            response = requests.get(url=get_branch_commit_url.format(git_project_id), params=para_dict)
            print(response.text)
            if response.status_code == 200:
                msg_dict = json.loads(response.text)
                for i in range(len(msg_dict)):
                    if 'id' in msg_dict[i].keys():
                        commit_id = msg_dict[i]['id']
                        commit_list.append(commit_id)
        else:
            para_dict['per_page'] = 100
            for i in range(1, 101):
                para_dict['page'] = i
                response = requests.get(url=get_branch_commit_url.format(git_project_id), params=para_dict)
                if response.status_code == 200:
                    msg_dict = json.loads(response.text)
                    for j in range(len(msg_dict)):
                        # 這裡有三個時間,不確定是哪個,先全部拿下來,暫時用committed_data_time
                        id = msg_dict[j]['id']
                        authored_data_time = msg_dict[j]['authored_date']
                        committed_data_time = msg_dict[j]['committed_date']
                        created_at_time = msg_dict[j]['created_at']
                        if compare_time(committed_data_time, begin_time):
                            commit_list.append(id)
    print('獲取到分支{}對應的commit內容{}'.format(branch_name,str(commit_list)))
    return commit_list


def get_commit_msg(private_token, git_project_id, commit_id):
    commit_msg_dict = {}
    if all([private_token, git_project_id, commit_id]):
        para_dict = {'private_token': private_token}
        response = requests.get(url=get_commit_msg_url.format(git_project_id, commit_id), params=para_dict)
        print(response.text)
        if response.status_code == 200:
            msg_dict = json.loads(response.text)
            commit_msg_dict['id'] = msg_dict['id']
            commit_msg_dict['title'] = msg_dict['title']
            if 'feat' in msg_dict['title'].lower():
                commit_msg_dict['git_type']='feat'
            elif 'fix' in msg_dict['title'].lower():
                commit_msg_dict['git_type'] = 'fix'
            elif 'merge' in msg_dict['title'].lower():
                commit_msg_dict['git_type'] = 'merge'
            else:
                commit_msg_dict['git_type'] = 'update'
            commit_msg_dict['message'] = msg_dict['message']
            commit_msg_dict['author_name'] = msg_dict['author_name']
            commit_msg_dict['committed_date'] = msg_dict['committed_date']
            commit_msg_dict['additions'] = msg_dict['stats']['additions']
            commit_msg_dict['deletions'] = msg_dict['stats']['deletions']
            commit_msg_dict['total'] = msg_dict['stats']['total']
    return commit_msg_dict


def get_gitProject_branch_commit_list(private_token, project_repo_url, branch, begin_time=None):
    commit_list = []
    try:
        git_project_id = get_git_project_id(private_token=private_token,
                                            project_repo_url=project_repo_url)
        if git_project_id:
            git_project_branch = get_git_project_branchs(private_token=private_token,
                                                         git_project_id=git_project_id)

            if git_project_branch and branch in git_project_branch:
                branch_commit_list = get_branch_commit(private_token=private_token,
                                                       git_project_id=git_project_id,
                                                       branch_name=branch,
                                                       begin_time=begin_time)

                if branch_commit_list:
                    for i in range(len(branch_commit_list)):
                        commit_msg_dict = get_commit_msg(private_token=private_token,
                                                         git_project_id=git_project_id,
                                                         commit_id=branch_commit_list[i])

                        if commit_msg_dict:
                            commit_list.append(commit_msg_dict)

            else:
                print('該分支{}不在遠端分支列表中{},或獲取到的遠端分支為空'.format(branch, git_project_branch))

        print('獲取到該專案地址{},分支{}的commit內容為{}'.format(project_repo_url,branch,commit_list))
    except Exception as e:
        print('獲取該專案地址{},分支{}的commit內容過程中發生異常'.format(project_repo_url,branch))
        print('異常資訊為:{}'.format(e))

    return commit_list

def compare_time(time1, time2_compare):
    if any(key in time1 for key in ['T','+08:']):
        time1=time1.replace('T',' ').replace('+08:','')
    d1 = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S.%f')
    d2 = datetime.datetime.strptime(time2_compare, '%Y-%m-%d %H:%M:%S.%f')
    delta = d1 - d2
    print(delta.total_seconds())
    if delta.total_seconds()>=0:
        return True
    else:
        return False


# get_gitProject_branch_commit_list(private_token='xxxxx',
#                                   project_repo_url='http://xxxxx.git',
#                                   branch='dev',
#                                   begin_time='2020-10-30 14:56:07.999999')

相關文章