Skip to content

Commit 5a24704

Browse files
committed
validate name, return serde_json::Error when applying integration settings
1 parent e75c177 commit 5a24704

16 files changed

+85
-236
lines changed

refact-agent/engine/src/integrations/docker/integr_docker.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,9 @@ pub struct ToolDocker {
5454
impl IntegrationTrait for ToolDocker {
5555
fn as_any(&self) -> &dyn std::any::Any { self }
5656

57-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
58-
match serde_json::from_value::<SettingsDocker>(value.clone()) {
59-
Ok(settings_docker) => {
60-
tracing::info!("Docker settings applied: {:?}", settings_docker);
61-
self.settings_docker = settings_docker
62-
},
63-
Err(e) => {
64-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
65-
return Err(e.to_string());
66-
}
67-
}
68-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
69-
Ok(x) => self.common = x,
70-
Err(e) => {
71-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
72-
return Err(e.to_string());
73-
}
74-
}
57+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
58+
self.settings_docker = serde_json::from_value(value.clone())?;
59+
self.common = serde_json::from_value(value.clone())?;
7560
self.config_path = config_path;
7661
Ok(())
7762
}

refact-agent/engine/src/integrations/docker/integr_isolation.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,11 @@ pub struct IntegrationIsolation {
3131
impl IntegrationTrait for IntegrationIsolation {
3232
fn as_any(&self) -> &dyn std::any::Any { self }
3333

34-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, _config_path: String, value: &serde_json::Value) -> Result<(), String> {
35-
match serde_json::from_value::<SettingsIsolation>(value.clone()) {
36-
Ok(settings_isolation) => {
37-
tracing::info!("Isolation settings applied: {:?}", settings_isolation);
38-
self.settings_isolation = settings_isolation
39-
},
40-
Err(e) => {
41-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
42-
return Err(e.to_string());
43-
}
44-
}
45-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
46-
Ok(x) => self.common = x,
47-
Err(e) => {
48-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
49-
return Err(e.to_string());
50-
}
51-
}
52-
Ok(())
53-
}
34+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, _config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
35+
self.settings_isolation = serde_json::from_value(value.clone())?;
36+
self.common = serde_json::from_value(value.clone())?;
37+
Ok(())
38+
}
5439

