not-lain commited on
Commit
c1e7476
·
1 Parent(s): 0fe19a0

update recovery after restart

Browse files
Files changed (1) hide show
  1. src/gradio_space_ci/webhook.py +44 -5
src/gradio_space_ci/webhook.py CHANGED
@@ -206,6 +206,8 @@ def recover_after_restart(space_id: str) -> None:
206
  # Found a PR that is not yet synced
207
  print(f"Recovery. Found an open PR that is not synced: {discussion.url}. Syncing it.")
208
  background_pool.submit(sync_ci_space, space_id=space_id, pr_num=discussion.num)
 
 
209
  if discussion.status == "merged" or discussion.status == "closed":
210
  ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=discussion.num)
211
  if repo_exists(repo_id=ci_space_id, repo_type="space"):
@@ -214,6 +216,42 @@ def recover_after_restart(space_id: str) -> None:
214
  background_pool.submit(delete_ci_space, space_id=space_id, pr_num=discussion.num)
215
 
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  ###
218
  # Define webhook on the Hub logic
219
  ###
@@ -437,7 +475,9 @@ def delete_ci_space(space_id: str, pr_num: int) -> None:
437
  def notify_pr(
438
  space_id: str,
439
  pr_num: int,
440
- action: Literal["created_not_configured", "created_and_configured", "updated", "deleted","trusted_pr","untrusted_pr"],
 
 
441
  ) -> None:
442
  ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=pr_num)
443
  if action == "created_not_configured":
@@ -448,7 +488,7 @@ def notify_pr(
448
  comment = NOTIFICATION_TEMPLATE_UPDATED.format(ci_space_id=ci_space_id)
449
  elif action == "deleted":
450
  comment = NOTIFICATION_TEMPLATE_DELETED
451
- elif action == "trusted_pr":
452
  comment = NOTIFICATION_TEMPLATE_TRUSTED_PR
453
  elif action == "untrusted_pr":
454
  comment = NOTIFICATION_TEMPLATE_UNTRUSTED_PR
@@ -516,11 +556,10 @@ def handle_command(space_id: str, payload: WebhookPayload) -> None:
516
  if event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
517
  if payload.comment.content == "/trust_pr":
518
  set_config(space_id=space_id, pr_num=pr_num)
519
- notify_pr(space_id=space_id,pr_num=pr_num,action="trusted_pr")
520
  elif payload.comment.content == "/untrust_pr":
521
  unset_config(space_id=space_id, pr_num=pr_num)
522
- notify_pr(space_id=space_id,pr_num=pr_num,action="untrusted_pr")
523
-
524
 
525
 
526
  NOTIFICATION_TEMPLATE_CREATED_AND_CONFIGURED = """\
 
206
  # Found a PR that is not yet synced
207
  print(f"Recovery. Found an open PR that is not synced: {discussion.url}. Syncing it.")
208
  background_pool.submit(sync_ci_space, space_id=space_id, pr_num=discussion.num)
209
+ # Check if this pr should be trusted or not
210
+ handle_trust_status(space_id=space_id, pr_num=discussion.num)
211
  if discussion.status == "merged" or discussion.status == "closed":
212
  ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=discussion.num)
213
  if repo_exists(repo_id=ci_space_id, repo_type="space"):
 
216
  background_pool.submit(delete_ci_space, space_id=space_id, pr_num=discussion.num)
217
 
218
 
219
+ def handle_trust_status(space_id: str, pr_num: int) -> None:
220
+ """after recovering we check each pr if it is trusted or not"""
221
+ events = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=pr_num).events
222
+ # flip history, last event is the first one
223
+ events.reverse()
224
+ for event in events:
225
+ # a commit and no commands found yet
226
+ # is it by a trusted author or not ?
227
+ if event.type == "commit":
228
+ if event.author not in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
229
+ # a commit by untrusted author
230
+ unset_config(space_id=space_id, pr_num=pr_num)
231
+ return
232
+ else:
233
+ # a commit by a trusted author
234
+ set_config(space_id=space_id, pr_num=pr_num)
235
+
236
+ # a comment by a trusted author
237
+ elif (
238
+ event.type == "comment"
239
+ and event.author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]
240
+ and event._event["data"]["edited"] is False
241
+ and event._event["data"]["hidden"] is False
242
+ ):
243
+ comment = event._event["data"]["latest"]["raw"]
244
+ if comment == "/trust_pr":
245
+ set_config(space_id=space_id, pr_num=pr_num)
246
+ return
247
+ elif comment == "/untrust_pr":
248
+ unset_config(space_id=space_id, pr_num=pr_num)
249
+ return
250
+ elif "<em>(This is an automated message.)</em>" in comment:
251
+ # synced and no commands yet
252
+ return
253
+
254
+
255
  ###
256
  # Define webhook on the Hub logic
257
  ###
 
475
  def notify_pr(
476
  space_id: str,
477
  pr_num: int,
478
+ action: Literal[
479
+ "created_not_configured", "created_and_configured", "updated", "deleted", "trusted_pr", "untrusted_pr"
480
+ ],
481
  ) -> None:
482
  ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=pr_num)
483
  if action == "created_not_configured":
 
488
  comment = NOTIFICATION_TEMPLATE_UPDATED.format(ci_space_id=ci_space_id)
489
  elif action == "deleted":
490
  comment = NOTIFICATION_TEMPLATE_DELETED
491
+ elif action == "trusted_pr":
492
  comment = NOTIFICATION_TEMPLATE_TRUSTED_PR
493
  elif action == "untrusted_pr":
494
  comment = NOTIFICATION_TEMPLATE_UNTRUSTED_PR
 
556
  if event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
557
  if payload.comment.content == "/trust_pr":
558
  set_config(space_id=space_id, pr_num=pr_num)
559
+ notify_pr(space_id=space_id, pr_num=pr_num, action="trusted_pr")
560
  elif payload.comment.content == "/untrust_pr":
561
  unset_config(space_id=space_id, pr_num=pr_num)
562
+ notify_pr(space_id=space_id, pr_num=pr_num, action="untrusted_pr")
 
563
 
564
 
565
  NOTIFICATION_TEMPLATE_CREATED_AND_CONFIGURED = """\