not-lain commited on
Commit
2e2c59c
·
1 Parent(s): 8f20ebc

using get_discussion_details to get author

Browse files
Files changed (1) hide show
  1. src/gradio_space_ci/webhook.py +17 -14
src/gradio_space_ci/webhook.py CHANGED
@@ -245,6 +245,20 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
245
  space_id = payload.repo.name
246
 
247
  has_task = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  if (
249
  # Means "a new PR has been opened"
250
  payload.event.scope.startswith("discussion")
@@ -253,9 +267,9 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
253
  and payload.discussion.isPullRequest
254
  and payload.discussion.status == "open"
255
  ):
256
- if not is_pr_synced(space_id=space_id, pr_num=payload.discussion.num):
257
  # New PR! Sync task scheduled
258
- task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=payload.discussion.num)
259
  has_task = True
260
  elif (
261
  # Means "a PR has been merged or closed"
@@ -268,7 +282,7 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
268
  task_queue.add_task(
269
  delete_ci_space,
270
  space_id=space_id,
271
- pr_num=payload.discussion.num,
272
  )
273
  has_task = True
274
  elif (
@@ -283,17 +297,6 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
283
  # Found a PR that is not yet synced
284
  task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
285
  has_task = True
286
- if (
287
- payload.event.scope == "discussion.comment"
288
- and payload.event.action == "create"
289
- and payload.discussion.isPullRequest
290
- and payload.discussion.status == "open"
291
- # TODO:
292
- # figure out how to get the username from the id
293
- # and payload.discussion.author.id in EPHEMERAL_SPACES_CONFIG["trusted_authors"]
294
- ):
295
- print("Comment detected with content:\n ", payload.comment.content)
296
-
297
 
298
  if has_task:
299
  return Response("Task scheduled to sync/delete Space", status_code=status.HTTP_202_ACCEPTED)
 
245
  space_id = payload.repo.name
246
 
247
  has_task = False
248
+
249
+ pr_num = payload.discussion.num
250
+ details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=pr_num)
251
+ event_author = details.events[-1]._event["author"]["name"] # username of that event
252
+ if (
253
+ # Means comment by a trusted author
254
+ payload.event.scope == "discussion.comment"
255
+ and payload.event.action == "create"
256
+ and payload.discussion.isPullRequest
257
+ and payload.discussion.status == "open"
258
+ and event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]
259
+ ):
260
+ print("Comment detected with content:\n ", payload.comment.content)
261
+
262
  if (
263
  # Means "a new PR has been opened"
264
  payload.event.scope.startswith("discussion")
 
267
  and payload.discussion.isPullRequest
268
  and payload.discussion.status == "open"
269
  ):
270
+ if not is_pr_synced(space_id=space_id, pr_num=pr_num):
271
  # New PR! Sync task scheduled
272
+ task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=pr_num)
273
  has_task = True
274
  elif (
275
  # Means "a PR has been merged or closed"
 
282
  task_queue.add_task(
283
  delete_ci_space,
284
  space_id=space_id,
285
+ pr_num=pr_num,
286
  )
287
  has_task = True
288
  elif (
 
297
  # Found a PR that is not yet synced
298
  task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
299
  has_task = True
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  if has_task:
302
  return Response("Task scheduled to sync/delete Space", status_code=status.HTTP_202_ACCEPTED)