Wauplin HF staff commited on
Commit
243864a
1 Parent(s): f17f531

more logs + delete repo

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  from pathlib import Path
3
  from typing import Literal, Optional
@@ -19,6 +21,8 @@ from huggingface_hub.repocard import RepoCard
19
  from pydantic import BaseModel
20
  from requests import HTTPError
21
 
 
 
22
  WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
23
  HF_TOKEN = os.getenv("HF_TOKEN")
24
 
@@ -37,6 +41,7 @@ class WebhookPayloadRepo(BaseModel):
37
  class WebhookPayloadDiscussion(BaseModel):
38
  num: int
39
  isPullRequest: bool
 
40
 
41
 
42
  class WebhookPayload(BaseModel):
@@ -59,22 +64,25 @@ async def post_webhook(
59
  task_queue: BackgroundTasks,
60
  x_webhook_secret: Optional[str] = Header(default=None),
61
  ):
62
- # Taken from https://huggingface.co/spaces/huggingface-projects/auto-retrain
63
  if x_webhook_secret is None:
 
64
  raise HTTPException(401)
65
  if x_webhook_secret != WEBHOOK_SECRET:
 
66
  raise HTTPException(403)
67
 
68
  if payload.repo.type != "space":
 
69
  raise HTTPException(400, f"Must be a Space, not {payload.repo.type}")
70
 
71
  space_id = payload.repo.name
72
 
73
  if (
74
  payload.event.scope.startswith("discussion")
 
75
  and payload.discussion is not None
76
  and payload.discussion.isPullRequest
77
- and payload.event.action == "create"
78
  ):
79
  # New PR!
80
  task_queue.add_task(
@@ -83,12 +91,28 @@ async def post_webhook(
83
  pr_num=payload.discussion.num,
84
  private=payload.repo.private,
85
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  elif (
87
  payload.event.scope.startswith("repo.content")
88
  and payload.event.action == "update"
89
  ):
90
  # New repo change. Is it a commit on a PR?
91
  # => loop through all PRs and check if new changes happened
 
92
  for discussion in get_repo_discussions(
93
  repo_id=space_id, repo_type="space", token=HF_TOKEN
94
  ):
@@ -100,15 +124,13 @@ async def post_webhook(
100
  pr_num=discussion.num,
101
  private=payload.repo.private,
102
  )
103
- elif False:
104
- # PR merged!
105
- task_queue.add_task(
106
- delete_ci_space,
107
- space_id=payload.repo.name,
108
- pr_num=payload.discussion.num,
109
- )
110
 
111
- return "Processed."
 
112
 
113
 
114
  def is_pr_synced(space_id: str, pr_num: int) -> bool:
 
1
+ # Taken from https://huggingface.co/spaces/huggingface-projects/auto-retrain
2
+ import logging
3
  import os
4
  from pathlib import Path
5
  from typing import Literal, Optional
 
21
  from pydantic import BaseModel
22
  from requests import HTTPError
23
 
24
+ logger = logging.getLogger(__file__)
25
+
26
  WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
27
  HF_TOKEN = os.getenv("HF_TOKEN")
28
 
 
41
  class WebhookPayloadDiscussion(BaseModel):
42
  num: int
43
  isPullRequest: bool
44
+ status: Literal["open", "closed", "merged"]
45
 
46
 
47
  class WebhookPayload(BaseModel):
 
64
  task_queue: BackgroundTasks,
65
  x_webhook_secret: Optional[str] = Header(default=None),
66
  ):
67
+ logger.info("Received new hook!")
68
  if x_webhook_secret is None:
69
+ logger.warning("HTTP 401: No webhook secret")
70
  raise HTTPException(401)
71
  if x_webhook_secret != WEBHOOK_SECRET:
72
+ logger.warning("HTTP 403: wrong webhook secret")
73
  raise HTTPException(403)
74
 
75
  if payload.repo.type != "space":
76
+ logger.warning("HTTP 400: not a space")
77
  raise HTTPException(400, f"Must be a Space, not {payload.repo.type}")
78
 
79
  space_id = payload.repo.name
80
 
81
  if (
82
  payload.event.scope.startswith("discussion")
83
+ and payload.event.action == "create"
84
  and payload.discussion is not None
85
  and payload.discussion.isPullRequest
 
86
  ):
87
  # New PR!
88
  task_queue.add_task(
 
91
  pr_num=payload.discussion.num,
92
  private=payload.repo.private,
93
  )
94
+ logger.info("New PR! Sync task scheduled")
95
+ elif (
96
+ payload.event.scope.startswith("discussion")
97
+ and payload.event.action == "update"
98
+ and payload.discussion is not None
99
+ and payload.discussion.isPullRequest
100
+ and payload.discussion.status == "merged"
101
+ ):
102
+ # PR merged!
103
+ task_queue.add_task(
104
+ delete_ci_space,
105
+ space_id=space_id,
106
+ pr_num=payload.discussion.num,
107
+ )
108
+ logger.info("PR is merged! Delete task scheduled")
109
  elif (
110
  payload.event.scope.startswith("repo.content")
111
  and payload.event.action == "update"
112
  ):
113
  # New repo change. Is it a commit on a PR?
114
  # => loop through all PRs and check if new changes happened
115
+ logger.info("New repo content update. Checking PRs state.")
116
  for discussion in get_repo_discussions(
117
  repo_id=space_id, repo_type="space", token=HF_TOKEN
118
  ):
 
124
  pr_num=discussion.num,
125
  private=payload.repo.private,
126
  )
127
+ logger.info(f"Scheduled update for PR {discussion.num}.")
128
+ logger.info(f"Done looping over PRs.")
129
+ else:
130
+ logger.info(f"Webhook ignored.")
 
 
 
131
 
132
+ logger.info(f"Done.")
133
+ return {"processed": True}
134
 
135
 
136
  def is_pr_synced(space_id: str, pr_num: int) -> bool: