Spaces:
Sleeping
Sleeping
separate into functions
Browse files- src/gradio_space_ci/webhook.py +31 -20
src/gradio_space_ci/webhook.py
CHANGED
@@ -256,21 +256,9 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
|
|
256 |
and payload.discussion.isPullRequest
|
257 |
and payload.discussion.status == "open"
|
258 |
):
|
259 |
-
# A comment, is it by a
|
260 |
if payload.event.scope == "discussion.comment":
|
261 |
-
|
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
|
@@ -298,12 +286,8 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
|
|
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 |
-
#
|
302 |
-
|
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)
|
@@ -509,6 +493,33 @@ def unset_config(space_id: str, pr_num: int) -> None:
|
|
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.
|
|
|
256 |
and payload.discussion.isPullRequest
|
257 |
and payload.discussion.status == "open"
|
258 |
):
|
259 |
+
# A comment, is it by a command ?
|
260 |
if payload.event.scope == "discussion.comment":
|
261 |
+
handle_command(space_id=space_id, payload=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
# Always sync (in case the space was sleeping or building)
|
263 |
if not is_pr_synced(space_id=space_id, pr_num=payload.discussion.num):
|
264 |
# New PR! Sync task scheduled
|
|
|
286 |
# => loop through all PRs and check if new changes happened
|
287 |
for discussion in get_repo_discussions(repo_id=space_id, repo_type="space"):
|
288 |
if discussion.is_pull_request and discussion.status == "open":
|
289 |
+
# If untrusted author made a modification we unset the config
|
290 |
+
handle_modification(space_id=space_id, discussion=discussion)
|
|
|
|
|
|
|
|
|
291 |
if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
|
292 |
# Found a PR that is not yet synced
|
293 |
task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
|
|
|
493 |
delete_space_storage(ci_space_id)
|
494 |
|
495 |
|
496 |
+
def handle_modification(space_id: str, discussion: Any) -> None:
|
497 |
+
ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=discussion.num)
|
498 |
+
if not repo_exists(ci_space_id):
|
499 |
+
return
|
500 |
+
details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=discussion.num)
|
501 |
+
event_author = details.events[-1]._event["author"]["name"] # username of that event
|
502 |
+
if event_author not in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
|
503 |
+
# Untrusted author, we unset the config as part or security reasons
|
504 |
+
unset_config(space_id=space_id, pr_num=discussion.num)
|
505 |
+
|
506 |
+
|
507 |
+
def handle_command(space_id: str, payload: WebhookPayload) -> None:
|
508 |
+
"""when a trusted author writes a command we handle it"""
|
509 |
+
pr_num = payload.discussion.num
|
510 |
+
details = get_discussion_details(repo_id=space_id, repo_type="space", discussion_num=pr_num)
|
511 |
+
event_author = details.events[-1]._event["author"]["name"] # username of that event
|
512 |
+
if event_author in EPHEMERAL_SPACES_CONFIG["trusted_authors"]:
|
513 |
+
if payload.comment.content == "/trust_pr":
|
514 |
+
print("trusting pr ...")
|
515 |
+
set_config(space_id=space_id, pr_num=pr_num)
|
516 |
+
print("pr has been trusted")
|
517 |
+
elif payload.comment.content == "/untrust_pr":
|
518 |
+
print("untrusting pr ...")
|
519 |
+
unset_config(space_id=space_id, pr_num=pr_num)
|
520 |
+
print("pr has been untrusted")
|
521 |
+
|
522 |
+
|
523 |
NOTIFICATION_TEMPLATE_CREATED_AND_CONFIGURED = """\
|
524 |
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.
|
525 |
Since this PR has been created by a trusted author, the ephemeral Space has been configured with the correct hardware, storage, and secrets.
|