-
-
Notifications
You must be signed in to change notification settings - Fork 271
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::{Author, Contributor}; | ||
use octocrab::Octocrab; | ||
|
||
/// Unit test for calls to the `/repos/OWNER/REPO/contributors` endpoint | ||
mod mock_error; | ||
|
||
const OWNER: &str = "XAMPPRocky"; | ||
const REPO: &str = "octocrab"; | ||
|
||
async fn setup_api(template: ResponseTemplate) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method("GET")) | ||
.and(path(format!("/repos/{OWNER}/{REPO}/contributors"))) | ||
.respond_with(template) | ||
.mount(&mock_server) | ||
.await; | ||
setup_error_handler( | ||
&mock_server, | ||
"GET on /repos/OWNER/REPO/contributors not called", | ||
) | ||
.await; | ||
mock_server | ||
} | ||
|
||
fn setup_octocrab(uri: &str) -> Octocrab { | ||
Octocrab::builder().base_uri(uri).unwrap().build().unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_return_repo_contributors() { | ||
let repo_contributors_response: Vec<Contributor> = | ||
serde_json::from_str(include_str!("resources/repo_contributors.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&repo_contributors_response); | ||
let mock_server = setup_api(template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
|
||
let result = client | ||
.repos(OWNER, REPO) | ||
.list_contributors() | ||
.anon(true) | ||
.send() | ||
.await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let contributors = result.unwrap(); | ||
|
||
assert!(contributors.items.len() > 0); | ||
|
||
let Contributor { | ||
author: Author { login, .. }, | ||
contributions, | ||
.. | ||
} = contributors.items.first().unwrap(); | ||
|
||
{ | ||
assert_eq!(login, "XAMPPRocky"); | ||
assert!(*contributions > 0); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.