2020WS_ACCESS_TOKEN = settings .WS_ACCESS_TOKEN
2121GITHUB_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+
2347async 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