Append link to gitlab to create link between open project and gitlab #7
3
.changes/unreleased/Added-20251117-221555.yaml
Normal file
3
.changes/unreleased/Added-20251117-221555.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
kind: Added
|
||||||
|
body: Append work package link to gitlab description after creating a WP from a gitlab issue
|
||||||
|
time: 2025-11-17T22:15:55.503651263+01:00
|
||||||
39
Cargo.lock
generated
39
Cargo.lock
generated
@@ -17,6 +17,15 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -180,6 +189,7 @@ dependencies = [
|
|||||||
"gitlab",
|
"gitlab",
|
||||||
"log",
|
"log",
|
||||||
"open",
|
"open",
|
||||||
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@@ -1155,6 +1165,35 @@ dependencies = [
|
|||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.12.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reqwest"
|
name = "reqwest"
|
||||||
version = "0.12.24"
|
version = "0.12.24"
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ simple-home-dir = "0.5.2"
|
|||||||
log = "0.4.28"
|
log = "0.4.28"
|
||||||
open = "5.3.2"
|
open = "5.3.2"
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
|
regex = "1.12.2"
|
||||||
|
|
||||||
[package.metadata.deb]
|
[package.metadata.deb]
|
||||||
maintainer = "Julien Fastré <julien.fastre@champs-libres.coop>"
|
maintainer = "Julien Fastré <julien.fastre@champs-libres.coop>"
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ use crate::gitlab::issue::{Issue, IssueBundle, Project};
|
|||||||
use crate::openproject::client::Client;
|
use crate::openproject::client::Client;
|
||||||
use crate::openproject::user::{GetMe, User};
|
use crate::openproject::user::{GetMe, User};
|
||||||
use crate::openproject::work::WorkPackageWriterAssignee;
|
use crate::openproject::work::WorkPackageWriterAssignee;
|
||||||
|
use crate::planning::utils::{append_related_issues, IssueRelated};
|
||||||
use crate::planning::Issue2WorkActionTrait;
|
use crate::planning::Issue2WorkActionTrait;
|
||||||
use crate::planning::Issue2WorkResult;
|
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;
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -71,6 +74,27 @@ impl Issue2WorkActionTrait for GitlabAction {
|
|||||||
.create_work_package(&(&dto).into(), &args.project_id)
|
.create_work_package(&(&dto).into(), &args.project_id)
|
||||||
.await?;
|
.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 {
|
let result = Issue2WorkResult {
|
||||||
work_package_url: format!(
|
work_package_url: format!(
|
||||||
"{}/projects/{}/work_packages/{}",
|
"{}/projects/{}/work_packages/{}",
|
||||||
|
|||||||
@@ -65,9 +65,29 @@ fn add_related_issues_section(issue: &IssueRelated) -> Vec<String> {
|
|||||||
|
|
||||||
fn convert_issue_link_items(issue: &IssueRelated) -> String {
|
fn convert_issue_link_items(issue: &IssueRelated) -> String {
|
||||||
match issue {
|
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 l’URL 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@@ -80,7 +100,8 @@ mod tests {
|
|||||||
r#"
|
r#"
|
||||||
## Related issues
|
## 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()))
|
append_related_issues(&issue, &("".to_string()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -97,7 +118,8 @@ Some content
|
|||||||
|
|
||||||
## Related issues
|
## Related issues
|
||||||
|
|
||||||
- [https://example/wp/1](https://example/wp/1)"#,
|
- [https://example/wp/1](https://example/wp/1)
|
||||||
|
- OP#1"#,
|
||||||
append_related_issues(
|
append_related_issues(
|
||||||
&issue,
|
&issue,
|
||||||
&"Something happens.\n\
|
&"Something happens.\n\
|
||||||
@@ -123,7 +145,8 @@ Some content
|
|||||||
## Related issues
|
## Related issues
|
||||||
|
|
||||||
- [https://example/wp/2](https://example/wp/2)
|
- [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(
|
append_related_issues(
|
||||||
&issue,
|
&issue,
|
||||||
&r#"Something happens.
|
&r#"Something happens.
|
||||||
@@ -154,6 +177,7 @@ Some content
|
|||||||
|
|
||||||
- [https://example/wp/2](https://example/wp/2)
|
- [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
|
||||||
|
|
||||||
## Other content
|
## Other content
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user