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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! A library for measuring Github repository contributions.

mod contribs;
mod github;
mod limit;
mod repo;

// Re-export.
pub use limit::rate_limit;

use anyhow::Context;
use chrono::{DateTime, Utc};
use counter::Counter;
use indicatif::ProgressBar;
use itertools::Itertools;
use reqwest::blocking::Client;
use reqwest::header;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::time::Duration;

/// A nicer collated form of the data pulled from Github regarding User
/// Contributions.
#[derive(Serialize)]
pub struct UserContribs {
    pub location: String,
    pub total_users: u32,
    pub contributions: Vec<User>,
}

impl fmt::Display for UserContribs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "# Top 100 Open Source Contributors in {}\n\n",
            self.location
        )?;
        write!(
            f,
            "There are currently {} Github users in {}.\n\n",
            self.total_users, self.location
        )?;
        for (i, user) in self.contributions.iter().enumerate() {
            writeln!(
                f,
                "{:3}. {} ({} contributions)",
                i + 1,
                user.login,
                user.public_contributions
            )?;
        }
        Ok(())
    }
}

/// A user and their contributions.
#[derive(Serialize)]
pub struct User {
    pub login: String,
    pub name: Option<String>,
    pub public_contributions: u32,
}

impl From<contribs::UserContribs> for User {
    fn from(uc: contribs::UserContribs) -> Self {
        let public_contributions = uc.contribs();
        User {
            login: uc.login,
            name: uc.name,
            public_contributions,
        }
    }
}

/// Any type that contains a `Thread`.
trait Threaded {
    fn the_thread(&self) -> &Thread;
}

/// A Github Issue.
#[derive(Debug)]
pub struct Issue(Thread);

impl Threaded for Issue {
    fn the_thread(&self) -> &Thread {
        &self.0
    }
}

/// A Github Pull Request.
#[derive(Debug)]
pub struct PR {
    pub thread: Thread,
    pub commits: usize,
    pub merged: Option<DateTime<Utc>>,
}

impl PR {
    /// Was this Pull Request merged?
    pub fn is_merged(&self) -> bool {
        self.merged.is_some()
    }

    /// Was this Pull Request closed without merging?
    pub fn is_closed_not_merged(&self) -> bool {
        self.thread.closed.is_some() && !self.is_merged()
    }
}

impl Threaded for PR {
    fn the_thread(&self) -> &Thread {
        &self.thread
    }
}

/// A thread of conversation on Github.
///
/// This could either be associated with an Issue or a PR.
#[derive(Debug)]
pub struct Thread {
    /// Who opened the thread?
    pub author: String,
    /// When was the thread opened?
    pub posted: DateTime<Utc>,
    /// If it's already closed, when was it?
    pub closed: Option<DateTime<Utc>>,
    /// Who responded first?
    pub first_responder: Option<String>,
    /// When, if ever, was the first response?
    pub first_response: Option<DateTime<Utc>>,
    /// When, if ever, was there an "official" response?
    pub first_official_response: Option<DateTime<Utc>>,
    /// Comment counts of everyone who participated.
    pub comments: HashMap<String, usize>,
}

/// A collection of Issue and Pull Request [`Thread`](struct.Thread.html)s.
#[derive(Debug)]
pub struct Postings {
    pub issues: Vec<Issue>,
    pub prs: Vec<PR>,
}

impl Postings {
    /// Combine the results of two repository lookups.
    pub fn combine(self, other: Postings) -> Postings {
        let mut issues = self.issues;
        let mut prs = self.prs;

        issues.extend(other.issues);
        prs.extend(other.prs);

        Postings { issues, prs }
    }

