grab_github 0.1.0

Obtain a list of files from a GitHub repository and download them to a folder using the GitHub API.
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use futures::future::{BoxFuture, FutureExt};
use std::{
    cell::RefCell,
    collections::{HashMap, LinkedList},
    path::{Component, Path, PathBuf},
    rc::Rc,
};

use serde::Deserialize;

use crate::{request::HttpRequest, Error};

/// A GitHub branch URL.
/// The fields should complete the URL `https://github.com/{user}/{repo}/tree/{branch}`.
#[derive(Debug)]
pub struct GithubBranchPath<'g> {
    /// The GitHub username of the repository owner.
    pub user: &'g str,
    /// The repository name.
    pub repo: &'g str,
    /// The branch or SHA1 hash of the commit tree to fetch.
    pub branch: &'g str,
}

impl<'g> GithubBranchPath<'g> {
    /// Creates a new [GithubBranchPath] to the given user, repo, and branch.
    pub fn new(user: &'g str, repo: &'g str, branch: &'g str) -> GithubBranchPath<'g> {
        GithubBranchPath { user, repo, branch }
    }

    /// Creates a new [GithubBranchPath] with the given branch and the same user and repo as this path.
    pub fn with_branch(&self, branch: &'g str) -> GithubBranchPath<'g> {
        GithubBranchPath {
            user: self.user,
            repo: self.repo,
            branch,
        }
    }

    /// Returns the URL of the tree API for this branch path.
    fn to_tree_url(&self) -> String {
        format!(
            "https://api.github.com/repos/{}/{}/git/trees/{}",
            self.user, self.repo, self.branch
        )
    }
}

/// The type of a single entry in a [SourceTree].
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub enum TreeEntryType {
    /// A blob (file) entry.
    #[serde(rename = "blob")]
    Blob,
    /// A tree (directory) entry.
    #[serde(rename = "tree")]
    Tree,
}

/// A tree representing the directories and files of a GitHub repo.
#[derive(Debug, Clone, PartialEq)]
pub struct SourceTree {
    /// The path of the file relative to the root of the repository.
    pub path: PathBuf,
    /// The unix permissions mode of the file, in numeric notation.  
    pub mode: String,
    /// The SHA1 hash identifying this blob or tree.
    ///
    /// This is NOT the same as the sha1 hash of the contents.
    pub sha: String,
    /// The type of the entry.
    pub entry_type: TreeEntryType,
    /// The size of the entry in bytes, or 0 for blob entries.
    pub size: u32,
    /// The API URL to call to get more information on this object.
    ///
    /// - For [TreeEntryType::Blob], this is the URL of the `Get a blob` API call for this entry.
    /// - For [TreeEntryType::Tree], this is the URL of the `Get a tree` API call for this entry.
    pub url: String,
    /// The children of this entry, if any.
    pub children: Vec<SourceTree>,
}

/// A type used while building a [SourceTree] from a [TreeModel].
#[derive(Clone)]
struct SourceTreeInter {
    pub path: PathBuf,
    pub mode: String,
    pub sha: String,
    pub entry_type: TreeEntryType,
    pub size: u32,
    pub url: String,
    pub children: Vec<Rc<RefCell<SourceTreeInter>>>,
}

impl SourceTree {
    /// Create a new empty [SourceTree] with the given [TreeEntryType].
    pub fn new(entry_type: TreeEntryType) -> SourceTree {
        SourceTree {
            url: String::new(),
            sha: String::new(),
            path: PathBuf::new(),
            mode: String::new(),
            entry_type,
            size: 0,
            children: Vec::new(),
        }
    }

    /// Obtain the entire [SourceTree] for a given [GithubBranchPath].
    pub async fn get<'p>(path: &'p GithubBranchPath<'p>) -> Result<SourceTree, Error> {
        let tree = TreeModel::get_tree(path).await?;
        Ok(tree.into())
    }

    /// Walks the tree to find a blob at the given path, if any.
    /// Equivalent to [resolve](SourceTree::resolve) with `find_blob` as `Some(true)`.
    pub fn resolve_blob(&self, path: &Path) -> Option<&SourceTree> {
        self.resolve(path, Some(true))
    }

    /// Walks the tree to find a tree (directory) at the given path, if any.
    /// Equivalent to [resolve](SourceTree::resolve) with `find_blob` as `Some(false)`.
    pub fn resolve_tree(&self, path: &Path) -> Option<&SourceTree> {
        self.resolve(path, Some(false))
    }

    /// Walks the tree to find a node at the given path, if any.
    /// Equivalent to [resolve](SourceTree::resolve) with `find_blob` as `None`.
    pub fn resolve_any(&self, path: &Path) -> Option<&SourceTree> {
        self.resolve(path, Some(false))
    }

    /// Walks the tree to find an entry at the given path, if any.
    ///
    /// - If `find_blob` is `Some(true)`, only blob entries will be returned.
    /// - If `find_blob` is `Some(false)`, only tree entries will be returned.
    /// - If `find_blob` is `None`, the first type of entry found will be returned.
    pub fn resolve(&self, path: &Path, find_blob: Option<bool>) -> Option<&SourceTree> {
        // we reverse the path because going parent->parent->parent is easier
        let components: Vec<Component> = path.components().into_iter().collect();
        self.resolve_inner(&components[..], find_blob)
    }

    fn resolve_inner(&self, path: &[Component], find_blob: Option<bool>) -> Option<&SourceTree> {
        if path.is_empty() {
            if let Some(find_blob) = find_blob {
                if (find_blob && self.entry_type != TreeEntryType::Blob)
                    || (!find_blob && self.entry_type != TreeEntryType::Tree)
                {
                    return None;
                }
            }

            return Some(self);
        }

        let file_name = &path[0];
        for c in &self.children {
            if let Some(name) = c.path.file_name() {
                if name == file_name.as_os_str() {
                    return c.resolve_inner(&path[1..], find_blob);
                }
            }
        }

        return None;
    }

    /// Creates a SourceTreeIterator that will walk down this tree and return a pointer for each node found.
    pub fn iter<'tree>(&'tree self) -> SourceTreeIterator<'tree> {
        let mut list = LinkedList::new();
        list.push_back((self, -1));
        SourceTreeIterator(list)
    }

    /// Creates a new [SourceTree] from this tree, only including child nodes where `f` returns true.
    pub fn prune(&self, predicate: for<'a> fn(&'a &SourceTree) -> bool) -> SourceTree {
        let new_children: Vec<SourceTree> = self
            .children
            .iter()
            .filter(predicate)
            .map(|c| c.prune(predicate))
            .collect();

        SourceTree {
            path: self.path.clone(),
            mode: self.mode.clone(),
            sha: self.sha.clone(),
            entry_type: self.entry_type.clone(),
            size: self.size,
            url: self.url.clone(),
            children: new_children,
        }
    }
}

