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

added repos/list_contributors #500

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use snafu::ResultExt;
mod branches;
mod collaborators;
mod commits;
mod contributors;
pub mod events;
mod file;
pub mod forks;
Expand All @@ -27,6 +28,7 @@ use crate::{models, params, Octocrab, Result};
pub use branches::ListBranchesBuilder;
pub use collaborators::ListCollaboratorsBuilder;
pub use commits::ListCommitsBuilder;
pub use contributors::ListContributorsBuilder;
pub use file::{DeleteFileBuilder, GetContentBuilder, UpdateFileBuilder};
pub use generate::GenerateRepositoryBuilder;
pub use merges::MergeBranchBuilder;
Expand Down Expand Up @@ -426,6 +428,17 @@ impl<'octo> RepoHandler<'octo> {
ListCollaboratorsBuilder::new(self)
}

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

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

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

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

/// Set to 1 or true to include anonymous contributors in results.
pub fn anon(mut self, include_anon: impl Into<bool>) -> Self {
Copy link
Contributor

@manchicken manchicken Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question: does it matter that these aren't &mut self? It seems like you'd want this to be a reference, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we consume self and then return it, as a part of a builder pattern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, sorry I missed it the first go-round.

self.anon = Some(include_anon.into());
self
}

/// 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::Contributor>> {
let route = format!(
"/repos/{owner}/{repo}/contributors",
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 @@ -443,6 +443,14 @@ pub struct Collaborator {
pub permissions: Permissions,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Contributor {
#[serde(flatten)]
pub author: Author,
pub contributions: u32,
}

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