Skip to content

Commit

Permalink
Merge pull request #9 from vicheanath/git-repo
Browse files Browse the repository at this point in the history
Add Test
  • Loading branch information
vicheanath authored Sep 18, 2024
2 parents dd4c525 + 5795866 commit 53f7474
Show file tree
Hide file tree
Showing 28 changed files with 357 additions and 139 deletions.
31 changes: 29 additions & 2 deletions src/SearchBugs.Api/Endpoints/RepoEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SearchBugs.Application.Git.CommitChanges;
using SearchBugs.Application.Git.CreateGitRepo;
using SearchBugs.Application.Git.DeleteGitRepo;
using SearchBugs.Application.Git.GetCommitDiff;
using SearchBugs.Application.Git.GetGitRepo;
using SearchBugs.Application.Git.GetGitReposDetails;
using SearchBugs.Application.Git.GetListTree;
using SearchBugs.Application.Git.GitHttpServer;

namespace SearchBugs.Api.Endpoints;
Expand All @@ -29,6 +32,32 @@ public static void MapRepoEndpoints(this WebApplication app)
repo.MapGet("{url}/{path}", GetRepositoryDetails).WithName(nameof(GetRepositoryDetails));
repo.MapPost("", CreateRepository).WithName(nameof(CreateRepository));
repo.MapDelete("{url}", DeleteRepository).WithName(nameof(DeleteRepository));
repo.MapGet("{url}/commit/{commitSha}", GetCommitDiff).WithName(nameof(GetCommitDiff));
repo.MapPost("{url}/commit/{commitSha}", CommitChanges).WithName(nameof(CommitChanges));
repo.MapGet("{url}/tree/{commitSha}", GetTree).WithName(nameof(GetTree));
}

public static async Task<IResult> GetCommitDiff(string url, string commitSha, ISender sender)
{
var query = new GetCommitDiffQuery(url, commitSha);
var result = await sender.Send(query);
return Results.Ok(result);
}

public record CommitChangeRequest(string Author, string Email, string Message, string Content);

public static async Task<IResult> CommitChanges([FromBody] CommitChangeRequest request, string url, string commitSha, ISender sender)
{
var command = new CommitChangeCommand(url, request.Author, request.Email, request.Message, request.Content);
var result = await sender.Send(command);
return Results.Ok(result);
}

public static async Task<IResult> GetTree(string url, string commitSha, ISender sender)
{
var query = new GetListTreeQuery(url, commitSha);
var result = await sender.Send(query);
return Results.Ok(result);
}

public static async Task<IResult> GetRepositoryDetails(string url, string path, ISender sender)
Expand All @@ -46,8 +75,6 @@ public static async Task<IResult> GetRepositories(ISender sender)
return Results.Ok(result);
}



public static async Task<IResult> CreateRepository([FromBody] CreateGitRepositoryRequest request, ISender sender)
{
var command = new CreateGitRepoCommand(request.Name, request.Description, request.Url, request.ProjectId);
Expand Down
63 changes: 34 additions & 29 deletions src/SearchBugs.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@
using SearchBugs.Infrastructure;
using SearchBugs.Persistence;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddInfrastructure();
builder.Services.AddPersistence(builder.Configuration);
builder.Services.AddApplication();

builder.Services.AddHttpContextAccessor();

var app = builder.Build();

if (app.Environment.IsDevelopment())
public partial class Program
{
app.UseSwagger();
app.UseSwaggerUI();
//app.ApplyMigrations();
private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddInfrastructure();
builder.Services.AddPersistence(builder.Configuration);
builder.Services.AddApplication();

builder.Services.AddHttpContextAccessor();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
//app.ApplyMigrations();
}
app.MapAuthenticationsEndpoints();
app.MapBugsEndpoints();
app.MapUserEndpoints();
app.MapProjectsEndpoints();
app.MapRepoEndpoints();

app.UseHttpsRedirection();
app.UseAuthentication();

app.UseMiddleware<ExceptionHandlingMiddleware>();
app.Run();
}
}
app.MapAuthenticationsEndpoints();
app.MapBugsEndpoints();
app.MapUserEndpoints();
app.MapProjectsEndpoints();
app.MapRepoEndpoints();

