allow to assign automatically current user on new work package
This commit is contained in:
@@ -1,41 +1,80 @@
|
||||
use crate::config::OpenProjectConfig;
|
||||
use crate::error::GeneralError;
|
||||
use crate::openproject::work::{WorkPackageWriter, WorkPackage};
|
||||
use crate::openproject::work::{WorkPackage, WorkPackageWriter};
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub(crate) struct Error {
|
||||
description: String,
|
||||
pub(crate) description: String,
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(value: reqwest::Error) -> Self {
|
||||
Error {
|
||||
description: format!("Error while connecting to openproject instance: {}", value)
|
||||
description: format!("Error while connecting to openproject instance: {}", value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for GeneralError {
|
||||
fn from(value: Error) -> GeneralError {
|
||||
GeneralError{description: value.description}
|
||||
GeneralError {
|
||||
description: value.description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Client {
|
||||
base_url: String,
|
||||
token: String,
|
||||
pub(crate) base_url: String,
|
||||
pub(crate) token: String,
|
||||
}
|
||||
|
||||
pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
|
||||
response: Response,
|
||||
error_message: &str,
|
||||
) -> Result<T, Error> {
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().to_string().clone();
|
||||
let content = response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|_| "Impossible to decode".into())
|
||||
.clone();
|
||||
|
||||
return Err(Error {
|
||||
description: format!(
|
||||
"{}, status: {}, content: {}",
|
||||
error_message, status, content
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
let t = response.json().await?;
|
||||
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn from_config(config: &OpenProjectConfig) -> Client {
|
||||
Client{base_url: config.base_url.clone(), token: config.token.clone()}
|
||||
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> {
|
||||
|
||||
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))
|
||||
.post(format!(
|
||||
"{}/api/v3/projects/{}/work_packages",
|
||||
self.base_url, project_id
|
||||
))
|
||||
.basic_auth("apikey", Some(&self.token))
|
||||
.json(&work_package)
|
||||
.send()
|
||||
|
13
src/openproject/hal.rs
Normal file
13
src/openproject/hal.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct HalEntity {
|
||||
#[serde(rename = "_type")]
|
||||
pub d_type: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Link {
|
||||
pub href: String,
|
||||
pub title: Option<String>,
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
pub(crate) mod client;
|
||||
mod work;
|
||||
|
||||
mod hal;
|
||||
pub(crate) mod root;
|
||||
pub(crate) mod user;
|
||||
pub(crate) mod work;
|
||||
|
42
src/openproject/root.rs
Normal file
42
src/openproject/root.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::openproject::client::{handle_response_status, Client, Error};
|
||||
use crate::openproject::hal::HalEntity;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct User {
|
||||
pub href: String,
|
||||
pub title: String,
|
||||
}
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Links {
|
||||
pub user: User,
|
||||
}
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Root {
|
||||
#[serde(rename = "instanceName")]
|
||||
pub instance_name: String,
|
||||
#[serde(rename = "_links")]
|
||||
pub links: Links,
|
||||
#[serde(flatten)]
|
||||
pub hal_entity: HalEntity,
|
||||
}
|
||||
|
||||
pub trait RootClient {
|
||||
async fn root(&self) -> Result<Root, Error>;
|
||||
}
|
||||
|
||||
impl RootClient for Client {
|
||||
async fn root(&self) -> Result<Root, Error> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = client
|
||||
.get(format!("{}/api/v3", self.base_url))
|
||||
.basic_auth("apikey", Some(&self.token))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let r = handle_response_status(response, "Error while retrieving root").await?;
|
||||
|
||||
Ok(r)
|
||||
}
|
||||
}
|
40
src/openproject/user.rs
Normal file
40
src/openproject/user.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::openproject::client::{handle_response_status, Client, Error};
|
||||
use crate::openproject::hal::Link;
|
||||
use crate::openproject::root::RootClient;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct UserLink {
|
||||
#[serde(rename = "self")]
|
||||
pub d_self: Link,
|
||||
}
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct User {
|
||||
#[serde(rename = "_type")]
|
||||
pub d_type: String,
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
#[serde(rename = "_links")]
|
||||
pub d_links: UserLink,
|
||||
}
|
||||
|
||||
pub trait GetMe {
|
||||
async fn me(&self) -> Result<User, Error>;
|
||||
}
|
||||
|
||||
impl GetMe for Client {
|
||||
async fn me(&self) -> Result<User, Error> {
|
||||
let r = self.root().await?;
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = client
|
||||
.get(format!("{}{}", self.base_url, r.links.user.href))
|
||||
.basic_auth("apikey", Some(&self.token))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let u = handle_response_status(response, "Error while retrieving user").await?;
|
||||
|
||||
Ok(u)
|
||||
}
|
||||
}
|
@@ -1,18 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::gitlab::issue::IssueBundle;
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct WorkPackageWriterAssignee {
|
||||
pub(crate) href: String,
|
||||
}
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct WorkPackageWriter {
|
||||
subject: String,
|
||||
pub(crate) subject: String,
|
||||
#[serde(alias = "type")]
|
||||
work_type: String,
|
||||
description: DescriptionWriter,
|
||||
pub(crate) work_type: String,
|
||||
pub(crate) description: DescriptionWriter,
|
||||
pub assignee: Option<WorkPackageWriterAssignee>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct DescriptionWriter {
|
||||
format: String,
|
||||
raw: String,
|
||||
pub(crate) format: String,
|
||||
pub(crate) raw: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -20,17 +24,3 @@ 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