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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! Statuses interface
extern crate serde_json;
extern crate serde;
extern crate futures;

use futures::future;
use {Github, Future};
use hyper::client::Connect;
use users::User;

/// interface for statuses associated with a repository
pub struct Statuses<C>
where
    C: Clone + Connect,
{
    github: Github<C>,
    owner: String,
    repo: String,
}

impl<C: Clone + Connect> Statuses<C> {
    #[doc(hidden)]
    pub fn new<O, R>(github: Github<C>, owner: O, repo: R) -> Self
    where
        O: Into<String>,
        R: Into<String>,
    {
        Statuses {
            github: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

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

    /// creates a new status for a target sha
    pub fn create(&self, sha: &str, status: &StatusOptions) -> Future<Status> {
        self.github.post(
            &self.path(&format!("/{}", sha)),
            json!(status),
        )
    }

    /// lists all statuses associated with a given git sha
    pub fn list(&self, sha: &str) -> Future<Vec<Status>> {
        self.github.get(&format!(
            "/repos/{}/{}/commits/{}/statuses",
            self.owner,
            self.repo,
            sha
        ))
    }

    /// list the combined statuses for a given git sha
    /// fixme: give this a type
    pub fn combined(&self, sha: &str) -> Future<String> {
        self.github.get(&format!(
            "/repos/{}/{}/commits/{}/status",
            self.owner,
            self.repo,
            sha
        ))
    }
}

// representations (todo: replace with derive_builder)

#[derive(Debug, Deserialize)]
pub struct Status {
    pub created_at: String,
    pub updated_at: String,
    pub state: State,
    pub target_url: String,
    pub description: String,
    pub id: u64,
    pub url: String,
    pub context: String,
    pub creator: User,
}

#[derive(Debug, Default, Serialize)]
pub struct StatusOptions {
    state: State,
    #[serde(skip_serializing_if = "Option::is_none")]
    target_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    context: Option<String>,
}

pub struct StatusOptionsBuilder(StatusOptions);

impl StatusOptionsBuilder {
    #[doc(hidden)]
    pub fn new(state: State) -> Self {
        StatusOptionsBuilder(StatusOptions {
            state: state,
            ..Default::default()
        })
    }

    pub fn target_url<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.0.target_url = Some(url.into());
        self
    }

    pub fn description<D>(&mut self, desc: D) -> &mut Self
    where
        D: Into<String>,
    {
        self.0.description = Some(desc.into());
        self
    }

    pub fn context<C>(&mut self, ctx: C) -> &mut Self
    where
        C: Into<String>,
    {
        self.0.context = Some(ctx.into());
        self
    }

    pub fn build(&self) -> StatusOptions {
        StatusOptions::new(
            self.0.state.clone(),
            self.0.target_url.clone(),
            self.0.description.clone(),
            self.0.context.clone(),
        )
    }
}

impl StatusOptions {
    #[doc(hidden)]
    pub fn new<T, D, C>(
        state: State,
        target_url: Option<T>,
        descr: Option<D>,
        context: Option<C>,
    ) -> Self
    where
        T: Into<String>,
        D: Into<String>,
        C: Into<String>,
    {
        StatusOptions {
            state: state,
            target_url: target_url.map(|t| t.into()),
            description: descr.map(|d| d.into()),
            context: context.map(|c| c.into()),
        }
    }

    pub fn builder(state: State) -> StatusOptionsBuilder {
        StatusOptionsBuilder::new(state)
    }
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum State {
    /// pending
    #[serde(rename = "pending")]
    Pending,
    /// success
    #[serde(rename = "success")]
    Success,
    /// error
    #[serde(rename = "error")]
    Error,
    /// failure
    #[serde(rename = "failure")]
    Failure,
}

impl Default for State {
    fn default() -> State {
        State::Pending
    }
}

#[cfg(test)]
mod tests {
    use serde::ser::Serialize;
    use super::*;
    use serde_json;


    fn test_encoding<E: Serialize>(tests: Vec<(E, &str)>) {
        for test in tests {
            match test {
                (k, v) => assert_eq!(serde_json::to_string(&k).unwrap(), v),
            }
        }
    }

    #[test]
    fn deserialize_status_state() {
        for (json, value) in vec![
            ("\"pending\"", State::Pending),
            ("\"success\"", State::Success),
            ("\"error\"", State::Error),
            ("\"failure\"", State::Failure),
        ]
        {
            assert_eq!(serde_json::from_str::<State>(json).unwrap(), value)
        }
    }

    #[test]
    fn serialize_status_state() {
        for (json, value) in vec![
            ("\"pending\"", State::Pending),
            ("\"success\"", State::Success),
            ("\"error\"", State::Error),
            ("\"failure\"", State::Failure),
        ]
        {
            assert_eq!(serde_json::to_string(&value).unwrap(), json)
        }
    }


    #[test]
    fn status_reqs() {
        let tests =
            vec![(StatusOptions::builder(State::Pending).build(), r#"{"state":"pending"}"#),
                 (StatusOptions::builder(State::Success)
                      .target_url("http://acme.com")
                      .build(),
                  r#"{"state":"success","target_url":"http://acme.com"}"#),
                 (StatusOptions::builder(State::Error)
                      .description("desc")
                      .build(),
                  r#"{"state":"error","description":"desc"}"#),
                 (StatusOptions::builder(State::Failure)
                      .target_url("http://acme.com")
                      .description("desc")
                      .build(),
                  r#"{"state":"failure","target_url":"http://acme.com","description":"desc"}"#)];
        test_encoding(tests)
    }


}