爬取gerrit代码提交信息

#! -*- encoding:utf-8 -*-

import time, datetime
import re,json
import requests


headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
        }

data = {
        "username":"account",
        "password":"test001"
        }

login_url = "htttp://***gerrit.com/login/%23%2Fq%sFstaus%3Amerged"

session = requests.Session()
resp = session.post(login_url, data)
GerritAccount_Cookie = session.cookies.get_dict()["GerritAccout"]
headers["Cookie"] = "GerritAccount=" + GerritAccount_Cookie

def commitRevison(url, num):

    dataUrl = url + "/changes/" + num + "/detail?O=5004"

    data = requests.get(dataUrl, headers = headers).text

    remove = re.compile("\)\]\}\'")
    data = data.replace(')]}\'',"")
    data = re.sub(remove,"",data)
    data_json = json.loads(data)
    reversion = data_json["current_revision"]
    return reversion

def commitFiles(url, num, revison):
    dataUrl = url + "/changes/" + num + "/revisons/" + revison + "/files"

    data = requests.get(dataUrl, headers=headers).text

    remove = re.compile("\)\]\}\'")
    data = data.replace(')]}\'', "")
    data = re.sub(remove, "", data)
    data_json = json.loads(data)
    data_json.pop("/COMMIT_MGS")
    Files = data_json.keys()
    return Files

def commit_lists(url,branch):
    dataUrl = url + "/changes/?q=status:merged&n=25&O=81"

    data = requests.get(dataUrl, headers=headers).text

    remove = re.compile("\)\]\}\'")
    data = data.replace(')]}\'', "")
    data = re.sub(remove, "", data)
    data_json = json.loads(data)

    now =datetime.datetime.now()
    one_day_ago = now + datetime.timedelta(days=-1)#一天前的时间
    commit_information = []
    for i in data_json:
        if i["branch"] == branch:
            timestring = i["submitted"].split(".")[0]
            timestamp = time.mktime(timestring,"%Y-%m-%d %H:%M:%S")
            datetime_sruct = datetime.datetime.fromtimestamp(timestamp) + datetime.timedelta(hours=8)#utc时间转化 loacl
            submittedTime = datetime_sruct.strftime("%Y-%m-%d %H:%M:%S")
            if submittedTime >= one_day_ago.strftime("%Y-%m-%d %H:%M:%S"):
                committer = i["owner"]["name"]
                projectName = i["project"]
                projectBranch = i["branch"]
                commitNum = str(i['_number'])
                reversion = commitReversion(url, commitNum)
                files = commitFiles(url, commitNum)
                detial_url = url + "/#/c/" + commitNum
                temp = {}
                temp["Committer"] = committer
                temp["Project"] = projectName
                temp["Branch"] = projectBranch
                temp["Files"] = files
                temp["Link"] = detial_url
                commit_information.append(temp)
            else:
                print("没有新代码合入")

    return commit_information

if __name__  == "__main__":
    commit_lists("gerrit地址","分支名")

 

上一篇:gerrit常用命令笔记


下一篇:如何压缩pdf文件的大小?