Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canary - 8/29/24 #441

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.Loader.UseRidGraph" Value="true" />
</ItemGroup>

<PropertyGroup>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/GitHubExtension/Widgets/GitHubIssuesWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public override void LoadContentData()

issuesData.Add("issues", issuesArray);
issuesData.Add("selected_repo", repository?.FullName ?? string.Empty);
issuesData.Add("widgetTitle", WidgetTitle);
issuesData.Add("widgetTitle", GetActualTitle());
issuesData.Add("is_loading_data", DataState == WidgetDataState.Unknown);
issuesData.Add("issues_icon_data", _issuesIconData);

Expand Down
2 changes: 1 addition & 1 deletion src/GitHubExtension/Widgets/GitHubPullsWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override void LoadContentData()

pullsData.Add("pulls", pullsArray);
pullsData.Add("selected_repo", repository?.FullName ?? string.Empty);
pullsData.Add("widgetTitle", WidgetTitle);
pullsData.Add("widgetTitle", GetActualTitle());
pullsData.Add("is_loading_data", DataState == WidgetDataState.Unknown);
pullsData.Add("pulls_icon_data", _pullsIconData);

Expand Down
2 changes: 1 addition & 1 deletion src/GitHubExtension/Widgets/GitHubReleasesWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public override void LoadContentData()

releasesData.Add("releases", releasesArray);
releasesData.Add("selected_repo", repository?.FullName ?? string.Empty);
releasesData.Add("widgetTitle", WidgetTitle);
releasesData.Add("widgetTitle", GetActualTitle());
releasesData.Add("is_loading_data", DataState == WidgetDataState.Unknown);
releasesData.Add("releases_icon_data", _releasesIconData);

Expand Down
110 changes: 41 additions & 69 deletions src/GitHubExtension/Widgets/GitHubRepositoryWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text.Json.Nodes;
using GitHubExtension.Client;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;

Expand All @@ -15,6 +14,8 @@ public abstract class GitHubRepositoryWidget : GitHubWidget
{
protected string RepositoryUrl { get; set; } = string.Empty;

private string? _message;

public GitHubRepositoryWidget()
: base()
{
Expand Down Expand Up @@ -66,14 +67,14 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)

switch (verb)
{
case WidgetAction.CheckUrl:
HandleCheckUrl(actionInvokedArgs);
break;

case WidgetAction.Save:
UpdateTitle(JsonNode.Parse(actionInvokedArgs.Data));
base.OnActionInvoked(actionInvokedArgs);
CorrectUrl();
if (HandleCheckUrl(actionInvokedArgs))
{
UpdateTitle(JsonNode.Parse(actionInvokedArgs.Data));
base.OnActionInvoked(actionInvokedArgs);
CorrectUrl();
}

break;

default:
Expand Down Expand Up @@ -142,10 +143,11 @@ private void UpdateTitle(JsonNode? dataObj)
}

GetTitleFromDataObject(dataObj);
if (string.IsNullOrEmpty(WidgetTitle))
{
WidgetTitle = GetRepositoryFromUrl(RepositoryUrl).FullName;
}
}

protected string GetActualTitle()
{
return string.IsNullOrEmpty(WidgetTitle) ? GetRepositoryFromUrl(RepositoryUrl).FullName : WidgetTitle;
}

protected override void ResetWidgetInfoFromState()
Expand Down Expand Up @@ -207,7 +209,7 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
base.OnCustomizationRequested(customizationRequestedArgs);
}

private void HandleCheckUrl(WidgetActionInvokedArgs args)
private bool HandleCheckUrl(WidgetActionInvokedArgs args)
{
// Set loading page while we fetch data from GitHub.
Page = WidgetPageState.Loading;
Expand All @@ -223,7 +225,19 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args)
RepositoryUrl = dataObject["url"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);

ConfigurationData = data;
var isGoodToSave = true;

