From 7675a87d8d49e1cfd7a59205246499bf16b220d7 Mon Sep 17 00:00:00 2001 From: Lauren Ciha Date: Mon, 23 Sep 2024 12:25:58 -0700 Subject: [PATCH 1/6] Prevent crash with catch statement --- .../ViewModels/WidgetViewModel.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs index eaf2824c1e..d0ff0791b9 100644 --- a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs +++ b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs @@ -25,6 +25,7 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.Windows.Widgets; using Microsoft.Windows.Widgets.Hosts; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; @@ -323,8 +324,28 @@ private string MergeJsonData(string jsonStringA, string jsonStringB) return jsonStringA; } - var objA = JObject.Parse(jsonStringA); - var objB = JObject.Parse(jsonStringB); + JObject objA; + JObject objB; + + try + { + objA = JObject.Parse(jsonStringA); + } + catch (JsonReaderException) + { + // If jsonStringA is not a valid JSON object, wrap it in an object + objA = new JObject { ["data"] = jsonStringA }; + } + + try + { + objB = JObject.Parse(jsonStringB); + } + catch (JsonReaderException) + { + // If jsonStringB is not a valid JSON object, wrap it in an object + objB = new JObject { ["data"] = jsonStringB }; + } objA.Merge(objB); From 802131607dfed634aacd1cda47c719d848747274 Mon Sep 17 00:00:00 2001 From: Lauren Ciha Date: Mon, 23 Sep 2024 13:27:05 -0700 Subject: [PATCH 2/6] Update HandleConnect for new args.Data structure --- .../Widgets/SSHWalletWidget.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs index e8fa556418..c331916bdf 100644 --- a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs +++ b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs @@ -144,13 +144,27 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c SetConfigure(); } + /* + * Now that the WidgetViewModel uses the MergeJsonData function, we can + * expect args.Data to be a json string object with the following structure: + * data: "hostname" + */ private void HandleConnect(WidgetActionInvokedArgs args) { + var jsonObject = JObject.Parse(args.Data); + var host = jsonObject["data"]?.ToString(); + + if (string.IsNullOrEmpty(host)) + { + Log.Error("Invalid data received for HandleConnect."); + return; + } + var cmd = new Process(); cmd.StartInfo = new ProcessStartInfo { FileName = "cmd.exe", - Arguments = $"/k \"ssh {args.Data}\"", + Arguments = $"/k \"ssh {host}\"", UseShellExecute = true, }; From 131d2d3e8662c8cbe66302fd026fb0554e3280ed Mon Sep 17 00:00:00 2001 From: Lauren Ciha Date: Mon, 23 Sep 2024 14:22:32 -0700 Subject: [PATCH 3/6] Refactor JSON string wrapping --- .../DevHome.Dashboard/ViewModels/WidgetViewModel.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs index d0ff0791b9..97f777597c 100644 --- a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs +++ b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs @@ -312,6 +312,11 @@ private Grid GetErrorCard(string error, string subError = null) return grid; } + private JObject WrapJsonString(string jsonString) + { + return new JObject { ["data"] = jsonString }; + } + private string MergeJsonData(string jsonStringA, string jsonStringB) { if (string.IsNullOrEmpty(jsonStringA)) @@ -333,8 +338,7 @@ private string MergeJsonData(string jsonStringA, string jsonStringB) } catch (JsonReaderException) { - // If jsonStringA is not a valid JSON object, wrap it in an object - objA = new JObject { ["data"] = jsonStringA }; + objA = WrapJsonString(jsonStringA); } try @@ -343,8 +347,7 @@ private string MergeJsonData(string jsonStringA, string jsonStringB) } catch (JsonReaderException) { - // If jsonStringB is not a valid JSON object, wrap it in an object - objB = new JObject { ["data"] = jsonStringB }; + objB = WrapJsonString(jsonStringB); } objA.Merge(objB); From dd10c32ee625b83f452d4ebbb4894faf6593afb8 Mon Sep 17 00:00:00 2001 From: Lauren Ciha Date: Mon, 23 Sep 2024 16:32:07 -0700 Subject: [PATCH 4/6] Clean up styling --- extensions/CoreWidgetProvider/CoreWidgetProvider.csproj | 1 + extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj b/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj index aa114bea22..dc95e0351f 100644 --- a/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj +++ b/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj @@ -25,6 +25,7 @@ + diff --git a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs index c331916bdf..1df6b243c7 100644 --- a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs +++ b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs @@ -144,11 +144,8 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c SetConfigure(); } - /* - * Now that the WidgetViewModel uses the MergeJsonData function, we can - * expect args.Data to be a json string object with the following structure: - * data: "hostname" - */ + // This function assumes arg.Data will be a json string object with the following structure: + // { "data": "hostname" } private void HandleConnect(WidgetActionInvokedArgs args) { var jsonObject = JObject.Parse(args.Data); From 0b3afdf2ca296279e8a35476a3d44efefac0d844 Mon Sep 17 00:00:00 2001 From: Lauren Ciha Date: Tue, 24 Sep 2024 09:08:01 -0700 Subject: [PATCH 5/6] Remove Newtonsoft dependency from SSHWalletWidget --- extensions/CoreWidgetProvider/CoreWidgetProvider.csproj | 1 - extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj b/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj index dc95e0351f..aa114bea22 100644 --- a/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj +++ b/extensions/CoreWidgetProvider/CoreWidgetProvider.csproj @@ -25,7 +25,6 @@ - diff --git a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs index 1df6b243c7..ece45d1e3e 100644 --- a/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs +++ b/extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs @@ -148,8 +148,8 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c // { "data": "hostname" } private void HandleConnect(WidgetActionInvokedArgs args) { - var jsonObject = JObject.Parse(args.Data); - var host = jsonObject["data"]?.ToString(); + var jsonObject = JsonDocument.Parse(args.Data).RootElement; + var host = jsonObject.GetProperty("data").GetString(); if (string.IsNullOrEmpty(host)) { From 88f5eb88db4ae03052ced5e550030c41409b031c Mon Sep 17 00:00:00 2001 From: Kristen Schau <47155823+krschau@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:18:43 -0400 Subject: [PATCH 6/6] User/krschau/fix ssh widget crash (#3900) --- .../ViewModels/WidgetViewModel.cs | 56 +++++-------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs index 97f777597c..4c69b9b962 100644 --- a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs +++ b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs @@ -25,8 +25,6 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.Windows.Widgets; using Microsoft.Windows.Widgets.Hosts; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using Serilog; namespace DevHome.Dashboard.ViewModels; @@ -312,42 +310,28 @@ private Grid GetErrorCard(string error, string subError = null) return grid; } - private JObject WrapJsonString(string jsonString) + private Newtonsoft.Json.Linq.JObject WrapJsonString(string jsonString) { - return new JObject { ["data"] = jsonString }; + return new Newtonsoft.Json.Linq.JObject { ["data"] = jsonString }; } - private string MergeJsonData(string jsonStringA, string jsonStringB) + private string MergeJsonData(Windows.Data.Json.JsonValue actionValue, Windows.Data.Json.JsonObject inputsObject) { - if (string.IsNullOrEmpty(jsonStringA)) - { - return jsonStringB; - } - - if (string.IsNullOrEmpty(jsonStringB)) - { - return jsonStringA; - } - - JObject objA; - JObject objB; + Newtonsoft.Json.Linq.JObject objA = []; + Newtonsoft.Json.Linq.JObject objB = []; - try + if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.Object) { - objA = JObject.Parse(jsonStringA); + objA = Newtonsoft.Json.Linq.JObject.Parse(actionValue.Stringify()); } - catch (JsonReaderException) + else if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.String) { - objA = WrapJsonString(jsonStringA); + objA = WrapJsonString(actionValue.Stringify()); } - try - { - objB = JObject.Parse(jsonStringB); - } - catch (JsonReaderException) + if (inputsObject?.ValueType != Windows.Data.Json.JsonValueType.Null) { - objB = WrapJsonString(jsonStringB); + objB = Newtonsoft.Json.Linq.JObject.Parse(inputsObject.Stringify()); } objA.Merge(objB); @@ -365,22 +349,10 @@ private async void HandleAdaptiveAction(RenderedAdaptiveCard sender, AdaptiveAct } else if (args.Action is AdaptiveExecuteAction executeAction) { - var actionData = string.Empty; - var inputsData = string.Empty; - - var dataType = executeAction.DataJson.ValueType; - if (dataType != Windows.Data.Json.JsonValueType.Null) - { - actionData = executeAction.DataJson.Stringify(); - } - - var inputType = args.Inputs.AsJson().ValueType; - if (inputType != Windows.Data.Json.JsonValueType.Null) - { - inputsData = args.Inputs.AsJson().Stringify(); - } + Windows.Data.Json.JsonValue actionValue = executeAction.DataJson; + Windows.Data.Json.JsonObject inputsObject = args.Inputs.AsJson(); - var dataToSend = MergeJsonData(actionData, inputsData); + var dataToSend = MergeJsonData(actionValue, inputsObject); _log.Information($"Verb = {executeAction.Verb}, Data = {dataToSend}"); await Widget.NotifyActionInvokedAsync(executeAction.Verb, dataToSend);