解决webhook推送的commits信息存在多分支的问题,将针对不同分支保存commits信息,并分别创建发行版

This commit is contained in:
floraachy 2023-04-21 09:55:24 +08:00
parent b66f07d7d7
commit 5cdb7929ec
5 changed files with 196 additions and 24 deletions

View File

@ -18,29 +18,33 @@ def create_version(host, bot):
logger.debug("开始进入create_version方法")
logger.debug(f"create_version全局变量-{type(GLOBAL_VAR)}|| {GLOBAL_VAR}")
if GLOBAL_VAR:
global_var_copy = GLOBAL_VAR
# 通过bot的私钥获取jwt_token
jwt_token = generate_jwt_token(bot)
logger.debug(f"jwt_token{jwt_token}")
# 通过jwt_token生成bot的access_token
# 初始化gitlinkapi
gitlink = GitLinkApi(host)
my_bot = MyBot(host)
for key, value in GLOBAL_VAR.items():
logger.debug(f"value={value}")
bot_installer_id = value.get("bot_installer_id")
bot_installer_id = value.get("installer_id")
access_token = my_bot.get_access_token(bot_installer_id, jwt_token)
logger.debug(
f"bot_id:{bot.get('bot_id')}, project: {key}, access_token{access_token}")
# 利用bot去请求创建发行版接口
gitlink = GitLinkApi(host)
req_data = {
"tag_name": f"{now_time}",
"name": f"{now_time}: bot触发创建",
"body": f"bot检测到过去24小时仓库有推送触发发行版的自动创建\n{value.get('commits_info')}",
"target_commitish": value.get('branch')
}
logger.debug(f"请求接口的参数:{req_data}")
# 调用接口创建发行版
res = gitlink.create_version(project=key, token=access_token, req_data=req_data)
logger.debug(f"创建发行版的响应数据::{res}")
GLOBAL_VAR.clear()
value.pop('installer_id')
for _k, _v in value.items():
# 将所有的commit信息拼接成一个字符串
body = ""
for i in range(len(_v)):
body += str(i + 1) + ". " + _v[i] + "\n"
req_data = {
"tag_name": f"_k-{now_time}",
"name": f"{now_time}: bot触发创建",
"body": f"bot检测到过去24小时仓库有推送触发发行版的自动创建\n\n{body}",
"target_commitish": _k
}
logger.debug(f"请求接口的参数:{req_data}")
# 调用接口创建发行版
res = gitlink.create_version(project=key, token=access_token, req_data=req_data)
logger.debug(f"{key} || {_k} || 创建发行版的响应数据::{res}")
GLOBAL_VAR.clear()

73
bot/server.py Normal file
View File

@ -0,0 +1,73 @@
from flask import Flask, request, render_template, 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')}")
args = dict(request.args)
return render_template('index.html', args=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']} 在分支 {branch} 提交了代码。 "
f"commit_id是{commit['id'][:10]} || commit内容是{commit['message']}")
# 收集这些关键信息作为全局变量,留待定时任务处理时需要
if project not in GLOBAL_VAR:
GLOBAL_VAR[project] = {}
if branch not in GLOBAL_VAR[project]:
GLOBAL_VAR[project][branch] = []
GLOBAL_VAR[project][branch].extend(commits_info)
GLOBAL_VAR[project]["installer_id"] = bot_installer_id
logger.debug(f"从服务获取的全局变量:{type(GLOBAL_VAR)}|| {GLOBAL_VAR}")
result = jsonify({
"code": 0,
"msg": "success",
"payload": request.json,
"installer_id": bot_installer_id
})
else:
result = jsonify({
"code": 1,
"msg": "没有检测到commits信息bot无法进行处理",
})
except Exception as e:
result = jsonify({
"code": -1,
"msg": "exception:" + str(e)
})
return result

11
bot/templates/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>mybot</title>
</head>
<body>
<h1>Welcome</h1>
<h3>参数:{{args}}</h3>
</body>
</html>

34
main.py
View File

