Skip to main content

lean_ctx/tools/
ctx_provider.rs

1use crate::core::providers::config::GitLabConfig;
2use crate::core::providers::{gitlab, ProviderResult};
3
4pub fn handle(args: &serde_json::Map<String, serde_json::Value>) -> String {
5    let action = args.get("action").and_then(|v| v.as_str()).unwrap_or("");
6
7    match action {
8        "gitlab_issues" => handle_gitlab_issues(args),
9        "gitlab_issue" => handle_gitlab_issue(args),
10        "gitlab_mrs" => handle_gitlab_mrs(args),
11        "gitlab_pipelines" => handle_gitlab_pipelines(args),
12        _ => format!(
13            "Unknown action: {action}. Available: gitlab_issues, gitlab_issue, gitlab_mrs, gitlab_pipelines"
14        ),
15    }
16}
17
18fn handle_gitlab_issues(args: &serde_json::Map<String, serde_json::Value>) -> String {
19    let config = match GitLabConfig::from_env() {
20        Ok(c) => c,
21        Err(e) => return format!("Error: {e}"),
22    };
23    let state = args.get("state").and_then(|v| v.as_str());
24    let labels = args.get("labels").and_then(|v| v.as_str());
25    let limit = args
26        .get("limit")
27        .and_then(serde_json::Value::as_u64)
28        .map(|n| n as usize);
29
30    match gitlab::list_issues(&config, state, labels, limit) {
31        Ok(result) => format_result(&result),
32        Err(e) => format!("Error: {e}"),
33    }
34}
35
36fn handle_gitlab_issue(args: &serde_json::Map<String, serde_json::Value>) -> String {
37    let config = match GitLabConfig::from_env() {
38        Ok(c) => c,
39        Err(e) => return format!("Error: {e}"),
40    };
41    let iid = args
42        .get("iid")
43        .and_then(serde_json::Value::as_u64)
44        .unwrap_or(0);
45    if iid == 0 {
46        return "Error: iid is required for gitlab_issue".to_string();
47    }
48
49    match gitlab::show_issue(&config, iid) {
50        Ok(result) => format_result(&result),
51        Err(e) => format!("Error: {e}"),
52    }
53}
54
55fn handle_gitlab_mrs(args: &serde_json::Map<String, serde_json::Value>) -> String {
56    let config = match GitLabConfig::from_env() {
57        Ok(c) => c,
58        Err(e) => return format!("Error: {e}"),
59    };
60    let state = args.get("state").and_then(|v| v.as_str());
61    let limit = args
62        .get("limit")
63        .and_then(serde_json::Value::as_u64)
64        .map(|n| n as usize);
65
66    match gitlab::list_mrs(&config, state, limit) {
67        Ok(result) => format_result(&result),
68        Err(e) => format!("Error: {e}"),
69    }
70}
71
72fn handle_gitlab_pipelines(args: &serde_json::Map<String, serde_json::Value>) -> String {
73    let config = match GitLabConfig::from_env() {
74        Ok(c) => c,
75        Err(e) => return format!("Error: {e}"),
76    };
77    let status = args.get("status").and_then(|v| v.as_str());
78    let limit = args
79        .get("limit")
80        .and_then(serde_json::Value::as_u64)
81        .map(|n| n as usize);
82
83    match gitlab::list_pipelines(&config, status, limit) {
84        Ok(result) => format_result(&result),
85        Err(e) => format!("Error: {e}"),
86    }
87}
88
89fn format_result(result: &ProviderResult) -> String {
90    crate::core::redaction::redact_text_if_enabled(&result.format_compact())
91}