lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //! Definitions for GitHub GraphQL.
  2. //!
  3. //! See <https://docs.github.com/en/graphql> for more GitHub's GraphQL API.
  4. // This schema can be downloaded from https://docs.github.com/public/schema.docs.graphql
  5. pub mod queries {
  6. use super::schema;
  7. pub type Date = chrono::NaiveDate;
  8. pub type DateTime = chrono::DateTime<chrono::Utc>;
  9. cynic::impl_scalar!(Date, schema::Date);
  10. cynic::impl_scalar!(DateTime, schema::DateTime);
  11. #[derive(cynic::QueryVariables, Debug, Clone)]
  12. pub struct LeastRecentlyReviewedPullRequestsArguments<'a> {
  13. pub repository_owner: &'a str,
  14. pub repository_name: &'a str,
  15. pub after: Option<String>,
  16. }
  17. #[derive(cynic::QueryFragment, Debug)]
  18. #[cynic(
  19. graphql_type = "Query",
  20. variables = "LeastRecentlyReviewedPullRequestsArguments"
  21. )]
  22. pub struct LeastRecentlyReviewedPullRequests {
  23. #[arguments(owner: $repository_owner, name: $repository_name)]
  24. pub repository: Option<Repository>,
  25. }
  26. #[derive(cynic::QueryFragment, Debug)]
  27. #[cynic(variables = "LeastRecentlyReviewedPullRequestsArguments")]
  28. pub struct Repository {
  29. #[arguments(
  30. states: "OPEN",
  31. first: 100,
  32. after: $after,
  33. labels: ["S-waiting-on-review"],
  34. orderBy: {direction: "ASC", field: "UPDATED_AT"}
  35. )]
  36. pub pull_requests: PullRequestConnection,
  37. }
  38. #[derive(cynic::QueryFragment, Debug)]
  39. pub struct PullRequestConnection {
  40. pub total_count: i32,
  41. pub page_info: PageInfo,
  42. #[cynic(flatten)]
  43. pub nodes: Vec<PullRequest>,
  44. }
  45. #[derive(cynic::QueryFragment, Debug)]
  46. pub struct PullRequest {
  47. pub number: i32,
  48. pub created_at: DateTime,
  49. pub url: Uri,
  50. pub title: String,
  51. #[arguments(first = 100)]
  52. pub labels: Option<LabelConnection>,
  53. pub is_draft: bool,
  54. #[arguments(first = 100)]
  55. pub assignees: UserConnection,
  56. #[arguments(first = 100, orderBy: { direction: "DESC", field: "UPDATED_AT" })]
  57. pub comments: IssueCommentConnection,
  58. #[arguments(last = 20)]
  59. pub latest_reviews: Option<PullRequestReviewConnection>,
  60. }
  61. #[derive(cynic::QueryFragment, Debug)]
  62. pub struct PullRequestReviewConnection {
  63. pub total_count: i32,
  64. #[cynic(flatten)]
  65. pub nodes: Vec<PullRequestReview>,
  66. }
  67. #[derive(cynic::QueryFragment, Debug)]
  68. pub struct PullRequestReview {
  69. pub author: Option<Actor>,
  70. pub created_at: DateTime,
  71. }
  72. #[derive(cynic::QueryFragment, Debug)]
  73. pub struct UserConnection {
  74. #[cynic(flatten)]
  75. pub nodes: Vec<User>,
  76. }
  77. #[derive(cynic::QueryFragment, Debug)]
  78. pub struct User {
  79. pub login: String,
  80. }
  81. #[derive(cynic::QueryFragment, Debug)]
  82. pub struct PageInfo {
  83. pub has_next_page: bool,
  84. pub end_cursor: Option<String>,
  85. }
  86. #[derive(cynic::QueryFragment, Debug)]
  87. pub struct LabelConnection {
  88. #[cynic(flatten)]
  89. pub nodes: Vec<Label>,
  90. }
  91. #[derive(cynic::QueryFragment, Debug)]
  92. pub struct Label {
  93. pub name: String,
  94. }
  95. #[derive(cynic::QueryFragment, Debug)]
  96. pub struct IssueCommentConnection {
  97. pub total_count: i32,
  98. #[cynic(flatten)]
  99. pub nodes: Vec<IssueComment>,
  100. }
  101. #[derive(cynic::QueryFragment, Debug)]
  102. pub struct IssueComment {
  103. pub author: Option<Actor>,
  104. pub created_at: DateTime,
  105. }
  106. #[derive(cynic::QueryFragment, Debug)]
  107. pub struct Actor {
  108. pub login: String,
  109. }
  110. #[derive(cynic::Scalar, Debug, Clone)]
  111. #[cynic(graphql_type = "URI")]
  112. pub struct Uri(pub String);
  113. }
  114. pub mod docs_update_queries {
  115. use super::queries::{DateTime, PageInfo};
  116. use super::schema;
  117. #[derive(cynic::QueryVariables, Clone, Debug)]
  118. pub struct RecentCommitsArguments<'a> {
  119. pub branch: &'a str,
  120. pub name: &'a str,
  121. pub owner: &'a str,
  122. pub after: Option<String>,
  123. }
  124. /// Query for fetching recent commits and their associated PRs.
  125. ///
  126. /// This query is built from:
  127. ///
  128. /// ```text
  129. /// query RecentCommits($name: String!, $owner: String!, $branch: String!, $after: String) {
  130. /// repository(name: $name, owner: $owner) {
  131. /// ref(qualifiedName: $branch) {
  132. /// target {
  133. /// ... on Commit {
  134. /// history(first: 100, after: $after) {
  135. /// totalCount
  136. /// pageInfo {
  137. /// hasNextPage
  138. /// endCursor
  139. /// }
  140. /// nodes {
  141. /// oid
  142. /// parents(first: 1) {
  143. /// nodes {
  144. /// oid
  145. /// }
  146. /// }
  147. /// committedDate
  148. /// messageHeadline
  149. /// associatedPullRequests(first: 1) {
  150. /// nodes {
  151. /// number
  152. /// title
  153. /// }
  154. /// }
  155. /// }
  156. /// }
  157. /// }
  158. /// }
  159. /// }
  160. /// }
  161. /// }
  162. /// ```
  163. #[derive(cynic::QueryFragment, Debug)]
  164. #[cynic(graphql_type = "Query", variables = "RecentCommitsArguments")]
  165. pub struct RecentCommits {
  166. #[arguments(name: $name, owner: $owner)]
  167. pub repository: Option<Repository>,
  168. }
  169. #[derive(cynic::QueryFragment, Debug)]
  170. #[cynic(variables = "RecentCommitsArguments")]
  171. pub struct Repository {
  172. #[arguments(qualifiedName: $branch)]
  173. #[cynic(rename = "ref")]
  174. pub ref_: Option<Ref>,
  175. }
  176. #[derive(cynic::QueryFragment, Debug)]
  177. #[cynic(variables = "RecentCommitsArguments")]
  178. pub struct Ref {
  179. pub target: Option<GitObject>,
  180. }
  181. #[derive(cynic::QueryFragment, Debug)]
  182. #[cynic(variables = "RecentCommitsArguments")]
  183. pub struct Commit {
  184. #[arguments(first: 100, after: $after)]
  185. pub history: CommitHistoryConnection,
  186. }
  187. #[derive(cynic::QueryFragment, Debug)]
  188. pub struct CommitHistoryConnection {
  189. pub total_count: i32,
  190. pub page_info: PageInfo,
  191. #[cynic(flatten)]
  192. pub nodes: Vec<Commit2>,
  193. }
  194. #[derive(cynic::QueryFragment, Debug)]
  195. #[cynic(graphql_type = "Commit")]
  196. pub struct Commit2 {
  197. pub oid: GitObjectID,
  198. #[arguments(first = 1)]
  199. pub parents: CommitConnection,
  200. pub committed_date: DateTime,
  201. pub message_headline: String,
  202. #[arguments(first = 1)]
  203. pub associated_pull_requests: Option<PullRequestConnection>,
  204. }
  205. #[derive(cynic::QueryFragment, Debug)]
  206. pub struct PullRequestConnection {
  207. #[cynic(flatten)]
  208. pub nodes: Vec<PullRequest>,
  209. }
  210. #[derive(cynic::QueryFragment, Debug)]
  211. pub struct PullRequest {
  212. pub number: i32,
  213. pub title: String,
  214. }
  215. #[derive(cynic::QueryFragment, Debug)]
  216. pub struct CommitConnection {
  217. #[cynic(flatten)]
  218. pub nodes: Vec<Commit3>,
  219. }
  220. #[derive(cynic::QueryFragment, Debug)]
  221. #[cynic(graphql_type = "Commit")]
  222. pub struct Commit3 {
  223. pub oid: GitObjectID,
  224. }
  225. #[derive(cynic::InlineFragments, Debug)]
  226. #[cynic(variables = "RecentCommitsArguments")]
  227. pub enum GitObject {
  228. Commit(Commit),
  229. #[cynic(fallback)]
  230. Other,
  231. }
  232. #[derive(cynic::Scalar, Debug, Clone)]
  233. pub struct GitObjectID(pub String);
  234. }
  235. #[cynic::schema("github")]
  236. mod schema {}
  237. pub mod project_items {
  238. use super::queries::{Date, PageInfo, Uri};
  239. use super::schema;
  240. #[derive(cynic::QueryVariables, Debug, Clone)]
  241. pub struct Arguments {
  242. pub project_number: i32,
  243. pub after: Option<String>,
  244. }
  245. #[derive(cynic::QueryFragment, Debug)]
  246. #[cynic(variables = "Arguments")]
  247. pub struct Query {
  248. #[arguments(login: "rust-lang")]
  249. pub organization: Option<Organization>,
  250. }
  251. #[derive(cynic::QueryFragment, Debug)]
  252. #[cynic(variables = "Arguments")]
  253. pub struct Organization {
  254. #[arguments(number: $project_number)]
  255. pub project_v2: Option<ProjectV2>,
  256. }
  257. #[derive(cynic::QueryFragment, Debug)]
  258. #[cynic(variables = "Arguments")]
  259. pub struct ProjectV2 {
  260. #[arguments(first: 100, after: $after)]
  261. pub items: ProjectV2ItemConnection,
  262. }
  263. #[derive(cynic::QueryFragment, Debug)]
  264. pub struct ProjectV2ItemConnection {
  265. pub nodes: Option<Vec<Option<ProjectV2Item>>>,
  266. pub page_info: PageInfo,
  267. }
  268. #[derive(cynic::QueryFragment, Debug)]
  269. pub struct ProjectV2Item {
  270. pub content: Option<ProjectV2ItemContent>,
  271. // Currently we hard code the field names we care about here.
  272. #[cynic(rename = "fieldValueByName")]
  273. #[arguments(name = "Status")]
  274. pub status: Option<ProjectV2ItemFieldValue>,
  275. #[cynic(rename = "fieldValueByName")]
  276. #[arguments(name = "Date")]
  277. pub date: Option<ProjectV2ItemFieldValue>,
  278. }
  279. impl ProjectV2Item {
  280. pub fn status(&self) -> Option<&str> {
  281. let Some(ref status) = self.status else {
  282. return None;
  283. };
  284. status.as_str()
  285. }
  286. pub fn date(&self) -> Option<Date> {
  287. let Some(ref date) = self.date else {
  288. return None;
  289. };
  290. date.as_date()
  291. }
  292. }
  293. #[derive(cynic::InlineFragments, Debug)]
  294. pub enum ProjectV2ItemContent {
  295. Issue(Issue),
  296. #[cynic(fallback)]
  297. Other,
  298. }
  299. #[derive(cynic::InlineFragments, Debug)]
  300. pub enum ProjectV2ItemFieldValue {
  301. ProjectV2ItemFieldSingleSelectValue(ProjectV2ItemFieldSingleSelectValue),
  302. ProjectV2ItemFieldDateValue(ProjectV2ItemFieldDateValue),
  303. #[cynic(fallback)]
  304. Other,
  305. }
  306. impl ProjectV2ItemFieldValue {
  307. pub fn as_str(&self) -> Option<&str> {
  308. Some(match self {
  309. Self::ProjectV2ItemFieldSingleSelectValue(val) => val.name.as_deref()?,
  310. _ => return None,
  311. })
  312. }
  313. pub fn as_date(&self) -> Option<Date> {
  314. match self {
  315. Self::ProjectV2ItemFieldDateValue(val) => val.date,
  316. _ => None,
  317. }
  318. }
  319. }
  320. #[derive(cynic::QueryFragment, Debug)]
  321. pub struct Issue {
  322. pub title: String,
  323. pub url: Uri,
  324. pub number: i32,
  325. }
  326. #[derive(cynic::QueryFragment, Debug)]
  327. pub struct ProjectV2ItemFieldSingleSelectValue {
  328. pub name: Option<String>,
  329. }
  330. #[derive(cynic::QueryFragment, Debug)]
  331. pub struct ProjectV2ItemFieldDateValue {
  332. pub date: Option<Date>,
  333. }
  334. }