From 64f22ab9c04fa8e55b9ff1c54877abd6fed9dd84 Mon Sep 17 00:00:00 2001 From: Rogerluo Date: Wed, 8 Feb 2023 04:59:37 -0500 Subject: [PATCH] feat: support is_collaborators (#304) * feat: support is_collaborators * add an example --- examples/is_collab.rs | 18 ++++++++++++++++++ src/api/repos.rs | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 examples/is_collab.rs diff --git a/examples/is_collab.rs b/examples/is_collab.rs new file mode 100644 index 00000000..cd1de221 --- /dev/null +++ b/examples/is_collab.rs @@ -0,0 +1,18 @@ +use octocrab::Octocrab; + +#[tokio::main] +async fn main() -> octocrab::Result<()> { + let token = std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN env variable is required"); + + let octocrab = Octocrab::builder().personal_token(token).build()?; + + let repo = octocrab.repos("rust-lang", "rust").is_collaborator("Roger-luo").await?; + + if repo { + println!("Roger-luo is a collaborator of rust-lang/rust"); + } else { + println!("Roger-luo is not a collaborator of rust-lang/rust"); + } + + Ok(()) +} \ No newline at end of file diff --git a/src/api/repos.rs b/src/api/repos.rs index 6d0fe4e3..3cf5eb6c 100644 --- a/src/api/repos.rs +++ b/src/api/repos.rs @@ -521,4 +521,19 @@ impl<'octo> RepoHandler<'octo> { ))?; self.crab._get(url, None::<&()>).await } + + /// Check if a user is a repository collaborator + pub async fn is_collaborator( + &self, + username: impl AsRef, + ) -> Result { + let url = self.crab.absolute_url(format!( + "/repos/{owner}/{repo}/collaborators/{username}", + owner = self.owner, + repo = self.repo, + username = username.as_ref(), + ))?; + let response = self.crab._get(url, None::<&()>).await?; + Ok(response.status().is_success()) + } }