1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Teams interface

use std::collections::BTreeMap;
use std::fmt;

use futures::future;
use hyper::client::Connect;
use serde_json;

use {Github, Future, Stream, unfold};

/// Team repository permissions
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
}

/// reference to teams associated with a github repo
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: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

    /// list of teams for this repo
    pub fn list(&self) -> Future<Vec<Team>> {
        self.github.get(&format!(
            "/repos/{}/{}/teams",
            self.owner,
            self.repo
        ))
    }

    /// provides a stream over all pages of teams
    pub fn iter(&self) -> Stream<Team> {
        unfold(
            self.github.clone(),
            self.github.get_pages(&format!(
                "/repos/{}/{}/teams",
                self.owner,
                self.repo
            )),
            identity,
        )
    }
}

/// reference to teams associated with a github org
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: github,
            org: org.into(),
        }
    }

    /// list of teams for this org
    pub fn list(&self) -> Future<Vec<Team>> {
        self.github.get(&format!("/orgs/{}/teams", self.org))
    }

    /// provides an iterator over all pages of teams
    pub fn iter(&self) -> Stream<Team> {
        unfold(
            self.github.clone(),
            self.github.get_pages(&format!("/orgs/{}/teams", self.org)),
            identity,
        )
    }

    /// adds a repository permission to this team
    /// learn more [here](https://developer.github.com/v3/orgs/teams/#add-or-update-team-repository)
    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),
        )
    }
}

// representations (todo: replace with derive_builder)

#[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,
}