Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Allow multiple repos to be checked at the same time
Browse files Browse the repository at this point in the history
Their results are merged together. This is good for considering
the "credit" over a number of repos in an organization.
  • Loading branch information
fosskers committed Jun 19, 2020
1 parent c09d695 commit ab3477f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
19 changes: 15 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ pub struct Postings {
}

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();
Expand Down Expand Up @@ -278,7 +289,7 @@ impl Statistics {
// TODO Make this proper markdown.
pub fn report(self, repo: &str) -> String {
let issues = if self.all_issues == 0 {
"This repo has had no issues.".to_string()
"No issues found.".to_string()
} else {
let (any_median, any_mean) = self
.issue_first_resp_time
Expand All @@ -292,7 +303,7 @@ impl Statistics {

format!(
r#"
This repo has had {} issues, {} of which are now closed ({:.1}%).
{} 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.
Expand All @@ -319,7 +330,7 @@ Response Times (official):
};

let prs = if self.all_prs == 0 {
"This repo has had no Pull Requests.".to_string()
"No Pull Requests found.".to_string()
} else {
let (any_median, any_mean) = self
.pr_first_resp_time
Expand All @@ -338,7 +349,7 @@ Response Times (official):

format!(
r#"
This repo has had {} Pull Requests, {} of which are now merged ({:.1}%).
{} Pull Requests found, {} of which are now merged ({:.1}%).
{} have been closed without merging ({:.1}%).
- {} ({:.1}%) of these received a response.
Expand Down
48 changes: 38 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use anyhow::anyhow;
use gumdrop::{Options, ParsingStyle};
use itertools::Itertools;
use std::process;

//- ~credit~: Just pull as much as possible via the Github API.
Expand All @@ -28,17 +29,17 @@ struct Env {
/// Github personal access token
token: String,

/// A Github repository to check
/// A Github repository to check (can pass multiple times)
#[options(free, parse(try_from_str = "split_repo"))]
repo: (String, String),
repos: Vec<(String, String)>,

/// Output as JSON
json: bool,
}

fn main() {
let env = Env::parse_args_or_exit(ParsingStyle::AllOptions);
match work(&env) {
match work(env) {
Ok(result) => println!("{}", result),
Err(e) => {
eprintln!("{}", e);
Expand All @@ -47,16 +48,43 @@ fn main() {
}
}

fn work(env: &Env) -> anyhow::Result<String> {
fn work(env: Env) -> anyhow::Result<String> {
let client = credit::client(&env.token)?;
let postings = credit::repository_threads(&client, &env.repo.0, &env.repo.1)?;
let stats = postings.statistics();

if env.json {
let json = serde_json::to_string(&stats)?;
Ok(json)
if env.repos.is_empty() {
Err(anyhow!("No repositories given!"))
} else {
Ok(stats.report(&env.repo.1))
let (bads, goods): (Vec<_>, Vec<_>) = env
.repos
.iter()
.map(|(owner, repo)| credit::repository_threads(&client, &owner, &repo))
.partition_map(|r| From::from(r));

if !bads.is_empty() {
eprintln!("There were some errors:");
for e in bads {
eprintln!("{}", e);
}
}

if !goods.is_empty() {
let zero = credit::Postings {
issues: vec![],
prs: vec![],
};
let all = goods.into_iter().fold(zero, |acc, ps| acc.combine(ps));
let stats = all.statistics();

if env.json {
let json = serde_json::to_string(&stats)?;
Ok(json)
} else {
let name = env.repos.iter().map(|(_, name)| name).join(", ");
Ok(stats.report(&name))
}
} else {
Err(anyhow!("No results to show!"))
}
}
}

Expand Down

0 comments on commit ab3477f

Please sign in to comment.