gorc 1.0.1

GitHub Org Repostiory Clone (GORC) - A clone and fetch tool for entire GitHub organizations
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2026 Allyn L. Bottorff
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use clap::Parser;
use futures::StreamExt;
use serde::Deserialize;
use std::{
    env, fs,
    path::{Path, PathBuf},
    process::Command,
};

/// GitHub Org Repository Clone (GORC)
///
/// A simple tool to clone and sync all of the repositories from a single GitHub organization
/// This tool will attempt to find an authentication token for GitHub from the following sources,
/// in order of decreasing precedence:
/// `gh auth token` -> `GITHUB_TOKEN` -> `GITHUB_PAT`
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct CliFlags {
    ///Path to the directory where all repositories will be cloned or fetched.
    #[arg(short, long)]
    path: String,
    ///GitHub organization to clone.
    #[arg(short, long)]
    org: String,
    ///Use HTTP instead of SSH.
    #[arg(long)]
    http: bool,
    ///Use Jujutsu as the VCS.
    #[arg(short, long)]
    jj: bool,
    ///No output
    #[arg(short, long, default_value_t = false, conflicts_with = "verbose")]
    quiet: bool,
    ///Verbose output
    #[arg(short, long, default_value_t = false, conflicts_with = "quiet")]
    verbose: bool,
    ///Do not fetch updates to remote repositories, only clone new ones.
    #[arg(long, default_value_t = false)]
    nofetch: bool,
}

/// Determines whether to perform Git operations over HTTP or SSH
#[derive(Debug)]
enum Transport {
    Http,
    Ssh,
}

/// Determines whether to use native Git repositories or git-backed Jujutsu repositories. In the JJ
/// case, the --colocate flag is used to ensure Git compatibility.
#[derive(Debug)]
enum Vcs {
    Git,
    JJ,
}

/// Set the amount of output to the console during normal operations
#[derive(Debug)]
enum Verbosity {
    Quiet,   // Output nothing
    Normal,  // Normal status and progress output
    Verbose, // Error and debug information in addition to normal output
}

#[derive(Debug, Deserialize, Clone)]
struct GHRepo {
    /// Name of the repository according to GitHub
    name: String,
    /// Git protocol url
    // git_url: String,
    /// SSH clone url
    ssh_url: String,
    /// HTTP clone url
    clone_url: String,
}

/// Parsed configuration with CLI flags converted into some ergonomic types
#[derive(Debug)]
struct Config {
    org: String,
    transport: Transport,
    verbosity: Verbosity,
    vcs: Vcs,
    nofetch: bool,
    path: String,
}
impl Config {
    pub fn new_from_flags(flags: &CliFlags) -> Config {
        let transport = match flags.http {
            true => Transport::Http,
            false => Transport::Ssh,
        };
        let verbosity = match flags.quiet {
            true => Verbosity::Quiet,
            false => match flags.verbose {
                true => Verbosity::Verbose,
                false => Verbosity::Normal,
            },
        };
        let vcs = match flags.jj {
            true => Vcs::JJ,
            false => Vcs::Git,
        };

        Config {
            org: flags.org.trim().into(),
            transport,
            verbosity,
            vcs,
            nofetch: flags.nofetch,
            path: flags.path.clone().trim().into(),
        }
    }
}

