cargo fix + fmt

This commit is contained in:
Timothy Andrew 2020-06-03 13:45:28 +05:30
parent 26b1f1eca8
commit c9977c45a3
No known key found for this signature in database
GPG Key ID: ABD64509E977B249
6 changed files with 52 additions and 42 deletions

View File

@ -2,8 +2,8 @@ use crate::Credentials;
use reqwest::{Client, RequestBuilder}; use reqwest::{Client, RequestBuilder};
use std::time::Duration; use std::time::Duration;
pub mod search;
pub mod pull_request; pub mod pull_request;
pub mod search;
fn base_request(client: &Client, credentials: &Credentials, url: &str) -> RequestBuilder { fn base_request(client: &Client, credentials: &Credentials, url: &str) -> RequestBuilder {
client client

View File

@ -1,20 +1,23 @@
use serde::Serialize;
use std::error::Error; use std::error::Error;
use serde::{Serialize};
use std::rc::Rc; use std::rc::Rc;
use crate::{Credentials, api};
use crate::api::search::PullRequest; use crate::api::search::PullRequest;
use crate::{api, Credentials};
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
struct UpdateDescriptionRequest<'a> { struct UpdateDescriptionRequest<'a> {
body: &'a str body: &'a str,
} }
pub async fn update_description(description: String, pr: Rc<PullRequest>, c: &Credentials) -> Result<(), Box<dyn Error>> { pub async fn update_description(
let client = reqwest::Client::new(); description: String,
let body = UpdateDescriptionRequest { body: &description }; pr: Rc<PullRequest>,
let request = api::base_patch_request(&client, &c, pr.url()).json(&body); c: &Credentials,
request.send().await?; ) -> Result<(), Box<dyn Error>> {
Ok(()) 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(())
} }

View File

@ -25,7 +25,7 @@ pub struct PullRequest {
base: PullRequestRef, base: PullRequestRef,
title: String, title: String,
url: String, url: String,
body: String body: String,
} }
impl PullRequest { impl PullRequest {

View File

@ -4,8 +4,8 @@ use std::error::Error;
use std::process; use std::process;
use std::rc::Rc; use std::rc::Rc;
use gh_stack::{api, persist, graph, markdown};
use gh_stack::Credentials; use gh_stack::Credentials;
use gh_stack::{api, graph, markdown, persist};
use std::io::{self, Write}; use std::io::{self, Write};
@ -48,7 +48,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let response = read_cli_input("Going to update these PRs ☝️ (y/n): "); let response = read_cli_input("Going to update these PRs ☝️ (y/n): ");
match &response[..] { match &response[..] {
"y" => persist::persist(&prs, &table, &credentials).await?, "y" => persist::persist(&prs, &table, &credentials).await?,
_ => std::process::exit(1) _ => std::process::exit(1),
} }
persist::persist(&prs, &table, &credentials).await?; persist::persist(&prs, &table, &credentials).await?;

View File

@ -1,41 +1,48 @@
use futures::future::join_all;
use regex::Regex; use regex::Regex;
use std::error::Error; use std::error::Error;
use futures::future::join_all;
use std::rc::Rc; use std::rc::Rc;
use crate::api::pull_request;
use crate::api::search::PullRequest; use crate::api::search::PullRequest;
use crate::Credentials; use crate::Credentials;
use crate::api::pull_request;
const SHIELD_OPEN: &str = "<!---GHSTACKOPEN-->"; const SHIELD_OPEN: &str = "<!---GHSTACKOPEN-->";
const SHIELD_CLOSE: &str = "<!---GHSTACKCLOSE-->"; const SHIELD_CLOSE: &str = "<!---GHSTACKCLOSE-->";
fn safe_replace(body: &str, table: &str) -> String { 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) { if body.contains(SHIELD_OPEN) {
let matcher = format!("(?s){}.*{}", regex::escape(SHIELD_OPEN), regex::escape(SHIELD_CLOSE)); let matcher = format!(
let re = Regex::new(&matcher).unwrap(); "(?s){}.*{}",
re.replace_all(body, &new[..]).into_owned() regex::escape(SHIELD_OPEN),
} else { regex::escape(SHIELD_CLOSE)
let mut body: String = body.to_owned(); );
body.push_str(&new); let re = Regex::new(&matcher).unwrap();
body 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<Rc<PullRequest>>, table: &str, c: &Credentials) -> Result<(), Box<dyn Error>> { pub async fn persist(
let futures = prs.iter().map(|pr| { prs: &Vec<Rc<PullRequest>>,
let description = safe_replace(pr.body(), table); table: &str,
pull_request::update_description(description, pr.clone(), c) c: &Credentials,
}); ) -> Result<(), Box<dyn Error>> {
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::<Vec<_>>()).await; let results = join_all(futures.collect::<Vec<_>>()).await;
for result in results { for result in results {
result.unwrap(); result.unwrap();
} }
Ok(())
Ok(())
} }