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 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")
}
}

View File

@ -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<PullRequest>, c: &Credentials) -> Result<(), Box<dyn Error>> {
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(())
}
pub async fn update_description(
description: String,
pr: Rc<PullRequest>,
c: &Credentials,
) -> Result<(), Box<dyn Error>> {
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,
title: String,
url: String,
body: String
body: String,
}
impl PullRequest {

View File

@ -18,4 +18,4 @@ pub fn build(prs: &Vec<Rc<PullRequest>>) -> Graph<Rc<PullRequest>, usize> {
}
tree
}
}

View File

@ -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<dyn Error>> {
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

View File

@ -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 = "<!---GHSTACKOPEN-->";
const SHIELD_CLOSE: &str = "<!---GHSTACKCLOSE-->";
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<Rc<PullRequest>>, table: &str, 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)
});
pub async fn persist(
prs: &Vec<Rc<PullRequest>>,
table: &str,
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 {
result.unwrap();
}
for result in results {
result.unwrap();
}
Ok(())
}
Ok(())
}