fn main() -> Result<()> {
    let cli_flags = CliFlags::parse();

    let token = get_github_token(&cli_flags);

    if token.is_none() {
        println!("Unable to get a GitHub token from the environment");
    }

    // Create the static CONFIG struct that can be freely referenced everywhere
    // Abort if this doesn't succeed.
    let config = Box::new(Config::new_from_flags(&cli_flags));
    let config: &'static Config = Box::leak(config);

    // Create a reference for the config for this scope that's a little more ergonomic. If this
    // can't be accessed, abort.

    match &config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            print!("Getting org repository list... ");
        }
    }
    let repos = match get_org_repositories(config, token) {
        Ok(r) => r,
        Err(e) => {
            eprintln!();
            eprintln!("Failed to get org repositories: {}\n", e);
            return Err(e);
        }
    };
    match &config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            println!("Complete!");
        }
    }

    // Create the requested path if it doesn't exist. Abort if this cannot be created.
    fs::create_dir_all(&config.path)?;

    // If the base path can't be canonicalized after we've guaranteed its creation, then something
    // is very wrong and we should bail out.
    let base_path = Box::new(fs::canonicalize(&config.path)?);
    let base_path: &'static PathBuf = Box::leak(base_path);

    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            match config.nofetch {
                true => {
                    futures::stream::iter(repos)
                        .filter(|repo| no_existing_repo(base_path, repo.name.clone()))
                        .map(|repo| clone_one_repo(config, repo))
                        .buffer_unordered(100)
                        .for_each(|result| async {
                            match result {
                                Ok(_) => {}
                                Err(e) => {
                                    eprintln!("{}", e);
                                }
                            }
                        })
                        .await;
                }
                false => {
                    futures::stream::iter(repos)
                        .map(|repo| clone_or_fetch_wrapper(config, base_path, repo))
                        .buffer_unordered(100)
                        .for_each(|result| async {
                            match result {
                                Ok(_) => {}
                                Err(e) => {
                                    eprintln!("{}", e);
                                }
                            }
                        })
                        .await;
                }
            }
        });

    Ok(())
}

// Helper function to determine if a repo already exists by name
async fn no_existing_repo(base_path: &Path, name: String) -> bool {
    match fs::exists(base_path.join(name)).ok() {
        Some(exists) => match exists {
            true => false,
            false => true,
        },
        None => true,
    }
}

/// Get organization's repository list from the GitHub API
fn get_org_repositories(config: &Config, token: Option<String>) -> Result<Vec<GHRepo>> {
    let mut url = format!(
        "https://api.github.com/orgs/{}/repos?per_page=100",
        config.org
    );

    let token = token.unwrap_or("".into());

    let token_string = format!("Bearer {}", token);

    let mut pagination_required = true;

    let mut repositories = Vec::new();

    while pagination_required {
        let mut resp = ureq::get(&url)
            .header("User-Agent", "gorc")
            .header("Authorization", &token_string)
            .header("Accept", "application/vnd.github+json")
            .call()?;

        repositories.append(&mut resp.body_mut().read_json()?);

        let link_header = resp.headers().get("link");

        match link_header {
            Some(link) => match check_pagination(link.to_str()?) {
                Some(next_url) => {
                    url = next_url;
                }
                None => {
                    pagination_required = false;
                }
            },
            None => {
                pagination_required = false;
            }
        }
    }

    Ok(repositories)
}

fn check_pagination(link_header: &str) -> Option<String> {
    let next_link_identifier = "rel=\"next\"";
    if link_header.contains(next_link_identifier) {
        let parts = link_header.split(",");

        for part in parts {
            let part = part.trim();
            if part.contains(next_link_identifier) {
                match part.split_once(";") {
                    Some(n) => {
                        let trimmed = n.0.trim().trim_end_matches('>').trim_start_matches('<');
                        return Some(trimmed.to_owned());
                    }
                    None => return None,
                }
            }
        }
    }
    None
}

