forked from Trustie-Study-Group/PrWelcomeBot
121 lines
3.6 KiB
Python
121 lines
3.6 KiB
Python
import sys
|
||
|
||
sys.path.append('..')
|
||
import config.baseConfig as baseConfig
|
||
import pymysql
|
||
import datetime
|
||
import commons.apiUtil as api
|
||
|
||
|
||
# 插入一条pull表记录
|
||
def insert_pull(values):
|
||
# 打开数据库连接
|
||
db = pymysql.connect(**baseConfig.db_config)
|
||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||
cursor = db.cursor()
|
||
|
||
# 插入语句
|
||
# value = (1, 2, "b", "b", now_time, now_time, 0)
|
||
ins_sql = """INSERT INTO pull(`pullid`, `index`, `owner`, `repo`, `created`, `checktime`, `status`)
|
||
VALUES (%s, %s, "%s", "%s","%s", "%s", %s)""" % values
|
||
print(ins_sql)
|
||
try:
|
||
cursor.execute(ins_sql)
|
||
db.commit()
|
||
except Exception as e:
|
||
print("ERR0R!insert pull error:")
|
||
db.rollback()
|
||
print(e)
|
||
# 关闭数据库连接
|
||
db.close()
|
||
|
||
|
||
# 更新一条pull表记录
|
||
def update_pull(index, owner, repo):
|
||
# 打开数据库连接
|
||
db = pymysql.connect(**baseConfig.db_config)
|
||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||
cursor = db.cursor()
|
||
# 更新语句
|
||
sql_update = "UPDATE pull set `status` = 1 where `index` = '%d' AND `owner` = '%s' AND `repo` = '%s'" % (
|
||
index, owner, repo)
|
||
try:
|
||
cursor.execute(sql_update)
|
||
db.commit()
|
||
except Exception as e:
|
||
print("ERR0R!update pull error:")
|
||
db.rollback()
|
||
print(e)
|
||
# 关闭数据库连接
|
||
db.close()
|
||
|
||
|
||
# 查询并更新
|
||
def check_and_update_pull():
|
||
# 打开数据库连接
|
||
db = pymysql.connect(**baseConfig.db_config)
|
||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||
cursor = db.cursor()
|
||
|
||
now_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
|
||
sql_query = "select * from pull where `status` = 0 and `checktime` < '%s'" % now_time
|
||
# print(sql_query)
|
||
|
||
try:
|
||
# 执行SQL语句
|
||
cursor.execute(sql_query)
|
||
# 获取所有记录列表
|
||
results = cursor.fetchall()
|
||
print(results)
|
||
for item in results:
|
||
# 调用接口查询pr状态
|
||
# (7, 1, 2, 'b', 'b', datetime.datetime(), None, datetime.datetime(2023, 3, 23, 11, 46, 13), 0)
|
||
# 提取 pr记录信息
|
||
pull_id = item[0]
|
||
pull_index = item[2]
|
||
owner = item[3]
|
||
repo = item[4]
|
||
pr_infor = api.get_pull_infor(owner, repo, pull_index)
|
||
print(pr_infor)
|
||
issue_id = pr_infor["issue"]["id"];
|
||
|
||
# pr 状态标志位,0表示未完成,1表示已经完成
|
||
flag = check_pr(pr_infor)
|
||
|
||
# 数据库 pr记录 更新语句
|
||
sql_update = ""
|
||
# 若新增评论或者状态为关闭 则修改数据库
|
||
if flag == 1:
|
||
# pr已完成更新语句
|
||
sql_update = "UPDATE pull set status = 1 where id = '%d'" % pull_id
|
||
# 否则对不活跃的pr调用接口,发表评论提醒处理
|
||
|
||
try:
|
||
cursor.execute(sql_update)
|
||
db.commit()
|
||
except Exception as e:
|
||
db.rollback()
|
||
print("ERR0R!update pull error:")
|
||
print(e)
|
||
except Exception as e:
|
||
db.rollback()
|
||
print("ERR0R!deal pull error:")
|
||
print(e)
|
||
# 关闭数据库连接
|
||
db.close()
|
||
|
||
|
||
# 检查 pr状态,已完成返回1,未完成返回0
|
||
def check_pr(pr_infor):
|
||
# pr已经关闭
|
||
if pr_infor["status"] == "closed":
|
||
return 1
|
||
# pr有动态
|
||
if pr_infor["issue"]["journals_count"] != 0:
|
||
return 1
|
||
return 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
check_and_update_pull()
|