Skip to content

Commit 5176e7a

Browse files
authored
Merge pull request #6 from AptS-1547/enchancement-branch
fix: 更新文档,优化匹配规则描述并添加分支匹配功能
2 parents ddc55f9 + 750d595 commit 5176e7a

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
- **安全验证**:支持 Webhook 签名验证,确保请求安全性
2929
- **OneBot 协议支持**:通过 OneBot 协议将推送信息转发到指定的 QQ 群或私聊
3030
- **灵活配置**:可配置监听的仓库、分支和事件类型
31-
- **高级仓库匹配**
32-
- 支持大小写不敏感的仓库名匹配
33-
- 支持 `用户名/*` 通配符匹配用户的所有仓库
31+
- **高级匹配规则**
32+
- **仓库匹配**:支持通配符模式(如 `user/*``*/*-api``org/[abc]*` 等)
33+
- **分支匹配**:支持通配符模式(如 `main``release-*``feature/*` 等)
34+
- 大小写不敏感匹配
3435
- **格式化消息**:结构化的推送通知,包含仓库、分支、推送者和最新提交信息
3536

3637
### 计划实现功能
@@ -101,12 +102,15 @@ ONEBOT_ACCESS_TOKEN: "your_token" # OneBot 访问令牌
101102

102103
GITHUB_WEBHOOK:
103104
- NAME: "github" # webhook 名称
104-
REPO: # 监听的仓库列表,支持用户名/* 匹配用户所有仓库
105+
REPO: # 监听的仓库列表,支持通配符匹配
105106
- "username/repo"
106107
- "username/*"
107-
BRANCH: # 监听的分支列表
108+
- "*/*-api"
109+
BRANCH: # 监听的分支列表,支持通配符匹配
108110
- "main"
109111
- "develop"
112+
- "feature/*"
113+
- "release-*"
110114
SECRET: "your_secret" # GitHub Webhook 密钥
111115
EVENTS: # 监听的事件类型
112116
- "push"
@@ -272,7 +276,7 @@ GITHUB_API_POLLING:
272276
## 项目结构
273277

274278
- app.py: 主应用入口和 Web 服务器
275-
- github_webhook.py: GitHub Webhook 处理逻辑
279+
- hooks/github_webhook.py: GitHub Webhook 处理逻辑
276280
- send_message.py: OneBot 消息发送客户端
277281
- settings.py: 配置加载和验证
278282
- requirements.txt: 项目依赖

docs/src/deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 部署建议
22

3-
### Docker 部署(计划支持)
3+
### Docker 部署
44

55
```bash
66
docker run -d \

docs/src/introduction.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
- **安全验证**:支持 Webhook 签名验证,确保请求安全性
2929
- **OneBot 协议支持**:通过 OneBot 协议将推送信息转发到指定的 QQ 群或私聊
3030
- **灵活配置**:可配置监听的仓库、分支和事件类型
31-
- **高级仓库匹配**
32-
- 支持大小写不敏感的仓库名匹配
33-
- 支持 `用户名/*` 通配符匹配用户的所有仓库
31+
- **高级匹配规则**
32+
- **仓库匹配**:支持通配符模式(如 `user/*``*/*-api``org/[abc]*` 等)
33+
- **分支匹配**:支持通配符模式(如 `main``release-*``feature/*` 等)
34+
- 大小写不敏感匹配
3435
- **格式化消息**:结构化的推送通知,包含仓库、分支、推送者和最新提交信息
3536

3637
### 计划实现功能

hooks/github_webhook.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,40 @@
1010
import hmac
1111
import hashlib
1212
import logging
13+
import fnmatch
1314
from typing import Optional, Dict, Any, List
1415

1516
from fastapi import Request, HTTPException, Header
1617

1718
logger = logging.getLogger(__name__)
1819

19-
def match_repository(repo_name: str, pattern: str) -> bool:
20+
def match_pattern(value: str, pattern: str) -> bool:
2021
"""
21-
检查仓库名是否匹配配置中的模式
22-
支持大小写不敏感匹配和通配符(用户名/*)形式
22+
检查字符串是否匹配配置中的模式
23+
支持大小写不敏感匹配和通配符模式
2324
2425
Args:
25-
repo_name: 实际的仓库全名 (例如 'user/repo')
26-
pattern: 配置中的仓库模式 (例如 'user/repo' 或 'user/*')
26+
value: 要匹配的字符串 (例如 'user/repo' 或 'main')
27+
pattern: 配置中的模式 (例如 'user/*', 'feature/*')
28+
treat_star_as_all: 是否将单独的 "*" 视为匹配所有内容(用于分支匹配)
2729
2830
Returns:
2931
bool: 是否匹配
3032
"""
31-
if not repo_name or not pattern:
33+
if not value or not pattern:
3234
return False
3335

34-
repo_name = repo_name.lower()
36+
value = value.lower()
3537
pattern = pattern.lower()
3638

37-
if pattern.endswith('/*'):
38-
user = pattern[:-2]
39-
return repo_name.startswith(f"{user}/")
39+
# 分支匹配特殊情况:单独的 "*" 匹配所有内容
40+
if pattern == "*":
41+
return True
42+
43+
if '*' in pattern or '?' in pattern or '[' in pattern:
44+
return fnmatch.fnmatch(value, pattern)
4045

41-
return repo_name == pattern
46+
return value == pattern
4247

4348
async def verify_signature(
4449
request: Request,
@@ -58,7 +63,7 @@ async def verify_signature(
5863

5964
webhook_secret = None
6065
for webhook in webhooks:
61-
if any(match_repository(repo_name, repo_pattern) for repo_pattern in webhook.REPO):
66+
if any(match_pattern(repo_name, repo_pattern) for repo_pattern in webhook.REPO):
6267
webhook_secret = webhook.SECRET
6368
break
6469

@@ -101,12 +106,16 @@ def find_matching_webhook(
101106
匹配的 webhook 配置或 None
102107
"""
103108
for webhook in webhooks:
104-
repo_matches = any(match_repository(repo_name, repo_pattern)
105-
for repo_pattern in webhook.REPO)
109+
# 检查仓库名是否匹配配置中的任一模式
110+
repo_matches = any(match_pattern(repo_name, repo_pattern)
111+
for repo_pattern in webhook.REPO)
112+
113+
# 检查分支名是否匹配配置中的任一模式
114+
branch_matches = any(match_pattern(branch, branch_pattern)
115+
for branch_pattern in webhook.BRANCH)
106116

107-
if (repo_matches and
108-
branch in webhook.BRANCH and
109-
event_type in webhook.EVENTS):
117+
# 检查事件类型是否匹配
118+
if (repo_matches and branch_matches and event_type in webhook.EVENTS):
110119
return webhook
111120

112121
return None

requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
aiohttp
2-
fastapi
3-
pydantic
4-
pydantic-settings
5-
pyyaml
6-
uvicorn
1+
aiohttp~=3.11.18
2+
fastapi~=0.115.12
3+
pydantic~=2.11.4
4+
pydantic-settings~=2.9.1
5+
pyyaml~=6.0.2
6+
uvicorn~=0.34.2

0 commit comments

Comments
 (0)