try
{
GetRepositoryFromUrl(RepositoryUrl);
ConfigurationData = data;
_message = null;
}
catch (Exception ex)
{
_message = ex.Message;
isGoodToSave = false;
}

var updateRequestOptions = new WidgetUpdateRequestOptions(Id)
{
Expand All @@ -233,71 +247,29 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args)
};

WidgetManager.GetDefault().UpdateWidget(updateRequestOptions);

// Already shown error message while updating above,
// can reset it to null here.
_message = null;
return isGoodToSave;
}

UpdateWidget();

return false;
}

public string GetConfiguration(string dataUrl)
{
var configurationData = new JsonObject
{
{ "submitIcon", IconLoader.GetIconAsBase64("arrow.png") },
{ "widgetTitle", WidgetTitle },
{ "url", dataUrl },
{ "savedRepositoryUrl", SavedConfigurationData },
{ "errorMessage", _message },
};

if (dataUrl == string.Empty)
{
configurationData.Add("hasConfiguration", false);
var repositoryData = new JsonObject
{
{ "url", string.Empty },
};

configurationData.Add("configuration", repositoryData);
configurationData.Add("savedRepositoryUrl", SavedConfigurationData);
configurationData.Add("saveEnabled", false);

return configurationData.ToString();
}
else
{
try
{
var repository = GetRepositoryFromUrl(dataUrl);
var repositoryData = new JsonObject
{
{ "name", repository.FullName },
{ "label", repository.Name },
{ "owner", repository.Owner.Login },
{ "milestone", string.Empty },
{ "project", repository.Description },
{ "url", RepositoryUrl },
{ "query", GetUnescapedIssueQuery() },
};

configurationData.Add("hasConfiguration", true);
configurationData.Add("configuration", repositoryData);
configurationData.Add("savedRepositoryUrl", SavedConfigurationData);
configurationData.Add("saveEnabled", true);
}
catch (Exception ex)
{
Log.Error(ex, $"Failed getting configuration information for input url: {dataUrl}");
configurationData.Add("hasConfiguration", false);

var repositoryData = new JsonObject
{
{ "url", RepositoryUrl },
};

configurationData.Add("errorMessage", ex.Message);
configurationData.Add("configuration", repositoryData);
configurationData.Add("saveEnabled", false);

return configurationData.ToString();
}

return configurationData.ToJsonString();
}
return configurationData.ToJsonString();
}

public override string GetData(WidgetPageState page)
Expand Down
53 changes: 4 additions & 49 deletions src/GitHubExtension/Widgets/GitHubReviewWidget.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json.Nodes;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

namespace GitHubExtension.Widgets;

internal sealed class GitHubReviewWidget : GitHubUserWidget
{
public GitHubReviewWidget()
: base()
{
// This widget does not allow customization, so this value will not change.
ShowCategory = SearchCategory.PullRequests;
}

public override void RequestContentData()
{
var request = new SearchIssuesRequest($"review-requested:{UserName}");

RequestContentData(request);
}
protected override string DefaultShowCategory => "PullRequests";

protected override string GetTitleIconData()
{
return IconLoader.GetIconAsBase64("pulls.png");
}

// This widget does not have "ShowCategory" as a variable.
// So we override this method to not care about this data.
protected override void ResetWidgetInfoFromState()
{
base.ResetWidgetInfoFromState();
ShowCategory = SearchCategory.PullRequests;
}

// Overriding this method because this widget only cares about the account.
public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
public override void RequestContentData()
{
if (actionInvokedArgs.Verb == "Submit")
{
var data = actionInvokedArgs.Data;
var dataObject = JsonNode.Parse(data);

if (dataObject == null)
{
return;
}

DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);

ConfigurationData = data;
var request = new SearchIssuesRequest($"review-requested:{UserName}");

// If we got here during the customization flow, we need to LoadContentData again
// so we can show the loading page rather than stale data.
LoadContentData();
UpdateActivityState();
}
else
{
base.OnActionInvoked(actionInvokedArgs);
}
RequestContentData(request);
}