    /// Consumes the `Postings` to form all the statistics.
    pub fn statistics(self) -> Statistics {
        let all_issues = self.issues.len();

        let all_closed_issues = self.issues.iter().filter(|i| i.0.closed.is_some()).count();

        let issues_with_responses = self
            .issues
            .iter()
            .filter_map(|i| i.0.first_response)
            .count();

        let issues_with_official_responses = self
            .issues
            .iter()
            .filter_map(|i| i.0.first_official_response)
            .count();

        let issue_first_resp_time = self.resp_times(|| self.issues.iter(), |i| i.0.first_response);

        let issue_official_first_resp_time =
            self.resp_times(|| self.issues.iter(), |i| i.0.first_official_response);

        let all_prs = self.prs.len();

        let prs_with_responses = self
            .prs
            .iter()
            .filter_map(|p| p.thread.first_response)
            .count();

        let prs_with_official_responses = self
            .prs
            .iter()
            .filter_map(|p| p.thread.first_official_response)
            .count();

        let pr_first_resp_time = self.resp_times(|| self.prs.iter(), |p| p.thread.first_response);

        let pr_official_first_resp_time =
            self.resp_times(|| self.prs.iter(), |p| p.thread.first_official_response);

        let prs_merged = self.prs.iter().filter(|p| p.is_merged()).count();

        let prs_closed_without_merging =
            self.prs.iter().filter(|p| p.is_closed_not_merged()).count();

        let pr_merge_time = self.resp_times(|| self.prs.iter(), |p| p.merged);

        let mut contributor_commits = HashMap::new();
        self.prs
            .iter()
            .filter(|p| p.merged.is_some())
            .for_each(|p| {
                let counter = contributor_commits
                    .entry(p.thread.author.clone())
                    .or_insert(0);
                *counter += p.commits;
            });

        let code_contributors = self
            .prs
            .iter()
            .filter(|p| p.merged.is_some())
            .map(|p| p.thread.author.clone()) // Naughty clone.
            .collect::<Counter<_>>()
            .into_map();

        let commentors = hashmap_combine(
            self.issues
                .into_iter()
                .map(|i| i.0.comments)
                .fold(HashMap::new(), hashmap_combine),
            self.prs
                .into_iter()
                .map(|p| p.thread.comments)
                .fold(HashMap::new(), hashmap_combine),
        );

        Statistics {
            commentors,
            code_contributors,
            contributor_commits,
            all_issues,
            all_closed_issues,
            issues_with_responses,
            issues_with_official_responses,
            issue_first_resp_time,
            issue_official_first_resp_time,
            all_prs,
            prs_merged,
            prs_closed_without_merging,
            prs_with_responses,
            prs_with_official_responses,
            pr_first_resp_time,
            pr_official_first_resp_time,
            pr_merge_time,
        }
    }

    /// Gather the mean/median response times in a generic way.
    fn resp_times<'a, F, G, T, A>(&self, f: F, g: G) -> Option<ResponseTimes>
    where
        F: FnOnce() -> T,
        G: Fn(&A) -> Option<DateTime<Utc>>,
        T: Iterator<Item = &'a A>,
        A: 'a + Threaded,
    {
        let resp_times: Vec<chrono::Duration> = f()
            .filter_map(|t| g(t).map(|r| r - t.the_thread().posted))
            .sorted()
            .collect();

        if resp_times.is_empty() {
            None
        } else {
            // `to_std` will error if the `Duration` is less than 0. That shouldn't
            // happen, since comments should always occur after the initial posting
            // time of the thread.
            let median = resp_times
                .get(resp_times.len() / 2)
                .and_then(|t| t.to_std().ok())?;
            let mean: i64 = resp_times.iter().map(|d| d.num_seconds()).sum();
            let mean = Duration::from_secs(mean as u64 / resp_times.len() as u64);

            Some(ResponseTimes { median, mean })
        }
    }
}

/// Statistics involving [`Thread`](struct.Thread.html) response times.
#[derive(Debug, Deserialize, Serialize)]
pub struct ResponseTimes {
    pub median: Duration,
    pub mean: Duration,
}

impl ResponseTimes {
    /// A human-friendly report of the median reponse time.
    pub fn median_time(&self) -> String {
        ResponseTimes::period(&self.median)
    }

    /// A human-friendly report of the average reponse time.
    pub fn average_time(&self) -> String {
        ResponseTimes::period(&self.mean)
    }

    fn period(duration: &Duration) -> String {
        let hours = duration.as_secs() / 3600;
        let (num, period) = if hours > 48 {
            (hours / 24, "days")
        } else if hours > 1 {
            (hours, "hours")
        } else if hours == 1 {
            (1, "hour")
        } else {
            (duration.as_secs() / 60, "minutes")
        };
        format!("{} {}", num, period)
    }
}

