contributor-graphs 1.3.1

Generate contributor timeline graphs (static SVG + interactive HTML) for any git or GitHub repository
Documentation
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use crate::cache::Caches;
use crate::identity::Cluster;
use crate::model::{Commit, Contributor};
use base64::Engine;
use std::collections::HashMap;
use std::io::IsTerminal;
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::time::Duration;

const THREADS: usize = 8;

/// A GitHub user profile reduced to (display name, affiliation).
type Profile = (Option<String>, Option<String>);

pub struct GhClient {
    agent: ureq::Agent,
    token: Option<String>,
}

/// Get a GitHub token: $GITHUB_TOKEN / $GH_TOKEN, else `gh auth token`.
pub fn find_token() -> Option<String> {
    for var in ["GITHUB_TOKEN", "GH_TOKEN"] {
        if let Ok(t) = std::env::var(var) {
            let t = t.trim().to_string();
            if !t.is_empty() {
                return Some(t);
            }
        }
    }
    let out = Command::new("gh").args(["auth", "token"]).output().ok()?;
    if out.status.success() {
        let t = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if !t.is_empty() {
            return Some(t);
        }
    }
    None
}

/// Parse GitHub noreply addresses: `12345+login@users.noreply.github.com`
/// or the older `login@users.noreply.github.com`.
pub fn parse_noreply(email: &str) -> Option<(Option<u64>, String)> {
    let local = email.strip_suffix("@users.noreply.github.com")?;
    match local.split_once('+') {
        Some((id, login)) => {
            let id = id.parse::<u64>().ok();
            Some((id, login.to_string()))
        }
        None => Some((None, local.to_string())),
    }
}

impl GhClient {
    pub fn new(token: Option<String>) -> Self {
        let agent = ureq::AgentBuilder::new()
            .timeout(Duration::from_secs(30))
            .user_agent("contributor-graphs (https://github.com/ewels/contributor-graphs)")
            .build();
        GhClient { agent, token }
    }

    pub fn has_token(&self) -> bool {
        self.token.is_some()
    }

    fn get_json(&self, url: &str) -> Option<serde_json::Value> {
        let mut req = self
            .agent
            .get(url)
            .set("Accept", "application/vnd.github+json")
            .set("X-GitHub-Api-Version", "2022-11-28");
        if let Some(t) = &self.token {
            req = req.set("Authorization", &format!("Bearer {t}"));
        }
        match req.call() {
            Ok(resp) => resp.into_json().ok(),
            Err(ureq::Error::Status(code, _)) => {
                if code == 403 || code == 429 {
                    eprintln!("  warning: GitHub API rate limited (HTTP {code})");
                }
                None
            }
            Err(_) => None,
        }
    }

    /// Resolve the GitHub login + avatar for the author of a commit.
    fn commit_author(&self, slug: &str, sha: &str) -> Option<(String, String)> {
        let v = self.get_json(&format!(
            "https://api.github.com/repos/{slug}/commits/{sha}"
        ))?;
        let author = v.get("author")?;
        let login = author.get("login")?.as_str()?.to_string();
        let avatar = author
            .get("avatar_url")
            .and_then(|a| a.as_str())
            .map(String::from)
            .unwrap_or_else(|| format!("https://avatars.githubusercontent.com/{login}"));
        Some((login, avatar))
    }

    /// Fetch a user profile: (display name, company/affiliation).
    fn user_profile(&self, login: &str) -> Profile {
        let Some(v) = self.get_json(&format!("https://api.github.com/users/{login}")) else {
            return (None, None);
        };
        let name = v
            .get("name")
            .and_then(|n| n.as_str())
            .map(str::trim)
            .filter(|n| !n.is_empty())
            .map(String::from);
        let company = v
            .get("company")
            .and_then(|c| c.as_str())
            .and_then(normalize_company);
        (name, company)
    }

