From e4535ef7fb8c7d21abac2b0fa109e448382ed428 Mon Sep 17 00:00:00 2001 From: zetroot Date: Sat, 6 Nov 2021 12:18:56 +0300 Subject: [PATCH 01/10] github directory implementation --- .../DotNetRu.Commune.GithubFilesystem.csproj | 2 +- .../GithubDirectory.cs | 80 +++++++++++++++++++ .../GithubFileSystem.cs | 68 ---------------- .../BizLayerServiceRegistry.cs | 2 +- DotNetRu.Commune.WasmClient/Pages/Index.razor | 2 +- 5 files changed, 83 insertions(+), 71 deletions(-) create mode 100644 DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs delete mode 100644 DotNetRu.Commune.GithubFilesystem/GithubFileSystem.cs diff --git a/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj b/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj index 2b613b1..d8a1bea 100644 --- a/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj +++ b/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj @@ -9,7 +9,7 @@ - + diff --git a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs new file mode 100644 index 0000000..d8d5a2a --- /dev/null +++ b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DotNetRu.Auditor.Storage.FileSystem; +using Octokit; + +namespace DotNetRu.Commune.GithubFileSystem +{ + /// + /// Virtual directory in github repository. Implements + /// + public class GithubDirectory : IDirectory + { + private readonly IGitHubClient _gitHubClient; + private readonly Repository _repository; + private readonly Reference _branch; + private IRepositoryContentsClient ContentsClient => _gitHubClient.Repository.Content; + + private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) + { + _gitHubClient = gitHubClient; + _repository = repository; + _branch = branch; + Name = name; + FullName = fullName; + } + + /// + /// Factory method for building root directory + /// + /// Github client for this directory, stores authentication data in it + /// repository, contents are accessed to + /// branch in the reposiroty, to work with + /// new directory instance pointing to the root of content of this branch in this repository + public static IDirectory ForRoot(IGitHubClient gitHubClient, Repository repository, Reference branch) + { + return new GithubDirectory(gitHubClient, repository, branch, string.Empty, string.Empty); + } + + /// + public ValueTask ExistsAsync() => throw new NotImplementedException(); + + /// + public string Name { get; } + + /// + public string FullName { get; } + + /// + public IDirectory GetDirectory(string childDirectoryName) + { + var childFullName = FullName switch + { + null => childDirectoryName, + "" => childDirectoryName, + {} s when s.EndsWith("/") => $"{FullName}{childDirectoryName}", + _ => $"{FullName}/{childDirectoryName}" + }; + return new GithubDirectory(_gitHubClient, _repository, _branch, childDirectoryName, childFullName); + } + + /// + public IFile GetFile(string childFileName) => throw new NotImplementedException(); + + /// + public async IAsyncEnumerable EnumerateDirectoriesAsync() + { + var contents = await ContentsClient.GetAllContentsByRef(_repository.Id, FullName, _branch.Ref) + .ConfigureAwait(false); + foreach (var content in contents.Where(x => x.Type.Value == ContentType.Dir)) + { + yield return new GithubDirectory(_gitHubClient, _repository, _branch, content.Name, content.Path); + } + } + + /// + public IAsyncEnumerable EnumerateFilesAsync() => throw new NotImplementedException(); + } +} diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFileSystem.cs b/DotNetRu.Commune.GithubFilesystem/GithubFileSystem.cs deleted file mode 100644 index c718baa..0000000 --- a/DotNetRu.Commune.GithubFilesystem/GithubFileSystem.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using DotNetRu.Auditor.Storage.FileSystem; -using Octokit; -using Octokit.Helpers; -using Octokit.Internal; - -namespace DotNetRu.Commune.GithubFileSystem -{ - /// - /// Virtual filesystem implementation using GitHub repositories for storing files. Implements - /// - public class GithubFileSystem : IFileSystem - { - private const string ProductName = "DotNetRuCommune"; - private EditingContext? _editingContext; - - /// - /// Begin work with filesytem. Creates a fork of original repository, then creates a new branch in this fork and - /// stores all the data inside editing context. Without calling this method you can't do anything else - enumerate, read, create - /// - /// Personal access token - /// Original repository name - /// Original repository owner (user or organization) - public async Task StartContext(string token, string originRepo, string originOwner) - { - var credStore = new InMemoryCredentialStore(new(token, AuthenticationType.Bearer)); - var client = new GitHubClient(new Connection(new ProductHeaderValue(ProductName), - GitHubClient.GitHubApiUrl, credStore, - new HttpClientAdapter(Net5HttpMessageHandlerFactory.CreateDefault), - new SimpleJsonSerializer())); - - var originalRepo = await client.Repository.Get(originOwner, originRepo).ConfigureAwait(false); - var originalBranch = await client.Git.Reference.Get(originalRepo.Id, "heads/master").ConfigureAwait(false); - var fork = await client.Repository.Forks.Create(originalRepo.Id, new ()).ConfigureAwait(false); - var currentBranch = await client.Git.Reference.CreateBranch(fork.Owner.Login, fork.Name, Guid.NewGuid().ToString("N")).ConfigureAwait(false); - _editingContext = new EditingContext(client, originalRepo, originalBranch, fork, currentBranch); - } - - /// - public string Name => "/"; - - /// - public string FullName => "/"; - - /// - public bool Exists => true; - - /// - public ValueTask GetDirectoryInfoAsync(string subPath) => throw new System.NotImplementedException(); - - /// - public ValueTask GetFileInfoAsync(string subPath) => throw new System.NotImplementedException(); - - /// - public IAsyncEnumerable EnumerateDirectoriesAsync() => throw new System.NotImplementedException(); - - /// - public IAsyncEnumerable EnumerateFilesAsync() => throw new System.NotImplementedException(); - - /// - public ValueTask CreateFileAsync(string subPath) => throw new System.NotImplementedException(); - - /// - public ValueTask DeleteFileAsync(string subPath) => throw new System.NotImplementedException(); - } -} diff --git a/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs b/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs index 7bab80e..af232e9 100644 --- a/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs +++ b/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs @@ -20,7 +20,7 @@ internal static class BizLayerServiceRegistry { if (services is null) throw new ArgumentNullException(nameof(services)); // здесь регистрируются службы слоя бизнес-логики - services.TryAddSingleton(); + services.TryAddSingleton(); return services; } } diff --git a/DotNetRu.Commune.WasmClient/Pages/Index.razor b/DotNetRu.Commune.WasmClient/Pages/Index.razor index e461fab..a9e9161 100644 --- a/DotNetRu.Commune.WasmClient/Pages/Index.razor +++ b/DotNetRu.Commune.WasmClient/Pages/Index.razor @@ -4,7 +4,7 @@ @using DotNetRu.Commune.WasmClient.Model @using DotNetRu.Commune.GithubFileSystem @inject ILogger _logger; -@inject GithubFileSystem githubFs; +@inject GithubDirectory githubFs; @inject IOptions auditSettings; @inject NavigationManager _navigationManager; From 52a0637f502f3071340659d573704868a5a6f0f4 Mon Sep 17 00:00:00 2001 From: zetroot Date: Sat, 6 Nov 2021 12:37:20 +0300 Subject: [PATCH 02/10] added github file, extracted base abstraction for file and directory --- .../GithubDirectory.cs | 54 ++++++++--------- .../GithubFile.cs | 35 +++++++++++ .../GithubFilesystemEntry.cs | 59 +++++++++++++++++++ 3 files changed, 119 insertions(+), 29 deletions(-) create mode 100644 DotNetRu.Commune.GithubFilesystem/GithubFile.cs create mode 100644 DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs diff --git a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs index d8d5a2a..5db3ee5 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs +++ b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs @@ -10,20 +10,11 @@ namespace DotNetRu.Commune.GithubFileSystem /// /// Virtual directory in github repository. Implements /// - public class GithubDirectory : IDirectory + public class GithubDirectory : GithubFilesystemEntry, IDirectory { - private readonly IGitHubClient _gitHubClient; - private readonly Repository _repository; - private readonly Reference _branch; - private IRepositoryContentsClient ContentsClient => _gitHubClient.Repository.Content; - - private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) + private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : + base(gitHubClient, repository, branch, name, fullName) { - _gitHubClient = gitHubClient; - _repository = repository; - _branch = branch; - Name = name; - FullName = fullName; } /// @@ -38,26 +29,20 @@ public static IDirectory ForRoot(IGitHubClient gitHubClient, Repository reposito return new GithubDirectory(gitHubClient, repository, branch, string.Empty, string.Empty); } - /// - public ValueTask ExistsAsync() => throw new NotImplementedException(); - - /// - public string Name { get; } - - /// - public string FullName { get; } - - /// - public IDirectory GetDirectory(string childDirectoryName) - { - var childFullName = FullName switch + private string GetChildFullName(string childDirectoryName) => + FullName switch { null => childDirectoryName, "" => childDirectoryName, {} s when s.EndsWith("/") => $"{FullName}{childDirectoryName}", _ => $"{FullName}/{childDirectoryName}" }; - return new GithubDirectory(_gitHubClient, _repository, _branch, childDirectoryName, childFullName); + + /// + public IDirectory GetDirectory(string childDirectoryName) + { + var childFullName = GetChildFullName(childDirectoryName); + return new GithubDirectory(GitHubClient, Repository, Branch, childDirectoryName, childFullName); } /// @@ -66,15 +51,26 @@ public IDirectory GetDirectory(string childDirectoryName) /// public async IAsyncEnumerable EnumerateDirectoriesAsync() { - var contents = await ContentsClient.GetAllContentsByRef(_repository.Id, FullName, _branch.Ref) + var contents = await ContentsClient.GetAllContentsByRef(Repository.Id, FullName, Branch.Ref) .ConfigureAwait(false); foreach (var content in contents.Where(x => x.Type.Value == ContentType.Dir)) { - yield return new GithubDirectory(_gitHubClient, _repository, _branch, content.Name, content.Path); + yield return new GithubDirectory(GitHubClient, Repository, Branch, content.Name, content.Path); + } + } + + /// + public async IAsyncEnumerable EnumerateFilesAsync() + { + var contents = await ContentsClient.GetAllContentsByRef(Repository.Id, FullName, Branch.Ref) + .ConfigureAwait(false); + foreach (var content in contents.Where(x => x.Type.Value == ContentType.File)) + { + yield return new GithubFile(GitHubClient, Repository, Branch, content.Name, content.Path); } } /// - public IAsyncEnumerable EnumerateFilesAsync() => throw new NotImplementedException(); + public override ValueTask ExistsAsync() => throw new NotImplementedException(); } } diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFile.cs b/DotNetRu.Commune.GithubFilesystem/GithubFile.cs new file mode 100644 index 0000000..50b1a1d --- /dev/null +++ b/DotNetRu.Commune.GithubFilesystem/GithubFile.cs @@ -0,0 +1,35 @@ +using System.IO; +using System.Threading.Tasks; +using DotNetRu.Auditor.Storage.FileSystem; +using Octokit; + +namespace DotNetRu.Commune.GithubFileSystem +{ + /// + /// Files from github repository + /// + public class GithubFile : GithubFilesystemEntry, IFile + { + /// + /// ctor + /// + /// github client + /// github repository + /// branch in this repository + /// file name + /// file full path in repository + public GithubFile(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : + base(gitHubClient, repository, branch, name, fullName) + { + } + /// + public override ValueTask ExistsAsync() => throw new System.NotImplementedException(); + + /// + public Task OpenForReadAsync() => throw new System.NotImplementedException(); + + /// + public Task RequestWriteAccessAsync() => throw new System.NotImplementedException(); + + } +} diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs b/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs new file mode 100644 index 0000000..c81e186 --- /dev/null +++ b/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs @@ -0,0 +1,59 @@ +using System; +using System.Threading.Tasks; +using DotNetRu.Auditor.Storage.FileSystem; +using Octokit; + +namespace DotNetRu.Commune.GithubFileSystem +{ + /// + /// base class for all filesystem objects - directories and files + /// + public abstract class GithubFilesystemEntry : IFileSystemEntry + { + /// + /// Github client, used to access github data + /// + protected readonly IGitHubClient GitHubClient; + + /// + /// repository in github where data contents are stored + /// + protected readonly Repository Repository; + + /// + /// repository branch wich contents are browsed or modified + /// + protected readonly Reference Branch; + + /// + /// helper property to access contents client. Contents client is a wrapper over contents endpoint, it is used for manipulating data in repository + /// + protected IRepositoryContentsClient ContentsClient => GitHubClient.Repository.Content; + + /// + /// ctor + /// + /// github client + /// github repository + /// branch in repository + /// name of this entry + /// full name, aka path of this entry + protected GithubFilesystemEntry(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) + { + GitHubClient = gitHubClient; + Repository = repository; + Branch = branch; + Name = name; + FullName = fullName; + } + + /// + public string Name { get; } + + /// + public string FullName { get; } + + /// + public abstract ValueTask ExistsAsync(); + } +} From ddd8c05917451eff04f7b19db8584e39cfeb413e Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 00:02:18 +0300 Subject: [PATCH 03/10] added github file implementation, added exist method implementation, various fixes --- .../ClientFactory.cs | 27 +++++++++++++++++++ .../GithubDirectory.cs | 20 ++++++++++---- .../GithubFile.cs | 19 +++++++++++-- .../GithubFilesystemEntry.cs | 11 ++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 DotNetRu.Commune.GithubFilesystem/ClientFactory.cs diff --git a/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs new file mode 100644 index 0000000..502dffd --- /dev/null +++ b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs @@ -0,0 +1,27 @@ +using Octokit; +using Octokit.Internal; + +namespace DotNetRu.Commune.GithubFileSystem +{ + /// + /// Factoy for building clients + /// + public static class ClientFactory + { + private const string ProductName = "DotNetRuCommune"; + + /// + /// Build anonymous client - no credentials needed + /// + /// + public static GitHubClient Anonymous() + { + var credStore = new InMemoryCredentialStore(Credentials.Anonymous); + var client = new GitHubClient(new Connection(new ProductHeaderValue(ProductName), + GitHubClient.GitHubApiUrl, credStore, + new HttpClientAdapter(Net5HttpMessageHandlerFactory.CreateDefault), + new SimpleJsonSerializer())); + return client; + } + } +} diff --git a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs index 5db3ee5..0dd161c 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs +++ b/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DotNetRu.Auditor.Storage.FileSystem; @@ -26,7 +25,7 @@ private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Refer /// new directory instance pointing to the root of content of this branch in this repository public static IDirectory ForRoot(IGitHubClient gitHubClient, Repository repository, Reference branch) { - return new GithubDirectory(gitHubClient, repository, branch, string.Empty, string.Empty); + return new GithubDirectory(gitHubClient, repository, branch, string.Empty, "/"); } private string GetChildFullName(string childDirectoryName) => @@ -46,7 +45,11 @@ public IDirectory GetDirectory(string childDirectoryName) } /// - public IFile GetFile(string childFileName) => throw new NotImplementedException(); + public IFile GetFile(string childFileName) + { + var childFullName = GetChildFullName(childFileName); + return new GithubFile(GitHubClient, Repository, Branch, childFileName, childFullName); + } /// public async IAsyncEnumerable EnumerateDirectoriesAsync() @@ -71,6 +74,13 @@ public async IAsyncEnumerable EnumerateFilesAsync() } /// - public override ValueTask ExistsAsync() => throw new NotImplementedException(); + public override async ValueTask ExistsAsync() + { + var parentDirectory = GetParentDirectory(); + var contents = await ContentsClient.GetAllContentsByRef(Repository.Id, parentDirectory, Branch.Ref) + .ConfigureAwait(false); + + return contents.Any(x => x.Name == Name && x.Type.Value == ContentType.File); + } } } diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFile.cs b/DotNetRu.Commune.GithubFilesystem/GithubFile.cs index 50b1a1d..9de5ce4 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubFile.cs +++ b/DotNetRu.Commune.GithubFilesystem/GithubFile.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Linq; using System.Threading.Tasks; using DotNetRu.Auditor.Storage.FileSystem; using Octokit; @@ -22,11 +23,25 @@ public GithubFile(IGitHubClient gitHubClient, Repository repository, Reference b base(gitHubClient, repository, branch, name, fullName) { } + /// - public override ValueTask ExistsAsync() => throw new System.NotImplementedException(); + public override async ValueTask ExistsAsync() + { + var parentDirectory = GetParentDirectory(); + var contents = await ContentsClient.GetAllContentsByRef(Repository.Id, parentDirectory, Branch.Ref) + .ConfigureAwait(false); + + return contents.Any(x => x.Name == Name && x.Type.Value == ContentType.File); + } /// - public Task OpenForReadAsync() => throw new System.NotImplementedException(); + public async Task OpenForReadAsync() + { + var contents = await ContentsClient + .GetRawContentByRef(Repository.Owner.Login, Repository.Name, FullName, Branch.Ref) + .ConfigureAwait(false); + return new MemoryStream(contents, false); + } /// public Task RequestWriteAccessAsync() => throw new System.NotImplementedException(); diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs b/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs index c81e186..30ed843 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs +++ b/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs @@ -55,5 +55,16 @@ protected GithubFilesystemEntry(IGitHubClient gitHubClient, Repository repositor /// public abstract ValueTask ExistsAsync(); + + /// + /// Get parent directory name containing this entry + /// + /// path of the parent directory + protected string GetParentDirectory() => + FullName.Remove(FullName.Length - Name.Length) switch + { + "/" => string.Empty, + {} s => s + }; } } From 901e9cc30d57fe577c93875d4f4577f2d15db63f Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 16:42:09 +0300 Subject: [PATCH 04/10] renaming Github to GitHub --- .../{GithubDirectory.cs => GitHubDirectory.cs} | 14 +++++++------- .../{GithubFile.cs => GitHubFile.cs} | 4 ++-- ...FilesystemEntry.cs => GitHubFilesystemEntry.cs} | 5 ++--- 3 files changed, 11 insertions(+), 12 deletions(-) rename DotNetRu.Commune.GithubFilesystem/{GithubDirectory.cs => GitHubDirectory.cs} (87%) rename DotNetRu.Commune.GithubFilesystem/{GithubFile.cs => GitHubFile.cs} (93%) rename DotNetRu.Commune.GithubFilesystem/{GithubFilesystemEntry.cs => GitHubFilesystemEntry.cs} (94%) diff --git a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs b/DotNetRu.Commune.GithubFilesystem/GitHubDirectory.cs similarity index 87% rename from DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs rename to DotNetRu.Commune.GithubFilesystem/GitHubDirectory.cs index 0dd161c..e51559a 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubDirectory.cs +++ b/DotNetRu.Commune.GithubFilesystem/GitHubDirectory.cs @@ -9,9 +9,9 @@ namespace DotNetRu.Commune.GithubFileSystem /// /// Virtual directory in github repository. Implements /// - public class GithubDirectory : GithubFilesystemEntry, IDirectory + public class GitHubDirectory : GitHubFilesystemEntry, IDirectory { - private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : + private GitHubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : base(gitHubClient, repository, branch, name, fullName) { } @@ -25,7 +25,7 @@ private GithubDirectory(IGitHubClient gitHubClient, Repository repository, Refer /// new directory instance pointing to the root of content of this branch in this repository public static IDirectory ForRoot(IGitHubClient gitHubClient, Repository repository, Reference branch) { - return new GithubDirectory(gitHubClient, repository, branch, string.Empty, "/"); + return new GitHubDirectory(gitHubClient, repository, branch, string.Empty, "/"); } private string GetChildFullName(string childDirectoryName) => @@ -41,14 +41,14 @@ private string GetChildFullName(string childDirectoryName) => public IDirectory GetDirectory(string childDirectoryName) { var childFullName = GetChildFullName(childDirectoryName); - return new GithubDirectory(GitHubClient, Repository, Branch, childDirectoryName, childFullName); + return new GitHubDirectory(GitHubClient, Repository, Branch, childDirectoryName, childFullName); } /// public IFile GetFile(string childFileName) { var childFullName = GetChildFullName(childFileName); - return new GithubFile(GitHubClient, Repository, Branch, childFileName, childFullName); + return new GitHubFile(GitHubClient, Repository, Branch, childFileName, childFullName); } /// @@ -58,7 +58,7 @@ public async IAsyncEnumerable EnumerateDirectoriesAsync() .ConfigureAwait(false); foreach (var content in contents.Where(x => x.Type.Value == ContentType.Dir)) { - yield return new GithubDirectory(GitHubClient, Repository, Branch, content.Name, content.Path); + yield return new GitHubDirectory(GitHubClient, Repository, Branch, content.Name, content.Path); } } @@ -69,7 +69,7 @@ public async IAsyncEnumerable EnumerateFilesAsync() .ConfigureAwait(false); foreach (var content in contents.Where(x => x.Type.Value == ContentType.File)) { - yield return new GithubFile(GitHubClient, Repository, Branch, content.Name, content.Path); + yield return new GitHubFile(GitHubClient, Repository, Branch, content.Name, content.Path); } } diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFile.cs b/DotNetRu.Commune.GithubFilesystem/GitHubFile.cs similarity index 93% rename from DotNetRu.Commune.GithubFilesystem/GithubFile.cs rename to DotNetRu.Commune.GithubFilesystem/GitHubFile.cs index 9de5ce4..2425362 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubFile.cs +++ b/DotNetRu.Commune.GithubFilesystem/GitHubFile.cs @@ -9,7 +9,7 @@ namespace DotNetRu.Commune.GithubFileSystem /// /// Files from github repository /// - public class GithubFile : GithubFilesystemEntry, IFile + public class GitHubFile : GitHubFilesystemEntry, IFile { /// /// ctor @@ -19,7 +19,7 @@ public class GithubFile : GithubFilesystemEntry, IFile /// branch in this repository /// file name /// file full path in repository - public GithubFile(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : + public GitHubFile(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) : base(gitHubClient, repository, branch, name, fullName) { } diff --git a/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs b/DotNetRu.Commune.GithubFilesystem/GitHubFilesystemEntry.cs similarity index 94% rename from DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs rename to DotNetRu.Commune.GithubFilesystem/GitHubFilesystemEntry.cs index 30ed843..ece93fa 100644 --- a/DotNetRu.Commune.GithubFilesystem/GithubFilesystemEntry.cs +++ b/DotNetRu.Commune.GithubFilesystem/GitHubFilesystemEntry.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using DotNetRu.Auditor.Storage.FileSystem; using Octokit; @@ -8,7 +7,7 @@ namespace DotNetRu.Commune.GithubFileSystem /// /// base class for all filesystem objects - directories and files /// - public abstract class GithubFilesystemEntry : IFileSystemEntry + public abstract class GitHubFilesystemEntry : IFileSystemEntry { /// /// Github client, used to access github data @@ -38,7 +37,7 @@ public abstract class GithubFilesystemEntry : IFileSystemEntry /// branch in repository /// name of this entry /// full name, aka path of this entry - protected GithubFilesystemEntry(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) + protected GitHubFilesystemEntry(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) { GitHubClient = gitHubClient; Repository = repository; From 5c793f62991d1ddb661f9fb6ac886c641c5a383c Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 16:55:21 +0300 Subject: [PATCH 05/10] added radzen blazor framework --- .../DotNetRu.Commune.WasmClient.csproj | 1 + DotNetRu.Commune.WasmClient/Program.cs | 8 ++++++++ DotNetRu.Commune.WasmClient/Shared/MainLayout.razor | 5 +++++ DotNetRu.Commune.WasmClient/_Imports.razor | 1 + DotNetRu.Commune.WasmClient/wwwroot/index.html | 2 ++ 5 files changed, 17 insertions(+) diff --git a/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj b/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj index 5f13f53..6ca848a 100644 --- a/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj +++ b/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj @@ -13,6 +13,7 @@ + diff --git a/DotNetRu.Commune.WasmClient/Program.cs b/DotNetRu.Commune.WasmClient/Program.cs index 22c524d..f8f7702 100644 --- a/DotNetRu.Commune.WasmClient/Program.cs +++ b/DotNetRu.Commune.WasmClient/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Radzen; using Serilog; namespace DotNetRu.Commune.WasmClient @@ -29,6 +30,13 @@ public static async Task Main(string[] args) /// Конфигурация private static void ConfigureServices(IServiceCollection services, IConfiguration configuration) { + // registering radzen blazor services for using notifications, dialogs, tooltips and custom context menus + services + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped(); + services.Configure(configuration.GetSection(nameof(AuditSettings))); services.AddBizLogic(); } diff --git a/DotNetRu.Commune.WasmClient/Shared/MainLayout.razor b/DotNetRu.Commune.WasmClient/Shared/MainLayout.razor index 2c5c391..f163994 100644 --- a/DotNetRu.Commune.WasmClient/Shared/MainLayout.razor +++ b/DotNetRu.Commune.WasmClient/Shared/MainLayout.razor @@ -7,3 +7,8 @@ + + + + + diff --git a/DotNetRu.Commune.WasmClient/_Imports.razor b/DotNetRu.Commune.WasmClient/_Imports.razor index 38bbe35..504943a 100644 --- a/DotNetRu.Commune.WasmClient/_Imports.razor +++ b/DotNetRu.Commune.WasmClient/_Imports.razor @@ -8,3 +8,4 @@ @using Microsoft.JSInterop @using DotNetRu.Commune.WasmClient @using DotNetRu.Commune.WasmClient.Shared +@using Radzen.Blazor diff --git a/DotNetRu.Commune.WasmClient/wwwroot/index.html b/DotNetRu.Commune.WasmClient/wwwroot/index.html index cf39cdc..728eca1 100644 --- a/DotNetRu.Commune.WasmClient/wwwroot/index.html +++ b/DotNetRu.Commune.WasmClient/wwwroot/index.html @@ -9,6 +9,7 @@ + @@ -20,6 +21,7 @@ 🗙 + From 6df2daf364e476e38c6a61e0533c1be6c8cf6baa Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 17:24:37 +0300 Subject: [PATCH 06/10] updated wasm application to show community list --- .../ClientFactory.cs | 6 +- .../BizLayerServiceRegistry.cs | 5 +- DotNetRu.Commune.WasmClient/Pages/Index.razor | 69 ++++++++++++++----- .../wwwroot/appsettings.json | 4 +- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs index 502dffd..ada5c1a 100644 --- a/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs +++ b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs @@ -6,15 +6,15 @@ namespace DotNetRu.Commune.GithubFileSystem /// /// Factoy for building clients /// - public static class ClientFactory + public class ClientFactory { private const string ProductName = "DotNetRuCommune"; /// /// Build anonymous client - no credentials needed /// - /// - public static GitHubClient Anonymous() + /// Github client with anonymous credentials + public GitHubClient Anonymous() { var credStore = new InMemoryCredentialStore(Credentials.Anonymous); var client = new GitHubClient(new Connection(new ProductHeaderValue(ProductName), diff --git a/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs b/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs index af232e9..8889b39 100644 --- a/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs +++ b/DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using DotNetRu.Commune.GithubFileSystem; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -16,11 +17,11 @@ internal static class BizLayerServiceRegistry /// коллекция служб /// она же для соединения в цепочку /// если переданная коллекция была null - [return:NotNull] public static IServiceCollection AddBizLogic([NotNull]this IServiceCollection services) + public static IServiceCollection AddBizLogic(this IServiceCollection services) { if (services is null) throw new ArgumentNullException(nameof(services)); // здесь регистрируются службы слоя бизнес-логики - services.TryAddSingleton(); + services.TryAddSingleton(); return services; } } diff --git a/DotNetRu.Commune.WasmClient/Pages/Index.razor b/DotNetRu.Commune.WasmClient/Pages/Index.razor index a9e9161..785efb6 100644 --- a/DotNetRu.Commune.WasmClient/Pages/Index.razor +++ b/DotNetRu.Commune.WasmClient/Pages/Index.razor @@ -3,35 +3,68 @@ @using Microsoft.Extensions.Options @using DotNetRu.Commune.WasmClient.Model @using DotNetRu.Commune.GithubFileSystem +@using Radzen +@using DotNetRu.Auditor.Storage +@using DotNetRu.Auditor.Data.Model @inject ILogger _logger; -@inject GithubDirectory githubFs; +@inject ClientFactory _clientFactory; @inject IOptions auditSettings; @inject NavigationManager _navigationManager; +@inject NotificationService _notificationService;

DotNetRu Commune

-

- Это тестовое приложение DotNetRU.Commune.
- Введите свой PAT для авторизации клиента github:
- - -

+
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + +
+
+
@code { - string pat = string.Empty; - - private async Task ProvidePat() + private readonly List _communities = new(); + protected override async Task OnInitializedAsync() { - try - { - await githubFs.StartContext(pat, auditSettings.Value.RepositoryName, auditSettings.Value.OriginalOwner); - _navigationManager.NavigateTo("AuthSuccess"); - } - catch (Exception e) + var client = _clientFactory.Anonymous(); + + var repository = await client.Repository.Get(auditSettings.Value.OriginalOwner, auditSettings.Value.RepositoryName); + _logger.LogDebug("Got repository for {Owner}/{RepoName}: {@Repo}", auditSettings.Value.OriginalOwner, auditSettings.Value.RepositoryName, repository); + + var masterBranch = await client.Git.Reference.Get(repository.Id, "heads/master"); + _logger.LogDebug("Got masterbransh ref: {@Branch}", masterBranch); + + var auditDirectory = GitHubDirectory.ForRoot(client, repository, masterBranch); + var store = await AuditStore.OpenAsync(auditDirectory); + var session = store.OpenSession(); + var communities = session.QueryAsync(); + _logger.LogDebug("Got communities, starting to enumerate the sequence"); + await foreach (var item in communities) { - _logger.LogError(e, "Ошибка старта контекста реадктирования"); - _navigationManager.NavigateTo("AuthFailed"); + _logger.LogDebug("Got new community: {@Commuinity}", item); + _communities.Add(item); } + await base.OnInitializedAsync(); } } diff --git a/DotNetRu.Commune.WasmClient/wwwroot/appsettings.json b/DotNetRu.Commune.WasmClient/wwwroot/appsettings.json index d797186..891984d 100644 --- a/DotNetRu.Commune.WasmClient/wwwroot/appsettings.json +++ b/DotNetRu.Commune.WasmClient/wwwroot/appsettings.json @@ -11,7 +11,7 @@ } }, "AuditSettings":{ - "RepositoryName":"DEMO", - "OriginalOwner":"SelectFromGroup-By" + "RepositoryName":"Audit", + "OriginalOwner":"DotNetRu" } } From 068ff5daa548c29e73dd51d85478d092810d24e1 Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 17:25:55 +0300 Subject: [PATCH 07/10] nuget updates --- .../DotNetRu.Commune.GithubFilesystem.csproj | 2 +- .../DotNetRu.Commune.WasmClient.csproj | 12 ++++++------ .../DotNetRu.Commune.Test.Fs.WasmClient.fsproj | 5 +++-- .../DotNetRu.Commune.Test.WasmClient.csproj | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj b/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj index d8a1bea..d2a6fb9 100644 --- a/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj +++ b/DotNetRu.Commune.GithubFilesystem/DotNetRu.Commune.GithubFilesystem.csproj @@ -9,7 +9,7 @@ - + diff --git a/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj b/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj index 6ca848a..5299770 100644 --- a/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj +++ b/DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj @@ -8,17 +8,17 @@ - - + + - - - - + + + + diff --git a/test/DotNetRu.Commune.Test.Fs.WasmClient/DotNetRu.Commune.Test.Fs.WasmClient.fsproj b/test/DotNetRu.Commune.Test.Fs.WasmClient/DotNetRu.Commune.Test.Fs.WasmClient.fsproj index d827d00..94ca427 100644 --- a/test/DotNetRu.Commune.Test.Fs.WasmClient/DotNetRu.Commune.Test.Fs.WasmClient.fsproj +++ b/test/DotNetRu.Commune.Test.Fs.WasmClient/DotNetRu.Commune.Test.Fs.WasmClient.fsproj @@ -13,16 +13,17 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all + diff --git a/test/DotNetRu.Commune.Test.WasmClient/DotNetRu.Commune.Test.WasmClient.csproj b/test/DotNetRu.Commune.Test.WasmClient/DotNetRu.Commune.Test.WasmClient.csproj index ac35d4c..5c12ab6 100644 --- a/test/DotNetRu.Commune.Test.WasmClient/DotNetRu.Commune.Test.WasmClient.csproj +++ b/test/DotNetRu.Commune.Test.WasmClient/DotNetRu.Commune.Test.WasmClient.csproj @@ -7,13 +7,13 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all From c74777c6e8a2d62e91709246d39223f0f99949ea Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 17:43:25 +0300 Subject: [PATCH 08/10] fixing community loading --- DotNetRu.Commune.WasmClient/Pages/Index.razor | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/DotNetRu.Commune.WasmClient/Pages/Index.razor b/DotNetRu.Commune.WasmClient/Pages/Index.razor index 785efb6..b1d2cec 100644 --- a/DotNetRu.Commune.WasmClient/Pages/Index.razor +++ b/DotNetRu.Commune.WasmClient/Pages/Index.razor @@ -24,7 +24,7 @@
- + @@ -44,7 +44,9 @@ @code { - private readonly List _communities = new(); + private List _communities = new(); + private int _communityCnt; + private RadzenDataGrid? _dataGrid; protected override async Task OnInitializedAsync() { var client = _clientFactory.Anonymous(); @@ -60,11 +62,14 @@ var session = store.OpenSession(); var communities = session.QueryAsync(); _logger.LogDebug("Got communities, starting to enumerate the sequence"); + var buffer = new List(); await foreach (var item in communities) { _logger.LogDebug("Got new community: {@Commuinity}", item); - _communities.Add(item); + buffer.Add(item); } + _communities = buffer; + _communityCnt = _communities.Count; await base.OnInitializedAsync(); } } From 5df734a06230c1ed67570e137146717d58f16fcb Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 18:01:53 +0300 Subject: [PATCH 09/10] added factory method for authenticated clients --- .../ClientFactory.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs index ada5c1a..009dd7f 100644 --- a/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs +++ b/DotNetRu.Commune.GithubFilesystem/ClientFactory.cs @@ -23,5 +23,20 @@ public GitHubClient Anonymous() new SimpleJsonSerializer())); return client; } + + /// + /// Get an authenticated client with specific token + /// + /// personal access token for github + /// GitHUb client with injeccted personal access token + public GitHubClient WithToken(string token) + { + var credStore = new InMemoryCredentialStore(new (token, AuthenticationType.Bearer)); + var client = new GitHubClient(new Connection(new ProductHeaderValue(ProductName), + GitHubClient.GitHubApiUrl, credStore, + new HttpClientAdapter(Net5HttpMessageHandlerFactory.CreateDefault), + new SimpleJsonSerializer())); + return client; + } } } From 2841d73a860a69fdbdeb9dfa3acab1dc5f668b77 Mon Sep 17 00:00:00 2001 From: zetroot Date: Sun, 7 Nov 2021 18:28:05 +0300 Subject: [PATCH 10/10] using base database directory for auditor store --- DotNetRu.Commune.WasmClient/Pages/Index.razor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DotNetRu.Commune.WasmClient/Pages/Index.razor b/DotNetRu.Commune.WasmClient/Pages/Index.razor index b1d2cec..19d2429 100644 --- a/DotNetRu.Commune.WasmClient/Pages/Index.razor +++ b/DotNetRu.Commune.WasmClient/Pages/Index.razor @@ -57,7 +57,7 @@ var masterBranch = await client.Git.Reference.Get(repository.Id, "heads/master"); _logger.LogDebug("Got masterbransh ref: {@Branch}", masterBranch); - var auditDirectory = GitHubDirectory.ForRoot(client, repository, masterBranch); + var auditDirectory = GitHubDirectory.ForRoot(client, repository, masterBranch).GetDirectory("db"); var store = await AuditStore.OpenAsync(auditDirectory); var session = store.OpenSession(); var communities = session.QueryAsync();