Append link to gitlab to create link between open project and gitlab (#7)
Check go code / build-and-release (push) Successful in 1m7s

Reviewed-on: #7
Co-authored-by: Julien Fastré <julien.fastre@champs-libres.coop>
Co-committed-by: Julien Fastré <julien.fastre@champs-libres.coop>
This commit was merged in pull request #7.
This commit is contained in:
2025-11-18 08:45:31 +00:00
committed by Julien Fastré
parent f72f03e7d4
commit 69eafc509a
6 changed files with 106 additions and 15 deletions
+25 -1
View File
@@ -6,9 +6,12 @@ use crate::gitlab::issue::{Issue, IssueBundle, Project};
use crate::openproject::client::Client;
use crate::openproject::user::{GetMe, User};
use crate::openproject::work::WorkPackageWriterAssignee;
use crate::planning::utils::{append_related_issues, IssueRelated};
use crate::planning::Issue2WorkActionTrait;
use crate::planning::Issue2WorkResult;
use gitlab::api::{issues, projects, AsyncQuery};
use gitlab::api::projects::issues::EditIssue;
use gitlab::api::{issues, projects, ApiError, AsyncQuery};
use gitlab::RestError;
use url::Url;
#[derive(Debug)]
@@ -71,6 +74,27 @@ impl Issue2WorkActionTrait for GitlabAction {
.create_work_package(&(&dto).into(), &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), &issue.description);
let endpoint_put_result = EditIssue::builder()
.project(issue.project_id)
.issue(issue.iid)
.description(content)
.build();
if let Ok(endpoint_put) = endpoint_put_result {
let r: Result<Issue, ApiError<RestError>> = endpoint_put.query_async(&client).await;
if r.is_err() {
eprintln!("Error while updating the issue description: {:?}", r);
}
}
let result = Issue2WorkResult {
work_package_url: format!(
"{}/projects/{}/work_packages/{}",
+28 -4
View File
@@ -65,10 +65,30 @@ fn add_related_issues_section(issue: &IssueRelated) -> Vec<String> {
fn convert_issue_link_items(issue: &IssueRelated) -> String {
match issue {
IssueRelated::OpenProjectIssue(issue_url) => format!("- [{}]({})", issue_url, issue_url),
IssueRelated::OpenProjectIssue(issue_url) => {
let m = format!("- [{}]({})", issue_url, issue_url);
match extract_id(issue_url) {
Some(n) => format!("{}\n- OP#{}", m, n),
None => m,
}
}
}
}
use regex::Regex;
fn extract_id(url: &str) -> Option<u32> {
// Cette regex capture le dernier segment numérique si lURL se termine par :
// - /work_packages/<id>
// - /wp/<id>
// Et accepte n'importe quel segment avant.
let re = Regex::new(r"/(?:work_packages|wp)/(\d+)$").unwrap();
re.captures(url)
.and_then(|caps| caps.get(1))
.and_then(|m| m.as_str().parse::<u32>().ok())
}
#[cfg(test)]
mod tests {
use crate::planning::utils::{append_related_issues, IssueRelated};
@@ -80,7 +100,8 @@ mod tests {
r#"
## Related issues
- [https://example/wp/1](https://example/wp/1)"#,
- [https://example/wp/1](https://example/wp/1)
- OP#1"#,
append_related_issues(&issue, &("".to_string()))
);
}
@@ -97,7 +118,8 @@ Some content
## Related issues
- [https://example/wp/1](https://example/wp/1)"#,
- [https://example/wp/1](https://example/wp/1)
- OP#1"#,
append_related_issues(
&issue,
&"Something happens.\n\
@@ -123,7 +145,8 @@ Some content
## Related issues
- [https://example/wp/2](https://example/wp/2)
- [https://example/wp/1](https://example/wp/1)"#,
- [https://example/wp/1](https://example/wp/1)
- OP#1"#,
append_related_issues(
&issue,
&r#"Something happens.
@@ -154,6 +177,7 @@ Some content
- [https://example/wp/2](https://example/wp/2)
- [https://example/wp/1](https://example/wp/1)
- OP#1
## Other content
+10 -10
View File
@@ -172,10 +172,10 @@ mod tests {
fn deserializes_issue2work_variant() {
// Given: a JSON with the external tag "type" matching the enum variant name
let payload = json!({
"type": "Issue2Work",
"url": "https://example.com/issues/123",
"project": "test"
});
"type": "Issue2Work",
"url": "https://example.com/issues/123",
"project": "test"
});
// When: deserializing to InputMessage
let msg: InputMessage = serde_json::from_value(payload).expect("valid enum JSON");
@@ -192,10 +192,10 @@ mod tests {
#[test]
fn fails_on_unknown_type_tag() {
let payload = json!({
"type": "Unknown",
"url": "https://example.com/issues/123",
"project": "test"
});
"type": "Unknown",
"url": "https://example.com/issues/123",
"project": "test"
});
let err = serde_json::from_value::<InputMessage>(payload).unwrap_err();
// Basic sanity check that it's a data error mentioning the unrecognized variant
@@ -210,8 +210,8 @@ mod tests {
fn fails_when_missing_required_fields() {
// Missing "url" and "project"
let payload = json!({
"type": "Issue2Work"
});
"type": "Issue2Work"
});
let err = serde_json::from_value::<InputMessage>(payload).unwrap_err();
let msg = err.to_string();