Spaces:
Build error
Build error
sync with upstream
Browse files
src/gradio_space_ci/webhook.py
CHANGED
|
@@ -57,6 +57,9 @@ if SPACE_ID is not None: # If running in a Space (i.e. not locally)
|
|
| 57 |
|
| 58 |
EPHEMERAL_SPACES_CONFIG: Dict[str, Any] = {}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def enable_space_ci() -> None:
|
| 62 |
"""Enable Space CI for the current Space based on config from the README.md file.
|
|
@@ -201,13 +204,13 @@ background_pool = ThreadPoolExecutor(max_workers=1)
|
|
| 201 |
def recover_after_restart(space_id: str) -> None:
|
| 202 |
print("Looping through PRs to check if any needs to be synced.")
|
| 203 |
for discussion in get_repo_discussions(repo_id=space_id, repo_type="space", discussion_type="pull_request"):
|
| 204 |
-
if discussion.status
|
|
|
|
|
|
|
| 205 |
if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
|
| 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,42 +219,6 @@ def recover_after_restart(space_id: str) -> None:
|
|
| 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 |
###
|
|
@@ -292,7 +259,7 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
|
|
| 292 |
and payload.event.action == "create"
|
| 293 |
and payload.discussion is not None
|
| 294 |
and payload.discussion.isPullRequest
|
| 295 |
-
and payload.discussion.status
|
| 296 |
):
|
| 297 |
# A comment, is it by a command ?
|
| 298 |
if payload.event.scope == "discussion.comment":
|
|
@@ -323,9 +290,7 @@ async def trigger_ci_on_pr(payload: WebhookPayload, task_queue: BackgroundTasks)
|
|
| 323 |
# New repo change. Is it a commit on a PR?
|
| 324 |
# => loop through all PRs and check if new changes happened
|
| 325 |
for discussion in get_repo_discussions(repo_id=space_id, repo_type="space"):
|
| 326 |
-
if discussion.is_pull_request and discussion.status
|
| 327 |
-
# If untrusted author made a modification we unset the config
|
| 328 |
-
handle_modification(space_id=space_id, discussion=discussion)
|
| 329 |
if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
|
| 330 |
# Found a PR that is not yet synced
|
| 331 |
task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
|
|
|
|
| 57 |
|
| 58 |
EPHEMERAL_SPACES_CONFIG: Dict[str, Any] = {}
|
| 59 |
|
| 60 |
+
# Draft and open PRs are considered as active (in opposition to closed and merged PRs)
|
| 61 |
+
ACTIVE_PR_STATUS = ("draft", "open")
|
| 62 |
+
|
| 63 |
|
| 64 |
def enable_space_ci() -> None:
|
| 65 |
"""Enable Space CI for the current Space based on config from the README.md file.
|
|
|
|
| 204 |
def recover_after_restart(space_id: str) -> None:
|
| 205 |
print("Looping through PRs to check if any needs to be synced.")
|
| 206 |
for discussion in get_repo_discussions(repo_id=space_id, repo_type="space", discussion_type="pull_request"):
|
| 207 |
+
if discussion.status in ACTIVE_PR_STATUS:
|
| 208 |
+
# check pr trust status
|
| 209 |
+
handle_modification(space_id=space_id, discussion=discussion)
|
| 210 |
if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
|
| 211 |
# Found a PR that is not yet synced
|
| 212 |
print(f"Recovery. Found an open PR that is not synced: {discussion.url}. Syncing it.")
|
| 213 |
background_pool.submit(sync_ci_space, space_id=space_id, pr_num=discussion.num)
|
|
|
|
|
|
|
| 214 |
if discussion.status == "merged" or discussion.status == "closed":
|
| 215 |
ci_space_id = _get_ci_space_id(space_id=space_id, pr_num=discussion.num)
|
| 216 |
if repo_exists(repo_id=ci_space_id, repo_type="space"):
|
|
|
|
| 219 |
background_pool.submit(delete_ci_space, space_id=space_id, pr_num=discussion.num)
|
| 220 |
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
###
|
| 223 |
# Define webhook on the Hub logic
|
| 224 |
###
|
|
|
|
| 259 |
and payload.event.action == "create"
|
| 260 |
and payload.discussion is not None
|
| 261 |
and payload.discussion.isPullRequest
|
| 262 |
+
and payload.discussion.status in ACTIVE_PR_STATUS
|
| 263 |
):
|
| 264 |
# A comment, is it by a command ?
|
| 265 |
if payload.event.scope == "discussion.comment":
|
|
|
|
| 290 |
# New repo change. Is it a commit on a PR?
|
| 291 |
# => loop through all PRs and check if new changes happened
|
| 292 |
for discussion in get_repo_discussions(repo_id=space_id, repo_type="space"):
|
| 293 |
+
if discussion.is_pull_request and discussion.status in ACTIVE_PR_STATUS:
|
|
|
|
|
|
|
| 294 |
if not is_pr_synced(space_id=space_id, pr_num=discussion.num):
|
| 295 |
# Found a PR that is not yet synced
|
| 296 |
task_queue.add_task(sync_ci_space, space_id=space_id, pr_num=discussion.num)
|