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

GitHub reoganization #160

Merged
merged 5 commits into from
Dec 9, 2023
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
3 changes: 2 additions & 1 deletion src/Tql.Plugins.GitHub/Categories/IssueMatchBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Octokit;
using Tql.Abstractions;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

Expand All @@ -12,7 +13,7 @@ internal abstract class IssueMatchBase(IssueMatchDto dto)
{
protected IssueMatchDto Dto { get; } = dto;

public string Text => $"{Dto.RepositoryName}: #{Dto.Number} {Dto.Title}";
public string Text => MatchText.Path(Dto.RepositoryName, $"#{Dto.Number} {Dto.Title}");
public abstract ImageSource Icon { get; }
public abstract MatchTypeId TypeId { get; }

Expand Down
15 changes: 2 additions & 13 deletions src/Tql.Plugins.GitHub/Categories/IssuesMatch.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
using Octokit;
using Tql.Abstractions;
using Tql.Plugins.GitHub.Data;
using Tql.Plugins.GitHub.Services;

namespace Tql.Plugins.GitHub.Categories;

internal class IssuesMatch(
RootItemDto dto,
RepositoryItemMatchDto dto,
GitHubApi api,
ICache<GitHubData> cache,
ConfigurationManager configurationManager,
IMatchFactory<IssueMatch, IssueMatchDto> factory
)
: IssuesMatchBase<IssueMatch>(
dto,
api,
cache,
IssueTypeQualifier.Issue,
configurationManager,
factory
)
) : IssuesMatchBase<IssueMatch>(dto, api, IssueTypeQualifier.Issue, factory)
{
public override MatchTypeId TypeId => TypeIds.Issues;
public override string SearchHint => Labels.IssuesMatch_SearchHint;
Expand Down
35 changes: 14 additions & 21 deletions src/Tql.Plugins.GitHub/Categories/IssuesMatchBase.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
using Octokit;
using Tql.Abstractions;
using Tql.Plugins.GitHub.Data;
using Tql.Plugins.GitHub.Services;
using Tql.Plugins.GitHub.Support;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

internal abstract class IssuesMatchBase<T>(
RootItemDto dto,
RepositoryItemMatchDto dto,
GitHubApi api,
ICache<GitHubData> cache,
IssueTypeQualifier type,
ConfigurationManager configurationManager,
IMatchFactory<T, IssueMatchDto> factory
) : ISearchableMatch, ISerializableMatch
where T : IssueMatchBase
{
public string Text =>
MatchUtils.GetMatchLabel(
MatchText.Path(
$"{dto.Owner}/{dto.RepositoryName}",
type == IssueTypeQualifier.Issue
? Labels.IssuesMatchBase_IssueLabel
: Labels.IssuesMatchBase_PullRequestLabel,
type == IssueTypeQualifier.Issue
? Labels.IssuesMatchBase_MyIssueLabel
: Labels.IssuesMatchBase_MyPullRequestLabel,
configurationManager.Configuration,
dto
: Labels.IssuesMatchBase_PullRequestLabel
);

public ImageSource Icon => Images.Issue;
public ImageSource Icon => type == IssueTypeQualifier.Issue ? Images.Issue : Images.PullRequest;

public abstract MatchTypeId TypeId { get; }
public abstract string SearchHint { get; }

Expand All @@ -39,17 +33,16 @@ public async Task<IEnumerable<IMatch>> Search(
CancellationToken cancellationToken
)
{
if (dto.Scope == RootItemScope.Global && text.IsWhiteSpace())
return Array.Empty<IMatch>();

await context.DebounceDelay(cancellationToken);

var client = await api.GetClient(dto.Id);
var client = await api.GetClient(dto.ConnectionId);

var request = new SearchIssuesRequest(await GitHubUtils.GetSearchPrefix(dto, cache) + text)
{
Type = type
};
var request = string.IsNullOrEmpty(text)
? new SearchIssuesRequest()
: new SearchIssuesRequest(text);

request.Type = type;
request.Repos.Add(dto.Owner, dto.RepositoryName);

if (text.IsWhiteSpace())
{
Expand All @@ -67,7 +60,7 @@ CancellationToken cancellationToken
p =>
factory.Create(
new IssueMatchDto(
dto.Id,
dto.ConnectionId,
GitHubUtils.GetRepositoryName(p.HtmlUrl),
p.Number,
p.Title,
Expand Down
4 changes: 1 addition & 3 deletions src/Tql.Plugins.GitHub/Categories/IssuesType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Plugins.GitHub.Support;

namespace Tql.Plugins.GitHub.Categories;

[RootMatchType(SupportsUserScope = true)]
internal class IssuesType(
IMatchFactory<IssuesMatch, RootItemDto> factory,
IMatchFactory<IssuesMatch, RepositoryItemMatchDto> factory,
ConfigurationManager configurationManager
) : IssuesTypeBase<IssuesMatch, IssueMatch>(factory, configurationManager)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Tql.Plugins.GitHub/Categories/IssuesTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Tql.Plugins.GitHub.Categories;

internal abstract class IssuesTypeBase<TCategory, TMatch>(
IMatchFactory<TCategory, RootItemDto> factory,
IMatchFactory<TCategory, RepositoryItemMatchDto> factory,
ConfigurationManager configurationManager
) : MatchType<TCategory, RootItemDto>(factory)
) : MatchType<TCategory, RepositoryItemMatchDto>(factory)
where TCategory : IssuesMatchBase<TMatch>
where TMatch : IssueMatchBase
{
protected override bool IsValid(RootItemDto dto) =>
configurationManager.Configuration.HasConnection(dto.Id);
protected override bool IsValid(RepositoryItemMatchDto dto) =>
configurationManager.Configuration.HasConnection(dto.ConnectionId);
}
94 changes: 94 additions & 0 deletions src/Tql.Plugins.GitHub/Categories/MilestoneMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Windows.Forms;
using Microsoft.Extensions.DependencyInjection;
using Octokit;
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Plugins.GitHub.Support;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

internal class MilestoneMatch(
MilestoneMatchDto dto,
GitHubApi api,
IMatchFactory<IssueMatch, IssueMatchDto> factory
) : IRunnableMatch, ISerializableMatch, ICopyableMatch, ISearchableMatch
{
public string Text => MatchText.Path($"{dto.Owner}/{dto.RepositoryName}", dto.Title);
public ImageSource Icon => Images.Milestone;
public MatchTypeId TypeId => TypeIds.Milestone;
public string SearchHint => Labels.MilestoneMatch_SearchHint;

public Task Run(IServiceProvider serviceProvider, IWin32Window owner)
{
serviceProvider.GetRequiredService<IUI>().OpenUrl(dto.Url);

return Task.CompletedTask;
}

public string Serialize()
{
return JsonSerializer.Serialize(dto);
}

public Task Copy(IServiceProvider serviceProvider)
{
var clipboard = serviceProvider.GetRequiredService<IClipboard>();

clipboard.CopyMarkdown(Text, dto.Url);

return Task.CompletedTask;
}

public async Task<IEnumerable<IMatch>> Search(
ISearchContext context,
string text,
CancellationToken cancellationToken
)
{
await context.DebounceDelay(cancellationToken);

var client = await api.GetClient(dto.ConnectionId);

var request = string.IsNullOrEmpty(text)
? new SearchIssuesRequest()
: new SearchIssuesRequest(text);

request.Milestone = dto.Title;
request.Repos.Add(dto.Owner, dto.RepositoryName);

if (text.IsWhiteSpace())
{
request.SortField = IssueSearchSort.Created;
request.Order = SortDirection.Descending;
}

var response = await client.Search.SearchIssues(request);

cancellationToken.ThrowIfCancellationRequested();

return response
.Items
.Select(
p =>
factory.Create(
new IssueMatchDto(
dto.ConnectionId,
GitHubUtils.GetRepositoryName(p.HtmlUrl),
p.Number,
p.Title,
p.HtmlUrl,
p.State.Value
)
)
);
}
}

internal record MilestoneMatchDto(
Guid ConnectionId,
string Owner,
string RepositoryName,
string Title,
string Url
);
16 changes: 16 additions & 0 deletions src/Tql.Plugins.GitHub/Categories/MilestoneType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

internal class MilestoneType(
IMatchFactory<MilestoneMatch, MilestoneMatchDto> factory,
ConfigurationManager configurationManager
) : MatchType<MilestoneMatch, MilestoneMatchDto>(factory)
{
public override Guid Id => TypeIds.Milestone.Id;

protected override bool IsValid(MilestoneMatchDto dto) =>
configurationManager.Configuration.HasConnection(dto.ConnectionId);
}
61 changes: 61 additions & 0 deletions src/Tql.Plugins.GitHub/Categories/MilestonesMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

internal class MilestonesMatch(
RepositoryItemMatchDto dto,
GitHubApi api,
IMatchFactory<MilestoneMatch, MilestoneMatchDto> factory
) : ISearchableMatch, ISerializableMatch
{
public string Text =>
MatchText.Path($"{dto.Owner}/{dto.RepositoryName}", Labels.MilestonesMatch_Label);
public ImageSource Icon => Images.Milestone;
public MatchTypeId TypeId => TypeIds.Milestones;
public string SearchHint => Labels.MilestonesMatch_SearchHint;

public async Task<IEnumerable<IMatch>> Search(
ISearchContext context,
string text,
CancellationToken cancellationToken
)
{
var task = context.GetDataCached(
$"{GetType().FullName}|{dto.Owner}|{dto.RepositoryName}",
_ => GetMilestones()
);

if (!task.IsCompleted)
await context.DebounceDelay(cancellationToken);

return context.Filter(await task);
}

private async Task<ImmutableArray<MilestoneMatch>> GetMilestones()
{
var client = await api.GetClient(dto.ConnectionId);

return (
from milestone in await client
.Issue
.Milestone
.GetAllForRepository(dto.Owner, dto.RepositoryName)
select factory.Create(
new MilestoneMatchDto(
dto.ConnectionId,
dto.Owner,
dto.RepositoryName,
milestone.Title,
milestone.HtmlUrl
)
)
).ToImmutableArray();
}

public string Serialize()
{
return JsonSerializer.Serialize(dto);
}
}
16 changes: 16 additions & 0 deletions src/Tql.Plugins.GitHub/Categories/MilestonesType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Utilities;

namespace Tql.Plugins.GitHub.Categories;

internal class MilestonesType(
IMatchFactory<MilestonesMatch, RepositoryItemMatchDto> factory,
ConfigurationManager configurationManager
) : MatchType<MilestonesMatch, RepositoryItemMatchDto>(factory)
{
public override Guid Id => TypeIds.Milestones.Id;

protected override bool IsValid(RepositoryItemMatchDto dto) =>
configurationManager.Configuration.HasConnection(dto.ConnectionId);
}
15 changes: 2 additions & 13 deletions src/Tql.Plugins.GitHub/Categories/PullRequestsMatch.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
using Octokit;
using Tql.Abstractions;
using Tql.Plugins.GitHub.Data;
using Tql.Plugins.GitHub.Services;

namespace Tql.Plugins.GitHub.Categories;

internal class PullRequestsMatch(
RootItemDto dto,
RepositoryItemMatchDto dto,
GitHubApi api,
ICache<GitHubData> cache,
ConfigurationManager configurationManager,
IMatchFactory<PullRequestMatch, IssueMatchDto> factory
)
: IssuesMatchBase<PullRequestMatch>(
dto,
api,
cache,
IssueTypeQualifier.PullRequest,
configurationManager,
factory
)
) : IssuesMatchBase<PullRequestMatch>(dto, api, IssueTypeQualifier.PullRequest, factory)
{
public override MatchTypeId TypeId => TypeIds.PullRequests;
public override string SearchHint => Labels.PullRequestsMatch_SearchHint;
Expand Down
4 changes: 1 addition & 3 deletions src/Tql.Plugins.GitHub/Categories/PullRequestsType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Tql.Abstractions;
using Tql.Plugins.GitHub.Services;
using Tql.Plugins.GitHub.Support;

namespace Tql.Plugins.GitHub.Categories;

[RootMatchType(SupportsUserScope = true)]
internal class PullRequestsType(
IMatchFactory<PullRequestsMatch, RootItemDto> factory,
IMatchFactory<PullRequestsMatch, RepositoryItemMatchDto> factory,
ConfigurationManager configurationManager
) : IssuesTypeBase<PullRequestsMatch, PullRequestMatch>(factory, configurationManager)
{
Expand Down
Loading
Loading