allow to assign automatically current user on new work package

This commit is contained in:
2024-03-15 23:10:21 +01:00
parent 8da5d6ed87
commit 34f6eac006
17 changed files with 310 additions and 92 deletions

View File

@@ -1,11 +1,13 @@
use crate::cli::Issue2Work;
use crate::config::Config;
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;
use crate::openproject::client::Client;
use crate::openproject::user::{GetMe, User};
use crate::openproject::work::WorkPackageWriterAssignee;
use gitlab::api::{issues, projects, AsyncQuery};
use gitlab::{GitlabBuilder, Issue, Project};
use url::Url;
#[derive(Debug)]
struct IssueInfo {
@@ -13,19 +15,56 @@ struct IssueInfo {
iid: u64,
}
/// details on how to create a work package from various informations
#[derive(Debug)]
struct Issue2WorkPackageDTO {
pub issue: IssueBundle,
pub assign_to: Option<User>,
}
impl From<&Issue2WorkPackageDTO> for Option<WorkPackageWriterAssignee> {
fn from(value: &Issue2WorkPackageDTO) -> Self {
match &value.assign_to {
None => None,
Some(w) => Some(WorkPackageWriterAssignee {
href: w.clone().d_links.d_self.href,
}),
}
}
}
impl From<&Issue2WorkPackageDTO> for crate::openproject::work::WorkPackageWriter {
fn from(value: &Issue2WorkPackageDTO) -> Self {
crate::openproject::work::WorkPackageWriter {
subject: format!(
"{} ({}/{})",
value.issue.issue.title,
value.issue.project.name_with_namespace,
value.issue.issue.iid
),
work_type: "TASK".into(),
description: crate::openproject::work::DescriptionWriter {
format: "markdown".into(),
raw: format!("From gitlab: {}", value.issue.issue.web_url),
},
assignee: value.into(),
}
}
}
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 url = Url::parse(&*args.issue_url).expect("issue_url is not valid");
let data = extract_issue_info(&url).unwrap();
let client = GitlabBuilder::new("gitlab.com", config.gitlab.token).build_async().await?;
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()
;
.unwrap();
let issues: Vec<Issue> = endpoint.query_async(&client).await.unwrap();
let issue = issues.first().unwrap();
@@ -41,15 +80,33 @@ pub(crate) async fn issue2work(config: Config, args: &Issue2Work) -> Result<(),
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?;
let dto = Issue2WorkPackageDTO {
issue: issue_bundle,
assign_to: match args.assign_to_me {
true => {
let u = open_project_client.me().await?;
Some(u)
}
false => None,
},
};
println!("new work package created: {:?}, edit at {}/projects/{}/work_packages/{}", work_package.subject, config.openproject.base_url, args.project_id, work_package.id);
let work_package = open_project_client
.create_work_package(&(&dto).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> {
let parts = url.path_segments().expect("Could not parse path segment of given url");
let parts = url
.path_segments()
.expect("Could not parse path segment of given url");
let mut project_url: Vec<String> = Vec::with_capacity(3);
let mut iid: Option<String> = None;
@@ -61,7 +118,7 @@ fn extract_issue_info(url: &Url) -> Option<IssueInfo> {
continue;
}
if el == "issues" {
continue
continue;
}
if !project_found {
project_url.push(String::from(el));
@@ -74,7 +131,9 @@ fn extract_issue_info(url: &Url) -> Option<IssueInfo> {
Some(IssueInfo {
project: project_url.join("/"),
iid: iid.expect("iid of the issue not found")
.parse().expect("could not transform issue id to u64")
iid: iid
.expect("iid of the issue not found")
.parse()
.expect("could not transform issue id to u64"),
})
}
}