From daea360a4cbe794c1d320f5adf81d0fdd48a819b Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 18 Dec 2021 20:08:46 -0500 Subject: [PATCH] implemented creating an issue --- src/arg.rs | 2 +- src/issue.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 12 deletions(-) diff --git a/src/arg.rs b/src/arg.rs index bf35e71..83cd6cb 100644 --- a/src/arg.rs +++ b/src/arg.rs @@ -44,7 +44,7 @@ pub fn get_args() -> ArgMatches<'static> { .arg(Arg::with_name("create") .short("c") .long("c") - .value_names(&["OWNER"]) + .value_names(&["OWNER", "REPO"]) .help("Create an issue for a repository") ) ) diff --git a/src/issue.rs b/src/issue.rs index 333a49e..c2591ba 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -1,27 +1,121 @@ use std::collections::HashMap; -use std::io; -use clap::ArgMatches; use colored::*; -use reqwest::{StatusCode, Client}; +use reqwest::StatusCode; use serde_derive::{Serialize, Deserialize}; +use serde_json::Value; -use crate::config::Configuration; +use crate::request::Request; +use crate::util; pub struct Issue; +#[derive(Serialize, Deserialize)] +pub struct MultipleIssues { + pub data: Vec, + pub ok: bool, +} + +#[derive(Serialize, Deserialize)] +pub struct IssueResponse { + pub id: i64, + pub url: String, + pub html_url: String, + pub number: i64, + pub user: User, + pub original_author: String, + pub original_author_id: i64, + pub title: String, + pub body: String, + pub ref_field: String, + pub labels: Vec, + pub milestone: Value, + pub assignee: Value, + pub assignees: Value, + pub state: String, + pub is_locked: bool, + pub comments: i64, + pub created_at: String, + pub updated_at: String, + pub closed_at: Value, + pub due_date: Value, + pub pull_request: Value, + pub repository: Repository, +} + +#[derive(Serialize, Deserialize)] +pub struct User { + pub id: i64, + pub login: String, + pub full_name: String, + pub email: String, + pub avatar_url: String, + pub language: String, + pub is_admin: bool, + pub last_login: String, + pub created: String, + pub restricted: bool, + pub username: String, +} + +#[derive(Serialize, Deserialize)] +pub struct Repository { + pub id: i64, + pub name: String, + pub owner: String, + pub full_name: String, +} + + impl Issue { pub fn new() -> Issue { Issue {} } - pub fn create_issue(&self, config: &Configuration, arg: &ArgMatches) { - let client = Client::new(); - } + pub fn create_issue(&self, request: &Request) { + let issue_title: String; + let issue_description: String; + let _issue_assignee: Option; + let _issue_milestone: Option; + let _issue_tags: Option; + + let client = &request.client; + let arg_value: Vec<&str> = request + .arg_value + .subcommand() + .1 + .unwrap() + .values_of("create") + .unwrap() + .collect(); + let mut map: HashMap<&str, String> = HashMap::new(); + let url = format!( + "{request}/repos/{owner}/{repo}/issues?token={api_token}", + request = request.url.as_ref().unwrap(), + owner = arg_value[0], + repo = arg_value[1], + api_token = request.authentication.credentials.1.as_ref().unwrap() + ); - pub fn get_input(&self) -> String { - let mut input = String::new(); - io::stdin().read_line(&mut input).unwrap(); - input.trim().to_string() + //let issue_title = read!("Enter a title for the issue: {}\n"); + //let issue_description = read!("Enter a description for the issue: {}\n"); + issue_title = util::get_input("Enter a title for the issue: ".to_string()); + issue_description = util::get_input("Enter a description for the issue: ".to_string()); + map.insert("title", issue_title); + map.insert("body", issue_description); + + let response = client.post(&url).json(&map).send(); + + match response { + Ok(repo) => { + match repo.status() { + StatusCode::CREATED => println!("{}", "Issue successfully created!".green()), + StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()), + StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()), + _ => println!(), + } + }, + Err(e) => panic!("{}", e), + } } }