This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGitHubUserWidget.cs
More file actions
400 lines (342 loc) · 13.4 KB
/
Copy pathGitHubUserWidget.cs
File metadata and controls
400 lines (342 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
using System.Text.Json.Nodes;
using GitHubExtension.DataManager;
using GitHubExtension.DeveloperId;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.DevHome.SDK;
using Microsoft.Windows.Widgets.Providers;
using Octokit;
namespace GitHubExtension.Widgets;
internal abstract class GitHubUserWidget : GitHubWidget
{
protected string DeveloperLoginId { get; set; } = string.Empty;
protected SearchCategory ShowCategory { get; set; } = SearchCategory.Unknown;
private string _userName = string.Empty;
protected string UserName
{
get => _userName = DeveloperLoginId;
set => _userName = value;
}
public GitHubUserWidget()
: base()
{
GitHubSearchManager.OnResultsAvailable += SearchManagerResultsAvailableHandler;
ShowCategory = SearchCategory.Unknown;
UserName = string.Empty;
}
~GitHubUserWidget()
{
GitHubSearchManager.OnResultsAvailable -= SearchManagerResultsAvailableHandler;
}
protected abstract string GetTitleIconData();
public override void DeleteWidget(string widgetId, string customState)
{
// Remove event handler.
GitHubSearchManager.OnResultsAvailable -= SearchManagerResultsAvailableHandler;
base.DeleteWidget(widgetId, customState);
}
public override void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs)
{
Enabled = contextChangedArgs.WidgetContext.IsActive;
UpdateActivityState();
}
protected void UpdateTitle(JsonNode dataObj)
{
if (dataObj == null)
{
return;
}
GetTitleFromDataObject(dataObj);
}
protected string GetActualTitle()
{
return string.IsNullOrEmpty(WidgetTitle) ? UserName : WidgetTitle;
}
protected override void ResetWidgetInfoFromState()
{
JsonNode? dataObject = null;
try
{
dataObject = JsonNode.Parse(ConfigurationData);
}
catch (JsonException e)
{
Log.Warning($"Failed to parse ConfigurationData; attempting migration. {e.Message}");
Log.Debug($"Json parse failure.", e);
try
{
// Old data versioning was not a Json string. If we attempt to parse
// and we get a failure, assume it is the old version.
if (!string.IsNullOrEmpty(ConfigurationData))
{
Log.Information($"Found string data format, migrating to JSON format. Data: {ConfigurationData}");
var migratedState = new JsonObject
{
{ "showCategory", ConfigurationData },
};
// Prior to this configuration change, multi-account was not supported. Assume that
// if there is exactly one Developer Id that is the one the user had configured.
// There is no else case here because if we leave this value absent, the widget will
// change state to configuring, where the user can select the developer ID they want.
if (DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds.Count() == 1)
{
migratedState.Add("account", DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds.First().LoginId);
}
ConfigurationData = migratedState.ToJsonString();
}
else
{
ConfigurationData = EmptyJson;
}
}
catch (Exception ex)
{
// Adding for abundance of caution because we have seen crashes in this space.
Log.Error(ex, $"Unexpected failure during migration.");
}
}
try
{
dataObject ??= JsonNode.Parse(ConfigurationData);
ShowCategory = EnumHelper.StringToSearchCategory(dataObject!["showCategory"]?.GetValue<string>() ?? string.Empty);
DeveloperLoginId = dataObject!["account"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);
}
catch (Exception e)
{
// If we fail to parse configuration data, do nothing, report the failure, and don't
// crash the entire extension.
DeveloperLoginId = string.Empty;
ShowCategory = SearchCategory.Unknown;
Log.Error(e, $"Unexpected error while resetting state: {e.Message}");
}
}
public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
ShowCategory = SearchCategory.Unknown;
base.OnCustomizationRequested(customizationRequestedArgs);
}
public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
{
if (actionInvokedArgs.Verb == "Submit")
{
var data = actionInvokedArgs.Data;
var dataObject = JsonNode.Parse(data);
if (dataObject == null)
{
return;
}
ShowCategory = EnumHelper.StringToSearchCategory(dataObject["showCategory"]?.GetValue<string>() ?? string.Empty);
DeveloperLoginId = dataObject["account"]?.GetValue<string>() ?? string.Empty;
UpdateTitle(dataObject);
ConfigurationData = data;
// 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);
}
}
public override void UpdateActivityState()
{
// State logic for the Widget:
// Signed in -> Configure -> Active / Inactive per widget host.
if (!IsUserLoggedIn())
{
SetSignIn();
return;
}
if (ShowCategory == SearchCategory.Unknown || string.IsNullOrEmpty(DeveloperLoginId))
{
SetConfigure();
return;
}
if (Enabled)
{
if (ContentData == EmptyJson)
{
SetLoading();
}
else
{
SetActive();
}
return;
}
SetInactive();
}
protected void RequestContentData(SearchIssuesRequest request)
{
// Throttle protection against a widget trigging rapid data updates.
if (DateTime.Now - LastUpdated < WidgetDataRequestMinTime)
{
Log.Debug("Data request too soon, skipping.");
}
if (ActivityState == WidgetActivityState.Configure)
{
return;
}
try
{
Log.Information($"Requesting data update for {UserName}");
var requestOptions = new RequestOptions
{
ApiOptions = new ApiOptions
{
PageSize = 10,
PageCount = 1,
StartPage = 1,
},
UsePublicClientAsFallback = true,
};
var searchManager = GitHubSearchManager.CreateInstance();
var developerId = GetWidgetDeveloperId();
if (developerId == null)
{
throw new InvalidOperationException($"DevID does not exist for login id: {DeveloperLoginId}");
}
searchManager?.SearchForGitHubIssuesOrPRs(request, Id, ShowCategory, developerId, requestOptions);
Log.Information($"Requested data update for {UserName}");
DataState = WidgetDataState.Requested;
}
catch (Exception ex)
{
Log.Error(ex, "Failed requesting data update.");
}
}
public override void LoadContentData()
{
var issuesData = new JsonObject
{
{ "openCount", 0 },
{ "items", new JsonArray() },
{ "userName", UserName },
{ "widgetTitle", GetActualTitle() },
{ "titleIconUrl", GetTitleIconData() },
{ "is_loading_data", true },
};
ContentData = issuesData.ToJsonString();
}
public void LoadContentData(IEnumerable<Octokit.Issue> items)
{
Log.Debug("Getting Data for Category in Widget");
try
{
var issuesData = new JsonObject();
var issuesArray = new JsonArray();
issuesData.Add("openCount", items.Count());
foreach (var item in items)
{
var issue = new JsonObject
{
{ "title", item.Title },
{ "url", item.HtmlUrl },
{ "number", item.Number },
{ "date", TimeSpanHelper.DateTimeOffsetToDisplayString(item.UpdatedAt, Log) },
{ "user", item.User.Login },
{ "avatar", item.User.AvatarUrl },
{ "iconUrl", IconLoader.GetIconAsBase64(item.PullRequest == null ? "issues.png" : "pulls.png") },
};
var issueLabels = new JsonArray();
foreach (var label in item.Labels)
{
var issueLabel = new JsonObject
{
{ "name", label.Name },
{ "color", label.Color },
};
((IList<JsonNode?>)issueLabels).Add(issueLabel);
}
issue.Add("labels", issueLabels);
((IList<JsonNode?>)issuesArray).Add(issue);
var parsedUrl = item.HtmlUrl.Split('/');
var repo = parsedUrl[3] + '/' + parsedUrl[4];
issue.Add("repo", repo);
}
issuesData.Add("items", issuesArray);
issuesData.Add("userName", UserName);
issuesData.Add("titleIconUrl", GetTitleIconData());
issuesData.Add("widgetTitle", GetActualTitle());
LastUpdated = DateTime.Now;
ContentData = issuesData.ToJsonString();
DataState = WidgetDataState.Okay;
}
catch (Exception e)
{
Log.Error(e, "Error retrieving data.");
DataState = WidgetDataState.Failed;
return;
}
}
public override string GetData(WidgetPageState page)
{
return page switch
{
WidgetPageState.SignIn => new JsonObject { { "message", Resources.GetResource(@"Widget_Template/SignInRequired", Log) } }.ToJsonString(),
WidgetPageState.Configure => GetConfigurationData(),
WidgetPageState.Content => ContentData,
WidgetPageState.Loading => EmptyJson,
_ => throw new NotImplementedException(Page.GetType().Name),
};
}
protected IDeveloperId? GetWidgetDeveloperId()
{
foreach (var devId in DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds)
{
if (devId.LoginId == DeveloperLoginId)
{
return devId;
}
}
return null;
}
protected void AddDevIds(ref JsonObject configurationData)
{
var developerIdsData = new JsonArray();
foreach (var developerId in DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds)
{
Log.Information(developerId.LoginId);
developerIdsData.Add(new JsonObject
{
{ "devId", developerId.LoginId },
});
}
configurationData.Add("accounts", developerIdsData);
}
public string GetConfigurationData()
{
var configurationData = new JsonObject
{
{ "showCategory", EnumHelper.SearchCategoryToString(ShowCategory == SearchCategory.Unknown ? SearchCategory.IssuesAndPullRequests : ShowCategory) },
{ "savedShowCategory", SavedConfigurationData },
{ "configuring", true },
{ "widgetTitle", WidgetTitle },
};
if (!string.IsNullOrEmpty(DeveloperLoginId))
{
configurationData.Add("selectedDevId", DeveloperLoginId);
}
else if (DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds.Count() == 1)
{
configurationData.Add("selectedDevId", DeveloperIdProvider.GetInstance().GetLoggedInDeveloperIds().DeveloperIds.First().LoginId);
}
AddDevIds(ref configurationData);
return configurationData.ToJsonString();
}
private void SearchManagerResultsAvailableHandler(IEnumerable<Octokit.Issue> results, string widgetId)
{
Log.Debug($"Results Available Event: ID={widgetId}");
if (widgetId == Id)
{
Log.Information($"Received matching repository update event.");
LoadContentData(results);
UpdateActivityState();
}
}
}