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
use serde::Deserialize;

use super::auth;
use super::escape;
use super::ApiErrorResponse;
use super::Filter;
use super::JsonError;
use super::Project;
use super::Provider;

const ACCEPT_HEADER_JSON: &str = "application/vnd.github.v3+json";
const GITHUB_API_BASEURL: &str =
    option_env!("GITHUB_API_BASEURL").unwrap_or("https://api.github.com");

#[derive(Deserialize)]
pub struct GithubProject {
    pub name: String,
    pub full_name: String,
    pub clone_url: String,
    pub ssh_url: String,
    pub private: bool,
}

#[derive(Deserialize)]
struct GithubUser {
    #[serde(rename = "login")]
    pub username: String,
}

impl Project for GithubProject {
    fn name(&self) -> String {
        self.name.clone()
    }

    fn namespace(&self) -> Option<String> {
        if let Some((namespace, _name)) = self.full_name.rsplit_once('/') {
            Some(namespace.to_string())
        } else {
            None
        }
    }

    fn ssh_url(&self) -> String {
        self.ssh_url.clone()
    }

    fn http_url(&self) -> String {
        self.clone_url.clone()
    }

    fn private(&self) -> bool {
        self.private
    }
}

#[derive(Deserialize)]
pub struct GithubApiErrorResponse {
    pub message: String,
}

impl JsonError for GithubApiErrorResponse {
    fn to_string(self) -> String {
        self.message
    }
}

pub struct Github {
    filter: Filter,
    secret_token: auth::AuthToken,
}

impl Provider for Github {
    type Project = GithubProject;
    type Error = GithubApiErrorResponse;

    fn new(
        filter: Filter,
        secret_token: auth::AuthToken,
        api_url_override: Option<String>,
    ) -> Result<Self, String> {
        if api_url_override.is_some() {
            return Err("API URL overriding is not supported for Github".to_string());
        }
        Ok(Self {
            filter,
            secret_token,
        })
    }

    fn filter(&self) -> &Filter {
        &self.filter
    }

    fn secret_token(&self) -> &auth::AuthToken {
        &self.secret_token
    }

    fn auth_header_key() -> &'static str {
        "token"
    }

    fn get_user_projects(
        &self,
        user: &str,
    ) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
        self.call_list(
            &format!("{GITHUB_API_BASEURL}/users/{}/repos", escape(user)),
            Some(ACCEPT_HEADER_JSON),
        )
    }

    fn get_group_projects(
        &self,
        group: &str,
    ) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
        self.call_list(
            &format!("{GITHUB_API_BASEURL}/orgs/{}/repos?type=all", escape(group)),
            Some(ACCEPT_HEADER_JSON),
        )
    }

    fn get_accessible_projects(
        &self,
    ) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
        self.call_list(
            &format!("{GITHUB_API_BASEURL}/user/repos"),
            Some(ACCEPT_HEADER_JSON),
        )
    }

    fn get_current_user(&self) -> Result<String, ApiErrorResponse<GithubApiErrorResponse>> {
        Ok(super::call::<GithubUser, GithubApiErrorResponse>(
            &format!("{GITHUB_API_BASEURL}/user"),
            Self::auth_header_key(),
            self.secret_token(),
            Some(ACCEPT_HEADER_JSON),
        )?
        .username)
    }
}