simplify the markdown table generator

This commit is contained in:
Timothy Andrew 2020-06-11 17:39:57 +05:30
parent dbb856a7d9
commit b5462a9751
No known key found for this signature in database
GPG Key ID: ABD64509E977B249
3 changed files with 35 additions and 30 deletions

View File

@ -72,7 +72,7 @@ impl PullRequest {
pub fn note(&self) -> &str { pub fn note(&self) -> &str {
match &self.state { match &self.state {
PullRequestStatus::Open => "N/A", PullRequestStatus::Open => "N/A",
PullRequestStatus::Closed => "Merged" PullRequestStatus::Closed => "Merged",
} }
} }
} }

View File

@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
match command { match command {
"github" => { "github" => {
let table = markdown::build_table(tree, pattern); let table = markdown::build_table(graph::log(&tree), pattern);
let output = match prelude { let output = match prelude {
Some(prelude) => build_final_output(prelude, &table), Some(prelude) => build_final_output(prelude, &table),

View File

@ -1,10 +1,11 @@
use petgraph::visit::Bfs;
use petgraph::visit::EdgeRef;
use petgraph::{Direction, Graph};
use regex::Regex;
use std::rc::Rc;
use crate::api::search::PullRequest;
use regex::Regex;
use crate::api::search::{PullRequestStatus};
use crate::graph::FlatDep;
fn process(row: String) -> String { fn process(row: String) -> String {
// TODO: Make this configurable // TODO: Make this configurable
@ -12,33 +13,37 @@ fn process(row: String) -> String {
regex.replace_all(&row, "").into_owned() regex.replace_all(&row, "").into_owned()
} }
pub fn build_table(graph: Graph<Rc<PullRequest>, usize>, title: &str) -> String { pub fn build_table(deps: FlatDep, title: &str) -> String {
let is_complete = deps
.iter()
.all(|(node, _)| node.state() == &PullRequestStatus::Closed);
let mut out = String::new(); let mut out = String::new();
out.push_str(&format!("### Stacked PR Chain: {}\n", title)); if is_complete {
out.push_str(&format!("### ✅ Stacked PR Chain: {}\n", title));
} else {
out.push_str(&format!("### Stacked PR Chain: {}\n", title));
}
out.push_str("| PR | Title | Merges Into |\n"); out.push_str("| PR | Title | Merges Into |\n");
out.push_str("|:--:|:------|:-------------:|\n"); out.push_str("|:--:|:------|:-------------:|\n");
// TODO: Use graph::log to simplify this for (node, parent) in deps {
let roots: Vec<_> = graph.externals(Direction::Incoming).collect(); let row = match parent {
Some(parent) => format!(
"|#{}|{}|#{}|\n",
node.number(),
node.title(),
parent.number()
),
None => format!(
"|#{}|{}|**{}**|\n",
node.number(),
node.title(),
node.note()
),
};
for root in roots { out.push_str(&process(row));
let mut bfs = Bfs::new(&graph, root);
while let Some(node) = bfs.next(&graph) {
let parent = graph.edges_directed(node, Direction::Incoming).next();
let node: Rc<PullRequest> = graph[node].clone();
let row = match parent {
Some(parent) => format!(
"|#{}|{}|#{}|\n",
node.number(),
node.title(),
graph[parent.source()].number()
),
None => format!("|#{}|{}|**{}**|\n", node.number(), node.title(), node.note()),
};
out.push_str(&process(row));
}
} }
out out