use std::collections::BTreeMap;
use std::fmt;
use futures::future;
use hyper::client::Connect;
use serde_json;
use {unfold, Future, Github, Stream};
#[derive(Clone, Copy)]
pub enum Permission {
Pull,
Push,
Admin,
}
impl fmt::Display for Permission {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Permission::Pull => "pull",
Permission::Push => "push",
Permission::Admin => "admin",
}.fmt(f)
}
}
fn identity<T>(x: T) -> T {
x
}
pub struct RepoTeams<C>
where
C: Connect + Clone,
{
github: Github<C>,
owner: String,
repo: String,
}
impl<C: Connect + Clone> RepoTeams<C> {
#[doc(hidden)]
pub fn new<O, R>(github: Github<C>, owner: O, repo: R) -> Self
where
O: Into<String>,
R: Into<String>,
{
RepoTeams {
github,
owner: owner.into(),
repo: repo.into(),
}
}
pub fn list(&self) -> Future<Vec<Team>> {
self.github
.get(&format!("/repos/{}/{}/teams", self.owner, self.repo))
}
pub fn iter(&self) -> Stream<Team> {
unfold(
self.github.clone(),
self.github
.get_pages(&format!("/repos/{}/{}/teams", self.owner, self.repo)),
identity,
)
}
}
pub struct OrgTeams<C>
where
C: Connect + Clone,
{
github: Github<C>,
org: String,
}
impl<C: Clone + Connect> OrgTeams<C> {
#[doc(hidden)]
pub fn new<O>(github: Github<C>, org: O) -> Self
where
O: Into<String>,
{
OrgTeams {
github,
org: org.into(),
}
}
pub fn list(&self) -> Future<Vec<Team>> {
self.github.get(&format!("/orgs/{}/teams", self.org))
}
pub fn iter(&self) -> Stream<Team> {
unfold(
self.github.clone(),
self.github.get_pages(&format!("/orgs/{}/teams", self.org)),
identity,
)
}
pub fn add_repo_permission<N>(
&self,
team_id: u64,
repo_name: N,
permission: Permission,
) -> Future<()>
where
N: Into<String>,
{
let mut payload = BTreeMap::new();
payload.insert("permission", permission.to_string());
self.github.put_no_response(
&format!("/teams/{}/repos/{}/{}", team_id, self.org, repo_name.into()),
json!(payload),
)
}
}
#[derive(Debug, Deserialize)]
pub struct Team {
pub id: u64,
pub url: String,
pub name: String,
pub slug: String,
pub description: Option<String>,
pub privacy: String,
pub members_url: String,
pub repositories_url: String,
pub permission: String,
}