-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.py
More file actions
224 lines (187 loc) · 7.34 KB
/
webhook.py
File metadata and controls
224 lines (187 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2025 AptS-1547
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
GitHub Webhook 处理模块
本模块用于处理 GitHub Webhook 事件,包括验证签名、查找匹配的 webhook 配置和提取 push 事件数据。
作者:AptS:1547
版本:0.1.0-alpha
日期:2025-04-17
本程序遵循 Apache License 2.0 许可证
"""
import hmac
import hashlib
import logging
from typing import Optional, Dict, Any, List
from fastapi import Request, HTTPException, Header
from app.utils.matching import match_pattern
logger = logging.getLogger(__name__)
class GitHubWebhookHandler:
"""
GitHub Webhook 处理类
用于处理 GitHub Webhook 事件,包括验证签名、查找匹配的 webhook 配置和提取 push 事件数据。
"""
@staticmethod
async def verify_signature(
request: Request,
webhooks: List,
x_hub_signature_256: Optional[str] = Header(None)
) -> bool:
"""验证 GitHub webhook 签名"""
try:
body = await request.body()
payload = await request.json()
repo_name = payload.get("repository", {}).get("full_name")
except Exception as e:
logger.error("解析请求体失败: %s", str(e))
raise HTTPException(
status_code=400, detail="Invalid JSON payload") from e
webhook_secret = None
for webhook in webhooks:
if any(match_pattern(repo_name, repo_pattern) for repo_pattern in webhook.REPO):
webhook_secret = webhook.SECRET
break
if not webhook_secret:
logger.warning("仓库 %s 未配置 webhook 密钥,跳过签名验证", repo_name)
return True
if not x_hub_signature_256:
raise HTTPException(
status_code=401, detail="Missing X-Hub-Signature-256 header")
signature = hmac.new(
key=webhook_secret.encode(),
msg=body,
digestmod=hashlib.sha256
).hexdigest()
expected_signature = f"sha256={signature}"
if not hmac.compare_digest(expected_signature, x_hub_signature_256):
raise HTTPException(status_code=401, detail="Invalid signature")
return True
@staticmethod
def find_matching_webhook(
repo_name: str,
branch: str,
event_type: str,
webhooks: List
) -> Optional[Any]:
"""
查找匹配的 webhook 配置
Args:
repo_name: 仓库名
branch: 分支名 (可能为空,对于非push事件)
event_type: 事件类型
webhooks: webhook 配置列表
Returns:
匹配的 webhook 配置或 None
"""
logger.debug("尝试匹配 webhook: 仓库=%s, 分支=%s, 事件类型=%s", repo_name, branch, event_type)
for webhook in webhooks:
repo_matches = any(match_pattern(repo_name, repo_pattern)
for repo_pattern in webhook.REPO)
if not repo_matches:
continue
if event_type not in webhook.EVENTS:
continue
if event_type in ["push", "pull_request"]:
branch_matches = any(match_pattern(branch, branch_pattern)
for branch_pattern in webhook.BRANCH)
if not branch_matches:
continue
else:
if getattr(webhook, "BRANCH_CHECK_ALL", False) and branch:
branch_matches = any(match_pattern(branch, branch_pattern)
for branch_pattern in webhook.BRANCH)
if not branch_matches:
continue
logger.debug("找到匹配的webhook配置: %s", webhook)
return webhook
logger.debug("未找到匹配的webhook配置")
return None
@staticmethod
def extract_push_data(payload: Dict[str, Any]) -> Dict[str, Any]:
"""
从 push 事件中提取相关数据
Args:
payload: GitHub webhook 负载
Returns:
包含提取数据的字典
"""
return {
"repo_name": payload.get("repository", {}).get("full_name"),
"branch": payload.get("ref", "").replace("refs/heads/", ""),
"pusher": payload.get("pusher", {}).get("name"),
"commits": payload.get("commits", []),
"commit_count": len(payload.get("commits", []))
}
@staticmethod
def extract_pull_request_data(payload: Dict[str, Any]) -> Dict[str, Any]:
"""
从 pull request 事件中提取相关数据
Args:
payload: GitHub webhook 负载
Returns:
包含提取数据的字典
"""
return {
"repo_name": payload.get("repository", {}).get("full_name"),
"pull_request_number": payload.get("number"),
"action": payload.get("action"),
"pull_request": payload.get("pull_request", {}),
"user": payload.get("sender", {}).get("login")
}
@staticmethod
def extract_issue_data(payload: Dict[str, Any]) -> Dict[str, Any]:
"""
从 issue 事件中提取相关数据
Args:
payload: GitHub webhook 负载
Returns:
包含提取数据的字典
"""
return {
"repo_name": payload.get("repository", {}).get("full_name"),
"issue_number": payload.get("issue", {}).get("number"),
"action": payload.get("action"),
"issue": payload.get("issue", {}),
"user": payload.get("sender", {}).get("login")
}
@staticmethod
def extract_release_data(payload: Dict[str, Any]) -> Dict[str, Any]:
"""
从 release 事件中提取相关数据
Args:
payload: GitHub webhook 负载
Returns:
包含提取数据的字典
"""
return {
"repo_name": payload.get("repository", {}).get("full_name"),
"release_tag": payload.get("release", {}).get("tag_name"),
"action": payload.get("action"),
"release": payload.get("release", {}),
"user": payload.get("sender", {}).get("login")
}
@staticmethod
def extract_issue_comment_data(payload: Dict[str, Any]) -> Dict[str, Any]:
"""
从 issue comment 事件中提取相关数据
Args:
payload: GitHub webhook 负载
Returns:
包含提取数据的字典
"""
return {
"repo_name": payload.get("repository", {}).get("full_name"),
"issue_number": payload.get("issue", {}).get("number"),
"action": payload.get("action"),
"comment": payload.get("comment", {}),
"user": payload.get("sender", {}).get("login")
}