/// An iterator for a [SourceTree] that walks the tree and returns a pointer to each node found.
pub struct SourceTreeIterator<'tree>(LinkedList<(&'tree SourceTree, isize)>);

impl<'tree> Iterator for SourceTreeIterator<'tree> {
    type Item = &'tree SourceTree;

    fn next(&mut self) -> Option<Self::Item> {
        let state = self.0.pop_back();
        if state.is_none() {
            return None;
        }

        let (node, mut pos) = state.unwrap();
        if pos >= (node.children.len() as isize) && self.0.is_empty() {
            // no children
            return None;
        }

        let ptr = match pos == -1 {
            true => node,
            false => &node.children[pos as usize],
        };

        pos = pos + 1;
        if pos < (node.children.len() as isize) {
            self.0.push_back((node, pos));
        }

        if !ptr.children.is_empty() {
            self.0.push_back((ptr, 0));
        }

        return Some(ptr);
    }
}

impl From<SourceTreeInter> for SourceTree {
    fn from(value: SourceTreeInter) -> Self {
        SourceTree {
            path: value.path,
            mode: value.mode,
            sha: value.sha,
            url: value.url,
            entry_type: value.entry_type,
            size: value.size,
            children: value
                .children
                .into_iter()
                .map(|s| s.borrow().clone().into())
                .collect(),
        }
    }
}

impl From<TreeEntryModel> for SourceTreeInter {
    fn from(value: TreeEntryModel) -> Self {
        SourceTreeInter {
            path: PathBuf::from(value.path),
            mode: value.mode,
            sha: value.sha,
            entry_type: value.entry_type,
            size: value.size,
            url: value.url,
            children: Vec::new(),
        }
    }
}