@ -5,27 +5,47 @@
# @Software: PyCharm
# @Desc:
import schedule
from config.config import host, bot
from bot.bot_excute import create_version
from server import app
from bot.server import app
from loguru import logger
from config.path import LOG_DIR
import os
from flask_apscheduler import APScheduler
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = APScheduler()
# 设置系统环境变量的timezone
os.environ['TZ'] = "Asia/Shanghai"
# 设置APScheduler注册的timezone
scheduler = APScheduler(BackgroundScheduler(timezone="Asia/Shanghai"))
# 定义一个定时任务:通过cron的形式来定时启动任务, 每天的xx:xx:xx时刻执行一次函数
@scheduler.task('cron', id='do_job', day='*', hour='00', minute='00', second='00')
# # 定义一个定时任务:通过cron的形式来定时启动任务, 每天的xx:xx:xx时刻执行一次函数
# @scheduler.task('cron', id='do_job', day='*', hour='00', minute='00', second='00')
# def do_job():
# create_version(host, bot)
# 定义一个定时任务:通过interval的形式来定时启动任务, 每小时执行一次函数
# @scheduler.task('interval', id='do_job', hours=1)
# def do_job():
# create_version(host, bot)
# 定义一个定时任务:通过interval的形式来定时启动任务, 每10分钟执行一次函数
@scheduler.task('interval', id='do_job', minutes=10)
def do_job():
create_version(host, bot)
# 定义一个定时任务:通过interval的形式来定时启动任务, 每10秒执行一次函数
# @scheduler.task('interval', id='do_job', seconds=60)
# def do_job():
# create_version(host, bot)
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}")
logger.add(os.path.join(LOG_DIR, "mybot_{time:YYYY-MM-DD}.log"), encoding="utf-8",
format="{time:YYYY-MM-DD HH:mm:ss} {level} From {file}.{line} : {message}", rotation="00:00")
scheduler.init_app(app)
scheduler.start()
# 0.0.0.0代表电脑所有的IP。以上我们绑定了8080端口, 启动服务后我们访问的网址将是: http://127.0.0.1:8080/

View File

@ -34,10 +34,74 @@ req_data = {
},
],
"repository": {
"full_name": "floraachy/123123123",
"full_name": "floraachy/bot",
}
}
req_data2 = {
"ref": "refs/heads/gitlink",
"before": "22222222222bcf749fdcf4b31b79d5195ff0acb751ef82",
"after": "333333333333333333affe9f31e910782b24bb73aaec1540",
"commits": [
{
"id": "444444444444444441c1cb8882a966cbae239e09c8abc4b0",
"message": "xttttt--sjsjjsjs",
"author": {
"name": "floraachy",
"email": "flower@qq.com",
"username": ""
},
"timestamp": "2023-04-07T17:28:28+08:00"
},
{
"id": "b222222222229c1cb8882a966cbae239e09c8abc4b0",
"message": "test-0099999",
"author": {
"name": "flora",
"email": "flower@gitlink.com",
"username": ""
},
"timestamp": "2023-04-07T17:28:28+08:00"
},
],
"repository": {
"full_name": "floraachy/bot",
}
}
req_data3 = {
"ref": "refs/heads/gitlink",
"before": "5555555555554b31b79d5195ff0acb751ef82",
"after": "66666666666666affe9f31e910782b24bb73aaec1540",
"commits": [
{
"id": "77777777777711111c1cb8882a966cbae239e09c8abc4b0",
"message": "mybot--sjsjjsjs",
"author": {
"name": "floraachy",
"email": "flower@qq.com",
"username": ""
},
"timestamp": "2023-04-07T17:28:28+08:00"
},
{
"id": "88888888888c1cb8882a966cbae239e09c8abc4b0",
"message": "mybot-0099999",
"author": {
"name": "flora",
"email": "flower@gitlink.com",
"username": ""
},
"timestamp": "2023-04-07T17:28:28+08:00"
},
],
"repository": {
"full_name": "floraachy/mybot",
}
}
res = requests.post("http://127.0.0.1:8070/mybot/?installer_id=85589", json=req_data)
res2 = requests.post("http://127.0.0.1:8070/mybot/?installer_id=85589", json=req_data2)
res3 = requests.post("http://127.0.0.1:8070/mybot/?installer_id=85500", json=req_data3)
print(res.status_code)
print(res.text)