not-lain commited on
Commit
126f2de
1 Parent(s): 754e7df

rollback and switch to a more vanilla method

Browse files
Files changed (1) hide show
  1. src/gradio_space_ci/webhook.py +30 -34
src/gradio_space_ci/webhook.py CHANGED
@@ -30,7 +30,7 @@ from huggingface_hub import (
30
  upload_folder,
31
  delete_space_secret,
32
  delete_space_variable,
33
- delete_space_storage
34
  )
35
  from huggingface_hub.repocard import RepoCard
36
  from huggingface_hub.utils import (
@@ -248,22 +248,33 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
248
  space_id = payload.repo.name
249
 
250
  has_task = False
251
-
252
- pr_num = payload.discussion.num
253
- details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=pr_num)
254
- event_author = details.events[-1]._event["author"]["name"] # username of that event
255
-
256
  if (
257
- # Means "a new PR has been opened"
258
  payload.event.scope.startswith("discussion")
259
  and payload.event.action == "create"
260
  and payload.discussion is not None
261
  and payload.discussion.isPullRequest
262
  and payload.discussion.status == "open"
263
  ):
264
- if not is_pr_synced(space_id=space_id, pr_num=pr_num):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  # New PR! Sync task scheduled
266
- task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=pr_num)
267
  has_task = True