/// Get GitHub token from the environment. Early return on successfully finding a token
fn get_github_token(cli_flags: &CliFlags) -> Option<String> {
    // Attempt to get the GitHub token from the gh cli tool
    let output = Command::new("gh").args(["auth", "token"]).output();
    match output {
        Ok(token) => {
            match String::from_utf8(token.stdout) {
                Ok(token) => {
                    //TODO(alb): Validate the token
                    if !token.is_empty() {
                        let token: String = token.trim().into();
                        return Some(token);
                    }
                }
                Err(e) => {
                    if cli_flags.verbose {
                        eprintln!("Error parsing gh auth token output: {e}");
                    }
                }
            }
        }
        Err(e) => {
            if cli_flags.verbose {
                eprintln!("Error executing gh auth token: {e}");
            }
        }
    }

    // Attempt to get the token from the GITHUB_TOKEN env var
    match env::var("GITHUB_TOKEN") {
        Ok(token) => {
            //TODO(alb): Improve the token validation
            let token: String = token.trim().into();
            if !token.is_empty() {
                return Some(token);
            }
        }
        Err(e) => {
            if cli_flags.verbose {
                eprintln!("Error reading GITHUB_TOKEN env var: {e}")
            }
        }
    }

    // Attempt to get the token from the GITHUB_PAT env var
    match env::var("GITHUB_PAT") {
        Ok(token) => {
            //TODO(alb): Improve the token validation
            let token: String = token.trim().into();
            if !token.is_empty() {
                return Some(token);
            }
        }
        Err(e) => {
            if cli_flags.verbose {
                eprintln!("Error reading GITHUB_PAT env var: {e}")
            }
        }
    }
    None
}

async fn clone_or_fetch_wrapper(
    config: &Config,
    base_path: &Path,
    repo: GHRepo,
) -> Result<std::process::ExitStatus, std::io::Error> {
    match fs::exists(base_path.join(&repo.name))? {
        true => fetch_one_repo_sync(config, repo).await,
        false => clone_one_repo(config, repo).await,
    }
}

/// Clone a single repository using either Git or JJ depending on the configuration
async fn clone_one_repo(
    config: &Config,
    repo: GHRepo,
) -> Result<std::process::ExitStatus, std::io::Error> {
    let url = match config.transport {
        Transport::Http => &repo.clone_url,
        Transport::Ssh => &repo.ssh_url,
    };

    let path = fs::canonicalize(&config.path).unwrap();

    match config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            println!("Cloning:     {}", &repo.name);
        }
    }
    let result = match config.vcs {
        Vcs::Git => {
            tokio::process::Command::new("git")
                .current_dir(path)
                .arg("clone")
                .arg(url)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()?
                .wait()
                .await
        }
        Vcs::JJ => {
            tokio::process::Command::new("jj")
                .current_dir(path)
                .arg("git")
                .arg("clone")
                .arg("--colocate")
                .arg(url)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()?
                .wait()
                .await
        }
    };
    match config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            println!("Complete:    {}", &repo.name);
        }
    }

    result
}

/// Fetch a single repo using either Git or JJ depending on the configuration
async fn fetch_one_repo_sync(
    config: &Config,
    repo: GHRepo,
) -> Result<std::process::ExitStatus, std::io::Error> {
    let path = fs::canonicalize(&config.path).unwrap().join(&repo.name);

    match config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            println!("Fetching:    {}", &repo.name);
        }
    }

    let result = match config.vcs {
        Vcs::Git => {
            tokio::process::Command::new("git")
                .current_dir(path)
                .arg("fetch")
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()?
                .wait()
                .await
        }
        Vcs::JJ => {
            tokio::process::Command::new("jj")
                .current_dir(path)
                .arg("git")
                .arg("fetch")
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()?
                .wait()
                .await
        }
    };
    match config.verbosity {
        Verbosity::Quiet => {}
        _ => {
            println!("Complete:    {}", &repo.name);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_found_link_header() {
        let sample = r#"<https://api.github.com/repositories/1300192/issues?page=2>; rel="prev", <https://api.github.com/repositories/1300192/issues?page=4>; rel="next", <https://api.github.com/repositories/1300192/issues?page=515>; rel="last", <https://api.github.com/repositories/1300192/issues?page=1>; rel="first""#;

        let next_url = check_pagination(sample);

        assert_eq!(
            next_url,
            Some("https://api.github.com/repositories/1300192/issues?page=4".to_owned())
        )
    }

    #[test]
    fn parse_no_found_link_header() {
        let sample = r#"<https://api.github.com/repositories/1300192/issues?page=2>; rel="prev", <https://api.github.com/repositories/1300192/issues?page=4>; rel="last", <https://api.github.com/repositories/1300192/issues?page=1>; rel="first""#;

        let next_url = check_pagination(sample);

        assert_eq!(next_url, None,)
    }
}