Skip to content

Commit f0a11de

Browse files
committed
Simplify github query code
1 parent fe4f504 commit f0a11de

File tree

2 files changed

+49
-76
lines changed

2 files changed

+49
-76
lines changed

github-graphql/src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ pub mod queries {
4545
pub struct PullRequestConnection {
4646
pub total_count: i32,
4747
pub page_info: PageInfo,
48-
pub nodes: Option<Vec<Option<PullRequest>>>,
48+
#[cynic(flatten)]
49+
pub nodes: Vec<PullRequest>,
4950
}
5051

5152
#[derive(cynic::QueryFragment, Debug)]
@@ -68,7 +69,8 @@ pub mod queries {
6869
#[derive(cynic::QueryFragment, Debug)]
6970
pub struct PullRequestReviewConnection {
7071
pub total_count: i32,
71-
pub nodes: Option<Vec<Option<PullRequestReview>>>,
72+
#[cynic(flatten)]
73+
pub nodes: Vec<PullRequestReview>,
7274
}
7375

7476
#[derive(cynic::QueryFragment, Debug)]
@@ -79,7 +81,8 @@ pub mod queries {
7981

8082
#[derive(cynic::QueryFragment, Debug)]
8183
pub struct UserConnection {
82-
pub nodes: Option<Vec<Option<User>>>,
84+
#[cynic(flatten)]
85+
pub nodes: Vec<User>,
8386
}
8487

8588
#[derive(cynic::QueryFragment, Debug)]
@@ -95,7 +98,8 @@ pub mod queries {
9598

9699
#[derive(cynic::QueryFragment, Debug)]
97100
pub struct LabelConnection {
98-
pub nodes: Option<Vec<Option<Label>>>,
101+
#[cynic(flatten)]
102+
pub nodes: Vec<Label>,
99103
}
100104

101105
#[derive(cynic::QueryFragment, Debug)]
@@ -106,7 +110,8 @@ pub mod queries {
106110
#[derive(cynic::QueryFragment, Debug)]
107111
pub struct IssueCommentConnection {
108112
pub total_count: i32,
109-
pub nodes: Option<Vec<Option<IssueComment>>>,
113+
#[cynic(flatten)]
114+
pub nodes: Vec<IssueComment>,
110115
}
111116

112117
#[derive(cynic::QueryFragment, Debug)]
@@ -209,7 +214,8 @@ pub mod docs_update_queries {
209214
pub struct CommitHistoryConnection {
210215
pub total_count: i32,
211216
pub page_info: PageInfo,
212-
pub nodes: Option<Vec<Option<Commit2>>>,
217+
#[cynic(flatten)]
218+
pub nodes: Vec<Commit2>,
213219
}
214220

215221
#[derive(cynic::QueryFragment, Debug)]
@@ -226,7 +232,8 @@ pub mod docs_update_queries {
226232

227233
#[derive(cynic::QueryFragment, Debug)]
228234
pub struct PullRequestConnection {
229-
pub nodes: Option<Vec<Option<PullRequest>>>,
235+
#[cynic(flatten)]
236+
pub nodes: Vec<PullRequest>,
230237
}
231238

232239
#[derive(cynic::QueryFragment, Debug)]
@@ -237,7 +244,8 @@ pub mod docs_update_queries {
237244

238245
#[derive(cynic::QueryFragment, Debug)]
239246
pub struct CommitConnection {
240-
pub nodes: Option<Vec<Option<Commit3>>>,
247+
#[cynic(flatten)]
248+
pub nodes: Vec<Commit3>,
241249
}
242250

243251
#[derive(cynic::QueryFragment, Debug)]

src/github.rs

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,9 +1314,7 @@ impl Repository {
13141314
let commits = commit
13151315
.history
13161316
.nodes
1317-
.ok_or_else(|| anyhow::anyhow!("No history."))?
13181317
.into_iter()
1319-
.filter_map(|node| node)
13201318
// Don't include anything newer than `newest`
13211319
.skip_while(|node| {
13221320
if found_newest || node.oid.0 == newest {
@@ -1338,11 +1336,7 @@ impl Repository {
13381336
// Skip nodes that aren't the first parent
13391337
.filter(|node| {
13401338
let this_first_parent = node.parents.nodes
1341-
.as_ref()
1342-
// Grab the first parent
1343-
.and_then(|nodes| nodes.first())
1344-
// Strip away the useless Option
1345-
.and_then(|parent_opt| parent_opt.as_ref())
1339+
.first()
13461340
.map(|parent| parent.oid.0.clone());
13471341

13481342
match &next_first_parent {
@@ -1377,12 +1371,8 @@ impl Repository {
13771371
.filter_map(|node| {
13781372
// Determine if this is associated with a PR or not.
13791373
match node.associated_pull_requests
1380-
// Strip away the useless Option
1381-
.and_then(|pr| pr.nodes)
13821374
// Get the first PR (we only care about one)
1383-
.and_then(|mut nodes| nodes.pop())
1384-
// Strip away the useless Option
1385-
.flatten() {
1375+
.and_then(|mut pr| pr.nodes.pop()) {
13861376
Some(pr) => {
13871377
// Only include a PR once
13881378
if prs_seen.insert(pr.number) {
@@ -2008,7 +1998,7 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
20081998
let repository_owner = repo.owner().to_owned();
20091999
let repository_name = repo.name().to_owned();
20102000

2011-
let mut prs: Vec<Option<queries::PullRequest>> = vec![];
2001+
let mut prs: Vec<queries::PullRequest> = vec![];
20122002

20132003
let mut args = queries::LeastRecentlyReviewedPullRequestsArguments {
20142004
repository_owner,
@@ -2033,13 +2023,7 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
20332023
.ok_or_else(|| anyhow::anyhow!("No data returned."))?
20342024
.repository
20352025
.ok_or_else(|| anyhow::anyhow!("No repository."))?;
2036-
prs.extend(
2037-
repository
2038-
.pull_requests
2039-
.nodes
2040-
.unwrap_or_default()
2041-
.into_iter(),
2042-
);
2026+
prs.extend(repository.pull_requests.nodes);
20432027
let page_info = repository.pull_requests.page_info;
20442028
if !page_info.has_next_page || page_info.end_cursor.is_none() {
20452029
break;
@@ -2049,23 +2033,17 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
20492033

20502034
let mut prs: Vec<_> = prs
20512035
.into_iter()
2052-
.filter_map(|pr| pr)
20532036
.filter_map(|pr| {
20542037
if pr.is_draft {
20552038
return None;
20562039
}
2057-
let labels = pr.labels;
2058-
let labels = (|| -> Option<_> {
2059-
let labels = labels?;
2060-
let nodes = labels.nodes?;
2061-
let labels = nodes
2062-
.into_iter()
2063-
.filter_map(|node| node)
2064-
.map(|node| node.name)
2065-
.collect::<Vec<_>>();
2066-
Some(labels)
2067-
})()
2068-
.unwrap_or_default();
2040+
let labels = pr
2041+
.labels
2042+
.map(|l| l.nodes)
2043+
.unwrap_or_default()
2044+
.into_iter()
2045+
.map(|node| node.name)
2046+
.collect::<Vec<_>>();
20692047
if !labels.iter().any(|label| label == "T-compiler") {
20702048
return None;
20712049
}
@@ -2074,49 +2052,36 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
20742052
let assignees: Vec<_> = pr
20752053
.assignees
20762054
.nodes
2077-
.unwrap_or_default()
20782055
.into_iter()
2079-
.filter_map(|user| user)
20802056
.map(|user| user.login)
20812057
.collect();
20822058

2083-
let latest_reviews = pr.latest_reviews;
2084-
let mut reviews = (|| -> Option<_> {
2085-
let reviews = latest_reviews?;
2086-
let nodes = reviews.nodes?;
2087-
let reviews = nodes
2088-
.into_iter()
2089-
.filter_map(|node| node)
2090-
.filter_map(|node| {
2091-
let created_at = node.created_at;
2092-
node.author.map(|author| (author, created_at))
2093-
})
2094-
.map(|(author, created_at)| (author.login, created_at))
2095-
.collect::<Vec<_>>();
2096-
Some(reviews)
2097-
})()
2098-
.unwrap_or_default();
2059+
let mut reviews = pr
2060+
.latest_reviews
2061+
.map(|connection| connection.nodes)
2062+
.unwrap_or_default()
2063+
.into_iter()
2064+
.filter_map(|node| {
2065+
let created_at = node.created_at;
2066+
node.author.map(|author| (author, created_at))
2067+
})
2068+
.map(|(author, created_at)| (author.login, created_at))
2069+
.collect::<Vec<_>>();
2070+
20992071
reviews.sort_by_key(|r| r.1);
21002072

2101-
let comments = pr.comments;
2102-
let comments = (|| -> Option<_> {
2103-
let nodes = comments.nodes?;
2104-
let comments = nodes
2105-
.into_iter()
2106-
.filter_map(|node| node)
2107-
.filter_map(|node| {
2108-
let created_at = node.created_at;
2109-
node.author.map(|author| (author, created_at))
2110-
})
2111-
.map(|(author, created_at)| (author.login, created_at))
2112-
.collect::<Vec<_>>();
2113-
Some(comments)
2114-
})()
2115-
.unwrap_or_default();
2116-
let mut comments: Vec<_> = comments
2073+
let mut comments = pr
2074+
.comments
2075+
.nodes
21172076
.into_iter()
2077+
.filter_map(|node| {
2078+
let created_at = node.created_at;
2079+
node.author.map(|author| (author, created_at))
2080+
})
2081+
.map(|(author, created_at)| (author.login, created_at))
21182082
.filter(|comment| assignees.contains(&comment.0))
2119-
.collect();
2083+
.collect::<Vec<_>>();
2084+
21202085
comments.sort_by_key(|c| c.1);
21212086

21222087
let updated_at = std::cmp::max(

0 commit comments

Comments
 (0)