_webhooks_payload.py 3.5 KB

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