From 2a3c35a55cf233aa77bb6e3655e466f83d32c8a9 Mon Sep 17 00:00:00 2001 From: xtao-dada Date: Sat, 5 Mar 2022 16:10:53 +0800 Subject: [PATCH] [first] commit --- .gitignore | 4 +- app.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++ ci.py | 21 ++++++++++ config.gen.ini | 15 +++++++ main.py | 9 ++++ requirements.txt | 4 ++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 app.py create mode 100644 ci.py create mode 100644 config.gen.ini create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 55be276..83ddfd7 100644 --- a/.gitignore +++ b/.gitignore @@ -150,5 +150,7 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ +# Data +config.ini diff --git a/app.py b/app.py new file mode 100644 index 0000000..8dc3ade --- /dev/null +++ b/app.py @@ -0,0 +1,104 @@ +from aioflask import Flask +from ci import app as bot + +app = Flask(__name__) + + +@app.route("/", methods=['GET', 'POST']) +async def webhook(request): + data = request.json + # json contains an attribute that differentiates between the types, see + # https://docs.gitlab.com/ce/user/project/integrations/webhooks.html + # for more infos + kind = data['object_kind'] + if kind == 'push': + msg = generatePushMsg(data) + elif kind == 'tag_push': + msg = generatePushMsg(data) # TODO:Make own function for this + elif kind == 'issue': + msg = generateIssueMsg(data) + elif kind == 'note': + msg = generateCommentMsg(data) + elif kind == 'merge_request': + msg = generateMergeRequestMsg(data) + elif kind == 'wiki_page': + msg = generateWikiMsg(data) + elif kind == 'pipeline': + msg = generatePipelineMsg(data) + elif kind == 'build': + msg = generateBuildMsg(data) + else: + msg = "Unknown event." + await bot.send_message(-1001441461877, msg) + return jsonify({'status': 'ok'}) + + +def generatePushMsg(data): + msg = f'*🔨 {data["total_commits_count"]} new commits to ' \ + f'{data["project"]["name"]}:{data["project"]["default_branch"]}*:\n\n' + for commit in data['commits']: + tag_link = f'{commit["id"][:7]}' + msg = msg + f"{tag_link}: {commit['message'].rstrip()}\n" + return msg + + +def generateIssueMsg(data): + action = data['object_attributes']['action'] + if action == 'open': + assignees = '' + for assignee in data.get('assignees', []): + assignees += assignee['name'] + ' ' + msg = '*{0}* new issue for *{1}*:\n' \ + .format(data['project']['name'], assignees) + elif action == 'reopen': + assignees = '' + for assignee in data.get('assignees', []): + assignees += assignee['name'] + ' ' + msg = '*{0}* issue re-opened for *{1}*:\n' \ + .format(data['project']['name'], assignees) + elif action == 'close': + msg = '*{0}* issue closed by *{1}*:\n' \ + .format(data['project']['name'], data['user']['name']) + elif action == 'update': + assignees = '' + for assignee in data.get('assignees', []): + assignees += assignee['name'] + ' ' + msg = '*{0}* issue assigned to *{1}*:\n' \ + .format(data['project']['name'], assignees) + else: + msg = "" + + msg = msg + '[{0}]({1})' \ + .format(data['object_attributes']['title'], data['object_attributes']['url']) + return msg + + +def generateCommentMsg(data): + ntype = data['object_attributes']['noteable_type'] + if ntype == 'Commit': + msg = 'note to commit' + elif ntype == 'MergeRequest': + msg = 'note to MergeRequest' + elif ntype == 'Issue': + msg = 'note to Issue' + elif ntype == 'Snippet': + msg = 'note on code snippet' + else: + msg = "" + return msg + + +def generateMergeRequestMsg(data): + return 'new MergeRequest' + + +def generateWikiMsg(data): + return 'new wiki stuff' + + +def generatePipelineMsg(data): + return 'new pipeline stuff' + + +def generateBuildMsg(data): + return 'new build stuff' diff --git a/ci.py b/ci.py new file mode 100644 index 0000000..12a421d --- /dev/null +++ b/ci.py @@ -0,0 +1,21 @@ +from configparser import RawConfigParser +from pyrogram import Client +from httpx import AsyncClient, get + +# 读取配置文件 +config = RawConfigParser() +config.read("config.ini") +bot_token: str = "" +admin_id: int = 0 +bot_token = config.get("basic", "bot_token", fallback=bot_token) +admin_id = config.getint("basic", "admin", fallback=admin_id) +guess_time = 30 # 猜语音游戏持续时间 +""" Init httpx client """ +# 使用自定义 UA +headers = { + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" +} +client = AsyncClient(timeout=10.0, headers=headers) +me = get(f"https://api.telegram.org/bot{bot_token}/getme").json() +# 初始化客户端 +app = Client("bot", bot_token=bot_token) diff --git a/config.gen.ini b/config.gen.ini new file mode 100644 index 0000000..29b6ce2 --- /dev/null +++ b/config.gen.ini @@ -0,0 +1,15 @@ +[pyrogram] +api_id = 12345 +api_hash = 0123456789abc0123456789abc + +[basic] +bot_token = 111:abc +admin = 777000 + +[plugins] +root = plugins + +[proxy] +enabled = False +hostname = 127.0.0.1 +port = 1080 diff --git a/main.py b/main.py new file mode 100644 index 0000000..90e3f10 --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +import logging +from ci import app as bot +from app import app + +# 日志记录 +logging.basicConfig(level=logging.INFO) +bot.start() +app.run("0.0.0.0", port=10111) +bot.stop() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0a2b214 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +aioflask +Pyrogram>=1.3.5 +Tgcrypto>=1.2.3 +httpx