完成基本的工单消息推送

This commit is contained in:
San Liy 2023-12-18 22:47:18 +08:00
parent a674f98e14
commit 2f168f1287
2 changed files with 68 additions and 15 deletions

View File

@ -1,9 +1,8 @@
from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from enum import Enum from enum import Enum
from typing import Optional, List, Dict from typing import Optional, List, Dict
from pydantic import BaseModel from pydantic import BaseModel, Field
class User(BaseModel): class User(BaseModel):
@ -43,6 +42,7 @@ class User(BaseModel):
visibility: Optional[str] = None visibility: Optional[str] = None
"""the user's website""" """the user's website"""
website: Optional[str] = None website: Optional[str] = None
username: Optional[str] = None
class Attachment(BaseModel): class Attachment(BaseModel):
@ -132,7 +132,6 @@ class Organization(BaseModel):
website: Optional[str] = None website: Optional[str] = None
@dataclass
class PermissionEnum(Enum): class PermissionEnum(Enum):
ADMIN = "admin" ADMIN = "admin"
NONE = "none" NONE = "none"
@ -341,10 +340,39 @@ class Repository(BaseModel):
website: Optional[str] = None website: Optional[str] = None
class WebHook(BaseModel): class Changes(BaseModel):
action: str class Body(BaseModel):
issue: Issue old_ctx: Optional[str] = Field(None, alias='from')
comment: Comment
repository: Repository body: Optional[Body] = None
sender: User
is_pull: bool
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

View File

@ -4,10 +4,10 @@ from typing import Dict
from nacl.signing import SigningKey from nacl.signing import SigningKey
from sanic import Sanic, Request from sanic import Sanic, Request
from sanic.log import logger, Colors from sanic.log import logger, Colors
from sanic.response import json from sanic.response import text
from socketio import AsyncClient from socketio import AsyncClient
from gitea_model import WebHook from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent
from model import Ctx, SioConfig, Message from model import Ctx, SioConfig, Message
app = Sanic('GiteaPush', ctx=Ctx) app = Sanic('GiteaPush', ctx=Ctx)
@ -31,10 +31,35 @@ async def setup_before_start(_app):
@app.post('/receive') @app.post('/receive')
async def receive(rqt: Request): async def receive(rqt: Request):
data = WebHook(**rqt.json) match (rqt.headers['X-Gitea-Event']):
message = Message(content=f'webhook test: {data.issue.body}', room_id=-777186831) 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()) await app.ctx.sio.emit('sendMessage', message.to_json())
return json({}) return text(rsp)
""" """