write workpackage on openproject
This commit is contained in:
@@ -25,5 +25,6 @@ pub(crate) enum Planning {
|
||||
|
||||
#[derive(Args, Debug)]
|
||||
pub(crate) struct Issue2Work {
|
||||
pub issue_url: String
|
||||
pub issue_url: String,
|
||||
pub project_id: String,
|
||||
}
|
||||
|
@@ -3,9 +3,16 @@ use serde::Deserialize;
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub(crate) struct Config {
|
||||
pub gitlab: GitlabConfig,
|
||||
pub openproject: OpenProjectConfig,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub(crate) struct GitlabConfig {
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub(crate) struct OpenProjectConfig {
|
||||
pub token: String,
|
||||
pub base_url: String,
|
||||
}
|
4
src/error.rs
Normal file
4
src/error.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
pub struct GeneralError {
|
||||
pub(crate) description: String,
|
||||
}
|
14
src/gitlab/issue.rs
Normal file
14
src/gitlab/issue.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use gitlab::Issue;
|
||||
use gitlab::Project;
|
||||
|
||||
/// A struct which contains Issue and Project
|
||||
pub struct IssueBundle {
|
||||
pub issue: Issue,
|
||||
pub project: Project
|
||||
}
|
||||
|
||||
impl IssueBundle {
|
||||
pub fn new(issue: &Issue, project: &Project) -> Self {
|
||||
IssueBundle{issue: issue.clone(), project: project.clone()}
|
||||
}
|
||||
}
|
13
src/gitlab/mod.rs
Normal file
13
src/gitlab/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
pub mod issue;
|
||||
|
||||
use gitlab::GitlabError;
|
||||
use crate::error::GeneralError;
|
||||
|
||||
impl From<GitlabError> for GeneralError {
|
||||
fn from(value: GitlabError) -> Self {
|
||||
GeneralError{
|
||||
description: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
42
src/main.rs
42
src/main.rs
@@ -1,35 +1,57 @@
|
||||
extern crate serde;
|
||||
extern crate clap;
|
||||
extern crate reqwest;
|
||||
extern crate simple_home_dir;
|
||||
|
||||
mod cli;
|
||||
mod config;
|
||||
mod planning;
|
||||
mod openproject;
|
||||
mod error;
|
||||
mod gitlab;
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
use clap::Parser;
|
||||
use cli::Cli;
|
||||
use config::Config;
|
||||
use crate::cli::Commands::Planning;
|
||||
use crate::cli::Planning::I2work;
|
||||
use crate::error::GeneralError;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
let default_config_path = PathBuf::from("/etc/config.toml");
|
||||
let mut default_config_path = PathBuf::new();
|
||||
default_config_path.push(simple_home_dir::home_dir().unwrap());
|
||||
default_config_path.push(".config/cl-cli/config.toml");
|
||||
|
||||
let config_path = match cli.config.as_deref() {
|
||||
Some(p) => p,
|
||||
None => &default_config_path
|
||||
};
|
||||
|
||||
let config_path_content = fs::read_to_string(config_path)
|
||||
.expect("Could not read config file");
|
||||
let config_path_content = match fs::read_to_string(config_path) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
println!("Could not read config file at {:?}, error: {}", config_path, e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let config: Config = toml::from_str(&*config_path_content).expect("Could not parse config");
|
||||
|
||||
match cli.command {
|
||||
Some(Planning(I2work(args))) => {
|
||||
planning::issue2work::issue2work(config, &args);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
let result = match cli.command {
|
||||
Some(Planning(I2work(args))) => planning::issue2work::issue2work(config, &args).await,
|
||||
None => Err(GeneralError{description: "No command launched".to_string()})
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(()) => exit(0),
|
||||
Err(e) => {
|
||||
println!("Error: {}", e.description);
|
||||
exit(1)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
48
src/openproject/client.rs
Normal file
48
src/openproject/client.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::config::OpenProjectConfig;
|
||||
use crate::error::GeneralError;
|
||||
use crate::openproject::work::{WorkPackageWriter, WorkPackage};
|
||||
|
||||
pub(crate) struct Error {
|
||||
description: String,
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(value: reqwest::Error) -> Self {
|
||||
Error {
|
||||
description: format!("Error while connecting to openproject instance: {}", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for GeneralError {
|
||||
fn from(value: Error) -> GeneralError {
|
||||
GeneralError{description: value.description}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Client {
|
||||
base_url: String,
|
||||
token: String,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn from_config(config: &OpenProjectConfig) -> Client {
|
||||
Client{base_url: config.base_url.clone(), token: config.token.clone()}
|
||||
}
|
||||
|
||||
pub async fn create_work_package(&self, work_package: &WorkPackageWriter, project_id: &String) -> Result<WorkPackage, Error> {
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let work_package: WorkPackage = client
|
||||
.post(format!("{}/api/v3/projects/{}/work_packages", self.base_url, project_id))
|
||||
.basic_auth("apikey", Some(&self.token))
|
||||
.json(&work_package)
|
||||
.send()
|
||||
.await?
|
||||
.json()
|
||||
.await?;
|
||||
|
||||
Ok(work_package)
|
||||
}
|
||||
}
|
3
src/openproject/mod.rs
Normal file
3
src/openproject/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub(crate) mod client;
|
||||
mod work;
|
||||
|
36
src/openproject/work.rs
Normal file
36
src/openproject/work.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::gitlab::issue::IssueBundle;
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct WorkPackageWriter {
|
||||
subject: String,
|
||||
#[serde(alias = "type")]
|
||||
work_type: String,
|
||||
description: DescriptionWriter,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct DescriptionWriter {
|
||||
format: String,
|
||||
raw: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct WorkPackage {
|
||||
pub id: u64,
|
||||
pub subject: String,
|
||||
}
|
||||
|
||||
impl From<&IssueBundle> for WorkPackageWriter {
|
||||
|
||||
fn from(value: &IssueBundle) -> Self {
|
||||
WorkPackageWriter {
|
||||
subject: format!("{} ({}/{})", value.issue.title, value.project.name_with_namespace, value.issue.iid),
|
||||
work_type: "TASK".into(),
|
||||
description: DescriptionWriter {
|
||||
format: "markdown".into(),
|
||||
raw: format!("From gitlab: {}", value.issue.web_url)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,11 @@
|
||||
use crate::cli::Issue2Work;
|
||||
use crate::config::Config;
|
||||
use gitlab::{Gitlab, Issue};
|
||||
use gitlab::api::{Query, issues};
|
||||
use crate::openproject::client::Client;
|
||||
use gitlab::{ GitlabBuilder, Issue, Project};
|
||||
use gitlab::api::{issues, AsyncQuery, projects};
|
||||
use url::Url;
|
||||
use crate::error::GeneralError;
|
||||
use crate::gitlab::issue::IssueBundle;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct IssueInfo {
|
||||
@@ -10,22 +13,39 @@ struct IssueInfo {
|
||||
iid: u64,
|
||||
}
|
||||
|
||||
pub(crate) fn issue2work(config: Config, args: &Issue2Work) {
|
||||
pub(crate) async fn issue2work(config: Config, args: &Issue2Work) -> Result<(), GeneralError> {
|
||||
let url = Url::parse(&*args.issue_url)
|
||||
.expect("issue_url is not valid");
|
||||
let data = extract_issue_info(&url).unwrap();
|
||||
|
||||
let client = Gitlab::new("gitlab.com", config.gitlab.token).unwrap();
|
||||
let client = GitlabBuilder::new("gitlab.com", config.gitlab.token).build_async().await?;
|
||||
|
||||
let endpoint = issues::ProjectIssues::builder()
|
||||
.iid(data.iid)
|
||||
.project(String::from(data.project))
|
||||
.build()
|
||||
.unwrap()
|
||||
;
|
||||
|
||||
let issues: Vec<Issue> = endpoint.query_async(&client).await.unwrap();
|
||||
let issue = issues.first().unwrap();
|
||||
|
||||
let project_endpoint = projects::Project::builder()
|
||||
.project(issue.project_id.value())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let issue: Vec<Issue> = endpoint.query(&client).unwrap();
|
||||
let project: Project = project_endpoint.query_async(&client).await.unwrap();
|
||||
|
||||
println!("{:?}", issue.first());
|
||||
let issue_bundle = IssueBundle::new(&issue, &project);
|
||||
|
||||
let open_project_client = Client::from_config(&config.openproject);
|
||||
|
||||
let work_package = open_project_client.create_work_package(&(&issue_bundle).into(), &args.project_id).await?;
|
||||
|
||||
println!("new work package created: {:?}, edit at {}/projects/{}/work_packages/{}", work_package.subject, config.openproject.base_url, args.project_id, work_package.id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_issue_info(url: &Url) -> Option<IssueInfo> {
|
||||
|
Reference in New Issue
Block a user