Skip to content

Commit

Permalink
Add list_teams() and list_collaborators() to repos() (#395)
Browse files Browse the repository at this point in the history
* Added list_teams() and list_collaborators() to repos()

* Corrected list_collaborators example

* cargo fmt
  • Loading branch information
aisrael authored Jul 8, 2023
1 parent 4b8bd44 commit f2ce798
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use http::Uri;
use snafu::ResultExt;

mod branches;
mod collaborators;
mod commits;
pub mod events;
mod file;
Expand All @@ -18,10 +19,12 @@ mod secrets;
mod stargazers;
mod status;
mod tags;
mod teams;

use crate::error::HttpSnafu;
use crate::{models, params, Octocrab, Result};
pub use branches::ListBranchesBuilder;
pub use collaborators::ListCollaboratorsBuilder;
pub use commits::ListCommitsBuilder;
pub use file::{DeleteFileBuilder, GetContentBuilder, UpdateFileBuilder};
pub use generate::GenerateRepositoryBuilder;
Expand All @@ -32,6 +35,7 @@ pub use secrets::RepoSecretsHandler;
pub use stargazers::ListStarGazersBuilder;
pub use status::{CreateStatusBuilder, ListStatusesBuilder};
pub use tags::ListTagsBuilder;
pub use teams::ListTeamsBuilder;

/// Handler for GitHub's repository API.
///
Expand Down Expand Up @@ -381,6 +385,28 @@ impl<'octo> RepoHandler<'octo> {
ListCommitsBuilder::new(self)
}

/// List teams from a repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let teams = octocrab::instance().repos("owner", "repo").list_teams().send().await?;
/// # Ok(())
/// # }
/// ```
pub fn list_teams(&self) -> ListTeamsBuilder<'_, '_> {
ListTeamsBuilder::new(self)
}

/// List collaborators from a repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let collaborators = octocrab::instance().repos("owner", "repo").list_collaborators().send().await?;
/// # Ok(())
/// # }
/// ```
pub fn list_collaborators(&self) -> ListCollaboratorsBuilder<'_, '_> {
ListCollaboratorsBuilder::new(self)
}

/// List star_gazers from a repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
Expand Down
43 changes: 43 additions & 0 deletions src/api/repos/collaborators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::*;

#[derive(serde::Serialize)]
pub struct ListCollaboratorsBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r RepoHandler<'octo>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}

impl<'octo, 'r> ListCollaboratorsBuilder<'octo, 'r> {
pub fn new(handler: &'r RepoHandler<'octo>) -> Self {
Self {
handler,
per_page: None,
page: None,
}
}

/// Results per page (max 100).
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.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}

/// Sends the actual request.
pub async fn send(self) -> crate::Result<crate::Page<crate::models::Collaborator>> {
let route = format!(
"/repos/{owner}/{repo}/collaborators",
owner = self.handler.owner,
repo = self.handler.repo
);
self.handler.crab.get(route, Some(&self)).await
}
}
43 changes: 43 additions & 0 deletions src/api/repos/teams.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::*;

#[derive(serde::Serialize)]
pub struct ListTeamsBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r RepoHandler<'octo>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}

impl<'octo, 'r> ListTeamsBuilder<'octo, 'r> {
pub fn new(handler: &'r RepoHandler<'octo>) -> Self {
Self {
handler,
per_page: None,
page: None,
}
}

/// Results per page (max 100).
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.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}

/// Sends the actual request.
pub async fn send(self) -> crate::Result<crate::Page<crate::models::teams::Team>> {
let route = format!(
"/repos/{owner}/{repo}/teams",
owner = self.handler.owner,
repo = self.handler.repo
);
self.handler.crab.get(route, Some(&self)).await
}
}
8 changes: 8 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ pub struct Author {
pub site_admin: bool,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Collaborator {
#[serde(flatten)]
pub author: Author,
pub permissions: Permissions,
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct StarGazer {
Expand Down

0 comments on commit f2ce798

Please sign in to comment.