burtenshaw commited on
Commit
d0a5416
Β·
1 Parent(s): 49da546

adapt application to actual webhook event

Browse files
Files changed (1) hide show
  1. app.py +42 -9
app.py CHANGED
@@ -148,7 +148,8 @@ async def process_webhook_comment(webhook_data: Dict[str, Any]):
148
  discussion_title = webhook_data["discussion"]["title"]
149
  repo_name = webhook_data["repo"]["name"]
150
  discussion_num = webhook_data["discussion"]["num"]
151
- comment_author = webhook_data["comment"]["author"]
 
152
 
153
  # Extract potential tags from the comment and discussion title
154
  comment_tags = extract_tags_from_text(comment_content)
@@ -215,7 +216,7 @@ async def process_webhook_comment(webhook_data: Dict[str, Any]):
215
  # If no JSON found, use the response as is
216
  msg = f"Tag '{tag}': {response_text}"
217
 
218
- except Exception as parse_error:
219
  msg = f"Tag '{tag}': Response parse error - {response_text}"
220
 
221
  result_messages.append(msg)
@@ -249,16 +250,40 @@ async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
249
  """Handle HF Hub webhooks"""
250
  webhook_secret = request.headers.get("X-Webhook-Secret")
251
  if webhook_secret != WEBHOOK_SECRET:
 
252
  return {"error": "Invalid webhook secret"}
253
 
254
  payload = await request.json()
 
 
255
  event = payload.get("event", {})
 
 
 
 
 
 
 
 
 
 
 
256
 
257
- scope_check = event.get("scope") == "discussion.comment"
258
- if event.get("action") == "create" and scope_check:
 
 
 
 
 
 
 
 
 
259
  background_tasks.add_task(process_webhook_comment, payload)
260
  return {"status": "processing"}
261
 
 
262
  return {"status": "ignored"}
263
 
264
 
@@ -270,17 +295,25 @@ async def simulate_webhook(
270
  return "Please fill in all fields."
271
 
272
  mock_payload = {
273
- "event": {"action": "create", "scope": "discussion.comment"},
274
  "comment": {
275
  "content": comment_content,
276
- "author": "test-user",
277
- "created_at": datetime.now().isoformat(),
 
278
  },
279
  "discussion": {
280
  "title": discussion_title,
281
  "num": len(tag_operations_store) + 1,
 
 
 
 
 
 
 
 
282
  },
283
- "repo": {"name": repo_name},
284
  }
285
 
286
  response = await process_webhook_comment(mock_payload)
@@ -326,7 +359,7 @@ def create_gradio_app():
326
  sim_result = gr.Textbox(label="Result", lines=8)
327
 
328
  sim_btn.click(
329
- simulate_webhook,
330
  inputs=[sim_repo, sim_title, sim_comment],
331
  outputs=sim_result,
332
  )
 
148
  discussion_title = webhook_data["discussion"]["title"]
149
  repo_name = webhook_data["repo"]["name"]
150
  discussion_num = webhook_data["discussion"]["num"]
151
+ # Author is an object with "id" field
152
+ comment_author = webhook_data["comment"]["author"].get("id", "unknown")
153
 
154
  # Extract potential tags from the comment and discussion title
155
  comment_tags = extract_tags_from_text(comment_content)
 
216
  # If no JSON found, use the response as is
217
  msg = f"Tag '{tag}': {response_text}"
218
 
219
+ except Exception:
220
  msg = f"Tag '{tag}': Response parse error - {response_text}"
221
 
222
  result_messages.append(msg)
 
250
  """Handle HF Hub webhooks"""
251
  webhook_secret = request.headers.get("X-Webhook-Secret")
252
  if webhook_secret != WEBHOOK_SECRET:
253
+ print("❌ Invalid webhook secret")
254
  return {"error": "Invalid webhook secret"}
255
 
256
  payload = await request.json()
257
+ print(f"πŸ“₯ Received webhook payload: {json.dumps(payload, indent=2)}")
258
+
259
  event = payload.get("event", {})
260
+ scope = event.get("scope")
261
+ action = event.get("action")
262
+
263
+ print(f"πŸ” Event details - scope: {scope}, action: {action}")
264
+
265
+ # Check if this is a discussion comment creation
266
+ scope_check = scope == "discussion"
267
+ action_check = action == "create"
268
+
269
+ print(f"βœ… scope_check: {scope_check}")
270
+ print(f"βœ… action_check: {action_check}")
271
 
272
+ if scope_check and action_check:
273
+ # Verify we have the required fields
274
+ required_fields = ["comment", "discussion", "repo"]
275
+ missing_fields = [field for field in required_fields if field not in payload]
276
+
277
+ if missing_fields:
278
+ error_msg = f"Missing required fields: {missing_fields}"
279
+ print(f"❌ {error_msg}")
280
+ return {"error": error_msg}
281
+
282
+ print(f"πŸš€ Processing webhook for repo: {payload['repo']['name']}")
283
  background_tasks.add_task(process_webhook_comment, payload)
284
  return {"status": "processing"}
285
 
286
+ print(f"⏭️ Ignoring webhook - scope: {scope}, action: {action}")
287
  return {"status": "ignored"}
288
 
289
 
 
295
  return "Please fill in all fields."
296
 
297
  mock_payload = {
298
+ "event": {"action": "create", "scope": "discussion"},
299
  "comment": {
300
  "content": comment_content,
301
+ "author": {"id": "test-user-id"},
302
+ "id": "mock-comment-id",
303
+ "hidden": False,
304
  },
305
  "discussion": {
306
  "title": discussion_title,
307
  "num": len(tag_operations_store) + 1,
308
+ "id": "mock-discussion-id",
309
+ "status": "open",
310
+ "isPullRequest": False,
311
+ },
312
+ "repo": {
313
+ "name": repo_name,
314
+ "type": "model",
315
+ "private": False,
316
  },
 
317
  }
318
 
319
  response = await process_webhook_comment(mock_payload)
 
359
  sim_result = gr.Textbox(label="Result", lines=8)
360
 
361
  sim_btn.click(
362
+ fn=simulate_webhook,
363
  inputs=[sim_repo, sim_title, sim_comment],
364
  outputs=sim_result,
365
  )