Skip to main content

github_smoke/
github_smoke.rs

1//! Read-only smoke run against github.com: lists the account logins, counts
2//! each section for the active account and loads one pull request's diff
3//! metadata. Prints no tokens and makes no writes.
4//!
5//! `cargo run -q -p subspace-client-github --example github_smoke`
6
7use std::sync::Arc;
8
9use subspace_client_github::Client;
10use subspace_client_github::model::{ItemKind, short_login};
11use subspace_core::config;
12use subspace_core::credentials::Credentials;
13
14fn main() {
15    let config = config::path()
16        .map(|path| config::load(&path).0)
17        .unwrap_or_default();
18    let client = Client::new(Arc::new(Credentials::from_system(&config)));
19    let login = active_login(&client);
20    let first_pr = print_sections(&client, &login);
21    print_diff(&client, &login, first_pr.as_deref());
22}
23
24/// The active account, or the first one. Exits when gh knows none.
25fn active_login(client: &Client) -> String {
26    let accounts = match client.accounts() {
27        Ok(accounts) => accounts,
28        Err(error) => {
29            eprintln!("accounts: {error}");
30            std::process::exit(1);
31        }
32    };
33    println!("accounts: {}", accounts.logins.join(", "));
34    let Some(login) = accounts
35        .active
36        .clone()
37        .or_else(|| accounts.logins.first().cloned())
38    else {
39        eprintln!("no account");
40        std::process::exit(1);
41    };
42    println!("active: {login} (short: {})", short_login(&login));
43    login
44}
45
46/// Counts every section and returns the first pull request it saw.
47fn print_sections(client: &Client, login: &str) -> Option<String> {
48    let mut first_pr = None;
49    for (id, result) in client.sections(login) {
50        match result {
51            Ok(section) => {
52                println!(
53                    "section {id}: {} items of {}",
54                    section.items.len(),
55                    section.total
56                );
57                if first_pr.is_none() {
58                    first_pr = section
59                        .items
60                        .iter()
61                        .find(|item| item.kind == ItemKind::Pr)
62                        .map(|item| item.key.clone());
63                }
64            }
65            Err(error) => println!("section {id}: error: {error}"),
66        }
67    }
68    first_pr
69}
70
71fn print_diff(client: &Client, login: &str, key: Option<&str>) {
72    let Some(key) = key else {
73        println!("diff: no pull request in any section");
74        return;
75    };
76    match client.diff(login, key) {
77        Ok(diff) => println!(
78            "diff: a pull request with {} files of {} (merge base {}…, head {}…)",
79            diff.files.len(),
80            diff.total,
81            &diff.base_sha[..7],
82            &diff.head_sha[..7]
83        ),
84        Err(error) => println!("diff: error: {error}"),
85    }
86}