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

Grant access to the repo #22

Merged
merged 1 commit into from
Jun 23, 2024
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
1 change: 1 addition & 0 deletions EduAutomation/Application/GitHub/IGitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ namespace EduAutomation.Application.GitHub;
public interface IGitHubService
{
Task<RepoAssignResponse> AssignRepoToAppropriateTeams(Repo repo);
Task<RepoAssignResponse> AssignRepo(GrantRepoAccess req);
}
7 changes: 7 additions & 0 deletions EduAutomation/Domain/GitHub/GrantRepoAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EduAutomation.Domain.GitHub;

public record GrantRepoAccess(
string Organization,
string Repository,
string UserOrTeamName,
bool IsUser);
15 changes: 15 additions & 0 deletions EduAutomation/Infrastructure/GitHub/GitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

internal class GitHubService(
IGitHubClientWrapper github,
ILogger<GitHubService> logger) : IGitHubService

Check warning on line 9 in EduAutomation/Infrastructure/GitHub/GitHubService.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Parameter 'logger' is unread.

Check warning on line 9 in EduAutomation/Infrastructure/GitHub/GitHubService.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Parameter 'logger' is unread.
{
public async Task<RepoAssignResponse> AssignRepoToAppropriateTeams(Repo repo)
{
Expand All @@ -21,6 +21,21 @@
return MapResponses(teamInfos, responses);
}

public async Task<RepoAssignResponse> 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<int, string> teamInfos,
List<GitHubUtils.Core.RepoAssignResponse> responses)
Expand Down
1 change: 1 addition & 0 deletions EduAutomation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
app.UseHttpsRedirection();

app.MapPost("/api/github-repo-webhook", GitHubEndpoints.RepositoryCreatedWebhook);
app.MapPost("/api/github/grant-access", GitHubEndpoints.GrantAccessToRepo);

app.Run();
23 changes: 22 additions & 1 deletion EduAutomation/Rest/GitHub/GitHubEndpoints.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,4 +29,24 @@ public static async ValueTask<IResult> RepositoryCreatedWebhook(
await webHookService.HandleRepoCreated(payload.ToDomainModel(), true, true);
return Results.Ok();
}

public static async Task<IResult> GrantAccessToRepo(
GrantRepoAccessRequest request,
CancellationToken cancellationToken,
[FromServices] IGitHubService githubService)
{
if (string.IsNullOrEmpty(request.Repository) ||
string.IsNullOrEmpty(request.UserOrTeamName))
{
var errors = new Dictionary<string, string[]>
{
{ "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);
}
}
12 changes: 12 additions & 0 deletions EduAutomation/Rest/GitHub/Mappers/GrantRepoAccessMapper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions EduAutomation/Rest/GitHub/Models/GrantRepoAccessRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EduAutomation.Rest.GitHub.Models;

/// <summary>
/// Represents a request to assign access to a user or team within a GitHub organization.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="Organization">The name of the GitHub organization.</param>
/// <param name="Repository">The name of the GitHub repository.</param>
/// <param name="UserOrTeamName">The username (if `IsUser` is true) or the team name (if `IsUser` is false) to grant access to.</param>
/// <param name="IsUser">Indicates whether access is being granted to a user (`true`) or a team (`false`).</param>
public record GrantRepoAccessRequest(
string Organization,
string Repository,
string UserOrTeamName,
bool IsUser);
Loading