Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #423 from kysect/fix/use-org-client
Browse files Browse the repository at this point in the history
Use service organization client instead of app client
  • Loading branch information
FrediKats authored Sep 16, 2022
2 parents fc32cd0 + 32aa9d4 commit a248bcd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Docs/TechDocs/GithubSetup.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ dotnet user-secrets set "GithubIntegrationConfiguration:GithubAuthConfiguration:
6. Add Webhook secret and save to dotnet secrets as `GithubIntegrationConfiguration:GithubAppConfiguration:GithubAppSecret`
7. Generate private key (*.pem file) and save to dotnet secrets as `GithubIntegrationConfiguration:GithubAppConfiguration:PrivateKey`
9. After app creation add App ID into `appsettings.json` as `GithubIntegrationConfiguration:GithubAppConfiguration:AppIntegrationId`
10. If you need to test Github App in another organization, you need to make your app public in Github App settings
11. Add App into your test organization (go to https://github.com/apps/your-app-name)
10. Add `GithubIntegrationConfiguration:GithubAppConfiguration:ServiceOrganizationName` into `appsettings.json` and set this field with the value `kysect`.
11. If you need to test Github App in another organization, you need to make your app public in Github App settings
12. Add App into your test organization (go to https://github.com/apps/your-app-name)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Octokit;

namespace Kysect.Shreks.Integration.Github.Client;

public interface IServiceOrganizationGithubClientProvider
{
Task<GitHubClient> GetClient();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Octokit;

namespace Kysect.Shreks.Integration.Github.Client;

public class ServiceOrganizationGithubClientProvider : IServiceOrganizationGithubClientProvider
{
private readonly IGitHubClient _appClient;
private readonly IInstallationClientFactory _clientFactory;
private readonly string _serviceOrganization;

public ServiceOrganizationGithubClientProvider(IGitHubClient appClient, IInstallationClientFactory clientFactory, string serviceOrganization)
{
ArgumentNullException.ThrowIfNull(appClient);
ArgumentNullException.ThrowIfNull(clientFactory);
ArgumentNullException.ThrowIfNull(serviceOrganization);

_appClient = appClient;
_clientFactory = clientFactory;
_serviceOrganization = serviceOrganization;
}

public async Task<GitHubClient> GetClient()
{
Installation installation = await _appClient.GitHubApps.GetOrganizationInstallationForCurrent(_serviceOrganization);
return _clientFactory.GetClient(installation.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ private static IServiceCollection AddClientFactory(

services.AddSingleton<IOrganizationGithubClientProvider, OrganizationGithubClientProvider>();

services.AddSingleton<IServiceOrganizationGithubClientProvider, ServiceOrganizationGithubClientProvider>(serviceProvider =>
{
IGitHubClient appClient = serviceProvider.GetRequiredService<IGitHubClient>();
IInstallationClientFactory installationClientFactory = serviceProvider.GetRequiredService<IInstallationClientFactory>();

return new ServiceOrganizationGithubClientProvider(appClient, installationClientFactory, githubIntegrationConfiguration.GithubAppConfiguration.ServiceOrganizationName);
});

return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public class GithubAppConfiguration : IShreksConfiguration
public string? PrivateKey { get; init; }
public int AppIntegrationId { get; init; }
public int JwtExpirationSeconds { get; init; }
public string? GithubAppSecret { get; set; }
public string? GithubAppSecret { get; init; }
public string? ServiceOrganizationName { get; init; }

public void Verify()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using Kysect.Shreks.Application.GithubWorkflow.Abstractions;
using Kysect.Shreks.Integration.Github.Client;
using Octokit;

namespace Kysect.Shreks.Integration.Github.Providers;

public class GithubUserProvider : IGithubUserProvider
{
private readonly IGitHubClient _appClient;
private readonly IServiceOrganizationGithubClientProvider _clientProvider;

public GithubUserProvider(IGitHubClient appClient)
public GithubUserProvider(IServiceOrganizationGithubClientProvider clientProvider)
{
_appClient = appClient;
_clientProvider = clientProvider;
}

public async Task<bool> IsGithubUserExists(string username)
{
ArgumentNullException.ThrowIfNull(username);

GitHubClient client = await _clientProvider.GetClient();

try
{
User user = await _appClient.User.Get(username);
User user = await client.User.Get(username);

return user.Login.Equals(username, StringComparison.Ordinal);
}
Expand Down

0 comments on commit a248bcd

Please sign in to comment.