    /// List every repository under a GitHub org or user, returning `owner/repo`
    /// slugs. Forks are skipped; archived repos are kept. Tries the org endpoint
    /// first and falls back to the user endpoint, so it works for either kind of
    /// account. Returns an empty vec if the owner can't be listed.
    pub fn list_owner_repos(&self, owner: &str) -> Vec<String> {
        for kind in ["orgs", "users"] {
            let mut slugs = Vec::new();
            let mut page = 1;
            let mut reached = false;
            loop {
                let url =
                    format!("https://api.github.com/{kind}/{owner}/repos?per_page=100&page={page}");
                let Some(v) = self.get_json(&url) else { break };
                reached = true;
                let Some(arr) = v.as_array() else { break };
                let count = arr.len();
                for repo in arr {
                    if repo.get("fork").and_then(|f| f.as_bool()).unwrap_or(false) {
                        continue;
                    }
                    if let Some(full) = repo.get("full_name").and_then(|n| n.as_str()) {
                        slugs.push(full.to_string());
                    }
                }
                if count < 100 {
                    break;
                }
                page += 1;
            }
            if reached && !slugs.is_empty() {
                return slugs;
            }
        }
        Vec::new()
    }

    pub fn fetch_bytes(&self, url: &str) -> Option<(Vec<u8>, String)> {
        let resp = self.agent.get(url).call().ok()?;
        let ct = resp.content_type().to_string();
        let mut buf = Vec::new();
        use std::io::Read;
        resp.into_reader()
            .take(4 * 1024 * 1024)
            .read_to_end(&mut buf)
            .ok()?;
        Some((buf, ct))
    }
}

/// Fill in `login` / `avatar_url` on clusters. Noreply emails resolve
/// offline; the rest are looked up via the commits API (one representative
/// commit per cluster), in parallel. `source_slugs` maps a commit's `src`
/// index to the `owner/repo` slug to query (or `None` for non-GitHub sources).
pub fn enrich_clusters(
    clusters: &mut [Cluster],
    commits: &[Commit],
    source_slugs: &[Option<String>],
    client: &GhClient,
    caches: &mut Caches,
    verbose: bool,
) {
    let slug_of = |c: &Commit| -> Option<&str> {
        source_slugs.get(c.src as usize).and_then(|s| s.as_deref())
    };
    let mut need_api: Vec<(usize, String, String)> = Vec::new();
    for (i, cl) in clusters.iter_mut().enumerate() {
        for email in &cl.emails {
            if let Some((id, login)) = parse_noreply(email) {
                cl.avatar_url = Some(match id {
                    Some(id) => format!("https://avatars.githubusercontent.com/u/{id}?v=4"),
                    None => format!("https://avatars.githubusercontent.com/{login}"),
                });
                cl.login = Some(login);
                break;
            }
        }
        if cl.login.is_none() {
            // Use the most recent commit that came from a GitHub source: old
            // commits are more likely to have stale email → account mappings.
            let rep = cl
                .commit_idxs
                .iter()
                .filter(|&&i| slug_of(&commits[i]).is_some())
                .max_by_key(|&&i| commits[i].ts);
            if let Some(&idx) = rep {
                let slug = slug_of(&commits[idx]).unwrap().to_string();
                need_api.push((i, slug, commits[idx].sha.clone()));
            }
        }
    }

    // A commit's author never changes, so resolved SHAs are cached forever.
    let mut from_cache = 0usize;
    need_api.retain(|(idx, _, sha)| match caches.author(sha) {
        Some(a) => {
            clusters[*idx].login = Some(a.login);
            clusters[*idx].avatar_url = Some(a.avatar_url);
            from_cache += 1;
            false
        }
        None => true,
    });

    if need_api.is_empty() || !client.has_token() {
        if !need_api.is_empty() && verbose {
            eprintln!(
                "  no GitHub token found ({} identities left unresolved) — run `gh auth login` to enable lookups",
                need_api.len()
            );
        }
        if from_cache > 0 && verbose {
            eprintln!("  resolved {from_cache} identities from cache");
        }
        return;
    }

    let cursor = AtomicUsize::new(0);
    let results: Mutex<HashMap<usize, (String, String)>> = Mutex::new(HashMap::new());
    let pb = crate::progress::bar(
        "resolving identities",
        need_api.len(),
        verbose && std::io::stderr().is_terminal(),
    );
    std::thread::scope(|s| {
        for _ in 0..THREADS.min(need_api.len()) {
            s.spawn(|| loop {
                let i = cursor.fetch_add(1, Ordering::Relaxed);
                let Some((cluster_idx, slug, sha)) = need_api.get(i) else {
                    break;
                };
                if let Some(found) = client.commit_author(slug, sha) {
                    results.lock().unwrap().insert(*cluster_idx, found);
                }
                pb.inc(1);
            });
        }
    });
    pb.finish_and_clear();

    let results = results.into_inner().unwrap();
    let resolved = results.len();
    // Map cluster index back to its representative SHA so resolved authors can
    // be cached against the (immutable) commit.
    let sha_of: HashMap<usize, &str> = need_api
        .iter()
        .map(|(idx, _, sha)| (*idx, sha.as_str()))
        .collect();
    for (idx, (login, avatar)) in results {
        if let Some(sha) = sha_of.get(&idx) {
            caches.put_author(sha.to_string(), login.clone(), avatar.clone());
        }
        clusters[idx].login = Some(login);
        clusters[idx].avatar_url = Some(avatar);
    }
    if verbose {
        eprintln!(
            "  resolved {resolved}/{} identities via GitHub API{}",
            need_api.len(),
            if from_cache > 0 {
                format!(" ({from_cache} more from cache)")
            } else {
                String::new()
            }
        );
    }
}

