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
//! Search interface

use std::collections::HashMap;
use std::fmt;

use hyper::client::Connect;
use serde::de::DeserializeOwned;
use url::{self, form_urlencoded};

use {Github, Stream, Future, SortDirection, unfold};
use labels::Label;
use users::User;

/// Sort directions for pull requests
#[derive(Debug, PartialEq)]
pub enum IssuesSort {
    /// Sort by time created
    Created,
    /// Sort by last updated
    Updated,
    /// Sort by number of comments
    Comments,
}

impl fmt::Display for IssuesSort {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            IssuesSort::Comments => "comments",
            IssuesSort::Created => "created",
            IssuesSort::Updated => "updated",
        }.fmt(f)
    }
}

/// Provides access to search operations
/// https://developer.github.com/v3/search/#search-issues
#[derive(Clone)]
pub struct Search<C>
where
    C: Clone + Connect,
{
    github: Github<C>,
}

fn items<D>(result: SearchResult<D>) -> Vec<D>
where
    D: DeserializeOwned + 'static,
{
    result.items
}

impl<C: Clone + Connect> Search<C> {
    #[doc(hidden)]
    pub fn new(github: Github<C>) -> Self {
        Self { github }
    }

    /// return a reference to a search interface for issues
    pub fn issues(&self) -> SearchIssues<C> {
        SearchIssues::new(self.clone())
    }

    fn iter<D>(&self, url: &str) -> Stream<D>
    where
        D: DeserializeOwned + 'static,
    {
        unfold(self.github.clone(), self.github.get_pages(url), items)
    }

    fn search<D>(&self, url: &str) -> Future<SearchResult<D>>
    where
        D: DeserializeOwned + 'static,
    {
        self.github.get(url)
    }
}

/// Provides access to issue search operations
/// https://developer.github.com/v3/search/#search-issues
pub struct SearchIssues<C>
where
    C: Clone + Connect,
{
    search: Search<C>,
}

impl<C: Clone + Connect> SearchIssues<C> {
    #[doc(hidden)]
    pub fn new(search: Search<C>) -> Self {
        Self { search }
    }

    fn search_uri<Q>(&self, q: Q, options: &SearchIssuesOptions) -> String
    where
        Q: Into<String>,
    {
        let mut uri = vec!["/search/issues".to_string()];
        let query_options = options.serialize().unwrap_or(String::new());
        let query = form_urlencoded::Serializer::new(query_options)
            .append_pair("q", &q.into())
            .finish();
        uri.push(query);
        uri.join("?")
    }

    /// Returns an Iterator over pages of search results
    /// Use this interface if you wish to iterate over all items
    /// in a result set
    pub fn iter<Q>(&self, q: Q, options: &SearchIssuesOptions) -> Stream<IssuesItem>
    where
        Q: Into<String>,
    {
        self.search.iter::<IssuesItem>(&self.search_uri(q, options))
    }

    /// Returns a single page of search results
    pub fn list<Q>(&self, q: Q, options: &SearchIssuesOptions) -> Future<SearchResult<IssuesItem>>
    where
        Q: Into<String>,
    {
        self.search.search::<IssuesItem>(
            &self.search_uri(q, options),
        )
    }
}

// representations (todo: replace with derive_builder)

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

impl SearchIssuesOptions {
    pub fn builder() -> SearchIssuesOptionsBuilder {
        SearchIssuesOptionsBuilder::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 {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

/// https://developer.github.com/v3/search/#search-issues
pub struct SearchIssuesOptionsBuilder(SearchIssuesOptions);

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

    pub fn per_page(&mut self, n: usize) -> &mut Self {
        self.0.params.insert("per_page", n.to_string());
        self
    }

    pub fn sort(&mut self, sort: IssuesSort) -> &mut Self {
        self.0.params.insert("sort", sort.to_string());
        self
    }

    pub fn order(&mut self, direction: SortDirection) -> &mut Self {
        self.0.params.insert("order", direction.to_string());
        self
    }

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

#[derive(Debug, Deserialize)]
pub struct SearchResult<D> {
    pub total_count: u64,
    pub incomplete_results: bool,
    pub items: Vec<D>,
}


#[derive(Debug, Deserialize)]
pub struct IssuesItem {
    pub url: String,
    pub repository_url: String,
    pub labels_url: String,
    pub comments_url: String,
    pub events_url: String,
    pub html_url: String,
    pub id: u64,
    pub number: u64,
    pub title: String,
    pub user: User,
    pub labels: Vec<Label>,
    pub state: String,
    pub locked: bool,
    pub assignee: Option<User>,
    pub assignees: Vec<User>,
    pub comments: u64,
    pub created_at: String,
    pub updated_at: String,
    pub closed_at: Option<String>,
    pub pull_request: Option<PullRequestInfo>,
    pub body: Option<String>,
}

impl IssuesItem {
    /// returns a tuple of (repo owner name, repo name) associated with this issue
    pub fn repo_tuple(&self) -> (String, String) {
        let parsed = url::Url::parse(&self.repository_url).unwrap();
        let mut path = parsed.path().split("/").collect::<Vec<_>>();
        path.reverse();
        (path[0].to_owned(), path[1].to_owned())
    }
}

#[derive(Debug, Deserialize)]
pub struct PullRequestInfo {
    pub url: String,
    pub html_url: String,
    pub diff_url: String,
    pub patch_url: String,
}