public override string GetTemplatePath(WidgetPageState page)
Expand Down
22 changes: 15 additions & 7 deletions src/GitHubExtension/Widgets/GitHubUserWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal abstract class GitHubUserWidget : GitHubWidget

protected SearchCategory ShowCategory { get; set; } = SearchCategory.Unknown;

protected virtual string DefaultShowCategory => string.Empty;

private string _userName = string.Empty;

protected string UserName
Expand Down Expand Up @@ -57,11 +59,17 @@ public override void OnWidgetContextChanged(WidgetContextChangedArgs contextChan

protected void UpdateTitle(JsonNode dataObj)
{
GetTitleFromDataObject(dataObj);
if (string.IsNullOrEmpty(WidgetTitle))
if (dataObj == null)
{
WidgetTitle = UserName;
return;
}

GetTitleFromDataObject(dataObj);
}

protected string GetActualTitle()
{
return string.IsNullOrEmpty(WidgetTitle) ? UserName : WidgetTitle;
}

protected override void ResetWidgetInfoFromState()
Expand Down Expand Up @@ -115,7 +123,7 @@ protected override void ResetWidgetInfoFromState()
try
{
dataObject ??= JsonNode.Parse(ConfigurationData);
ShowCategory = EnumHelper.StringToSearchCategory(dataObject!["showCategory"]?.GetValue<string>() ?? string.Empty);
ShowCategory = EnumHelper.StringToSearchCategory(dataObject!["showCategory"]?.GetValue<string>() ?? DefaultShowCategory);
DeveloperLoginId = dataObject!["account"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);
}
Expand Down Expand Up @@ -147,7 +155,7 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
return;
}

ShowCategory = EnumHelper.StringToSearchCategory(dataObject["showCategory"]?.GetValue<string>() ?? string.Empty);
ShowCategory = EnumHelper.StringToSearchCategory(dataObject["showCategory"]?.GetValue<string>() ?? DefaultShowCategory);
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);

Expand Down Expand Up @@ -249,7 +257,7 @@ public override void LoadContentData()
{ "openCount", 0 },
{ "items", new JsonArray() },
{ "userName", UserName },
{ "widgetTitle", WidgetTitle },
{ "widgetTitle", GetActualTitle() },
{ "titleIconUrl", GetTitleIconData() },
{ "is_loading_data", true },
};
Expand Down Expand Up @@ -302,7 +310,7 @@ public void LoadContentData(IEnumerable<Octokit.Issue> items)
issuesData.Add("items", issuesArray);
issuesData.Add("userName", UserName);
issuesData.Add("titleIconUrl", GetTitleIconData());
issuesData.Add("widgetTitle", WidgetTitle);
issuesData.Add("widgetTitle", GetActualTitle());

LastUpdated = DateTime.Now;
ContentData = issuesData.ToJsonString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
"type": "Input.Text",
"id": "url",
"label": "%Widget_Template_Label/Url%",
"inlineAction": {
"type": "Action.Execute",
"tooltip": "%Widget_Template_Tooltip/Submit%",
"verb": "CheckUrl",
"iconUrl": "data:image/png;base64,${submitIcon}"
},
"spacing": "Medium",
"style": "Url",
"placeholder": "%Widget_Template_Input/UrlPlaceholder%",
"value": "${$root.configuration.url}"
"value": "${url}"
},
{
"type": "Input.Text",
Expand Down Expand Up @@ -115,8 +109,7 @@
"type": "Action.Execute",
"title": "%Widget_Template_Button/Save%",
"verb": "Save",
"tooltip": "%Widget_Template_Tooltip/Save%",
"isEnabled": "${$root.saveEnabled}"
"tooltip": "%Widget_Template_Tooltip/Save%"
},
{
"type": "Action.Execute",
Expand Down
Loading
Loading