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

Implement organization repository list support #28

Merged
merged 2 commits into from
Mar 20, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions examples/orgs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate env_logger;
extern crate hyper;
extern crate hubcaps;

use hyper::Client;
use hubcaps::{Credentials, Github, OrganizationRepoListOptions};
use hubcaps::repositories::OrgRepoType;
use std::env;

fn main() {
env_logger::init().unwrap();
match env::var("GITHUB_TOKEN").ok() {
Some(token) => {
let client = Client::new();
let github = Github::new(format!("hubcaps/{}", env!("CARGO_PKG_VERSION")),
&client,
Credentials::Token(token));

let options = OrganizationRepoListOptions::builder().repo_type(OrgRepoType::Forks).build();

println!("Forks in the rust-lang organization:");

for repo in github.org_repos("rust-lang").list(&options).unwrap() {
println!("{}", repo.name)
}
}
_ => println!("example missing GITHUB_TOKEN"),
}
}
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use hyper::client::RequestBuilder;
use hyper::method::Method;
use hyper::header::{Authorization, ContentLength, UserAgent};
use hyper::status::StatusCode;
use repositories::{Repository, Repositories, UserRepositories};
use repositories::{Repository, Repositories, UserRepositories, OrganizationRepositories};
use std::fmt;
use std::io::Read;
use url::Url;
Expand Down Expand Up @@ -152,15 +152,15 @@ impl<'a> Github<'a> {
Repository::new(self, owner, repo)
}

/// Return a reference to the collection of repositories owned by an
/// Return a reference to the collection of repositories owned by and
/// associated with an owner
pub fn user_repos<S>(&self, owner: S) -> UserRepositories
where S: Into<String>
{
UserRepositories::new(self, owner)
}

/// Return a reference to the collection of repositores owned by the user
/// Return a reference to the collection of repositories owned by the user
/// associated with the current authentication credentials
pub fn repos(&self) -> Repositories {
Repositories::new(self)
Expand All @@ -179,6 +179,14 @@ impl<'a> Github<'a> {
Gists::new(self)
}

/// Return a reference to the collection of repositories owned by and
/// associated with an organization
pub fn org_repos<O>(&self, org: O) -> OrganizationRepositories
where O: Into<String>
{
OrganizationRepositories::new(self, org)
}

fn authenticate(&self, method: Method, uri: &str) -> RequestBuilder {
let url = format!("{}{}", self.host, uri);
match self.credentials {
Expand Down
2 changes: 1 addition & 1 deletion src/rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::{SortDirection, Error, State as StdState};
use super::issues::Sort as IssueSort;
use super::repositories::{Sort as RepoSort, Affiliation, Type as RepoType,
Visibility as RepoVisibility};
Visibility as RepoVisibility, OrgRepoType};
use std::collections::HashMap;
use std::hash::Hash;
use std::option::Option;
Expand Down
40 changes: 40 additions & 0 deletions src/rep.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,46 @@ impl UserRepoListOptionsBuilder {
}
}

#[derive(Default)]
pub struct OrganizationRepoListOptions {
params: HashMap<&'static str, String>
}

impl OrganizationRepoListOptions {
pub fn builder() -> OrganizationRepoListOptionsBuilder {
OrganizationRepoListOptionsBuilder::new()
}

/// serialize options as a string. returns None if no options are defined
pub fn serialize(&self) -> Option<String> {
if self.params.is_empty() {
None
} else {
Some(form_urlencoded::serialize(&self.params))
}
}
}

#[derive(Default)]
pub struct OrganizationRepoListOptionsBuilder {
params: HashMap<&'static str, String>
}

impl OrganizationRepoListOptionsBuilder {
pub fn new() -> OrganizationRepoListOptionsBuilder {
OrganizationRepoListOptionsBuilder { ..Default::default() }
}

pub fn repo_type(&mut self, tpe: OrgRepoType) -> &mut OrganizationRepoListOptionsBuilder {
self.params.insert("type", tpe.to_string());
self
}

pub fn build(&self) -> OrganizationRepoListOptions {
OrganizationRepoListOptions { params: self.params.clone() }
}
}

#[derive(Default)]
pub struct RepoListOptions {
params: HashMap<&'static str, String>
Expand Down
58 changes: 57 additions & 1 deletion src/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use issues::{IssueRef, Issues};
use labels::Labels;
use pulls::PullRequests;
use releases::Releases;
use rep::{Repo, RepoListOptions, UserRepoListOptions};
use rep::{Repo, RepoListOptions, UserRepoListOptions, OrganizationRepoListOptions};
use statuses::Statuses;
use std::fmt;

Expand Down Expand Up @@ -97,6 +97,32 @@ impl fmt::Display for Type {
}
}

/// Describes types of organization repositories
#[derive(Clone, Debug, PartialEq)]
pub enum OrgRepoType {
All,
Public,
Private,
Forks,
Sources,
Member,
}

impl fmt::Display for OrgRepoType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"{}",
match *self {
OrgRepoType::All => "all",
OrgRepoType::Public => "public",
OrgRepoType::Private => "private",
OrgRepoType::Forks => "forks",
OrgRepoType::Sources => "sources",
OrgRepoType::Member => "member",
})
}
}

pub struct Repositories<'a> {
github: &'a Github<'a>,
}
Expand Down Expand Up @@ -151,6 +177,36 @@ impl<'a> UserRepositories<'a> {
}
}

/// Provides access to an organization's repositories
pub struct OrganizationRepositories<'a> {
github: &'a Github<'a>,
org: String,
}

impl<'a> OrganizationRepositories<'a> {
pub fn new<O>(github: &'a Github<'a>, org: O) -> OrganizationRepositories<'a>
where O: Into<String>
{
OrganizationRepositories {
github: github,
org: org.into(),
}
}

fn path(&self, more: &str) -> String {
format!("/orgs/{}/repos{}", self.org, more)
}

/// https://developer.github.com/v3/repos/#list-organization-repositories
pub fn list(&self, options: &OrganizationRepoListOptions) -> Result<Vec<Repo>> {
let mut uri = vec![self.path("")];
if let Some(query) = options.serialize() {
uri.push(query);
}
self.github.get::<Vec<Repo>>(&uri.join("?"))
}
}

pub struct Repository<'a> {
github: &'a Github<'a>,
owner: String,
Expand Down