-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of GitHub Apps installation demo
- Loading branch information
1 parent
239513d
commit ac911d7
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |