forked from Trustie-Study-Group/PRWelBot4SECourse
Update prController.py
This commit is contained in:
parent
020b3675a4
commit
d489b77fb8
|
@ -1,17 +1,20 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('..')
|
sys.path.append('..') # 将上层目录添加到模块搜索路径中
|
||||||
from flask import Flask, jsonify, request
|
|
||||||
from flask_cors import CORS, cross_origin
|
|
||||||
import json
|
|
||||||
import services.pullService as pullService
|
|
||||||
import datetime
|
|
||||||
from commons.logUtil import logger
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
from flask import Flask, jsonify, request # 导入Flask相关模块
|
||||||
|
from flask_cors import CORS, cross_origin # 导入处理CORS的模块
|
||||||
|
import json # 导入处理JSON的模块
|
||||||
|
import services.pullService as pullService # 导入自定义的pullService模块
|
||||||
|
import datetime # 导入处理日期时间的模块
|
||||||
|
from commons.logUtil import logger # 导入自定义的日志工具模块
|
||||||
|
|
||||||
|
app = Flask(__name__) # 创建Flask应用实例
|
||||||
|
|
||||||
|
|
||||||
# 测试
|
|
||||||
|
|
||||||
|
# 测试路由,返回JSON响应
|
||||||
@app.route('/hello', methods=['get'])
|
@app.route('/hello', methods=['get'])
|
||||||
@cross_origin()
|
@cross_origin()
|
||||||
def hello():
|
def hello():
|
||||||
|
@ -20,15 +23,15 @@ def hello():
|
||||||
"msg": "hello"
|
"msg": "hello"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# 处理Gitlink Webhook的路由
|
||||||
# 功能:每当有一个新的PR创建/关闭时,根据状态执行存储或更新操作
|
# 功能:每当有一个新的PR创建/关闭时,根据状态执行存储或更新操作
|
||||||
@app.route('/prwelcome', methods=['POST'])
|
@app.route('/prwelcome', methods=['POST'])
|
||||||
@cross_origin()
|
@cross_origin()
|
||||||
def pr_welcome():
|
def pr_welcome():
|
||||||
try:
|
try:
|
||||||
payload = request.json
|
payload = request.json # 从请求中获取JSON数据
|
||||||
print(payload)
|
print(payload) # 打印获取到的JSON数据
|
||||||
logger.info("获取webhook信息成功:" + str(payload))
|
logger.info("获取webhook信息成功:" + str(payload)) # 记录日志
|
||||||
# # 判断是新创建issue,还是issue的状态发生改变
|
# # 判断是新创建issue,还是issue的状态发生改变
|
||||||
if payload["action"] == 'opened':
|
if payload["action"] == 'opened':
|
||||||
pull_id = payload["pull_request"]["id"]
|
pull_id = payload["pull_request"]["id"]
|
||||||
|
@ -40,14 +43,14 @@ def pr_welcome():
|
||||||
check_time = created + datetime.timedelta(hours=2)
|
check_time = created + datetime.timedelta(hours=2)
|
||||||
values = (pull_id, index, owner, repo, created, check_time, 0)
|
values = (pull_id, index, owner, repo, created, check_time, 0)
|
||||||
# print(values)
|
# print(values)
|
||||||
pullService.insert_pull(values)
|
pullService.insert_pull(values)# 执行插入操作
|
||||||
|
|
||||||
# 关闭 更新数据库对应pr状态 (合并或者拒绝都属于关闭)
|
# 关闭 更新数据库对应pr状态 (合并或者拒绝都属于关闭)
|
||||||
elif payload["action"] == 'closed':
|
elif payload["action"] == 'closed':
|
||||||
index = payload["pull_request"]["number"]
|
index = payload["pull_request"]["number"]
|
||||||
owner = payload["pull_request"]["user"]["login"]
|
owner = payload["pull_request"]["user"]["login"]
|
||||||
repo = payload["repository"]["full_name"]
|
repo = payload["repository"]["full_name"]
|
||||||
pullService.update_pull(index, owner, repo)
|
pullService.update_pull(index, owner, repo)# 执行更新操作
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"code": 0,
|
"code": 0,
|
||||||
|
|
Loading…
Reference in New Issue