84 lines
4.0 KiB
Python
84 lines
4.0 KiB
Python
from flask import Flask, request, jsonify # 使用 jsonify 工具函数响应JSON
|
||
from loguru import logger
|
||
from config.config import GLOBAL_VAR
|
||
|
||
app = Flask(__name__)
|
||
|
||
|
||
@app.route('/') # /表示网站的根路径,在浏览器窗口输入 ip:port/ 进行访问
|
||
def welcome():
|
||
args = request.args
|
||
logger.info(f"列出所有的url参数:{type(args)} || {args}")
|
||
# 可以通过request.full_path和request.path 查看浏览器传给我们的Flask服务的数据
|
||
logger.info(f"path={request.path}, full_path={request.full_path}")
|
||
# 要获取参数的键(name)对应的值
|
||
logger.info(f"{request.args.get('name', '')}")
|
||
# 当参数有多个值时,使用request.args.getlist获取某个参数的所有值,返回是一个列表
|
||
# logger.info(f"{request.args.getlist('app')}")
|
||
return args
|
||
|
||
|
||
# @app.route('/mybot', methods=['GET','POST']), 表示同时支持GET和POST,
|
||
@app.route('/mybot/', methods=['GET', 'POST'])
|
||
def get_webhook_info():
|
||
try:
|
||
logger.debug(f"fullpath: {request.full_path}")
|
||
# 如果POST数据是form格式,解析POST数据:request.form, 例如:request.form.get('name')
|
||
# 如果POST的数据是JSON格式,request.json会自动将json数据转换成Python类型(字典或者列表)
|
||
payload = request.json
|
||
logger.info(f"获取webhook信息-请求内容:{type(payload)}|| {payload}")
|
||
# 从地址中获取bot_installer_id
|
||
bot_installer_id = dict(request.args).get("installer_id")
|
||
logger.info(f"获取webhook信息-地址栏参数:{type(bot_installer_id)}|| {bot_installer_id}")
|
||
if "commits" in payload.keys():
|
||
logger.info("---------request.json中存在commits---------")
|
||
# 根据webhook请求内容 获取仓库的名称
|
||
repository = payload.get("repository")
|
||
project = repository.get("full_name")
|
||
# 根据webhook请求内容 获取本次推送的分支
|
||
branch = payload.get("ref").split("/")[-1]
|
||
# 根据webhook请求内容,获取当次push过来的commits信息
|
||
commits = payload.get("commits")
|
||
# 根据commits信息获取每次提交的message
|
||
commits_info = []
|
||
for commit in commits:
|
||
commits_info.append(
|
||
f"{commit['author']['name']}/{commit['author']['email']}于{commit['timestamp']}在提交了代码。"
|
||
f"commit_id是{commit['id'][:10]} || commit内容是:{commit['message']}")
|
||
# 收集这些关键信息作为全局变量,留待定时任务处理时需要
|
||
if not GLOBAL_VAR.get(project):
|
||
GLOBAL_VAR[project] = {}
|
||
if GLOBAL_VAR[project].get("bot_installer_id"):
|
||
GLOBAL_VAR[project]["commits_info"] = GLOBAL_VAR[project]["commits_info"] + commits_info
|
||
else:
|
||
GLOBAL_VAR[project] = {
|
||
"bot_installer_id": bot_installer_id,
|
||
"commits_info": commits_info,
|
||
"branch": branch
|
||
}
|
||
logger.debug(f"从服务获取的全局变量:{type(GLOBAL_VAR)}|| {GLOBAL_VAR}")
|
||
result = jsonify({
|
||
"code": 0,
|
||
"msg": "success",
|
||
"payload": request.json,
|
||
"installer_id": bot_installer_id
|
||
}, 200)
|
||
else:
|
||
result = jsonify({
|
||
"code": 1,
|
||
"msg": "没有检测到commits信息,bot无法进行处理",
|
||
}, 400)
|
||
except Exception as e:
|
||
result = jsonify({
|
||
"code": -1,
|
||
"msg": "exception:" + str(e)
|
||
}, 500)
|
||
return result
|
||
|
||
|
||
# if __name__ == '__main__':
|
||
# logger.add(os.path.join(LOG_DIR, "mybot_all.log"), enqueue=True, encoding="utf-8",
|
||
# format="{time:YYYY-MM-DD HH:mm:ss} {level} From {module}.{function} : {message}")
|
||
# # 0.0.0.0代表电脑所有的IP。以上我们绑定了8080端口, 启动服务后我们访问的网址将是: http://127.0.0.1:8080/
|
||
# app.run(host="0.0.0.0", port=8070, debug=False)
|