/// Various compiled statistics regarding contributions to a Github repository.
///
/// For the relevant fields below, an "official" response is any made by a
/// repository Owner, an organization Member, or an invited Collaborator.
#[derive(Debug, Deserialize, Serialize)]
pub struct Statistics {
    /// All issue/PR commentors.
    pub commentors: HashMap<String, usize>,
    /// All users who had PRs merged.
    pub code_contributors: HashMap<String, usize>,
    /// The commits-in-merged-PRs count for each user.
    #[serde(default)]
    pub contributor_commits: HashMap<String, usize>,
    /// The count of all issues, opened or closed.
    pub all_issues: usize,
    /// How many of the issues have been closed?
    pub all_closed_issues: usize,
    /// All issues that have been responded to in some way.
    pub issues_with_responses: usize,
    /// All issues that have been responded to "officially".
    pub issues_with_official_responses: usize,
    /// How long does it take for someone to respond to an issue?
    pub issue_first_resp_time: Option<ResponseTimes>,
    /// How long does it take for an "official" response?
    pub issue_official_first_resp_time: Option<ResponseTimes>,
    /// The count of all PRs, opened or closed.
    pub all_prs: usize,
    /// All PRs that have been responded to in some way.
    pub prs_with_responses: usize,
    /// All PRs that have been responded to officially.
    pub prs_with_official_responses: usize,
    /// How long does it take for someone to respond to a PR?
    pub pr_first_resp_time: Option<ResponseTimes>,
    /// How long does it take for an "official" response to a PR?
    pub pr_official_first_resp_time: Option<ResponseTimes>,
    /// How many PRs were merged?
    pub prs_merged: usize,
    /// The count of all PRs which were closed with being merged.
    pub prs_closed_without_merging: usize,
    /// How long does it take for PRs to be merged?
    pub pr_merge_time: Option<ResponseTimes>,
}

impl Statistics {
    pub fn report(self, repo: &str, commits: bool) -> String {
        let issues = if self.all_issues == 0 {
            "No issues found.".to_string()
        } else {
            let (any_median, any_mean) = self
                .issue_first_resp_time
                .map(|rt| (rt.median_time(), rt.average_time()))
                .unwrap_or_else(|| ("None".to_string(), "None".to_string()));

            let (official_median, official_mean) = self
                .issue_official_first_resp_time
                .map(|rt| (rt.median_time(), rt.average_time()))
                .unwrap_or_else(|| ("None".to_string(), "None".to_string()));

            format!(
                r#"
{} issues found, {} of which are now closed ({:.1}%).

- {} ({:.1}%) of these received a response.
- {} ({:.1}%) have an official response from a repo Owner or organization Member.

Response Times (any):
- Median: {}
- Average: {}

Response Times (official):
- Median: {}
- Average: {}"#,
                self.all_issues,
                self.all_closed_issues,
                percent(self.all_closed_issues, self.all_issues),
                self.issues_with_responses,
                percent(self.issues_with_responses, self.all_issues),
                self.issues_with_official_responses,
                percent(self.issues_with_official_responses, self.all_issues),
                any_median,
                any_mean,
                official_median,
                official_mean,
            )
        };

        let prs = if self.all_prs == 0 {
            "No Pull Requests found.".to_string()
        } else {
            let (any_median, any_mean) = self
                .pr_first_resp_time
                .map(|rt| (rt.median_time(), rt.average_time()))
                .unwrap_or_else(|| ("None".to_string(), "None".to_string()));

            let (official_median, official_mean) = self
                .pr_official_first_resp_time
                .map(|rt| (rt.median_time(), rt.average_time()))
                .unwrap_or_else(|| ("None".to_string(), "None".to_string()));

            let (merge_median, merge_mean) = self
                .pr_merge_time
                .map(|rt| (rt.median_time(), rt.average_time()))
                .unwrap_or_else(|| ("None".to_string(), "None".to_string()));

            format!(
                r#"
{} Pull Requests found, {} of which are now merged ({:.1}%).
{} have been closed without merging ({:.1}%).

- {} ({:.1}%) of these received a response.
- {} ({:.1}%) have an official response from a repo Owner or organization Member.

Response Times (any):
- Median: {}
- Average: {}

Response Times (official):
- Median: {}
- Average: {}

Time-to-Merge:
- Median: {}
- Average: {}"#,
                self.all_prs,
                self.prs_merged,
                percent(self.prs_merged, self.all_prs),
                self.prs_closed_without_merging,
                percent(self.prs_closed_without_merging, self.all_prs),
                self.prs_with_responses,
                percent(self.prs_with_responses, self.all_prs),
                self.prs_with_official_responses,
                percent(self.prs_with_official_responses, self.all_prs),
                any_median,
                any_mean,
                official_median,
                official_mean,
                merge_median,
                merge_mean,
            )
        };

        let contributors = format!(
            r#"
Top 10 Commentors (Issues and PRs):
{}

Top 10 Code Contributors (by merged PRs):
{}
"#,
            self.commentors
                .into_iter()
                .sorted_by(|a, b| b.1.cmp(&a.1))
                .take(10)
                .enumerate()
                .map(|(i, (name, issues))| format!("{:2}. {}: {}", i + 1, name, issues))
                .join("\n"),
            self.code_contributors
                .into_iter()
                .sorted_by(|a, b| b.1.cmp(&a.1))
                .take(10)
                .enumerate()
                .map(|(i, (name, prs))| format!("{:2}. {}: {}", i + 1, name, prs))
                .join("\n"),
        );

        let contributor_commits = if commits {
            format!(
                r#"
Top 10 Code Contributors (by commits-in-merged-PRs):
{}
"#,
                self.contributor_commits
                    .into_iter()
                    .sorted_by(|a, b| b.1.cmp(&a.1))
                    .take(10)
                    .enumerate()
                    .map(|(i, (name, commits))| format!("{:2}. {}: {}", i + 1, name, commits))
                    .join("\n"),
            )
        } else {
            "".to_string()
        };

        format!(
            r#"# Project Report for {}

## Issues
{}

## Pull Requests
{}

## Contributors
{}{}"#,
            repo, issues, prs, contributors, contributor_commits
        )
    }
}

