From 5eba97cbb775d84c346c70f00cd9063990c4244a Mon Sep 17 00:00:00 2001 From: 0xb10c Date: Fri, 26 May 2023 18:29:55 +0200 Subject: [PATCH] add: Builder for list_reviews for pulls Implemented similarly to listing comments on issues. Before it wasn't possible to query all review comments if there were more than 30. See the GitHub documentation: https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28 --- src/api/pulls.rs | 66 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/src/api/pulls.rs b/src/api/pulls.rs index 2a50b8d2..12718eb6 100644 --- a/src/api/pulls.rs +++ b/src/api/pulls.rs @@ -258,19 +258,18 @@ impl<'octo> PullRequestHandler<'octo> { /// Lists all of the `Review`s associated with the pull request. /// ```no_run /// # async fn run() -> octocrab::Result<()> { - /// let reviews = octocrab::instance().pulls("owner", "repo").list_reviews(101).await?; + /// let reviews = octocrab::instance() + /// .pulls("owner", "repo") + /// .list_reviews(21u64.into()) + /// .per_page(100) + /// .page(2u32) + /// .send() + /// .await?; /// # Ok(()) /// # } /// ``` - pub async fn list_reviews(&self, pr: u64) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/pulls/{pr}/reviews", - owner = self.owner, - repo = self.repo, - pr = pr - ); - - self.http_get(route, None::<&()>).await + pub fn list_reviews(&self, pr_number: u64) -> ListReviewsBuilder<'_, '_> { + ListReviewsBuilder::new(self, pr_number) } /// Request a review from users or teams. @@ -396,6 +395,53 @@ impl<'octo> PullRequestHandler<'octo> { } } +impl<'octo, 'r> ListReviewsBuilder<'octo, 'r> { + pub(crate) fn new(handler: &'r PullRequestHandler<'octo>, pr_number: u64) -> Self { + Self { + handler, + pr_number, + per_page: None, + page: None, + } + } + + /// Results per page (max 100). + pub fn per_page(mut self, per_page: impl Into) -> Self { + self.per_page = Some(per_page.into()); + self + } + + /// Page number of the results to fetch. + pub fn page(mut self, page: impl Into) -> Self { + self.page = Some(page.into()); + self + } + + /// Send the actual request. + pub async fn send(self) -> crate::Result> { + let route = format!( + "/repos/{owner}/{repo}/pulls/{pr}/reviews", + owner = self.handler.owner, + repo = self.handler.repo, + pr = self.pr_number, + ); + + self.handler.crab.get(route, Some(&self)).await + } +} + +#[derive(serde::Serialize)] +pub struct ListReviewsBuilder<'octo, 'r> { + #[serde(skip)] + handler: &'r PullRequestHandler<'octo>, + #[serde(skip)] + pr_number: u64, + #[serde(skip_serializing_if = "Option::is_none")] + per_page: Option, + #[serde(skip_serializing_if = "Option::is_none")] + page: Option, +} + impl<'octo> PullRequestHandler<'octo> { pub(crate) async fn http_get( &self,