impl From<TreeModel> for SourceTree {
    fn from(value: TreeModel) -> Self {
        let mut dirs_for_path: HashMap<PathBuf, Rc<RefCell<SourceTreeInter>>> = HashMap::new();
        let mut nodes: LinkedList<Rc<RefCell<SourceTreeInter>>> = LinkedList::new();

        // read entries into nodes
        for entry in value.tree {
            match entry.entry_type {
                TreeEntryType::Blob => {
                    nodes.push_back(Rc::new(RefCell::new(entry.into())));
                }
                TreeEntryType::Tree => {
                    let path = entry.path.clone();
                    let entry = Rc::new(RefCell::new(entry.into()));
                    nodes.push_back(entry.clone());
                    dirs_for_path.insert(path.into(), entry);
                }
            }
        }

        let root = RefCell::new(SourceTreeInter {
            url: value.url,
            sha: value.sha,
            path: PathBuf::new(),
            mode: String::new(),
            entry_type: TreeEntryType::Tree,
            size: 0,
            children: Vec::new(),
        });

        // assign files to dir tree
        while let Some(node) = nodes.pop_front() {
            let node_ref = node.borrow();
            let path = Path::new(&node_ref.path);
            let dir_path: &Path = path.parent().unwrap_or(Path::new(""));
            let dir = match dir_path.as_os_str().len() {
                // use root node for
                0 => &root,
                _ => dirs_for_path.get(dir_path).unwrap(),
            };

            drop(node_ref);

            dir.borrow_mut().children.push(node);
        }

        root.into_inner().into()
    }
}

#[derive(Deserialize)]
struct TreeEntryModel {
    pub path: String,
    pub mode: String,
    #[serde(rename = "type")]
    pub entry_type: TreeEntryType,
    #[serde(default)]
    pub size: u32,
    pub sha: String,
    pub url: String,
}

/// The result of a call to the GitHub `Get a tree` API
#[derive(Deserialize)]
struct TreeModel {
    pub sha: String,
    pub url: String,
    pub tree: Vec<TreeEntryModel>,
    pub truncated: bool,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum TreeOrError {
    Tree(TreeModel),
    Error { message: String },
}

impl<'path> TreeModel {
    /// Obtains a tree first recursively, and then non-recursively if truncated.
    async fn get_tree(path: &GithubBranchPath<'path>) -> Result<TreeModel, Error> {
        let recursive_tree = TreeModel::get_tree_request(path, true).await?;
        if !recursive_tree.truncated {
            return Ok(recursive_tree);
        }

        let initial_tree = TreeModel::get_tree_request(path, false).await?;
        let mut entries: Vec<TreeEntryModel> = Vec::new();
        for entry in &initial_tree.tree {
            if entry.entry_type == TreeEntryType::Tree {
                TreeModel::get_tree_manual(path, &entry.path, &mut entries).await?;
            }
        }

        entries.extend(initial_tree.tree.into_iter());

        Ok(TreeModel {
            sha: initial_tree.sha,
            url: initial_tree.url,
            tree: entries,
            truncated: false,
        })
    }

    /// Recursively fills out the tree using the non-recursive version of the endpoint, collecting entries in `entries`.
    fn get_tree_manual<'a>(
        path: &'a GithubBranchPath<'path>,
        parent_entry_path: &'a str,
        entries: &'a mut Vec<TreeEntryModel>,
    ) -> BoxFuture<'a, Result<&'a mut Vec<TreeEntryModel>, Error>>
    where
        'path: 'a,
    {
        // have to use boxed async here because we're calling an async recursively
        async move {
            let model = TreeModel::get_tree_request(path, false).await?;
            for entry in &model.tree {
                if entry.entry_type == TreeEntryType::Tree {
                    TreeModel::get_tree_manual(
                        &path.with_branch(&entry.sha),
                        &format!("{}/{}", parent_entry_path, entry.path),
                        entries,
                    )
                    .await?;
                }
            }

            entries.extend(model.tree.into_iter());

            Ok(entries)
        }
        .boxed()
    }

    /// Makes a request to the get tree endpoint
    async fn get_tree_request(
        path: &GithubBranchPath<'path>,
        recursive: bool,
    ) -> Result<TreeModel, Error> {
        let url = path.to_tree_url();

        let client = HttpRequest::client(&None)?;
        let request = match recursive {
            true => client.get(url).query(&[("recursive", true)]),
            false => client.get(url),
        };

        let request = request
            .header("Accept", "application/vnd.github+json")
            .build()?;

        let response = client.execute(request).await?;
        let body = response.text().await?;

        let result = serde_json::from_str::<TreeOrError>(&body)?;
        match result {
            TreeOrError::Error { message } => Err(Error::GithubError(message)),
            TreeOrError::Tree(t) => Ok(t),
        }
    }
}