Skip to content

Commit

Permalink
feat: support is_collaborators (#304)
Browse files Browse the repository at this point in the history
* feat: support is_collaborators

* add an example
  • Loading branch information
Roger-luo authored Feb 8, 2023
1 parent 7db3611 commit 64f22ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/is_collab.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
15 changes: 15 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>,
) -> Result<bool> {
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())
}
}

0 comments on commit 64f22ab

Please sign in to comment.