Skip to main content

Mountain/RPC/CocoonService/SCM/
UpdateScmGroup.rs

1
2//! Update a registered SCM provider's resource-state group via the trait
3//! (which mutates state and emits the deduplicated UI event). Falls back
4//! to a direct Sky emit if the trait wiring is unavailable.
5
6use serde_json::json;
7use tauri::Emitter;
8use tonic::{Response, Status};
9use CommonLibrary::SourceControlManagement::SourceControlManagementProvider::SourceControlManagementProvider;
10
11use crate::{
12	RPC::CocoonService::CocoonServiceImpl,
13	Vine::Generated::{Empty, UpdateScmGroupRequest},
14	dev_log,
15};
16
17pub async fn Fn(Service:&CocoonServiceImpl, Request:UpdateScmGroupRequest) -> Result<Response<Empty>, Status> {
18	dev_log!(
19		"cocoon",
20		"[CocoonService] update_scm_group: provider={} group={}",
21		Request.provider_id,
22		Request.group_id
23	);
24
25	let ResourceStates:Vec<serde_json::Value> = Request
26		.resource_states
27		.iter()
28		.map(|RS| {
29			json!({
30				"uri": RS.uri.as_ref().map(|U| U.value.as_str()).unwrap_or(""),
31				"decorations": RS.decorations,
32			})
33		})
34		.collect();
35
36	let ProviderHandle = Request
37		.provider_id
38		.as_bytes()
39		.iter()
40		.fold(0u32, |Acc, B| Acc.wrapping_mul(31).wrapping_add(*B as u32));
41
42	let GroupData = json!({
43		"groupId": Request.group_id,
44		"label": Request.group_id,
45		"resourceStates": ResourceStates,
46	});
47
48	if let Err(Error) = Service.environment.UpdateSourceControlGroup(ProviderHandle, GroupData).await {
49		dev_log!(
50			"cocoon",
51			"warn: [CocoonService] UpdateSourceControlGroup trait failed ({}); falling back to Sky emit",
52			Error
53		);
54
55		let _ = Service.environment.ApplicationHandle.emit(
56			"sky://scm/updateGroup",
57			json!({
58				"providerId": Request.provider_id,
59				"groupId": Request.group_id,
60				"resourceStates": ResourceStates,
61			}),
62		);
63	}
64
65	Ok(Response::new(Empty {}))
66}