From 4aee8d4ae6e080a84c41efd9483543ef17f610c0 Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Mon, 22 May 2023 19:19:13 +1000 Subject: [PATCH] list installations for user access token --- src/api/current.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++- src/page.rs | 1 + 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/api/current.rs b/src/api/current.rs index 5c4ae338..1bdd7802 100644 --- a/src/api/current.rs +++ b/src/api/current.rs @@ -1,7 +1,7 @@ //! Get data about the currently authenticated user. use crate::{ - models::{self, gists::Gist, orgs::MembershipInvitation, Repository}, + models::{self, gists::Gist, orgs::MembershipInvitation, Installation, Repository}, Octocrab, Page, Result, }; use chrono::{DateTime, Utc}; @@ -124,6 +124,26 @@ impl<'octo> CurrentAuthHandler<'octo> { ListStarredGistsBuilder::new(self.crab) } + /// Lists installations of your GitHub App that the authenticated user has explicit permission (:read, :write, or :admin) to access. + /// + /// ```no_run + /// # async fn run() -> octocrab::Result<()> { + /// octocrab::instance() + /// .current() + /// .list_app_installations_accessible_to_user() + /// .send() + /// .await?; + /// # Ok(()) + /// # } + /// ``` + /// + /// [See the GitHub API documentation](https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#list-app-installations-accessible-to-the-user-access-token) + pub fn list_app_installations_accessible_to_user( + &self, + ) -> ListAppInstallationsAccessibleToUserBuilder<'octo> { + ListAppInstallationsAccessibleToUserBuilder::new(self.crab) + } + /// Lists organizations that the current authenticated user is a member of. /// /// ```no_run @@ -519,3 +539,51 @@ impl<'octo> ListOrgMembershipsForAuthenticatedUserBuilder<'octo> { .await } } + +/// A builder pattern struct for listing the installations accessible to a user access token. +/// +/// Created by [`CurrentAuthHandler::list_app_installations_accessible_to_user`]. +/// +/// [`CurrentAuthHandler::list_app_installations_accessible_to_user`]: ./struct.CurrentAuthHandler.html#method.list_app_installations_accessible_to_user +#[derive(serde::Serialize)] +pub struct ListAppInstallationsAccessibleToUserBuilder<'octo> { + #[serde(skip)] + crab: &'octo Octocrab, + + #[serde(skip_serializing_if = "Option::is_none")] + per_page: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + page: Option, +} + +impl<'octo> ListAppInstallationsAccessibleToUserBuilder<'octo> { + fn new(crab: &'octo Octocrab) -> Self { + Self { + crab, + per_page: None, + page: None, + } + } + + /// Results per page (max 100). + /// + /// [See the GitHub API documentation](https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#list-app-installations-accessible-to-the-user-access-token--parameters) + 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. + /// + /// [See the GitHub API documentation](https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#list-app-installations-accessible-to-the-user-access-token--parameters) + pub fn page(mut self, page: impl Into) -> Self { + self.page = Some(page.into()); + self + } + + /// Sends the actual request. + pub async fn send(self) -> crate::Result> { + self.crab.get("/user/installations", (&self).into()).await + } +} diff --git a/src/page.rs b/src/page.rs index e064a2ba..a3720d0e 100644 --- a/src/page.rs +++ b/src/page.rs @@ -200,6 +200,7 @@ impl crate::FromResponse for Page { "jobs", "artifacts", "repositories", + "installations", ] .into_iter() .find(|v| json.get(v).is_some())