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

Обновление под новую версию Аудитора #148

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions DotNetRu.Commune.GithubFilesystem/ClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Octokit;
using Octokit.Internal;

namespace DotNetRu.Commune.GithubFileSystem
zetroot marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Factoy for building clients
/// </summary>
public class ClientFactory
{
private const string ProductName = "DotNetRuCommune";

/// <summary>
/// Build anonymous client - no credentials needed
/// </summary>
/// <returns>Github client with anonymous credentials</returns>
public 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;
}

/// <summary>
/// Get an authenticated client with specific token
/// </summary>
/// <param name="token">personal access token for github</param>
/// <returns>GitHUb client with injeccted personal access token</returns>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DotNetRu.Auditor" Version="0.2.0" />
<PackageReference Include="DotNetRu.Auditor" Version="0.6.0" />
<PackageReference Include="Octokit" Version="0.50.0" />
</ItemGroup>

Expand Down
86 changes: 86 additions & 0 deletions DotNetRu.Commune.GithubFilesystem/GitHubDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetRu.Auditor.Storage.FileSystem;
using Octokit;

namespace DotNetRu.Commune.GithubFileSystem
{
/// <summary>
/// Virtual directory in github repository. Implements <see cref="IDirectory"/>
/// </summary>
public class GitHubDirectory : GitHubFilesystemEntry, IDirectory
{
private GitHubDirectory(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) :
base(gitHubClient, repository, branch, name, fullName)
{
}

/// <summary>
/// Factory method for building root directory
/// </summary>
/// <param name="gitHubClient">Github client for this directory, stores authentication data in it</param>
/// <param name="repository">repository, contents are accessed to</param>
/// <param name="branch">branch in the reposiroty, to work with</param>
/// <returns>new directory instance pointing to the root of content of this branch in this repository</returns>
public static IDirectory ForRoot(IGitHubClient gitHubClient, Repository repository, Reference branch)
{
return new GitHubDirectory(gitHubClient, repository, branch, string.Empty, "/");
}

private string GetChildFullName(string childDirectoryName) =>
FullName switch
{
null => childDirectoryName,
"" => childDirectoryName,
{} s when s.EndsWith("/") => $"{FullName}{childDirectoryName}",
_ => $"{FullName}/{childDirectoryName}"
};

/// <inheritdoc />
public IDirectory GetDirectory(string childDirectoryName)
{
var childFullName = GetChildFullName(childDirectoryName);
return new GitHubDirectory(GitHubClient, Repository, Branch, childDirectoryName, childFullName);
}

/// <inheritdoc />
public IFile GetFile(string childFileName)
{
var childFullName = GetChildFullName(childFileName);
return new GitHubFile(GitHubClient, Repository, Branch, childFileName, childFullName);
}

/// <inheritdoc />
public async IAsyncEnumerable<IDirectory> 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);
}
}

/// <inheritdoc />
public async IAsyncEnumerable<IFile> 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);
}
}

/// <inheritdoc />
public override async ValueTask<bool> 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);
}
}
}
50 changes: 50 additions & 0 deletions DotNetRu.Commune.GithubFilesystem/GitHubFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DotNetRu.Auditor.Storage.FileSystem;
using Octokit;

namespace DotNetRu.Commune.GithubFileSystem
{
/// <summary>
/// Files from github repository
/// </summary>
public class GitHubFile : GitHubFilesystemEntry, IFile
{
/// <summary>
/// ctor
/// </summary>
/// <param name="gitHubClient">github client</param>
/// <param name="repository">github repository</param>
/// <param name="branch">branch in this repository</param>
/// <param name="name">file name</param>
/// <param name="fullName">file full path in repository</param>
public GitHubFile(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName) :
base(gitHubClient, repository, branch, name, fullName)
{
}

/// <inheritdoc />
public override async ValueTask<bool> 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);
}

/// <inheritdoc />
public async Task<Stream> OpenForReadAsync()
{
var contents = await ContentsClient
.GetRawContentByRef(Repository.Owner.Login, Repository.Name, FullName, Branch.Ref)
.ConfigureAwait(false);
return new MemoryStream(contents, false);
}

/// <inheritdoc />
public Task<IWritableFile?> RequestWriteAccessAsync() => throw new System.NotImplementedException();

}
}
69 changes: 69 additions & 0 deletions DotNetRu.Commune.GithubFilesystem/GitHubFilesystemEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Threading.Tasks;
using DotNetRu.Auditor.Storage.FileSystem;
using Octokit;

namespace DotNetRu.Commune.GithubFileSystem
{
/// <summary>
/// base class for all filesystem objects - directories and files
/// </summary>
public abstract class GitHubFilesystemEntry : IFileSystemEntry
{
/// <summary>
/// Github client, used to access github data
/// </summary>
protected readonly IGitHubClient GitHubClient;

/// <summary>
/// repository in github where data contents are stored
/// </summary>
protected readonly Repository Repository;

/// <summary>
/// repository branch wich contents are browsed or modified
/// </summary>
protected readonly Reference Branch;

/// <summary>
/// helper property to access contents client. Contents client is a wrapper over contents endpoint, it is used for manipulating data in repository
/// </summary>
protected IRepositoryContentsClient ContentsClient => GitHubClient.Repository.Content;

/// <summary>
/// ctor
/// </summary>
/// <param name="gitHubClient">github client</param>
/// <param name="repository">github repository</param>
/// <param name="branch">branch in repository</param>
/// <param name="name">name of this entry</param>
/// <param name="fullName">full name, aka path of this entry</param>
protected GitHubFilesystemEntry(IGitHubClient gitHubClient, Repository repository, Reference branch, string name, string fullName)
{
GitHubClient = gitHubClient;
Repository = repository;
Branch = branch;
Name = name;
FullName = fullName;
}

/// <inheritdoc />
public string Name { get; }

/// <inheritdoc />
public string FullName { get; }

/// <inheritdoc />
public abstract ValueTask<bool> ExistsAsync();

/// <summary>
/// Get parent directory name containing this entry
/// </summary>
/// <returns>path of the parent directory</returns>
protected string GetParentDirectory() =>
FullName.Remove(FullName.Length - Name.Length) switch
{
"/" => string.Empty,
{} s => s
};
}
}
68 changes: 0 additions & 68 deletions DotNetRu.Commune.GithubFilesystem/GithubFileSystem.cs

This file was deleted.

5 changes: 3 additions & 2 deletions DotNetRu.Commune.WasmClient/BizLayerServiceRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using DotNetRu.Commune.GithubFileSystem;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -16,11 +17,11 @@ internal static class BizLayerServiceRegistry
/// <param name="services">коллекция служб</param>
/// <returns>она же для соединения в цепочку</returns>
/// <exception cref="ArgumentNullException">если переданная коллекция была null</exception>
[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<GithubFileSystem.GithubFileSystem>();
services.TryAddSingleton<ClientFactory>();
return services;
}
}
Expand Down
13 changes: 7 additions & 6 deletions DotNetRu.Commune.WasmClient/DotNetRu.Commune.WasmClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.11" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Radzen.Blazor" Version="3.11.12" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.BrowserConsole" Version="1.0.0-dev-00019" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.BrowserConsole" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>

Expand Down
Loading