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
use failure::{Error, ResultExt};
use std::fmt::{self, Debug, Formatter};

use config::GithubConfig;
use utils::Paginated;
use {Provider, Repo};

/// An interface to the repositories stored on github.
#[derive(Clone)]
pub struct GitHub {
    cfg: GithubConfig,
}

impl GitHub {
    /// Create a new `GitHub` with the provided config.
    pub fn with_config(cfg: GithubConfig) -> GitHub {
        GitHub { cfg }
    }

    fn get_owned(&self) -> Result<Vec<Repo>, Error> {
        debug!("Fetching owned repositories");

        let mut owned = Vec::new();

        for repo in Paginated::new(
            self.cfg.api_key.reveal_str(),
            "https://api.github.com/user/repos",
        ) {
            let repo: RawRepo = repo?;
            owned.push(self.convert_repo(repo));
        }

        debug!("{} owned repos", owned.len());
        Ok(owned)
    }

    fn get_starred(&self) -> Result<Vec<Repo>, Error> {
        debug!("Fetching starred repositories");

        let mut starred = Vec::new();

        for repo in Paginated::new(
            self.cfg.api_key.reveal_str(),
            "https://api.github.com/user/starred",
        ) {
            let repo: RawRepo = repo?;
            starred.push(self.convert_repo(repo));
        }

        debug!("{} starred repos", starred.len());
        Ok(starred)
    }

    fn convert_repo(&self, raw: RawRepo) -> Repo {
        Repo {
            name: raw.name,
            owner: raw.owner.login,
            url: raw.clone_url,
            provider: self.name().to_string(),
        }
    }
}

impl Provider for GitHub {
    fn name(&self) -> &str {
        "github"
    }

    fn repositories(&self) -> Result<Vec<Repo>, Error> {
        let mut repos = Vec::new();

        if self.cfg.owned {
            repos.extend(
                self.get_owned()
                    .context("Unable to fetch owned repositories")?,
            );
        }
        if self.cfg.starred {
            repos.extend(
                self.get_starred()
                    .context("Unable to fetch starred repositories")?,
            );
        }

        Ok(repos)
    }
}

impl Debug for GitHub {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_struct("GitHub").finish()
    }
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
struct RawRepo {
    name: String,
    clone_url: String,
    owner: Owner,
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
struct Owner {
    login: String,
    #[serde(rename = "type")]
    kind: String,
}