diff --git a/src/api/mod.rs b/src/api/mod.rs index 94d9e93..1c294a9 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -2,8 +2,8 @@ use crate::Credentials; use reqwest::{Client, RequestBuilder}; use std::time::Duration; -pub mod search; pub mod pull_request; +pub mod search; fn base_request(client: &Client, credentials: &Credentials, url: &str) -> RequestBuilder { client @@ -19,4 +19,4 @@ fn base_patch_request(client: &Client, credentials: &Credentials, url: &str) -> .timeout(Duration::from_secs(5)) .header("Authorization", format!("token {}", credentials.token)) .header("User-Agent", "timothyandrew/gh-stack") -} \ No newline at end of file +} diff --git a/src/api/pull_request.rs b/src/api/pull_request.rs index 1030417..1732304 100644 --- a/src/api/pull_request.rs +++ b/src/api/pull_request.rs @@ -1,20 +1,23 @@ +use serde::Serialize; use std::error::Error; -use serde::{Serialize}; use std::rc::Rc; -use crate::{Credentials, api}; use crate::api::search::PullRequest; - +use crate::{api, Credentials}; #[derive(Serialize, Debug)] struct UpdateDescriptionRequest<'a> { - body: &'a str + body: &'a str, } -pub async fn update_description(description: String, pr: Rc, c: &Credentials) -> Result<(), Box> { - let client = reqwest::Client::new(); - let body = UpdateDescriptionRequest { body: &description }; - let request = api::base_patch_request(&client, &c, pr.url()).json(&body); - request.send().await?; - Ok(()) -} \ No newline at end of file +pub async fn update_description( + description: String, + pr: Rc, + c: &Credentials, +) -> Result<(), Box> { + let client = reqwest::Client::new(); + let body = UpdateDescriptionRequest { body: &description }; + let request = api::base_patch_request(&client, &c, pr.url()).json(&body); + request.send().await?; + Ok(()) +} diff --git a/src/api/search.rs b/src/api/search.rs index 91cc027..bf7ae8f 100644 --- a/src/api/search.rs +++ b/src/api/search.rs @@ -25,7 +25,7 @@ pub struct PullRequest { base: PullRequestRef, title: String, url: String, - body: String + body: String, } impl PullRequest { diff --git a/src/graph.rs b/src/graph.rs index 6bac1d6..99c8190 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -18,4 +18,4 @@ pub fn build(prs: &Vec>) -> Graph, usize> { } tree -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index a5de6a5..b93de2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ use std::error::Error; use std::process; use std::rc::Rc; -use gh_stack::{api, persist, graph, markdown}; use gh_stack::Credentials; +use gh_stack::{api, graph, markdown, persist}; use std::io::{self, Write}; @@ -48,13 +48,13 @@ async fn main() -> Result<(), Box> { let response = read_cli_input("Going to update these PRs ☝️ (y/n): "); match &response[..] { "y" => persist::persist(&prs, &table, &credentials).await?, - _ => std::process::exit(1) + _ => std::process::exit(1), } persist::persist(&prs, &table, &credentials).await?; println!("Done!"); - + Ok(()) /* # TODO diff --git a/src/persist.rs b/src/persist.rs index 50a5432..e3662a2 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -1,41 +1,48 @@ +use futures::future::join_all; use regex::Regex; use std::error::Error; -use futures::future::join_all; use std::rc::Rc; +use crate::api::pull_request; use crate::api::search::PullRequest; use crate::Credentials; -use crate::api::pull_request; const SHIELD_OPEN: &str = ""; const SHIELD_CLOSE: &str = ""; fn safe_replace(body: &str, table: &str) -> String { - let new = format!("\n{}\n{}\n{}\n", SHIELD_OPEN, table, SHIELD_CLOSE); + let new = format!("\n{}\n{}\n{}\n", SHIELD_OPEN, table, SHIELD_CLOSE); - if body.contains(SHIELD_OPEN) { - let matcher = format!("(?s){}.*{}", regex::escape(SHIELD_OPEN), regex::escape(SHIELD_CLOSE)); - let re = Regex::new(&matcher).unwrap(); - re.replace_all(body, &new[..]).into_owned() - } else { - let mut body: String = body.to_owned(); - body.push_str(&new); - body - } + if body.contains(SHIELD_OPEN) { + let matcher = format!( + "(?s){}.*{}", + regex::escape(SHIELD_OPEN), + regex::escape(SHIELD_CLOSE) + ); + let re = Regex::new(&matcher).unwrap(); + re.replace_all(body, &new[..]).into_owned() + } else { + let mut body: String = body.to_owned(); + body.push_str(&new); + body + } } -pub async fn persist(prs: &Vec>, table: &str, c: &Credentials) -> Result<(), Box> { - let futures = prs.iter().map(|pr| { - let description = safe_replace(pr.body(), table); - pull_request::update_description(description, pr.clone(), c) - }); +pub async fn persist( + prs: &Vec>, + table: &str, + c: &Credentials, +) -> Result<(), Box> { + let futures = prs.iter().map(|pr| { + let description = safe_replace(pr.body(), table); + pull_request::update_description(description, pr.clone(), c) + }); - let results = join_all(futures.collect::>()).await; + let results = join_all(futures.collect::>()).await; - for result in results { - result.unwrap(); - } + for result in results { + result.unwrap(); + } - Ok(()) - -} \ No newline at end of file + Ok(()) +}