Skip to content

Commit 9c9a230

Browse files
authored
Merge pull request #5 from AptS-1547/feat-repo-enchancement
feat: 增强仓库匹配逻辑,支持大小写不敏感匹配和通配符
2 parents 7854610 + c1ad489 commit 9c9a230

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

app.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@
2020
WS_ACCESS_TOKEN = settings.WS_ACCESS_TOKEN
2121
GITHUB_WEBHOOK = settings.GITHUB_WEBHOOK
2222

23+
def match_repository(repo_name: str, pattern: str) -> bool:
24+
"""
25+
检查仓库名是否匹配配置中的模式
26+
支持大小写不敏感匹配和通配符(用户名/*)形式
27+
28+
Args:
29+
repo_name: 实际的仓库全名 (例如 'user/repo')
30+
pattern: 配置中的仓库模式 (例如 'user/repo' 或 'user/*')
31+
32+
Returns:
33+
bool: 是否匹配
34+
"""
35+
if not repo_name or not pattern:
36+
return False
37+
38+
repo_name = repo_name.lower()
39+
pattern = pattern.lower()
40+
41+
if pattern.endswith('/*'):
42+
user = pattern[:-2]
43+
return repo_name.startswith(f"{user}/")
44+
45+
return repo_name == pattern
46+
2347
async def verify_signature(request: Request, x_hub_signature_256: Optional[str] = Header(None)):
2448
"""验证 GitHub webhook 签名"""
2549

@@ -33,7 +57,7 @@ async def verify_signature(request: Request, x_hub_signature_256: Optional[str]
3357

3458
webhook_secret = None
3559
for webhook in settings.GITHUB_WEBHOOK:
36-
if repo_name in webhook.REPO:
60+
if any(match_repository(repo_name, repo_pattern) for repo_pattern in webhook.REPO):
3761
webhook_secret = webhook.SECRET
3862
break
3963

@@ -83,7 +107,10 @@ async def github_webhook(request: Request):
83107

84108
matched_webhook = None
85109
for webhook in settings.GITHUB_WEBHOOK:
86-
if (repo_name in webhook.REPO and
110+
111+
repo_matches = any(match_repository(repo_name, repo_pattern) for repo_pattern in webhook.REPO)
112+
113+
if (repo_matches and
87114
branch in webhook.BRANCH and
88115
event_type in webhook.EVENTS):
89116
matched_webhook = webhook

0 commit comments

Comments
 (0)