Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for listing a users installations #378

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion src/api/current.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<u8>,

#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u8>,
}

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<u8>) -> 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<u8>) -> Self {
self.page = Some(page.into());
self
}

/// Sends the actual request.
pub async fn send(self) -> crate::Result<Page<Installation>> {
self.crab.get("/user/installations", (&self).into()).await
}
}
1 change: 1 addition & 0 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<T: serde::de::DeserializeOwned> crate::FromResponse for Page<T> {
"jobs",
"artifacts",
"repositories",
"installations",
]
.into_iter()
.find(|v| json.get(v).is_some())
Expand Down