_webhooks_payload.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # coding=utf-8
  2. # Copyright 2023-present, the HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Contains data structures to parse the webhooks payload."""
  16. from typing import List, Literal, Optional
  17. from .utils import is_pydantic_available
  18. if is_pydantic_available():
  19. from pydantic import BaseModel
  20. else:
  21. # Define a dummy BaseModel to avoid import errors when pydantic is not installed
  22. # Import error will be raised when trying to use the class
  23. class BaseModel: # type: ignore [no-redef]
  24. def __init__(self, *args, **kwargs) -> None:
  25. raise ImportError(
  26. "You must have `pydantic` installed to use `WebhookPayload`. This is an optional dependency that"
  27. " should be installed separately. Please run `pip install --upgrade pydantic` and retry."
  28. )
  29. # This is an adaptation of the ReportV3 interface implemented in moon-landing. V0, V1 and V2 have been ignored as they
  30. # are not in used anymore. To keep in sync when format is updated in
  31. # https://github.com/huggingface/moon-landing/blob/main/server/lib/HFWebhooks.ts (internal link).
  32. WebhookEvent_T = Literal[
  33. "create",
  34. "delete",
  35. "move",
  36. "update",
  37. ]
  38. RepoChangeEvent_T = Literal[
  39. "add",
  40. "move",
  41. "remove",
  42. "update",
  43. ]
  44. RepoType_T = Literal[
  45. "dataset",
  46. "model",
  47. "space",
  48. ]
  49. DiscussionStatus_T = Literal[
  50. "closed",
  51. "draft",
  52. "open",
  53. "merged",
  54. ]
  55. SupportedWebhookVersion = Literal[3]
  56. class ObjectId(BaseModel):
  57. id: str
  58. class WebhookPayloadUrl(BaseModel):
  59. web: str
  60. api: Optional[str] = None
  61. class WebhookPayloadMovedTo(BaseModel):
  62. name: str
  63. owner: ObjectId
  64. class WebhookPayloadWebhook(ObjectId):
  65. version: SupportedWebhookVersion
  66. class WebhookPayloadEvent(BaseModel):
  67. action: WebhookEvent_T
  68. scope: str
  69. class WebhookPayloadDiscussionChanges(BaseModel):
  70. base: str
  71. mergeCommitId: Optional[str] = None
  72. class WebhookPayloadComment(ObjectId):
  73. author: ObjectId
  74. hidden: bool
  75. content: Optional[str] = None
  76. url: WebhookPayloadUrl
  77. class WebhookPayloadDiscussion(ObjectId):
  78. num: int
  79. author: ObjectId
  80. url: WebhookPayloadUrl
  81. title: str
  82. isPullRequest: bool
  83. status: DiscussionStatus_T
  84. changes: Optional[WebhookPayloadDiscussionChanges] = None
  85. pinned: Optional[bool] = None
  86. class WebhookPayloadRepo(ObjectId):
  87. owner: ObjectId
  88. head_sha: Optional[str] = None
  89. name: str
  90. private: bool
  91. subdomain: Optional[str] = None
  92. tags: Optional[List[str]] = None
  93. type: Literal["dataset", "model", "space"]
  94. url: WebhookPayloadUrl
  95. class WebhookPayloadUpdatedRef(BaseModel):
  96. ref: str
  97. oldSha: Optional[str] = None
  98. newSha: Optional[str] = None
  99. class WebhookPayload(BaseModel):
  100. event: WebhookPayloadEvent
  101. repo: WebhookPayloadRepo
  102. discussion: Optional[WebhookPayloadDiscussion] = None
  103. comment: Optional[WebhookPayloadComment] = None
  104. webhook: WebhookPayloadWebhook
  105. movedTo: Optional[WebhookPayloadMovedTo] = None
  106. updatedRefs: Optional[List[WebhookPayloadUpdatedRef]] = None