实现了 GitLab 全功能集成,包含 Webhook 处理、API 客户端、消息收发、会话管理、安全校验、限流器等完整模块,支持 Issues、Merge Requests、流水线等事件监听与交互。
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import secrets
|
|
import time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CODE_TTL_SECONDS = 600
|
|
|
|
|
|
class GitLabPairing:
|
|
def __init__(self):
|
|
self._codes: dict[str, dict] = {}
|
|
self._locks: dict[str, asyncio.Lock] = {}
|
|
|
|
async def generate_code(self, peer_id: str, account_id: str = "default") -> str:
|
|
key = f"{account_id}:{peer_id}"
|
|
code = secrets.token_hex(3).upper()
|
|
self._codes[key] = {"code": code, "created_at": time.monotonic()}
|
|
logger.info("gitlab pairing code generated for %s: %s", peer_id, code)
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str, account_id: str = "default") -> bool:
|
|
key = f"{account_id}:{peer_id}"
|
|
entry = self._codes.get(key)
|
|
if not entry:
|
|
return False
|
|
if time.monotonic() - entry["created_at"] > CODE_TTL_SECONDS:
|
|
self._codes.pop(key, None)
|
|
return False
|
|
if entry["code"].upper() == code.upper().strip():
|
|
self._codes.pop(key, None)
|
|
return True
|
|
return False
|
|
|
|
def normalize_allow_entry(self, entry: str) -> str:
|
|
return str(entry).strip()
|