Skip to content

Commit

Permalink
Fix crash on SSH wallet widget (#3895)
Browse files Browse the repository at this point in the history
* Prevent crash with catch statement

* Update HandleConnect for new args.Data structure

* Refactor JSON string wrapping

* Clean up styling

* Remove Newtonsoft dependency from SSHWalletWidget

* User/krschau/fix ssh widget crash (#3900)

---------

Co-authored-by: Kristen Schau <47155823+krschau@users.noreply.github.com>
  • Loading branch information
lauren-ciha and krschau authored Sep 26, 2024
1 parent 3ab6c05 commit e000c37
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
13 changes: 12 additions & 1 deletion extensions/CoreWidgetProvider/Widgets/SSHWalletWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,24 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
SetConfigure();
}

// This function assumes arg.Data will be a json string object with the following structure:
// { "data": "hostname" }
private void HandleConnect(WidgetActionInvokedArgs args)
{
var jsonObject = JsonDocument.Parse(args.Data).RootElement;
var host = jsonObject.GetProperty("data").GetString();

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,
};

Expand Down
44 changes: 20 additions & 24 deletions tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.Widgets;
using Microsoft.Windows.Widgets.Hosts;
using Newtonsoft.Json.Linq;
using Serilog;

namespace DevHome.Dashboard.ViewModels;
Expand Down Expand Up @@ -311,20 +310,29 @@ private Grid GetErrorCard(string error, string subError = null)
return grid;
}

private string MergeJsonData(string jsonStringA, string jsonStringB)
private Newtonsoft.Json.Linq.JObject WrapJsonString(string jsonString)
{
if (string.IsNullOrEmpty(jsonStringA))
return new Newtonsoft.Json.Linq.JObject { ["data"] = jsonString };
}

private string MergeJsonData(Windows.Data.Json.JsonValue actionValue, Windows.Data.Json.JsonObject inputsObject)
{
Newtonsoft.Json.Linq.JObject objA = [];
Newtonsoft.Json.Linq.JObject objB = [];

if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.Object)
{
return jsonStringB;
objA = Newtonsoft.Json.Linq.JObject.Parse(actionValue.Stringify());
}

if (string.IsNullOrEmpty(jsonStringB))
else if (actionValue?.ValueType == Windows.Data.Json.JsonValueType.String)
{
return jsonStringA;
objA = WrapJsonString(actionValue.Stringify());
}

var objA = JObject.Parse(jsonStringA);
var objB = JObject.Parse(jsonStringB);
if (inputsObject?.ValueType != Windows.Data.Json.JsonValueType.Null)
{
objB = Newtonsoft.Json.Linq.JObject.Parse(inputsObject.Stringify());
}

objA.Merge(objB);

Expand All @@ -341,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);
Expand Down

0 comments on commit e000c37

Please sign in to comment.