write workpackage on openproject

This commit is contained in:
2024-01-08 13:26:09 +01:00
parent 5b34a51d90
commit a17901a6d6
15 changed files with 739 additions and 82 deletions

48
src/openproject/client.rs Normal file
View 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
View File

@@ -0,0 +1,3 @@
pub(crate) mod client;
mod work;

36
src/openproject/work.rs Normal file
View 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)
}
}
}
}