forked from microsoft/PullRequestQuantifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registrar.cs
62 lines (58 loc) · 3.21 KB
/
Registrar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace PullRequestQuantifier.GitHub.Client
{
using System.Collections.Generic;
using GitHubJwt;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using PullRequestQuantifier.Common;
using PullRequestQuantifier.Common.Azure.ServiceBus;
using PullRequestQuantifier.Common.Azure.Telemetry;
using PullRequestQuantifier.GitHub.Client.Events;
using PullRequestQuantifier.GitHub.Common.Events;
using PullRequestQuantifier.GitHub.Common.GitHubClient;
public static class Registrar
{
public static IServiceCollection RegisterServices(
this IServiceCollection serviceCollection,
IConfiguration configuration)
{
serviceCollection.Configure<GitHubAppFlavorSettings>(configuration.GetSection(nameof(GitHubAppFlavorSettings)));
serviceCollection.Configure<AzureServiceBusSettings>(
configuration.GetSection(nameof(AzureServiceBusSettings)));
serviceCollection.AddSingleton<IReadOnlyDictionary<string, IGitHubJwtFactory>>(
sp =>
{
// register a GitHubJwtFactory used to create tokens to access github for a particular org on behalf of the app
var gitHubAppFlavorSettings = sp.GetRequiredService<IOptions<GitHubAppFlavorSettings>>().Value;
ArgumentCheck.ParameterIsNotNull(gitHubAppFlavorSettings, nameof(gitHubAppFlavorSettings));
var ret = new Dictionary<string, IGitHubJwtFactory>();
foreach (var gitHubAppSettings in gitHubAppFlavorSettings.GitHubAppsSettings)
{
// Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file
ret[gitHubAppSettings.Key] = new GitHubJwtFactory(
new StringPrivateKeySource(gitHubAppSettings.Value.PrivateKey),
new GitHubJwtFactoryOptions
{
AppIntegrationId = int.Parse(gitHubAppSettings.Value.Id), // The GitHub App Id
ExpirationSeconds = 590 // 10 minutes is the maximum time allowed
});
}
return ret;
});
serviceCollection.AddSingleton<IGitHubClientAdapterFactory, GitHubClientAdapterFactory>();
serviceCollection.TryAddSingleton<IEventBus, AzureServiceBus>();
serviceCollection.TryAddEnumerable(
new[]
{
ServiceDescriptor.Singleton<IGitHubEventHandler, PullRequestEventHandler>(),
ServiceDescriptor.Singleton<IGitHubEventHandler, InstallationEventHandler>(),
ServiceDescriptor.Singleton<IGitHubEventHandler, InstallationRepositoriesEventHandler>()
});
serviceCollection.AddHostedService<GitHubEventHost>();
serviceCollection.AddApmForWebHost(configuration, typeof(Registrar).Namespace);
return serviceCollection;
}
}
}