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
use crate::{GithubClient, SortDirection, User};
use anyhow::{bail, format_err, Context, Result};
use async_trait::async_trait;
use jacklog::{debug, trace};
use reqwest::header::LINK;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, Deserialize, Serialize)]
pub struct Commit {
    pub author: GitUser,
    pub committer: GitUser,
    pub message: String,
    pub tree: Tree,
    pub comment_count: usize,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GitUser {
    pub name: String,
    pub email: String,
    pub date: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Tree {
    pub sha: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Stats {
    pub additions: usize,
    pub deletions: usize,
    pub total: usize,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct File {
    pub filename: String,
    pub additions: usize,
    pub deletions: usize,
    pub changes: usize,
    pub status: String,
    pub raw_url: String,
    pub blob_url: String,
    pub patch: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Repository {
    pub id: u64,
    pub node_id: String,
    pub name: String,
    pub full_name: String,
    pub owner: User,
    pub private: bool,
    pub html_url: String,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ListRepositoriesResponse {
    pub repositories: Vec<Repository>,
    pub next: Option<String>,
    pub last: Option<String>,
    pub first: Option<String>,
    pub prev: Option<String>,
}

#[derive(Default, Debug)]
pub struct ListRepositoriesRequest {
    pub org: String,
    pub _type: Option<RepositoryType>,
    pub sort: Option<ListRepositoriesSort>,
    pub direction: Option<SortDirection>,
    pub per_page: Option<usize>, // max 100
    pub page: Option<usize>,
}

#[derive(Default, Debug)]
pub struct GetCommitRequest {
    pub owner: String,
    pub repo: String,
    pub _ref: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GetCommitResponse {
    pub sha: String,
    pub commit: Commit,
    pub author: Option<User>,
    pub committer: User,
    pub parents: Vec<Tree>,
    pub stats: Stats,
    pub files: Vec<File>,
    pub next: Option<String>,
    pub last: Option<String>,
    pub first: Option<String>,
    pub prev: Option<String>,
}

#[derive(Default, Debug)]
pub struct GetRepositoryContentRequest {
    pub owner: String,
    pub repo: String,
    pub path: PathBuf,
    /// Defaults to the repository's default branch.
    pub _ref: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GetRepositoryContentResponse {
    #[serde(rename = "type")]
    pub _type: ContentType,
    pub encoding: String,
    pub size: u64,
    pub name: String,
    pub path: PathBuf,
    pub content: String,
    pub sha: String,
    pub html_url: String,
    pub download_url: String,
    pub next: Option<String>,
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ContentType {
    File,
    Symlink,
}

#[derive(Debug)]
pub enum RepositoryType {
    All,
    Public,
    Private,
    Forks,
    Sources,
    Member,
    Internal,
}

#[derive(Debug)]
pub enum ListRepositoriesSort {
    Created,
    Updated,
    Pushed,
    FullName,
}

#[async_trait]
pub trait Repositories {
    async fn list_repositories(
        &self,
        input: ListRepositoriesRequest,
    ) -> Result<ListRepositoriesResponse>;

    async fn get_commit(&self, input: GetCommitRequest) -> Result<GetCommitResponse>;

    async fn get_repository_content(
        &self,
        input: GetRepositoryContentRequest,
    ) -> Result<GetRepositoryContentResponse>;
}

#[async_trait]
impl Repositories for GithubClient {
    async fn list_repositories(
        &self,
        input: ListRepositoriesRequest,
    ) -> Result<ListRepositoriesResponse> {
        // Make the request.
        let res = self
            .client()
            .get(&format!("https://api.github.com/orgs/{}/repos", input.org,))
            .send()
            .await?;

        // Check the response code.
        if !res.status().is_success() {
            bail!("{}", res.status().canonical_reason().unwrap_or(&"unknown"));
        }

        // Get the link metadata.
        //let link = res.headers().get("link").unwrap().to_str()?.to_string();

        // Get the response text so we can dump it for debugging.
        let res = res.text().await?;
        trace!("{}", &res);

        //let res: Response = res.json().await?;
        let repositories: Vec<Repository> = serde_json::from_str(&res)?;
        debug!("{:?}", &repositories);

        Ok(ListRepositoriesResponse {
            repositories,
            //next: Some(link),
            ..Default::default()
        })
    }

    /// Get a commit from a repository.
    async fn get_commit(&self, input: GetCommitRequest) -> Result<GetCommitResponse> {
        // Make the request.
        let res = self
            .client()
            .get(&format!(
                "https://api.github.com/repos/{}/{}/commits/{}",
                input.owner, input.repo, input._ref
            ))
            .send()
            .await?;

        // Check the response code.
        if !res.status().is_success() {
            bail!(
                "buhtig: get_commit: error making request: {}",
                res.status().canonical_reason().unwrap_or(&"unknown")
            );
        }

        // Get the link metadata.
        let link = res
            .headers()
            .get("link")
            .map(|l| l.to_str().unwrap().to_string());

        // Get the response text so we can dump it for debugging.
        let res = res.text().await.context("getting text from request")?;
        trace!("{:?}", &res);

        //let res: Response = res.json().await?;
        let mut res: GetCommitResponse =
            serde_json::from_str(&res).context("parse GetCommitResponse")?;
        debug!("{:?}", &res);

        // Add the LINK metadata.
        res.next = link;

        Ok(res)
    }

    /// Get the content of a path at a given commit.
    ///
    /// TODO: Support getting directories in addition to files and symlinks.
    async fn get_repository_content(
        &self,
        input: GetRepositoryContentRequest,
    ) -> Result<GetRepositoryContentResponse> {
        // Make the request.
        let res = self
            .client()
            .get(&format!(
                "https://api.github.com/repos/{}/{}/contents/{}",
                input.owner,
                input.repo,
                input
                    .path
                    .as_path()
                    .to_str()
                    .ok_or(format_err!("path is not unicode"))?,
            ))
            .send()
            .await?;

        // Check the response code.
        if !res.status().is_success() {
            bail!("{}", res.status().canonical_reason().unwrap_or(&"unknown"));
        }

        // Get the link metadata.
        let link = res
            .headers()
            .get("link")
            .map(|l| l.to_str().unwrap().to_string());

        // Get the response text so we can dump it for debugging.
        let res = res.text().await?;
        trace!("{}", &res);

        //let res: Response = res.json().await?;
        let mut res: GetRepositoryContentResponse = serde_json::from_str(&res)?;
        debug!("{:?}", &res);

        // Add the LINK metadata.
        res.next = link;

        Ok(res)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;
    use crate::EnvironmentProvider;
    use crate::{org, repo};

    #[tokio::test]
    async fn test_list_repositories() {
        let client = GithubClient::new(&EnvironmentProvider::default()).unwrap();
        let res = client
            .list_repositories(ListRepositoriesRequest {
                org: org(),
                per_page: Some(1),
                ..Default::default()
            })
            .await
            .unwrap();

        // Check response.
        assert_eq!(res.repositories.first().unwrap().name, repo());

        // Check headers.
        //assert_eq!(res.next.unwrap(), "");
    }

    #[tokio::test]
    async fn test_get_repository_content() {
        let client = GithubClient::new(&EnvironmentProvider::default()).unwrap();
        let res = client
            .get_repository_content(GetRepositoryContentRequest {
                owner: org(),
                repo: repo(),
                path: PathBuf::from_str("foobar.txt").unwrap(),
                ..Default::default()
            })
            .await
            .unwrap();

        // Check response.
        let actual = base64::decode(&res.content.trim()).unwrap();
        assert_eq!(actual, "foobaz\n".as_bytes());
        assert_eq!(
            res.sha,
            "9dabfec9194affdefddeffab8a74ec018554bf56".to_string()
        );
    }
}

/*
curl -v \
  -H "Authorization: Bearer $GITHUB_TOKEN" \
  -H "Accept: application/vnd.github.v3+json" \
  'https://api.github.com/orgs/remind101/teams/r2d2/repos?per_page=1'
*/