Skip to content

Commit

Permalink
change quey with default user
Browse files Browse the repository at this point in the history
  • Loading branch information
8LWXpg committed Mar 16, 2024
1 parent d5e4bec commit c46602d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
17 changes: 16 additions & 1 deletion GithubResponse.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -85,6 +89,17 @@ public static async Task<QueryResult<List<GitHubRepo>, Exception>> UserRepoQuery
return await SendRequest<List<GitHubRepo>>($"https://api.github.com/users/{user}/repos?sort=updated", cts.Token);
}

public static async Task<QueryResult<List<GitHubRepo>, Exception>> DefaultUserRepoQuery(string user)
{
cts?.Cancel();
cts = new CancellationTokenSource();

// fallback to UserRepoQuery if authtoken is not set
return Client.DefaultRequestHeaders.Contains("Authorization") ?
await SendRequest<List<GitHubRepo>>("https://api.github.com/user/repos?sort=updated", cts.Token) :
await UserRepoQuery(user);
}

private static async Task<QueryResult<T, Exception>> SendRequest<T>(string url, CancellationToken token)
{
try
Expand Down
25 changes: 17 additions & 8 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,7 +71,7 @@ private record ResultData(string Url);
},
};

// handle user repo search
// handle user repo user
public List<Result> Query(Query query)
{
ArgumentNullException.ThrowIfNull(query);
Expand Down Expand Up @@ -133,7 +137,7 @@ public List<Result> Query(Query query)
string cacheKey = _defaultUser;
target = search[1..];

repos = _cache.GetOrAdd(cacheKey, () => UserRepoQuery(cacheKey));
repos = _cache.GetOrAdd(cacheKey, () => DefaultUserRepoQuery(cacheKey));
}
else
{
Expand Down Expand Up @@ -172,8 +176,13 @@ public List<Result> Query(Query query)
//results = results.Where(r => r.Title.StartsWith(target, StringComparison.OrdinalIgnoreCase)).ToList();
return results;

static List<GitHubRepo> UserRepoQuery(string search) =>
GitHub.UserRepoQuery(search).Result.Match(
static List<GitHubRepo> UserRepoQuery(string user) =>
GitHub.UserRepoQuery(user).Result.Match(
ok: r => r,
err: e => new List<GitHubRepo> { new(e.GetType().Name, string.Empty, e.Message, false) });

static List<GitHubRepo> DefaultUserRepoQuery(string user) =>
GitHub.DefaultUserRepoQuery(user).Result.Match(
ok: r => r,
err: e => new List<GitHubRepo> { new(e.GetType().Name, string.Empty, e.Message, false) });
}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c46602d

Please sign in to comment.