From 2f168f12877c496ffd6a73534d9877cd88db02ca Mon Sep 17 00:00:00 2001 From: adk23333 <2633103794@qq.com> Date: Mon, 18 Dec 2023 22:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=B7=A5=E5=8D=95=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gitea_model.py | 48 ++++++++++++++++++++++++++++++++++++++---------- server.py | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/gitea_model.py b/gitea_model.py index b37681a..a477ca9 100644 --- a/gitea_model.py +++ b/gitea_model.py @@ -1,9 +1,8 @@ -from dataclasses import dataclass from datetime import datetime from enum import Enum from typing import Optional, List, Dict -from pydantic import BaseModel +from pydantic import BaseModel, Field class User(BaseModel): @@ -43,6 +42,7 @@ class User(BaseModel): visibility: Optional[str] = None """the user's website""" website: Optional[str] = None + username: Optional[str] = None class Attachment(BaseModel): @@ -132,7 +132,6 @@ class Organization(BaseModel): website: Optional[str] = None -@dataclass class PermissionEnum(Enum): ADMIN = "admin" NONE = "none" @@ -341,10 +340,39 @@ class Repository(BaseModel): website: Optional[str] = None -class WebHook(BaseModel): - action: str - issue: Issue - comment: Comment - repository: Repository - sender: User - is_pull: bool +class Changes(BaseModel): + class Body(BaseModel): + old_ctx: Optional[str] = Field(None, alias='from') + + body: Optional[Body] = None + + +class Action(Enum): + edited = 'edited' + opened = 'opened' + created = 'created' + + +class GiteaEvent(Enum): + issue_comment = 'issue_comment' + issues = 'issues' + + +class WebHookIssueComment(BaseModel): + action: Optional[str] = None + issue: Optional[Issue] = None + comment: Optional[Comment] = None + changes: Optional[Changes] = None + repository: Optional[Repository] = None + sender: Optional[User] = None + is_pull: Optional[bool] = None + + +class WebHookIssue(BaseModel): + action: Optional[str] = None + number: Optional[int] = None + changes: Optional[Changes] = None + issue: Optional[Issue] = None + repository: Optional[Repository] = None + sender: Optional[User] = None + commit_id: Optional[str] = None diff --git a/server.py b/server.py index 78f58d9..2bb8268 100644 --- a/server.py +++ b/server.py @@ -4,10 +4,10 @@ from typing import Dict from nacl.signing import SigningKey from sanic import Sanic, Request from sanic.log import logger, Colors -from sanic.response import json +from sanic.response import text from socketio import AsyncClient -from gitea_model import WebHook +from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent from model import Ctx, SioConfig, Message app = Sanic('GiteaPush', ctx=Ctx) @@ -31,10 +31,35 @@ async def setup_before_start(_app): @app.post('/receive') async def receive(rqt: Request): - data = WebHook(**rqt.json) - message = Message(content=f'webhook test: {data.issue.body}', room_id=-777186831) + match (rqt.headers['X-Gitea-Event']): + case GiteaEvent.issues.value: + data = WebHookIssue(**rqt.json) + rsp_title = f"[{data.repository.full_name}][{data.action}] {data.issue.title} (Issue #{data.issue.id})" + rsp_sender = f"By {data.sender.username}" + rsp_ctx = f"{data.issue.body}" + + rsp_link = f"View it: {data.issue.html_url}" + cancel_subscribe = f"unsubscribe: {data.repository.html_url}" + + rsp = f"{rsp_title}\n\n{rsp_sender}\n------\n{rsp_ctx}\n------\n{rsp_link}\n{cancel_subscribe}" + + case GiteaEvent.issue_comment.value: + data = WebHookIssueComment(**rqt.json) + rsp_title = f"Re:[{data.repository.full_name}][{data.action}] {data.issue.title} (Issue #{data.issue.id})" + rsp_sender = f"By {data.sender.username}" + rsp_ctx = f"{data.comment.body}" + + rsp_link = f"View it: {data.comment.html_url}" + cancel_subscribe = f"unsubscribe: {data.repository.html_url}" + + rsp = f"{rsp_title}\n\n{rsp_sender}\n------\n{rsp_ctx}\n------\n{rsp_link}\n{cancel_subscribe}" + + case _: + rsp = "Unknown webhook type! Please contact the administrator." + + message = Message(content=rsp, room_id=-777186831) await app.ctx.sio.emit('sendMessage', message.to_json()) - return json({}) + return text(rsp) """