Skip to content

Commit

Permalink
Initial commit of GitHub Apps installation demo
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed May 8, 2024
1 parent 239513d commit ac911d7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions GitHub.Octokit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions cli/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Dictionary<string, object>>(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);
}
24 changes: 24 additions & 0 deletions cli/cli.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<RestoreSources>$(RestoreSources);../src/bin/Debug;https://api.nuget.org/v3/index.json</RestoreSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitHubJwt" Version="0.0.6" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.8.4" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.4.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.1.6" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.2.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.1.4" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.1.5" />
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../src/GitHub.Octokit.SDK.csproj" />
</ItemGroup>
</Project>

0 comments on commit ac911d7

Please sign in to comment.