app.UseHttpsRedirection();
app.UseAuthentication();

//app.UseAuthorization();
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.Run();

public partial class Program { }
11 changes: 7 additions & 4 deletions src/SearchBugs.Api/SearchBugs.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<UserSecretsId>3cad7f34-1c5c-43a6-a304-e97d0203b339</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<Compile Remove="repo\string\**" />
<Content Remove="repo\string\**" />
<EmbeddedResource Remove="repo\string\**" />
<None Remove="repo\string\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
Expand All @@ -24,8 +31,4 @@
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="repo\string\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Shared.Messaging;

namespace SearchBugs.Application.Git.CommitChanges;

public record CommitChangeCommand(string Url, string AuthorName, string AuthorEmail, string CommitMessage, string FileContent) : ICommand;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SearchBugs.Domain.Git;
using Shared.Messaging;
using Shared.Results;

namespace SearchBugs.Application.Git.CommitChanges;

internal sealed class CommitChangeCommandHandler : ICommandHandler<CommitChangeCommand>
{
private readonly IGitRepositoryService _gitRepositoryService;
public CommitChangeCommandHandler(IGitRepositoryService gitRepositoryService)
{
_gitRepositoryService = gitRepositoryService;
}
public Task<Result> Handle(CommitChangeCommand request, CancellationToken cancellationToken)
{
return Task.FromResult(_gitRepositoryService.CommitChanges(request.Url, request.AuthorName, request.AuthorEmail, request.CommitMessage));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using FluentValidation;

namespace SearchBugs.Application.Git.CommitChanges;

internal sealed class CommitChangeCommandValidator : AbstractValidator<CommitChangeCommand>
{
public CommitChangeCommandValidator()
{
RuleFor(x => x.Url).NotEmpty();
RuleFor(x => x.AuthorName).NotEmpty();
RuleFor(x => x.AuthorEmail).NotEmpty();
RuleFor(x => x.CommitMessage).NotEmpty();
RuleFor(x => x.FileContent).NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Shared.Messaging;

namespace SearchBugs.Application.Git.GetCommitDiff;

public record CommitDiffResult(string FilePath, string OldPath, string Status, string Patch) : IQuery<IEnumerable<CommitDiffResult>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


using Shared.Messaging;

namespace SearchBugs.Application.Git.GetCommitDiff;

public record GetCommitDiffQuery(string Url, string CommitSha) : IQuery<IEnumerable<CommitDiffResult>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using SearchBugs.Domain.Git;
using Shared.Messaging;
using Shared.Results;

namespace SearchBugs.Application.Git.GetCommitDiff;

internal sealed class GetCommitDiffQueryHandler : IQueryHandler<GetCommitDiffQuery, IEnumerable<CommitDiffResult>>
{
private readonly IGitRepositoryService _gitRepositoryService;

public GetCommitDiffQueryHandler(IGitRepositoryService gitRepositoryService)
{
_gitRepositoryService = gitRepositoryService;
}

public Task<Result<IEnumerable<CommitDiffResult>>> Handle(GetCommitDiffQuery request, CancellationToken cancellationToken)
{
var commitDiffResult = _gitRepositoryService.GetCommitDiff(request.Url, request.CommitSha);
if (commitDiffResult.IsFailure)
{
return Task.FromResult(Result.Failure<IEnumerable<CommitDiffResult>>(commitDiffResult.Error));
}

var commitDiffs = commitDiffResult.Value.Select(fileDiff => new CommitDiffResult
(
FilePath: fileDiff.FilePath,
OldPath: fileDiff.OldPath,
Status: fileDiff.Status,
Patch: fileDiff.Patch
));

return Task.FromResult(Result.Success(commitDiffs));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Shared.Messaging;

namespace SearchBugs.Application.Git.GetFileContents;

public record GetFileContentQuery(string Url, string CommitSha, string FilePath) : IQuery<string>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using SearchBugs.Domain.Git;
using Shared.Messaging;
using Shared.Results;

namespace SearchBugs.Application.Git.GetFileContents;

internal sealed class GetFileContentQueryHandler : IQueryHandler<GetFileContentQuery, string>
{
private readonly IGitRepositoryService _gitRepositoryService;

public GetFileContentQueryHandler(IGitRepositoryService gitRepositoryService)
{
_gitRepositoryService = gitRepositoryService;
}

public Task<Result<string>> Handle(GetFileContentQuery request, CancellationToken cancellationToken)
{
return Task.FromResult(_gitRepositoryService.GetFileContent(request.Url, request.CommitSha, request.FilePath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using FluentValidation;

namespace SearchBugs.Application.Git.GetFileContents;

internal sealed class GetFileContentQueryValidator : AbstractValidator<GetFileContentQuery>
{
public GetFileContentQueryValidator()
{
RuleFor(x => x.Url).NotEmpty();
RuleFor(x => x.CommitSha).NotEmpty();
RuleFor(x => x.FilePath).NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Shared.Messaging;

namespace SearchBugs.Application.Git.GetListTree;

public record GetListTreeQuery(string Url, string CommitSha) : IQuery<IEnumerable<GitTreeItemResult>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

using SearchBugs.Domain.Git;
using Shared.Messaging;
using Shared.Results;

namespace SearchBugs.Application.Git.GetListTree;

internal sealed class GetListTreeQueryHandler : IQueryHandler<GetListTreeQuery, IEnumerable<GitTreeItemResult>>
{
private readonly IGitRepositoryService _gitRepositoryService;

public GetListTreeQueryHandler(IGitRepositoryService gitRepositoryService)
{
_gitRepositoryService = gitRepositoryService;
}
public Task<Result<IEnumerable<GitTreeItemResult>>> Handle(GetListTreeQuery request, CancellationToken cancellationToken)
{
return Task.FromResult(_gitRepositoryService.ListTree(request.CommitSha, request.Url)
.Map(tree =>
tree.Select(item => new GitTreeItemResult(item.Path, item.Name, item.Type, ""))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FluentValidation;

namespace SearchBugs.Application.Git.GetListTree;

internal sealed class GitListTreeQueryValidator : AbstractValidator<GetListTreeQuery>
{
public GitListTreeQueryValidator()
{
RuleFor(x => x.Url).NotEmpty();
RuleFor(x => x.CommitSha).NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace SearchBugs.Application.Git.GetListTree;

public record GitTreeItemResult(string Path, string Name, string Type, string Url);
14 changes: 13 additions & 1 deletion src/SearchBugs.Domain/Git/IGitRepositoryService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
namespace SearchBugs.Domain.Git;
using SearchBugs.Infrastructure.Services;
using Shared.Results;

namespace SearchBugs.Domain.Git;

public interface IGitRepositoryService
{
Result CheckoutBranch(string repoPath, string branchName);
Result CommitChanges(string repoPath, string authorName, string authorEmail, string commitMessage);
Result<IEnumerable<FileDiff>> CompareCommits(string repoPath, string baseCommitSha, string compareCommitSha);
Result<IEnumerable<FileDiff>> GetCommitDiff(string repoPath, string commitSha);
Result<IEnumerable<Contributor>> GetContributors(string repoPath);
Result<IEnumerable<FileBlame>> GetFileBlame(string repoPath, string filePath);
Result<string> GetFileContent(string repoPath, string commitSha, string filePath);
Result<IEnumerable<GitTreeItem>> ListTree(string commitSha, string repoPath);
Result<MergeResult> MergeBranches(string repoPath, string sourceBranchName, string targetBranchName, string mergerName, string mergerEmail);
}
4 changes: 2 additions & 2 deletions src/SearchBugs.Ui/src/hooks/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ interface ErrorResult {
}

interface ApiResult<T> {
value: T[];
error: ErrorResult[];
value: T;
error: ErrorResult;
isFailure : boolean;
isSuccess : boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SearchBugs.Ui/src/layouts/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const MainLayout = () => {
</div>
<div className="flex flex-col">
<MobileSidebar />
<main className="flex flex-1 flex-col gap-4 p-4 lg:gap-6 lg:p-6">
<main className="flex flex-1 flex-col gap-4 p-4">
<Outlet />
</main>
</div>
Expand Down
Loading

0 comments on commit 53f7474

Please sign in to comment.