268
  elif (
269
  # Means "a PR has been merged or closed"
@@ -276,7 +287,7 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
276
  task_queue.add_task(
277
  delete_ci_space,
278
  space_id=space_id,
279
- pr_num=pr_num,
280
  )
281
  has_task = True
282
  elif (
@@ -287,30 +298,16 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
287
  # => loop through all PRs and check if new changes happened
288
  for discussion in get_repo_discussions(repo_id=space_id, repo_type="space"):
289
  if discussion.is_pull_request and discussion.status == "open":
 
 
 
 
 
 
290
  if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
291
  # Found a PR that is not yet synced
292
- if event_author not in EPHEMERAL_SPACES_CONFIG["trusted_authors"] :
293
- # if change by untrusted author unset config
294
- unset_config(space_id=space_id,pr_num=pr_num)
295
  task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
296
  has_task = True
297
- if (
298
- # Means comment by a trusted author
299
- payload.event.scope == "discussion.comment"
300
- and payload.event.action == "create"
301
- and payload.discussion.isPullRequest
302
- and payload.discussion.status == "open"
303
- and event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]
304
- ):
305
- print("Comment detected with content:\n", payload.comment.content)
306
- if payload.comment.content == "/trust_pr":
307
- print("trusting pr ...")
308
- set_config(space_id=space_id,pr_num=pr_num)
309
- print("pr has been trusted")
310
- elif payload.comment.content == "/untrust_pr":
311
- print("untrusting pr ...")
312
- unset_config(space_id=space_id,pr_num=pr_num)
313
- print("pr has been untrusted")
314
 
315
  if has_task:
316
  return Response("Task scheduled to sync/delete Space", status_code=status.HTTP_202_ACCEPTED)
@@ -503,16 +500,15 @@ def unset_config(space_id: str, pr_num: int) -> None:
503
  variables: Dict[str, str] = EPHEMERAL_SPACES_CONFIG["variables"]
504
  secrets: Dict[str, str] = EPHEMERAL_SPACES_CONFIG["secrets"]
505
  # Unset space variables and secrets
506
- for key in variables.keys():
507
  delete_space_variable(ci_space_id, key)
508
- for key in secrets.keys():
509
  delete_space_secret(ci_space_id, key)
510
  # Reset hardware
511
- request_space_hardware(ci_space_id,SpaceHardware.CPU_BASIC)
512
  delete_space_storage(ci_space_id)
513
 
514
 
515
-
516
  NOTIFICATION_TEMPLATE_CREATED_AND_CONFIGURED = """\
517
  Following the creation of this PR, an ephemeral Space [{ci_space_id}](https://huggingface.co/spaces/{ci_space_id}) has been started. Any changes pushed to this PR will be synced with the test Space.
518
  Since this PR has been created by a trusted author, the ephemeral Space has been configured with the correct hardware, storage, and secrets.
 
30
  upload_folder,
31
  delete_space_secret,
32
  delete_space_variable,
33
+ delete_space_storage,
34
  )
35
  from huggingface_hub.repocard import RepoCard
36
  from huggingface_hub.utils import (
 
248
  space_id = payload.repo.name
249
 
250
  has_task = False
 
 
 
 
 
251
  if (
252
+ # Means "a new PR has been opened" or a new comment
253
  payload.event.scope.startswith("discussion")
254
  and payload.event.action == "create"
255
  and payload.discussion is not None
256
  and payload.discussion.isPullRequest
257
  and payload.discussion.status == "open"
258
  ):
259
+ # A comment, is it by a trusted author ?
260
+ if payload.event.scope == "discussion.comment":
261
+ pr_num = payload.discussion.num
262
+ details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=pr_num)
263
+ event_author = details.events[-1]._event["author"]["name"] # username of that event
264
+ if event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
265
+ print("Comment detected with content:\n", payload.comment.content)
266
+ if payload.comment.content == "/trust_pr":
267
+ print("trusting pr ...")
268
+ set_config(space_id=space_id, pr_num=pr_num)
269
+ print("pr has been trusted")
270
+ elif payload.comment.content == "/untrust_pr":
271
+ print("untrusting pr ...")
272
+ unset_config(space_id=space_id, pr_num=pr_num)
273
+ print("pr has been untrusted")
274
+ # Always sync (in case the space was sleeping or building)
275
+ if not is_pr_synced(space_id=space_id, pr_num=payload.discussion.num):
276
  # New PR! Sync task scheduled
277
+ task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=payload.discussion.num)
278
  has_task = True
279
  elif (
280
  # Means "a PR has been merged or closed"
 
287
  task_queue.add_task(
288
  delete_ci_space,
289
  space_id=space_id,
290
+ pr_num=payload.discussion.num,
291
  )
292
  has_task = True
293
  elif (
 
298
  # => loop through all PRs and check if new changes happened
299
  for discussion in get_repo_discussions(repo_id=space_id, repo_type="space"):
300
  if discussion.is_pull_request and discussion.status == "open":
301
+ # Was the pr updated by an untrusted author ?
302
+ details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=discussion.num)
303
+ event_author = details.events[-1]._event["author"]["name"] # username of that event
304
+ if event_author not in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
305
+ # Untrusted author, we unset the config as part or security reasons
306
+ unset_config(space_id=space_id, pr_num=discussion.num)
307
  if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
308
  # Found a PR that is not yet synced
 
 
 
309
  task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
310
  has_task = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
 
312
  if has_task:
313
  return Response("Task scheduled to sync/delete Space", status_code=status.HTTP_202_ACCEPTED)
 
500
  variables: Dict[str, str] = EPHEMERAL_SPACES_CONFIG["variables"]
501
  secrets: Dict[str, str] = EPHEMERAL_SPACES_CONFIG["secrets"]
502
  # Unset space variables and secrets
503
+ for key in variables.keys():
504
  delete_space_variable(ci_space_id, key)
505
+ for key in secrets.keys():
506
  delete_space_secret(ci_space_id, key)
507
  # Reset hardware
508
+ request_space_hardware(ci_space_id, SpaceHardware.CPU_BASIC)
509
  delete_space_storage(ci_space_id)
510
 
511
 
 
512
  NOTIFICATION_TEMPLATE_CREATED_AND_CONFIGURED = """\
513
  Following the creation of this PR, an ephemeral Space [{ci_space_id}](https://huggingface.co/spaces/{ci_space_id}) has been started. Any changes pushed to this PR will be synced with the test Space.
514
  Since this PR has been created by a trusted author, the ephemeral Space has been configured with the correct hardware, storage, and secrets.