write workpackage on openproject
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user