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,17 +1,20 @@
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(
description: String,
pr: Rc<PullRequest>,
c: &Credentials,
) -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let body = UpdateDescriptionRequest { body: &description }; let body = UpdateDescriptionRequest { body: &description };
let request = api::base_patch_request(&client, &c, pr.url()).json(&body); let request = api::base_patch_request(&client, &c, pr.url()).json(&body);

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,11 +1,11 @@
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-->";
@ -14,7 +14,11 @@ 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!(
"(?s){}.*{}",
regex::escape(SHIELD_OPEN),
regex::escape(SHIELD_CLOSE)
);
let re = Regex::new(&matcher).unwrap(); let re = Regex::new(&matcher).unwrap();
re.replace_all(body, &new[..]).into_owned() re.replace_all(body, &new[..]).into_owned()
} else { } else {
@ -24,7 +28,11 @@ fn safe_replace(body: &str, table: &str) -> String {
} }
} }
pub async fn persist(prs: &Vec<Rc<PullRequest>>, table: &str, c: &Credentials) -> Result<(), Box<dyn Error>> { pub async fn persist(
prs: &Vec<Rc<PullRequest>>,
table: &str,
c: &Credentials,
) -> Result<(), Box<dyn Error>> {
let futures = prs.iter().map(|pr| { let futures = prs.iter().map(|pr| {
let description = safe_replace(pr.body(), table); let description = safe_replace(pr.body(), table);
pull_request::update_description(description, pr.clone(), c) pull_request::update_description(description, pr.clone(), c)
@ -37,5 +45,4 @@ pub async fn persist(prs: &Vec<Rc<PullRequest>>, table: &str, c: &Credentials) -
} }
Ok(()) Ok(())
} }