Skip to main content

Mountain/RPC/CocoonService/SCM/
UpdateScmGroup.rs

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