lib.rs 11 KB

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