Skip to content

Commit

Permalink
adds build pr message and template
Browse files Browse the repository at this point in the history
  • Loading branch information
jafow committed Dec 29, 2019
1 parent 01f0796 commit 1c27dc7
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::env;
use std::fs;

use std::fs::{File, OpenOptions};
use std::fs::{File};
use std::io::{self, prelude::*};
use std::path::Path;
use std::process::Command;
Expand All @@ -12,6 +12,7 @@ use async_std::task;
use regex::Regex;

// use serde_json::Result;
use surf;

extern crate clap;
use clap::{App, Arg};
Expand All @@ -36,6 +37,10 @@ fn current_branch(head_file_contents: String) -> Option<String> {

#[cfg(test)]
mod tests {
use super::*;
// override the path to PR_EDITMSG template for testing
const PR_EDITMSG_PATH: &str = "./tests/PR_EDITMSG";

#[test]
fn test_current_branch() {
let hf = head_file(&Path::new("./tests/HEAD_A")).expect("test file");
Expand Down Expand Up @@ -71,24 +76,52 @@ fn get_remote(text: &str) -> Result<Vec<&str>, ()> {
Ok(v)
}

fn launch_editor() -> std::io::Result<()> {
fn launch_editor(pr_file: &str) -> std::io::Result<()> {
let editor = env::var("GIT_EDITOR").expect("no $GIT_EDITOR set");
let sub = format!("{} ", editor);
let sub = format!("{} {}", editor, pr_file);
let cmd = Command::new("sh")
.args(&["-c", &sub])
.spawn()
.and_then(|mut c| c.wait())
.expect("error opening editor");
Ok(())
}

// let ecode = cmd.wait().expect("open editor failed");
fn build_pr_msg(msg_path: Option<&str>) -> Option<PullRequestMsg> {
let p = match msg_path {
Some(p) => p,
None => PR_EDITMSG_PATH
};
let pr_file: String = fs::read_to_string(p).expect("read test file");
let mut lines = pr_file.lines();
let mut title = String::new();
let mut body = String::new();

// set the first line as title
if let Some(_title) = lines.next() {
title = String::from(_title);
} else {
println!("Error getting title");
}

// dbg!(output);
dbg!(cmd);
Ok(())
while let Some(line) = lines.next() {
if line.starts_with("// Requesting a pull to") {
break;
} else {
body.push_str(line);
}
}

Some(
PullRequestMsg {
title: title,
body: body
}
)
}


fn build_message(target: &str, current: &str) -> std::io::Result<()> {
fn pr_msg_template(target: &str, current: &str) -> std::io::Result<()> {
let mut pr_file = File::create(PR_EDITMSG_PATH)?;

let msg = format!("
Expand All @@ -102,21 +135,23 @@ fn build_message(target: &str, current: &str) -> std::io::Result<()> {
Ok(())
}

fn build_request_payload(pr: PullRequest) -> (PullRequest, serde_json::Value) {
(pr, serde_json::json!({"title": "test title", "body": "this is a test msg body", "head": "test", "base": "master"}))
}

#[derive(Debug, PartialEq)]
struct PullRequestMsg {
title: String,
body: String
}

struct PullRequest {
target_branch: String,
head_branch: String,
#[derive(Debug, PartialEq)]
struct PullRequest<'a> {
target_branch: &'a str,
head_branch: &'a str,
message: PullRequestMsg
}

fn build_request(target: &str, current: &str, token: String) {

}

fn main() -> std::io::Result<()> {
let matches = App::new("git-pr")
.version("0.1.0")
Expand Down Expand Up @@ -150,29 +185,35 @@ fn main() -> std::io::Result<()> {
None => "master",
};

let token = env::var("GITHUB_TOKEN").expect("required GITHUB_TOKEN");

let git_head = head_file(&Path::new("./.git/HEAD")).expect("git HEAD");
let br = current_branch(git_head).unwrap();
let token = env::var("GITHUB_TOKEN").expect("required GITHUB_TOKEN");

build_message(target, &br)?;
launch_editor().expect("launch editor");
// build_request(target, &br, token);
pr_msg_template(&target, &br).expect("build PR message template");

launch_editor(PR_EDITMSG_PATH).expect("launch editor");

let msg: PullRequestMsg = build_pr_msg(None).expect("build pr message");

let pr = PullRequest {target_branch: &target, head_branch: &br, message: msg };

task::block_on(async {
let ff = fetch_api("jafow", &token, "git-pr").await;
let payload = build_request_payload(pr);
let ff = fetch_api("jafow", &token, "git-pr", &br).await;
match ff {
Ok(r) => r,
Err(e) => panic!("Error {}", e)
}
});


Ok(())
}

async fn fetch_api(uname: &str, password: &str, repo: &str) -> Result<surf::Response, Box<dyn std::error::Error + Send + Sync + 'static>> {
async fn fetch_api(uname: &str, password: &str, repo: &str, branch_head: &str) -> Result<surf::Response, Box<dyn std::error::Error + Send + Sync + 'static>> {
let url = format!("https://{}:{}@api.github.com/repos/{}/{}/pulls", &uname, &password, &uname, &repo);

let body = serde_json::json!({"title": "foo 3", "head": "feat", "base": "master", "body": "from serde_json"});
let body = serde_json::json!({"title": "foo 3", "head": format!("\"{}\"", branch_head), "base": "master", "body": "from serde_json"});
let res = surf::post(&url).body_json(&body)?.await?;
Ok(res)
}

0 comments on commit 1c27dc7

Please sign in to comment.