From 5ff893453bfcd70546285b1d08ad65f40fd29386 Mon Sep 17 00:00:00 2001 From: FirePunch Date: Thu, 7 Dec 2023 10:23:29 +0800 Subject: [PATCH] Update prController.py --- controllers/prController.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/controllers/prController.py b/controllers/prController.py index 9401e4d..5483d08 100644 --- a/controllers/prController.py +++ b/controllers/prController.py @@ -10,27 +10,30 @@ from commons.logUtil import logger app = Flask(__name__) - -# 测试 +# 测试接口 @app.route('/hello', methods=['get']) @cross_origin() def hello(): + # 返回JSON格式的问候语 return jsonify({ "code": 0, "msg": "hello" }) - -# 功能:每当有一个新的PR创建/关闭时,根据状态执行存储或更新操作 +# 功能:处理新建或关闭的PR(Pull Request)相关的Webhook请求 @app.route('/prwelcome', methods=['POST']) @cross_origin() def pr_welcome(): try: + # 获取请求的JSON数据 payload = request.json print(payload) + # 记录日志 logger.info("获取webhook信息成功:" + str(payload)) - # # 判断是新创建issue,还是issue的状态发生改变 + + # 如果是新建的PR if payload["action"] == 'opened': + # 提取PR的相关信息 pull_id = payload["pull_request"]["id"] index = payload["pull_request"]["number"] time = payload["pull_request"]["created_at"][:19] @@ -39,27 +42,30 @@ def pr_welcome(): created = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S") check_time = created + datetime.timedelta(hours=2) values = (pull_id, index, owner, repo, created, check_time, 0) - # print(values) + # 将提取的信息存储到数据库 pullService.insert_pull(values) - # 关闭 更新数据库对应pr状态 (合并或者拒绝都属于关闭) + # 如果PR被关闭 elif payload["action"] == 'closed': + # 提取PR的相关信息 index = payload["pull_request"]["number"] owner = payload["pull_request"]["user"]["login"] repo = payload["repository"]["full_name"] + # 更新数据库中的PR状态 pullService.update_pull(index, owner, repo) + # 返回成功的响应 return jsonify({ "code": 0, "msg": "success" }) except Exception as e: + # 返回异常响应 return jsonify({ "code": -1, "msg": "exception:" + str(e) }) - if __name__ == '__main__': + # 启动Flask应用 app.run(port=3090) -