/// Fetch a repository's description from the GitHub API, if it has one.
pub fn fetch_repo_description(client: &GhClient, slug: &str) -> Option<String> {
    let v = client.get_json(&format!("https://api.github.com/repos/{slug}"))?;
    v.get("description")
        .and_then(|d| d.as_str())
        .map(str::trim)
        .filter(|d| !d.is_empty())
        .map(String::from)
}

/// Fetch a GitHub avatar (e.g. an org/owner) and return it as a data URI,
/// caching the result by login and size.
pub fn fetch_avatar(
    client: &GhClient,
    caches: &mut Caches,
    login: &str,
    size: u32,
) -> Option<String> {
    let key = format!("owner:{login}:{size}");
    if let Some(data) = caches.avatar(&key) {
        return Some(data);
    }
    let url = format!("https://avatars.githubusercontent.com/{login}?s={size}");
    let (bytes, ct) = client.fetch_bytes(&url)?;
    let ct = if ct.starts_with("image/") {
        ct
    } else {
        "image/png".into()
    };
    let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes);
    let data = format!("data:{ct};base64,{b64}");
    caches.put_avatar(key, data.clone());
    Some(data)
}

/// Clean up the free-text GitHub `company` field into a usable group name.
/// Handles common patterns like "@seqeralabs", "QBiC @qbicsoftware", and
/// multi-affiliation strings ("Seqera | SciLifeLab" → "Seqera").
pub fn normalize_company(raw: &str) -> Option<String> {
    let mut s = raw.trim();
    // Multiple affiliations: keep the first one.
    for sep in [" | ", " / ", ";", " · ", ","] {
        if let Some(i) = s.find(sep) {
            s = &s[..i];
        }
    }
    s = s.trim().trim_start_matches('@').trim();
    // "Company @githuborg" → "Company".
    if let Some(i) = s.find(" @") {
        s = &s[..i];
    }
    let s = s.trim().trim_end_matches(['.', ',', ';', '|', '/']).trim();
    if s.is_empty() || s.chars().count() > 60 {
        return None;
    }
    Some(s.to_string())
}

