community.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. """
  2. Data structures to interact with Discussions and Pull Requests on the Hub.
  3. See [the Discussions and Pull Requests guide](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)
  4. for more information on Pull Requests, Discussions, and the community tab.
  5. """
  6. from dataclasses import dataclass
  7. from datetime import datetime
  8. from typing import Literal, TypedDict
  9. from . import constants
  10. from .utils import parse_datetime
  11. DiscussionStatus = Literal["open", "closed", "merged", "draft"]
  12. @dataclass
  13. class Discussion:
  14. """
  15. A Discussion or Pull Request on the Hub.
  16. This dataclass is not intended to be instantiated directly.
  17. Attributes:
  18. title (`str`):
  19. The title of the Discussion / Pull Request
  20. status (`str`):
  21. The status of the Discussion / Pull Request.
  22. It must be one of:
  23. * `"open"`
  24. * `"closed"`
  25. * `"merged"` (only for Pull Requests )
  26. * `"draft"` (only for Pull Requests )
  27. num (`int`):
  28. The number of the Discussion / Pull Request.
  29. repo_id (`str`):
  30. The id (`"{namespace}/{repo_name}"`) of the repo on which
  31. the Discussion / Pull Request was open.
  32. repo_type (`str`):
  33. The type of the repo on which the Discussion / Pull Request was open.
  34. Possible values are: `"model"`, `"dataset"`, `"space"`.
  35. author (`str`):
  36. The username of the Discussion / Pull Request author.
  37. Can be `"deleted"` if the user has been deleted since.
  38. is_pull_request (`bool`):
  39. Whether or not this is a Pull Request.
  40. created_at (`datetime`):
  41. The `datetime` of creation of the Discussion / Pull Request.
  42. endpoint (`str`):
  43. Endpoint of the Hub. Default is https://huggingface.co.
  44. git_reference (`str`, *optional*):
  45. (property) Git reference to which changes can be pushed if this is a Pull Request, `None` otherwise.
  46. url (`str`):
  47. (property) URL of the discussion on the Hub.
  48. """
  49. title: str
  50. status: DiscussionStatus
  51. num: int
  52. repo_id: str
  53. repo_type: str
  54. author: str
  55. is_pull_request: bool
  56. created_at: datetime
  57. endpoint: str
  58. @property
  59. def git_reference(self) -> str | None:
  60. """
  61. If this is a Pull Request , returns the git reference to which changes can be pushed.
  62. Returns `None` otherwise.
  63. """
  64. if self.is_pull_request:
  65. return f"refs/pr/{self.num}"
  66. return None
  67. @property
  68. def url(self) -> str:
  69. """Returns the URL of the discussion on the Hub."""
  70. if self.repo_type is None or self.repo_type == constants.REPO_TYPE_MODEL:
  71. return f"{self.endpoint}/{self.repo_id}/discussions/{self.num}"
  72. return f"{self.endpoint}/{self.repo_type}s/{self.repo_id}/discussions/{self.num}"
  73. @dataclass
  74. class DiscussionWithDetails(Discussion):
  75. """
  76. Subclass of [`Discussion`].
  77. Attributes:
  78. title (`str`):
  79. The title of the Discussion / Pull Request
  80. status (`str`):
  81. The status of the Discussion / Pull Request.
  82. It can be one of:
  83. * `"open"`
  84. * `"closed"`
  85. * `"merged"` (only for Pull Requests )
  86. * `"draft"` (only for Pull Requests )
  87. num (`int`):
  88. The number of the Discussion / Pull Request.
  89. repo_id (`str`):
  90. The id (`"{namespace}/{repo_name}"`) of the repo on which
  91. the Discussion / Pull Request was open.
  92. repo_type (`str`):
  93. The type of the repo on which the Discussion / Pull Request was open.
  94. Possible values are: `"model"`, `"dataset"`, `"space"`.
  95. author (`str`):
  96. The username of the Discussion / Pull Request author.
  97. Can be `"deleted"` if the user has been deleted since.
  98. is_pull_request (`bool`):
  99. Whether or not this is a Pull Request.
  100. created_at (`datetime`):
  101. The `datetime` of creation of the Discussion / Pull Request.
  102. events (`list` of [`DiscussionEvent`])
  103. The list of [`DiscussionEvents`] in this Discussion or Pull Request.
  104. conflicting_files (`Union[list[str], bool, None]`, *optional*):
  105. A list of conflicting files if this is a Pull Request.
  106. `None` if `self.is_pull_request` is `False`.
  107. `True` if there are conflicting files but the list can't be retrieved.
  108. target_branch (`str`, *optional*):
  109. The branch into which changes are to be merged if this is a
  110. Pull Request . `None` if `self.is_pull_request` is `False`.
  111. merge_commit_oid (`str`, *optional*):
  112. If this is a merged Pull Request , this is set to the OID / SHA of
  113. the merge commit, `None` otherwise.
  114. diff (`str`, *optional*):
  115. The git diff if this is a Pull Request , `None` otherwise.
  116. endpoint (`str`):
  117. Endpoint of the Hub. Default is https://huggingface.co.
  118. git_reference (`str`, *optional*):
  119. (property) Git reference to which changes can be pushed if this is a Pull Request, `None` otherwise.
  120. url (`str`):
  121. (property) URL of the discussion on the Hub.
  122. """
  123. events: list["DiscussionEvent"]
  124. conflicting_files: list[str] | bool | None
  125. target_branch: str | None
  126. merge_commit_oid: str | None
  127. diff: str | None
  128. class DiscussionEventArgs(TypedDict):
  129. id: str
  130. type: str
  131. created_at: datetime
  132. author: str
  133. _event: dict
  134. @dataclass
  135. class DiscussionEvent:
  136. """
  137. An event in a Discussion or Pull Request.
  138. Use concrete classes:
  139. * [`DiscussionComment`]
  140. * [`DiscussionStatusChange`]
  141. * [`DiscussionCommit`]
  142. * [`DiscussionTitleChange`]
  143. Attributes:
  144. id (`str`):
  145. The ID of the event. An hexadecimal string.
  146. type (`str`):
  147. The type of the event.
  148. created_at (`datetime`):
  149. A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
  150. object holding the creation timestamp for the event.
  151. author (`str`):
  152. The username of the Discussion / Pull Request author.
  153. Can be `"deleted"` if the user has been deleted since.
  154. """
  155. id: str
  156. type: str
  157. created_at: datetime
  158. author: str
  159. _event: dict
  160. """Stores the original event data, in case we need to access it later."""
  161. @dataclass
  162. class DiscussionComment(DiscussionEvent):
  163. """A comment in a Discussion / Pull Request.
  164. Subclass of [`DiscussionEvent`].
  165. Attributes:
  166. id (`str`):
  167. The ID of the event. An hexadecimal string.
  168. type (`str`):
  169. The type of the event.
  170. created_at (`datetime`):
  171. A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
  172. object holding the creation timestamp for the event.
  173. author (`str`):
  174. The username of the Discussion / Pull Request author.
  175. Can be `"deleted"` if the user has been deleted since.
  176. content (`str`):
  177. The raw markdown content of the comment. Mentions, links and images are not rendered.
  178. edited (`bool`):
  179. Whether or not this comment has been edited.
  180. hidden (`bool`):
  181. Whether or not this comment has been hidden.
  182. """
  183. content: str
  184. edited: bool
  185. hidden: bool
  186. @property
  187. def rendered(self) -> str:
  188. """The rendered comment, as a HTML string"""
  189. return self._event["data"]["latest"]["html"]
  190. @property
  191. def last_edited_at(self) -> datetime:
  192. """The last edit time, as a `datetime` object."""
  193. return parse_datetime(self._event["data"]["latest"]["updatedAt"])
  194. @property
  195. def last_edited_by(self) -> str:
  196. """The last edit time, as a `datetime` object."""
  197. return self._event["data"]["latest"].get("author", {}).get("name", "deleted")
  198. @property
  199. def edit_history(self) -> list[dict]:
  200. """The edit history of the comment"""
  201. return self._event["data"]["history"]
  202. @property
  203. def number_of_edits(self) -> int:
  204. return len(self.edit_history)
  205. @dataclass
  206. class DiscussionStatusChange(DiscussionEvent):
  207. """A change of status in a Discussion / Pull Request.
  208. Subclass of [`DiscussionEvent`].
  209. Attributes:
  210. id (`str`):
  211. The ID of the event. An hexadecimal string.
  212. type (`str`):
  213. The type of the event.
  214. created_at (`datetime`):
  215. A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
  216. object holding the creation timestamp for the event.
  217. author (`str`):
  218. The username of the Discussion / Pull Request author.
  219. Can be `"deleted"` if the user has been deleted since.
  220. new_status (`str`):
  221. The status of the Discussion / Pull Request after the change.
  222. It can be one of:
  223. * `"open"`
  224. * `"closed"`
  225. * `"merged"` (only for Pull Requests )
  226. """
  227. new_status: str
  228. @dataclass
  229. class DiscussionCommit(DiscussionEvent):
  230. """A commit in a Pull Request.
  231. Subclass of [`DiscussionEvent`].
  232. Attributes:
  233. id (`str`):
  234. The ID of the event. An hexadecimal string.
  235. type (`str`):
  236. The type of the event.
  237. created_at (`datetime`):
  238. A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
  239. object holding the creation timestamp for the event.
  240. author (`str`):
  241. The username of the Discussion / Pull Request author.
  242. Can be `"deleted"` if the user has been deleted since.
  243. summary (`str`):
  244. The summary of the commit.
  245. oid (`str`):
  246. The OID / SHA of the commit, as a hexadecimal string.
  247. """
  248. summary: str
  249. oid: str
  250. @dataclass
  251. class DiscussionTitleChange(DiscussionEvent):
  252. """A rename event in a Discussion / Pull Request.
  253. Subclass of [`DiscussionEvent`].
  254. Attributes:
  255. id (`str`):
  256. The ID of the event. An hexadecimal string.
  257. type (`str`):
  258. The type of the event.
  259. created_at (`datetime`):
  260. A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
  261. object holding the creation timestamp for the event.
  262. author (`str`):
  263. The username of the Discussion / Pull Request author.
  264. Can be `"deleted"` if the user has been deleted since.
  265. old_title (`str`):
  266. The previous title for the Discussion / Pull Request.
  267. new_title (`str`):
  268. The new title.
  269. """
  270. old_title: str
  271. new_title: str
  272. def deserialize_event(event: dict) -> DiscussionEvent:
  273. """Instantiates a [`DiscussionEvent`] from a dict"""
  274. event_id: str = event["id"]
  275. event_type: str = event["type"]
  276. created_at = parse_datetime(event["createdAt"])
  277. common_args: DiscussionEventArgs = {
  278. "id": event_id,
  279. "type": event_type,
  280. "created_at": created_at,
  281. "author": event.get("author", {}).get("name", "deleted"),
  282. "_event": event,
  283. }
  284. if event_type == "comment":
  285. return DiscussionComment(
  286. **common_args,
  287. edited=event["data"]["edited"],
  288. hidden=event["data"]["hidden"],
  289. content=event["data"]["latest"]["raw"],
  290. )
  291. if event_type == "status-change":
  292. return DiscussionStatusChange(
  293. **common_args,
  294. new_status=event["data"]["status"],
  295. )
  296. if event_type == "commit":
  297. return DiscussionCommit(
  298. **common_args,
  299. summary=event["data"]["subject"],
  300. oid=event["data"]["oid"],
  301. )
  302. if event_type == "title-change":
  303. return DiscussionTitleChange(
  304. **common_args,
  305. old_title=event["data"]["from"],
  306. new_title=event["data"]["to"],
  307. )
  308. return DiscussionEvent(**common_args)