4763d48290
Extend Gitea client with a `patch` method for executing PATCH requests. Introduce `IssueWriteSetBody` struct for serializing issue update payloads. Update Gitea action to append related issues and send PATCH requests to update issue descriptions.
83 lines
2.7 KiB
Rust
83 lines
2.7 KiB
Rust
use crate::cli::Issue2Work;
|
|
use crate::config::Config;
|
|
use crate::error::GeneralError;
|
|
use crate::gitea::client::has_client_for_url;
|
|
use crate::gitea::issue::{issue_html_url_to_api, Issue, IssueWriteSetBody};
|
|
use crate::openproject::user::{GetMe, User};
|
|
use crate::openproject::work::WorkPackageWriter;
|
|
use crate::planning::utils::{append_related_issues, IssueRelated};
|
|
use crate::planning::Issue2WorkActionTrait;
|
|
use url::Url;
|
|
|
|
pub(crate) struct GiteaAction {}
|
|
|
|
impl Issue2WorkActionTrait for GiteaAction {
|
|
async fn run(&self, url: &Url, config: &Config, args: &Issue2Work) -> Result<(), GeneralError> {
|
|
let gitea_client = crate::gitea::client::Client::from_config(config.gitea.first().unwrap());
|
|
let issue: Issue = gitea_client.get(issue_html_url_to_api(url)?).await?;
|
|
let open_project_client =
|
|
crate::openproject::client::Client::from_config(&config.openproject);
|
|
|
|
let work_package = create_work_package_from_issue(
|
|
&issue,
|
|
match args.assign_to_me {
|
|
true => {
|
|
let u = open_project_client.me().await?;
|
|
Some(u)
|
|
}
|
|
false => None,
|
|
},
|
|
);
|
|
|
|
let work_package = open_project_client
|
|
.create_work_package(&work_package, &args.project_id)
|
|
.await?;
|
|
|
|
let url_wp = format!(
|
|
"{}/projects/{}/work_packages/{}",
|
|
config.openproject.base_url, args.project_id, work_package.id
|
|
);
|
|
|
|
let content = append_related_issues(
|
|
&IssueRelated::OpenProjectIssue(url_wp.to_string()),
|
|
&issue.body,
|
|
);
|
|
let _u: Issue = gitea_client
|
|
.patch(
|
|
issue_html_url_to_api(url)?,
|
|
&IssueWriteSetBody { body: content },
|
|
)
|
|
.await?;
|
|
|
|
println!(
|
|
"new work package created: {:?}, edit at {}",
|
|
work_package.subject, url_wp
|
|
);
|
|
|
|
if let Err(e) = open::that(url_wp) {
|
|
println!("failed to open work package in browser: {}", e);
|
|
};
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn supports(&self, url: &Url, config: &Config, _args: &Issue2Work) -> bool {
|
|
has_client_for_url(&url, &config)
|
|
}
|
|
}
|
|
|
|
fn create_work_package_from_issue(issue: &Issue, assignee: Option<User>) -> WorkPackageWriter {
|
|
WorkPackageWriter {
|
|
subject: format!(
|
|
"{} ({}/{})",
|
|
issue.title, issue.repository.full_name, issue.number
|
|
),
|
|
work_type: "TASK".into(),
|
|
description: crate::openproject::work::DescriptionWriter {
|
|
format: "markdown".into(),
|
|
raw: format!("From Gitea issue: {} \n\n{}", issue.html_url, issue.body),
|
|
},
|
|
assignee: assignee.into(),
|
|
}
|
|
}
|