diff --git a/GithubResponse.cs b/GithubResponse.cs index b7e4dc5..c302f65 100644 --- a/GithubResponse.cs +++ b/GithubResponse.cs @@ -1,7 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + using System.Net.Http; using System.Net.Http.Headers; -using System.Text.Json.Serialization; using System.Text.Json; +using System.Text.Json.Serialization; using Wox.Plugin.Logger; namespace Community.PowerToys.Run.Plugin.GitHubRepo @@ -85,6 +89,17 @@ public static async Task, Exception>> UserRepoQuery return await SendRequest>($"https://api.github.com/users/{user}/repos?sort=updated", cts.Token); } + public static async Task, Exception>> DefaultUserRepoQuery(string user) + { + cts?.Cancel(); + cts = new CancellationTokenSource(); + + // fallback to UserRepoQuery if authtoken is not set + return Client.DefaultRequestHeaders.Contains("Authorization") ? + await SendRequest>("https://api.github.com/user/repos?sort=updated", cts.Token) : + await UserRepoQuery(user); + } + private static async Task> SendRequest(string url, CancellationToken token) { try diff --git a/Main.cs b/Main.cs index dfa81c2..e741f20 100644 --- a/Main.cs +++ b/Main.cs @@ -1,11 +1,15 @@ -using System.Globalization; -using System.Text; -using System.Windows.Controls; -using System.Windows.Input; +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + using Community.PowerToys.Run.Plugin.GitHubRepo.Properties; using LazyCache; using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; +using System.Globalization; +using System.Text; +using System.Windows.Controls; +using System.Windows.Input; using Wox.Infrastructure; using Wox.Plugin; using Wox.Plugin.Logger; @@ -67,7 +71,7 @@ private record ResultData(string Url); }, }; - // handle user repo search + // handle user repo user public List Query(Query query) { ArgumentNullException.ThrowIfNull(query); @@ -133,7 +137,7 @@ public List Query(Query query) string cacheKey = _defaultUser; target = search[1..]; - repos = _cache.GetOrAdd(cacheKey, () => UserRepoQuery(cacheKey)); + repos = _cache.GetOrAdd(cacheKey, () => DefaultUserRepoQuery(cacheKey)); } else { @@ -172,8 +176,13 @@ public List Query(Query query) //results = results.Where(r => r.Title.StartsWith(target, StringComparison.OrdinalIgnoreCase)).ToList(); return results; - static List UserRepoQuery(string search) => - GitHub.UserRepoQuery(search).Result.Match( + static List UserRepoQuery(string user) => + GitHub.UserRepoQuery(user).Result.Match( + ok: r => r, + err: e => new List { new(e.GetType().Name, string.Empty, e.Message, false) }); + + static List DefaultUserRepoQuery(string user) => + GitHub.DefaultUserRepoQuery(user).Result.Match( ok: r => r, err: e => new List { new(e.GetType().Name, string.Empty, e.Message, false) }); } diff --git a/README.md b/README.md index 715daac..1f7c7b5 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ This is a plugin for [PowerToys Run](https://aka.ms/PowerToysOverview_PowerToysR ### Search repo with default user: `/repo` +If authtoken is set, it will list all the repositories the token has access to. Otherwise, +it will list all the public repositories of the default user. ![Search repo with default user](./assets/default_user.png) ### Context menu @@ -36,7 +38,9 @@ This is a plugin for [PowerToys Run](https://aka.ms/PowerToysOverview_PowerToysR ## Settings - **Default user**: The default user to search for when typed `/`. -- **GitHub auth token** (optional): The GitHub auth token to use for better rate limiting. You can generate a token with no scope. Detailed instructions [here](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic). +- **GitHub auth token** (optional): The GitHub auth token to use for better rate limiting and access to private repo. + You can generate a fine-grained token with read access to metadata. Detailed instructions + [here](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token). ## Building