diff --git a/GitHub.Octokit.sln b/GitHub.Octokit.sln index e765f3a8..4397c145 100644 --- a/GitHub.Octokit.sln +++ b/GitHub.Octokit.sln @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "test\Tests.csproj", "{7EF0200D-9F10-4A77-8F73-3E26D3EBD326}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cli", "cli\cli.csproj", "{3A6900D7-23D6-4AE5-B76A-76A05CD336F1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/cli/Program.cs b/cli/Program.cs new file mode 100644 index 00000000..b2d5917e --- /dev/null +++ b/cli/Program.cs @@ -0,0 +1,50 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using GitHub; +using GitHub.Octokit.Authentication; +using GitHub.Octokit.Client; +using Org.BouncyCastle.Ocsp; + +// Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file +var generator = new GitHubJwt.GitHubJwtFactory( + new GitHubJwt.FilePrivateKeySource("/home/kfcampbell/github/dev/dotnet-sdk/kfcampbell-terraform-provider.2024-04-30.private-key.pem"), + new GitHubJwt.GitHubJwtFactoryOptions + { + AppIntegrationId = 131977, // The GitHub App Id + ExpirationSeconds = 600 // 10 minutes is the maximum time allowed + } +); +var jwtToken = generator.CreateEncodedJwtToken(); + +var installationId = 20570954; + +var client = new HttpClient(); +client.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json"); +client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28"); +client.DefaultRequestHeaders.Add("User-Agent", "csharp-dummy-app-example"); +client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jwtToken}"); + +var resp = await client.PostAsync($"https://api.github.com/app/installations/{installationId}/access_tokens", null); +var body = await resp.Content.ReadAsStringAsync(); + +if (!resp.IsSuccessStatusCode) { + Console.WriteLine($"Error: {resp.StatusCode}"); + Console.WriteLine($"Body: {body}"); + return; +} + +var respBody = JsonSerializer.Deserialize>(body); +var tokenElement = (JsonElement)respBody["token"]; +var token = tokenElement.ValueKind == JsonValueKind.String ? tokenElement.GetString() : ""; + +// var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? ""; +var adapter = RequestAdapter.Create(new TokenAuthenticationProvider("Octokit.Gen", token)); +// adapter.BaseUrl = "http://api.github.localhost:1024"; +var gitHubClient = new GitHubClient(adapter); + +try { + var response = await gitHubClient.Installation.Repositories.GetAsync(); + response.Repositories.ForEach(repo => Console.WriteLine(repo.FullName)); +} catch (Exception e) { + Console.WriteLine(e.Message); +} diff --git a/cli/cli.csproj b/cli/cli.csproj new file mode 100644 index 00000000..b63e6015 --- /dev/null +++ b/cli/cli.csproj @@ -0,0 +1,24 @@ + + + Exe + net8.0 + enable + enable + + + $(RestoreSources);../src/bin/Debug;https://api.nuget.org/v3/index.json + + + + + + + + + + + + + + +