Files
autogen/src/libs/Microsoft.AI.DevTeam/Services/GithubAuthService.cs
Kosta Petan baabd413eb Update docs (#11)
* docs update WIP

* getting started guide updated

* update getting started guide

* clarify github app creation

* add webhook secret to getting started guide and gh-flow app

* restructure Readme

* fix the Organization assumption

* add mermaid diagram of the event flow

* devtunnel feature to devcontainer

* throw all the exceptions and add the history to the prompt

* Update github-flow.md

* update readme
2024-03-14 16:29:23 +01:00

47 lines
1.7 KiB
C#

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;
namespace Microsoft.AI.DevTeam;
public class GithubAuthService
{
private readonly GithubOptions _githubSettings;
private readonly ILogger<GithubAuthService> _logger;
public GithubAuthService(IOptions<GithubOptions> ghOptions, ILogger<GithubAuthService> logger)
{
_githubSettings = ghOptions.Value;
_logger = logger;
}
public async Task<GitHubClient> GetGitHubClient()
{
try
{
// Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file
var generator = new GitHubJwt.GitHubJwtFactory(
new GitHubJwt.StringPrivateKeySource(_githubSettings.AppKey),
new GitHubJwt.GitHubJwtFactoryOptions
{
AppIntegrationId = _githubSettings.AppId, // The GitHub App Id
ExpirationSeconds = 600 // 10 minutes is the maximum time allowed
}
);
var jwtToken = generator.CreateEncodedJwtToken();
var appClient = new GitHubClient(new ProductHeaderValue("SK-DEV-APP"))
{
Credentials = new Credentials(jwtToken, AuthenticationType.Bearer)
};
var response = await appClient.GitHubApps.CreateInstallationToken(_githubSettings.InstallationId);
return new GitHubClient(new ProductHeaderValue($"SK-DEV-APP-Installation{_githubSettings.InstallationId}"))
{
Credentials = new Credentials(response.Token)
};
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting GitHub client");
throw;
}
}
}