/// Fetch GitHub user profiles for every resolved login: improves display
/// names ("phue" → "Patrick Hüther") and yields company affiliations.
pub fn fetch_profiles(
    clusters: &mut [Cluster],
    client: &GhClient,
    caches: &mut Caches,
    verbose: bool,
) {
    let mut logins: Vec<String> = clusters.iter().filter_map(|c| c.login.clone()).collect();
    logins.sort();
    logins.dedup();
    if logins.is_empty() {
        return;
    }

    // Cached profiles skip the API; only the misses are fetched.
    let mut profiles: HashMap<String, Profile> = HashMap::new();
    let mut to_fetch: Vec<String> = Vec::new();
    for login in logins {
        match caches.profile(&login) {
            Some(p) => {
                profiles.insert(login, p);
            }
            None => to_fetch.push(login),
        }
    }
    let from_cache = profiles.len();

    if !to_fetch.is_empty() && client.has_token() {
        let cursor = AtomicUsize::new(0);
        let results: Mutex<HashMap<String, Profile>> = Mutex::new(HashMap::new());
        let pb = crate::progress::bar(
            "fetching profiles",
            to_fetch.len(),
            verbose && std::io::stderr().is_terminal(),
        );
        std::thread::scope(|s| {
            for _ in 0..THREADS.min(to_fetch.len()) {
                s.spawn(|| loop {
                    let i = cursor.fetch_add(1, Ordering::Relaxed);
                    let Some(login) = to_fetch.get(i) else { break };
                    let profile = client.user_profile(login);
                    results.lock().unwrap().insert(login.clone(), profile);
                    pb.inc(1);
                });
            }
        });
        pb.finish_and_clear();
        for (login, (name, company)) in results.into_inner().unwrap() {
            caches.put_profile(login.clone(), name.clone(), company.clone());
            profiles.insert(login, (name, company));
        }
    }

    let with_company = profiles.values().filter(|(_, c)| c.is_some()).count();
    for cl in clusters.iter_mut() {
        if let Some(login) = &cl.login {
            if let Some((name, company)) = profiles.get(login) {
                cl.profile_name = name.clone();
                cl.affiliation = company.clone();
            }
        }
    }
    if verbose {
        eprintln!(
            "  fetched {} profiles ({} with an affiliation, {from_cache} from cache)",
            profiles.len(),
            with_company,
        );
    }
}

/// Replace remote avatar URLs with embedded data URIs so the outputs are
/// fully self-contained (and render in places that block remote images).
pub fn embed_avatars(
    contributors: &mut [Contributor],
    client: &GhClient,
    caches: &mut Caches,
    size: u32,
    verbose: bool,
) {
    let mut urls: Vec<String> = Vec::new();
    for c in contributors.iter() {
        if let Some(u) = &c.avatar {
            if !u.starts_with("data:") && !urls.contains(u) {
                urls.push(u.clone());
            }
        }
    }
    if urls.is_empty() {
        return;
    }

    // Cache by avatar URL and size. Cached images skip the download.
    let key_of = |u: &str| format!("{u}|{size}");
    let mut embedded: HashMap<String, String> = HashMap::new();
    let mut to_fetch: Vec<String> = Vec::new();
    for u in urls {
        match caches.avatar(&key_of(&u)) {
            Some(data) => {
                embedded.insert(u, data);
            }
            None => to_fetch.push(u),
        }
    }
    let from_cache = embedded.len();

    if !to_fetch.is_empty() {
        let sized: Vec<String> = to_fetch
            .iter()
            .map(|u| {
                if u.contains('?') {
                    format!("{u}&s={size}")
                } else {
                    format!("{u}?s={size}")
                }
            })
            .collect();
        let cursor = AtomicUsize::new(0);
        let results: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
        let pb = crate::progress::bar(
            "embedding avatars",
            to_fetch.len(),
            verbose && std::io::stderr().is_terminal(),
        );
        std::thread::scope(|s| {
            for _ in 0..THREADS.min(to_fetch.len()) {
                s.spawn(|| loop {
                    let i = cursor.fetch_add(1, Ordering::Relaxed);
                    let (Some(orig), Some(fetch_url)) = (to_fetch.get(i), sized.get(i)) else {
                        break;
                    };
                    if let Some((bytes, ct)) = client.fetch_bytes(fetch_url) {
                        let ct = if ct.starts_with("image/") {
                            ct
                        } else {
                            "image/png".into()
                        };
                        let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes);
                        results
                            .lock()
                            .unwrap()
                            .insert(orig.clone(), format!("data:{ct};base64,{b64}"));
                    }
                    pb.inc(1);
                });
            }
        });
        pb.finish_and_clear();
        for (url, data) in results.into_inner().unwrap() {
            caches.put_avatar(key_of(&url), data.clone());
            embedded.insert(url, data);
        }
    }

    let n = embedded.len();
    for c in contributors.iter_mut() {
        if let Some(u) = &c.avatar {
            if let Some(data) = embedded.get(u) {
                c.avatar = Some(data.clone());
            }
        }
    }
    if verbose {
        eprintln!("  embedded {n} avatars as data URIs ({from_cache} from cache)");
    }
}