60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
use crate::cli::Issue2Work;
|
|
use crate::config::Config;
|
|
use gitlab::{Gitlab, Issue};
|
|
use gitlab::api::{Query, issues};
|
|
use url::Url;
|
|
|
|
#[derive(Debug)]
|
|
struct IssueInfo {
|
|
project: String,
|
|
iid: u64,
|
|
}
|
|
|
|
pub(crate) fn issue2work(config: Config, args: &Issue2Work) {
|
|
let url = Url::parse(&*args.issue_url)
|
|
.expect("issue_url is not valid");
|
|
let data = extract_issue_info(&url).unwrap();
|
|
|
|
let client = Gitlab::new("gitlab.com", config.gitlab.token).unwrap();
|
|
|
|
let endpoint = issues::ProjectIssues::builder()
|
|
.iid(data.iid)
|
|
.project(String::from(data.project))
|
|
.build()
|
|
.unwrap();
|
|
|
|
let issue: Vec<Issue> = endpoint.query(&client).unwrap();
|
|
|
|
println!("{:?}", issue.first());
|
|
}
|
|
|
|
fn extract_issue_info(url: &Url) -> Option<IssueInfo> {
|
|
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;
|
|
|
|
let mut project_found = false;
|
|
|
|
for el in parts {
|
|
if el == "-" {
|
|
project_found = true;
|
|
continue;
|
|
}
|
|
if el == "issues" {
|
|
continue
|
|
}
|
|
if !project_found {
|
|
project_url.push(String::from(el));
|
|
} else {
|
|
// must be the id
|
|
iid = Some(String::from(el));
|
|
break;
|
|
}
|
|
}
|
|
|
|
Some(IssueInfo {
|
|
project: project_url.join("/"),
|
|
iid: iid.expect("iid of the issue not found")
|
|
.parse().expect("could not transform issue id to u64")
|
|
})
|
|
} |