forked from Trustie-Study-Group/PRWelBot4SECourse
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
#!/usr/bin/python
|
||
import sys
|
||
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__)
|
||
|
||
|
||
# 测试
|
||
@app.route('/hello', methods=['get'])
|
||
@cross_origin()
|
||
def hello():
|
||
return jsonify({
|
||
"code": 0,
|
||
"msg": "hello"
|
||
})
|
||
|
||
|
||
# 功能:每当有一个新的PR创建/关闭时,根据状态执行存储或更新操作
|
||
@app.route('/prwelcome', methods=['POST'])
|
||
@cross_origin()
|
||
def pr_welcome():
|
||
try:
|
||
payload = request.json
|
||
print(payload)
|
||
logger.info("获取webhook信息成功:" + str(payload))
|
||
# # 判断是新创建issue,还是issue的状态发生改变
|
||
if payload["action"] == 'opened':
|
||
pull_id = payload["pull_request"]["id"]
|
||
index = payload["pull_request"]["number"]
|
||
time = payload["pull_request"]["created_at"][:19]
|
||
owner = payload["pull_request"]["user"]["login"]
|
||
repo = payload["repository"]["full_name"]
|
||
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状态 (合并或者拒绝都属于关闭)
|
||
elif payload["action"] == 'closed':
|
||
index = payload["pull_request"]["number"]
|
||
owner = payload["pull_request"]["user"]["login"]
|
||
repo = payload["repository"]["full_name"]
|
||
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__':
|
||
app.run(port=3090)
|
||
|