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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! Releases interface
extern crate serde_json;

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

use users::User;
use {Future, Github};

/// Provides access to assets for a release.
/// See the [github docs](https://developer.github.com/v3/repos/releases/)
/// for more information.
pub struct Assets<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    owner: String,
    repo: String,
    releaseid: u64,
}

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

    // todo: upload asset
    // todo: edit asset

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

    // todo: stream interface to download

    /// Get the asset information.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#get-a-single-release-asset)
    /// for more information.
    pub fn get(&self, id: u64) -> Future<Asset> {
        self.github.get(&self.path(&format!("/{}", id)))
    }

    /// Delete an asset by id.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#delete-a-release-asset)
    /// for more information.
    pub fn delete(&self, id: u64) -> Future<()> {
        self.github.delete(&self.path(&format!("/{}", id)))
    }

    /// List assets for a release.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#list-assets-for-a-release)
    /// for more information.
    pub fn list(&self) -> Future<Vec<Asset>> {
        self.github.get(&self.path(""))
    }
}

pub struct ReleaseRef<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    owner: String,
    repo: String,
    id: u64,
}

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

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

    /// Get the release information.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#get-a-single-release)
    /// for more information.
    pub fn get(&self) -> Future<Release> {
        self.github.get::<Release>(&self.path(""))
    }

    /// Get a reference to asset operations for a release.
    pub fn assets(&self) -> Assets<C> {
        Assets::new(
            self.github.clone(),
            self.owner.as_str(),
            self.repo.as_str(),
            self.id,
        )
    }
}

/// Provides access to published releases.
/// See the [github docs](https://developer.github.com/v3/repos/releases/)
/// for more information.
pub struct Releases<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    owner: String,
    repo: String,
}

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

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

    /// Create new a release.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#create-a-release)
    /// for more information.
    pub fn create(&self, rel: &ReleaseOptions) -> Future<Release> {
        self.github.post(&self.path(""), json!(rel))
    }

    /// Edit a release by id.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#edit-a-release)
    /// for more information.
    pub fn edit(&self, id: u64, rel: &ReleaseOptions) -> Future<Release> {
        self.github
            .patch(&self.path(&format!("/{}", id)), json!(rel))
    }

    /// Delete a release by id.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#delete-a-release)
    /// for more information.
    pub fn delete(&self, id: u64) -> Future<()> {
        self.github.delete(&self.path(&format!("/{}", id)))
    }

    /// List published releases and draft releases for users with push access.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository)
    /// for more information.
    pub fn list(&self) -> Future<Vec<Release>> {
        self.github.get(&self.path(""))
    }

    /// Return the latest full release. Draft releases and prereleases are not returned.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#get-the-latest-release)
    /// for more information.
    pub fn latest(&self) -> Future<Release> {
        self.github.get(&self.path("/latest"))
    }

    /// Return a release by tag name.
    ///
    /// See the [github docs](https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name)
    /// for more information.
    pub fn by_tag<S>(&self, tag_name: S) -> Future<Release>
    where
        S: Into<String>,
    {
        self.github
            .get(&self.path(&format!("/tags/{}", tag_name.into())))
    }

    /// Get a reference to a specific release associated with a repository
    pub fn get(&self, id: u64) -> ReleaseRef<C> {
        ReleaseRef::new(
            self.github.clone(),
            self.owner.as_str(),
            self.repo.as_str(),
            id,
        )
    }
}

// representations (todo: replace with derive_builder)

#[derive(Debug, Deserialize)]
pub struct Asset {
    pub url: String,
    pub browser_download_url: String,
    pub id: u64,
    pub name: String,
    pub label: Option<String>,
    pub state: String,
    pub content_type: String,
    pub size: u64,
    pub download_count: u64,
    pub created_at: String,
    pub updated_at: String,
    pub uploader: User,
}

#[derive(Debug, Deserialize)]
pub struct Release {
    pub url: String,
    pub html_url: String,
    pub assets_url: String,
    pub upload_url: String,
    pub tarball_url: String,
    pub zipball_url: String,
    pub id: u64,
    pub tag_name: String,
    pub target_commitish: String,
    pub name: String,
    pub body: String,
    pub draft: bool,
    pub prerelease: bool,
    pub created_at: String,
    pub published_at: String,
    pub author: User,
    pub assets: Vec<Asset>,
}

#[derive(Debug, Default, Serialize)]
pub struct ReleaseOptions {
    pub tag_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_commitish: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub draft: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prerelease: Option<bool>,
}

/// builder interface for ReleaseOptions
pub struct ReleaseOptionsBuilder(ReleaseOptions);

impl ReleaseOptionsBuilder {
    pub(crate) fn new<T>(tag: T) -> Self
    where
        T: Into<String>,
    {
        ReleaseOptionsBuilder(ReleaseOptions {
            tag_name: tag.into(),
            ..Default::default()
        })
    }

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

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

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

    pub fn draft(&mut self, draft: bool) -> &mut Self {
        self.0.draft = Some(draft);
        self
    }

    pub fn prerelease(&mut self, pre: bool) -> &mut Self {
        self.0.prerelease = Some(pre);
        self
    }

    pub fn build(&self) -> ReleaseOptions {
        ReleaseOptions::new(
            self.0.tag_name.as_str(),
            self.0.target_commitish.clone(),
            self.0.name.clone(),
            self.0.body.clone(),
            self.0.draft,
            self.0.prerelease,
        )
    }
}

impl ReleaseOptions {
    pub fn new<T, C, N, B>(
        tag: T,
        commit: Option<C>,
        name: Option<N>,
        body: Option<B>,
        draft: Option<bool>,
        prerelease: Option<bool>,
    ) -> Self
    where
        T: Into<String>,
        C: Into<String>,
        N: Into<String>,
        B: Into<String>,
    {
        ReleaseOptions {
            tag_name: tag.into(),
            target_commitish: commit.map(|c| c.into()),
            name: name.map(|n| n.into()),
            body: body.map(|b| b.into()),
            draft,
            prerelease,
        }
    }

    pub fn builder<T>(tag: T) -> ReleaseOptionsBuilder
    where
        T: Into<String>,
    {
        ReleaseOptionsBuilder::new(tag)
    }
}