/// Generate a client with preset headers for communicating with the Github API.
pub fn client(token: &str) -> anyhow::Result<Client> {
    let mut headers = header::HeaderMap::new();
    headers.insert(
        header::AUTHORIZATION,
        header::HeaderValue::from_str(&format!("bearer {}", token))?,
    );
    headers.insert(
        header::USER_AGENT,
        header::HeaderValue::from_static("credit"),
    );

    let client = Client::builder()
        .default_headers(headers)
        .build()
        .context("Failed to create initial HTTP client.")?;

    Ok(client)
}

/// Given a repository name, look up the [`Thread`](struct.Thread.html)
/// statistics of all its Issues.
pub fn repo_threads(
    client: &Client,
    ipb: &ProgressBar,
    ppb: &ProgressBar,
    serial: bool,
    commits: bool,
    start: &Option<DateTime<Utc>>,
    end: &Option<DateTime<Utc>>,
    owner: &str,
    repo: &str,
) -> anyhow::Result<Postings> {
    let i_msg = format!("Fetching Issues for {}/{}...", owner, repo);
    let p_msg = format!("Fetching Pull Requests for {}/{}...", owner, repo);

    let get_issues = || all_issues(client, start, end, owner, repo);
    let get_prs = || all_prs(client, start, end, commits, owner, repo);

    // Too much parallelism can trigger Github's abuse detection, so we offer
    // the "serial" option here.
    let (issues, prs) = if serial {
        let issues = with_progress(ipb, &i_msg, get_issues);
        let prs = with_progress(ppb, &p_msg, get_prs);
        (issues, prs)
    } else {
        rayon::join(
            || with_progress(ipb, &i_msg, get_issues),
            || with_progress(ppb, &p_msg, get_prs),
        )
    };

    Ok(Postings {
        issues: issues?,
        prs: prs?,
    })
}

/// Perform some action with an associated `ProgressBar`.
fn with_progress<F, A>(progress: &ProgressBar, msg: &str, f: F) -> A
where
    F: FnOnce() -> A,
{
    progress.enable_steady_tick(120);
    progress.set_message(&msg);
    let result = f();
    progress.finish_and_clear();
    result
}

