From b5462a9751d9e8cc12e45d5f0a7ac05399726e06 Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Thu, 11 Jun 2020 17:39:57 +0530 Subject: [PATCH] simplify the markdown table generator --- src/api/search.rs | 2 +- src/main.rs | 2 +- src/markdown.rs | 61 +++++++++++++++++++++++++---------------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/api/search.rs b/src/api/search.rs index 5c50d3b..45b442b 100644 --- a/src/api/search.rs +++ b/src/api/search.rs @@ -72,7 +72,7 @@ impl PullRequest { pub fn note(&self) -> &str { match &self.state { PullRequestStatus::Open => "N/A", - PullRequestStatus::Closed => "Merged" + PullRequestStatus::Closed => "Merged", } } } diff --git a/src/main.rs b/src/main.rs index a8837cc..488651f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box> { match command { "github" => { - let table = markdown::build_table(tree, pattern); + let table = markdown::build_table(graph::log(&tree), pattern); let output = match prelude { Some(prelude) => build_final_output(prelude, &table), diff --git a/src/markdown.rs b/src/markdown.rs index 2662853..8c5a0d8 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -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 { // TODO: Make this configurable @@ -12,33 +13,37 @@ fn process(row: String) -> String { regex.replace_all(&row, "").into_owned() } -pub fn build_table(graph: Graph, 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(); - 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("|:--:|:------|:-------------:|\n"); - // TODO: Use graph::log to simplify this - let roots: Vec<_> = graph.externals(Direction::Incoming).collect(); + for (node, parent) in deps { + 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 { - 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 = 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.push_str(&process(row)); } out