5540
fn integr_settings_as_json(&self) -> Value {
5641
serde_json::to_value(&self.settings_isolation).unwrap()

refact-agent/engine/src/integrations/integr_abstract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::global_context::GlobalContext;
1111
pub trait IntegrationTrait: Send + Sync {
1212
fn as_any(&self) -> &dyn std::any::Any;
1313
fn integr_schema(&self) -> &str;
14-
async fn integr_settings_apply(&mut self, gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String>;
14+
async fn integr_settings_apply(&mut self, gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error>;
1515
fn integr_settings_as_json(&self) -> serde_json::Value;
1616
fn integr_common(&self) -> IntegrationCommon;
1717
async fn integr_tools(&self, integr_name: &str) -> Vec<Box<dyn crate::tools::tools_description::Tool + Send>>; // integr_name is sometimes different, "cmdline_compile_my_project" != "cmdline"

refact-agent/engine/src/integrations/integr_chrome.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,9 @@ impl IntegrationSession for ChromeSession
152152
impl IntegrationTrait for ToolChrome {
153153
fn as_any(&self) -> &dyn std::any::Any { self }
154154

155-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
156-
match serde_json::from_value::<SettingsChrome>(value.clone()) {
157-
Ok(settings_chrome) => self.settings_chrome = settings_chrome,
158-
Err(e) => {
159-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
160-
return Err(e.to_string());
161-
}
162-
}
163-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
164-
Ok(x) => self.common = x,
165-
Err(e) => {
166-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
167-
return Err(e.to_string());
168-
}
169-
}
155+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
156+
self.settings_chrome = serde_json::from_value(value.clone())?;
157+
self.common = serde_json::from_value(value.clone())?;
170158
self.config_path = config_path;
171159
Ok(())
172160
}

refact-agent/engine/src/integrations/integr_cmdline.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,9 @@ pub struct ToolCmdline {
6666
impl IntegrationTrait for ToolCmdline {
6767
fn as_any(&self) -> &dyn std::any::Any { self }
6868

69-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
70-
match serde_json::from_value::<CmdlineToolConfig>(value.clone()) {
71-
Ok(x) => self.cfg = x,
72-
Err(e) => {
73-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
74-
return Err(e.to_string());
75-
}
76-
}
77-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
78-
Ok(x) => self.common = x,
79-
Err(e) => {
80-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
81-
return Err(e.to_string());
82-
}
83-
}
69+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
70+
self.cfg = serde_json::from_value(value.clone())?;
71+
self.common = serde_json::from_value(value.clone())?;
8472
self.config_path = config_path;
8573
Ok(())
8674
}

refact-agent/engine/src/integrations/integr_cmdline_service.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,9 @@ pub struct ToolService {
3434
impl IntegrationTrait for ToolService {
3535
fn as_any(&self) -> &dyn std::any::Any { self }
3636

37-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
38-
match serde_json::from_value::<CmdlineToolConfig>(value.clone()) {
39-
Ok(x) => self.cfg = x,
40-
Err(e) => {
41-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
42-
return Err(e.to_string());
43-
}
44-
}
45-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
46-
Ok(x) => self.common = x,
47-
Err(e) => {
48-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
49-
return Err(e.to_string());
50-
}
51-
}
37+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
38+
self.cfg = serde_json::from_value(value.clone())?;
39+
self.common = serde_json::from_value(value.clone())?;
5240
self.config_path = config_path;
5341
Ok(())
5442
}

refact-agent/engine/src/integrations/integr_github.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::sync::Arc;
22
use std::collections::HashMap;
33
use async_trait::async_trait;
4-
use tracing::{error, info};
54
use serde::{Deserialize, Serialize};
65
use tokio::sync::Mutex as AMutex;
76
use tokio::sync::RwLock as ARwLock;
@@ -35,23 +34,9 @@ pub struct ToolGithub {
3534
impl IntegrationTrait for ToolGithub {
3635
fn as_any(&self) -> &dyn std::any::Any { self }
3736

38-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
39-
match serde_json::from_value::<SettingsGitHub>(value.clone()) {
40-
Ok(settings_github) => {
41-
self.settings_github = settings_github;
42-
},
43-
Err(e) => {
44-
error!("Failed to apply settings: {}\n{:?}", e, value);
45-
return Err(e.to_string());
46-
}
47-
};
48-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
49-
Ok(x) => self.common = x,
50-
Err(e) => {
51-
error!("Failed to apply common settings: {}\n{:?}", e, value);
52-
return Err(e.to_string());
53-
}
54-
};
37+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
38+
self.settings_github = serde_json::from_value(value.clone())?;
39+
self.common = serde_json::from_value(value.clone())?;
5540
self.config_path = config_path;
5641
Ok(())
5742
}
@@ -185,7 +170,7 @@ fn parse_command_args(args: &HashMap<String, Value>) -> Result<Vec<String>, Stri
185170
return Err("Parsed command is empty".to_string());
186171
}
187172
for (i, arg) in parsed_args.iter().enumerate() {
188-
info!("argument[{}]: {}", i, arg);
173+
tracing::info!("argument[{}]: {}", i, arg);
189174
}
190175
if parsed_args[0] == "gh" {
191176
parsed_args.remove(0);

refact-agent/engine/src/integrations/integr_gitlab.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::sync::Arc;
22
use std::collections::HashMap;
33
use async_trait::async_trait;
4-
use tracing::{error, info};
54
use tokio::sync::Mutex as AMutex;
65
use tokio::sync::RwLock as ARwLock;
76
use tokio::process::Command;
@@ -34,24 +33,9 @@ pub struct ToolGitlab {
3433
impl IntegrationTrait for ToolGitlab {
3534
fn as_any(&self) -> &dyn std::any::Any { self }
3635

37-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
38-
match serde_json::from_value::<SettingsGitLab>(value.clone()) {
39-
Ok(settings_gitlab) => {
40-
info!("GitLab settings applied: {:?}", settings_gitlab);
41-
self.settings_gitlab = settings_gitlab;
42-
},
43-
Err(e) => {
44-
error!("Failed to apply settings: {}\n{:?}", e, value);
45-
return Err(e.to_string())
46-
}
47-
};
48-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
49-
Ok(x) => self.common = x,
50-
Err(e) => {
51-
error!("Failed to apply common settings: {}\n{:?}", e, value);
52-
return Err(e.to_string());
53-
}
54-
};
36+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
37+
self.settings_gitlab = serde_json::from_value(value.clone())?;
38+
self.common = serde_json::from_value(value.clone())?;
5539
self.config_path = config_path;
5640
Ok(())
5741
}
@@ -184,7 +168,7 @@ fn parse_command_args(args: &HashMap<String, Value>) -> Result<Vec<String>, Stri
184168
return Err("Parsed command is empty".to_string());
185169
}
186170
for (i, arg) in parsed_args.iter().enumerate() {
187-
info!("argument[{}]: {}", i, arg);
171+
tracing::info!("argument[{}]: {}", i, arg);
188172
}
189173
if parsed_args[0] == "glab" {
190174
parsed_args.remove(0);

refact-agent/engine/src/integrations/integr_mcp.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async fn _session_apply_settings(
9393
gcx: Arc<ARwLock<GlobalContext>>,
9494
config_path: String,
9595
new_cfg: SettingsMCP,
96-
) -> Result<(), String> {
96+
) {
9797
let session_key = format!("{}", config_path);
9898

9999
let session_arc = {
@@ -184,8 +184,6 @@ async fn _session_apply_settings(
184184
let session_downcasted = session_locked.as_any_mut().downcast_mut::<SessionMCP>().unwrap();
185185
session_downcasted.launched_coroutines.push(coroutine);
186186
}
187-
188-
Ok(())
189187
}
190188

191189
async fn _session_wait_coroutines(
@@ -213,32 +211,12 @@ impl IntegrationTrait for IntegrationMCP {
213211
self
214212
}
215213

216-
async fn integr_settings_apply(
217-
&mut self,
218-
gcx: Arc<ARwLock<GlobalContext>>,
219-
config_path: String,
220-
value: &serde_json::Value
221-
) -> Result<(), String> {
214+
async fn integr_settings_apply(&mut self, gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
222215
self.gcx_option = Some(Arc::downgrade(&gcx));
223-
224-
match serde_json::from_value::<SettingsMCP>(value.clone()) {
225-
Ok(x) => self.cfg = x,
226-
Err(e) => {
227-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
228-
return Err(e.to_string());
229-
}
230-
};
231-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
232-
Ok(x) => self.common = x,
233-
Err(e) => {
234-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
235-
return Err(e.to_string());
236-
}
237-
}
238-
self.config_path = config_path.clone();
239-
240-
_session_apply_settings(gcx.clone(), config_path.clone(), self.cfg.clone()).await?; // possibly saves coroutine in session
241-
216+
self.cfg = serde_json::from_value(value.clone())?;
217+
self.common = serde_json::from_value(value.clone())?;
218+
self.config_path = config_path;
219+
_session_apply_settings(gcx.clone(), self.config_path.clone(), self.cfg.clone()).await; // possibly saves coroutine in session
242220
Ok(())
243221
}
244222

refact-agent/engine/src/integrations/integr_mysql.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,9 @@ pub struct ToolMysql {
3838
impl IntegrationTrait for ToolMysql {
3939
fn as_any(&self) -> &dyn std::any::Any { self }
4040

41-
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), String> {
42-
match serde_json::from_value::<SettingsMysql>(value.clone()) {
43-
Ok(settings_mysql) => self.settings_mysql = settings_mysql,
44-
Err(e) => {
45-
tracing::error!("Failed to apply settings: {}\n{:?}", e, value);
46-
return Err(e.to_string());
47-
}
48-
}
49-
match serde_json::from_value::<IntegrationCommon>(value.clone()) {
50-
Ok(x) => self.common = x,
51-
Err(e) => {
52-
tracing::error!("Failed to apply common settings: {}\n{:?}", e, value);
53-
return Err(e.to_string());
54-
}
55-
}
41+
async fn integr_settings_apply(&mut self, _gcx: Arc<ARwLock<GlobalContext>>, config_path: String, value: &serde_json::Value) -> Result<(), serde_json::Error> {
42+
self.settings_mysql = serde_json::from_value(value.clone())?;
43+
self.common = serde_json::from_value(value.clone())?;
5644
self.config_path = config_path;
5745
Ok(())
5846
}

0 commit comments

Comments
 (0)