完成基本的工单消息推送

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 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

View File

@ -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)
"""