From 26c6c533207a759a5c28990e25b51afc62f10cdb Mon Sep 17 00:00:00 2001 From: Bardin08 Date: Sun, 12 May 2024 15:08:32 +0300 Subject: [PATCH] Assign repo to the team --- .../Application/GitHub/IGitHubService.cs | 1 + .../Domain/GitHub/GrantRepoAccess.cs | 7 ++++++ .../Infrastructure/GitHub/GitHubService.cs | 15 ++++++++++++ EduAutomation/Program.cs | 1 + EduAutomation/Rest/GitHub/GitHubEndpoints.cs | 23 ++++++++++++++++++- .../GitHub/Mappers/GrantRepoAccessMapper.cs | 12 ++++++++++ .../GitHub/Models/GrantRepoAccessRequest.cs | 18 +++++++++++++++ 7 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 EduAutomation/Domain/GitHub/GrantRepoAccess.cs create mode 100644 EduAutomation/Rest/GitHub/Mappers/GrantRepoAccessMapper.cs create mode 100644 EduAutomation/Rest/GitHub/Models/GrantRepoAccessRequest.cs diff --git a/EduAutomation/Application/GitHub/IGitHubService.cs b/EduAutomation/Application/GitHub/IGitHubService.cs index fc548d1..6293cd5 100644 --- a/EduAutomation/Application/GitHub/IGitHubService.cs +++ b/EduAutomation/Application/GitHub/IGitHubService.cs @@ -6,4 +6,5 @@ namespace EduAutomation.Application.GitHub; public interface IGitHubService { Task AssignRepoToAppropriateTeams(Repo repo); + Task AssignRepo(GrantRepoAccess req); } diff --git a/EduAutomation/Domain/GitHub/GrantRepoAccess.cs b/EduAutomation/Domain/GitHub/GrantRepoAccess.cs new file mode 100644 index 0000000..6c08099 --- /dev/null +++ b/EduAutomation/Domain/GitHub/GrantRepoAccess.cs @@ -0,0 +1,7 @@ +namespace EduAutomation.Domain.GitHub; + +public record GrantRepoAccess( + string Organization, + string Repository, + string UserOrTeamName, + bool IsUser); diff --git a/EduAutomation/Infrastructure/GitHub/GitHubService.cs b/EduAutomation/Infrastructure/GitHub/GitHubService.cs index e30aa7a..9dd395d 100644 --- a/EduAutomation/Infrastructure/GitHub/GitHubService.cs +++ b/EduAutomation/Infrastructure/GitHub/GitHubService.cs @@ -21,6 +21,21 @@ public async Task AssignRepoToAppropriateTeams(Repo repo) return MapResponses(teamInfos, responses); } + public async Task AssignRepo(GrantRepoAccess req) + { + if (!req.IsUser) + { + var teamInfo = await GetTeamInfos([req.UserOrTeamName], req.Organization); + var teamIds = teamInfo.Keys.ToArray(); + + var responses = await github.GrantAccessToRepository( + new RepoAssignRequest(req.Repository, req.Organization, teamIds)); + return MapResponses(teamInfo, responses); + } + + throw new NotImplementedException("This flow is not implemented yet"); + } + private RepoAssignResponse MapResponses( IReadOnlyDictionary teamInfos, List responses) diff --git a/EduAutomation/Program.cs b/EduAutomation/Program.cs index 9439540..fc17df0 100644 --- a/EduAutomation/Program.cs +++ b/EduAutomation/Program.cs @@ -21,5 +21,6 @@ app.UseHttpsRedirection(); app.MapPost("/api/github-repo-webhook", GitHubEndpoints.RepositoryCreatedWebhook); +app.MapPost("/api/github/grant-access", GitHubEndpoints.GrantAccessToRepo); app.Run(); diff --git a/EduAutomation/Rest/GitHub/GitHubEndpoints.cs b/EduAutomation/Rest/GitHub/GitHubEndpoints.cs index 85881fc..6c63c2e 100644 --- a/EduAutomation/Rest/GitHub/GitHubEndpoints.cs +++ b/EduAutomation/Rest/GitHub/GitHubEndpoints.cs @@ -1,4 +1,5 @@ -using EduAutomation.Application.GitHub.Services; +using EduAutomation.Application.GitHub; +using EduAutomation.Application.GitHub.Services; using EduAutomation.Rest.GitHub.Mappers; using EduAutomation.Rest.GitHub.Models; using Microsoft.AspNetCore.Mvc; @@ -28,4 +29,24 @@ public static async ValueTask RepositoryCreatedWebhook( await webHookService.HandleRepoCreated(payload.ToDomainModel(), true, true); return Results.Ok(); } + + public static async Task GrantAccessToRepo( + GrantRepoAccessRequest request, + CancellationToken cancellationToken, + [FromServices] IGitHubService githubService) + { + if (string.IsNullOrEmpty(request.Repository) || + string.IsNullOrEmpty(request.UserOrTeamName)) + { + var errors = new Dictionary + { + { "Invalid request model state", ["Soma mandatory fields are not complete"] } + }; + + return Results.ValidationProblem(errors); + } + + var response = await githubService.AssignRepo(request.ToDomainModel()); + return Results.Ok(response); + } } diff --git a/EduAutomation/Rest/GitHub/Mappers/GrantRepoAccessMapper.cs b/EduAutomation/Rest/GitHub/Mappers/GrantRepoAccessMapper.cs new file mode 100644 index 0000000..9a84c7b --- /dev/null +++ b/EduAutomation/Rest/GitHub/Mappers/GrantRepoAccessMapper.cs @@ -0,0 +1,12 @@ +using EduAutomation.Domain.GitHub; +using EduAutomation.Rest.GitHub.Models; + +namespace EduAutomation.Rest.GitHub.Mappers; + +public static class GrantRepoAccessMapper +{ + public static GrantRepoAccess ToDomainModel(this GrantRepoAccessRequest r) + { + return new GrantRepoAccess(r.Organization, r.Repository, r.UserOrTeamName, r.IsUser); + } +} diff --git a/EduAutomation/Rest/GitHub/Models/GrantRepoAccessRequest.cs b/EduAutomation/Rest/GitHub/Models/GrantRepoAccessRequest.cs new file mode 100644 index 0000000..a5695b0 --- /dev/null +++ b/EduAutomation/Rest/GitHub/Models/GrantRepoAccessRequest.cs @@ -0,0 +1,18 @@ +namespace EduAutomation.Rest.GitHub.Models; + +/// +/// Represents a request to assign access to a user or team within a GitHub organization. +/// +/// +/// Later, the ability to grant specific permission levels (e.g., admin access) will be added. +/// Currently, this is not implemented due to the absence of an authentication mechanism. +/// +/// The name of the GitHub organization. +/// The name of the GitHub repository. +/// The username (if `IsUser` is true) or the team name (if `IsUser` is false) to grant access to. +/// Indicates whether access is being granted to a user (`true`) or a team (`false`). +public record GrantRepoAccessRequest( + string Organization, + string Repository, + string UserOrTeamName, + bool IsUser);