fn all_issues(
    client: &Client,
    start: &Option<DateTime<Utc>>,
    end: &Option<DateTime<Utc>>,
    owner: &str,
    repo: &str,
) -> anyhow::Result<Vec<Issue>> {
    repo::issues(client, end, &repo::Mode::Issues, owner, repo).map(|is| {
        is.into_iter()
            .filter(|i| {
                let after = start.map(|s| i.created_at >= s).unwrap_or(true);
                let before = end.map(|e| i.created_at <= e).unwrap_or(true);
                after && before
            })
            .map(|i| Issue(issue_thread(i)))
            .collect()
    })
}

fn all_prs(
    client: &Client,
    start: &Option<DateTime<Utc>>,
    end: &Option<DateTime<Utc>>,
    commits: bool,
    owner: &str,
    repo: &str,
) -> anyhow::Result<Vec<PR>> {
    let mode = if commits {
        repo::Mode::PRsWithCommits
    } else {
        repo::Mode::PRs
    };
    repo::issues(client, end, &mode, owner, repo).map(|is| {
        is.into_iter()
            .filter(|i| {
                let after = start.map(|s| i.created_at >= s).unwrap_or(true);
                let before = end.map(|e| i.created_at <= e).unwrap_or(true);
                after && before
            })
            .map(|i| {
                let merged = i.merged_at;
                let commits = i.commits.as_ref().map(|cc| cc.total_count).unwrap_or(0);
                let thread = issue_thread(i);
                PR {
                    thread,
                    merged,
                    commits,
                }
            })
            .collect()
    })
}

fn ghost(author: &Option<repo::Author>) -> String {
    author
        .as_ref()
        .map(|a| a.login.clone())
        .unwrap_or_else(|| "@ghost".to_string())
}

fn issue_thread(issue: repo::Issue) -> Thread {
    let comments: Vec<repo::Comment> = issue.comments.edges.into_iter().map(|n| n.node).collect();

    // Need to be careful, since the first physical response might have been
    // from the Issue author.
    let first_comment = comments.iter().find(|c| !c.author_association.is_author());
    let first_responder = first_comment.map(|c| ghost(&c.author));
    let first_response = first_comment.map(|c| c.created_at);
    let first_official_response = comments
        .iter()
        .find(|c| c.author_association.is_official())
        .map(|c| c.created_at);
    let comment_counts = comments
        .iter()
        .map(|c| ghost(&c.author))
        .collect::<Counter<_>>()
        .into_map();

    Thread {
        author: ghost(&issue.author),
        posted: issue.created_at,
        closed: issue.closed_at,
        first_responder,
        first_response,
        first_official_response,
        comments: comment_counts,
    }
}

/// A curated list of the Top 100 users in a given location, ranked via their
/// contribution counts and weighted by followers.
pub fn user_contributions(client: &Client, location: &str) -> anyhow::Result<UserContribs> {
    let total_users = contribs::user_count(client, location)?.user_count;
    let contributions = contribs::user_contributions(client, location)?
        .into_iter()
        .sorted_by(|a, b| b.contribs().cmp(&a.contribs()))
        .take(500)
        .sorted_by(|a, b| b.followers.total_count.cmp(&a.followers.total_count))
        .take(250)
        .sorted_by(|a, b| b.contribs().cmp(&a.contribs()))
        .take(100)
        .map(|uc| User::from(uc))
        .collect();

    Ok(UserContribs {
        location: location.to_string(),
        total_users,
        contributions,
    })
}

fn hashmap_combine<K, V>(mut a: HashMap<K, V>, b: HashMap<K, V>) -> HashMap<K, V>
where
    K: Eq + std::hash::Hash,
    V: std::ops::AddAssign + Copy,
{
    for (k, v) in b {
        a.entry(k).and_modify(|e| *e += v).or_insert(v);
    }

    a
}

fn percent(a: usize, b: usize) -> f64 {
    100.0 * (a as f64) / (b as f64)
}

#[test]
fn hashmap_extend() {
    let mut first = HashMap::new();
    let mut second = HashMap::new();
    let message = "ABCDEF";

    for c in message.chars() {
        first.insert(c, 1);
        second.insert(c, 1);
    }

    let third = hashmap_combine(first, second);
    let elems: Vec<usize> = third.values().map(|v| *v).collect();

    assert_eq!(vec![2, 2, 2, 2, 2, 2], elems);
}