octokit / dotnet-sdk

MIT License
52 stars 8 forks source link

Octokit: .NET SDK

Build and test .NET SDK CodeQL Publish Release to NuGet

An "alpha" version of a generated .NET SDK in C# from GitHub's OpenAPI spec, built on Kiota. View on NuGet.

How do I use it?

Installation

To install the package, you can use either of the following options:

Make your first request

using GitHub;
using GitHub.Octokit.Client;
using GitHub.Octokit.Client.Authentication;

var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? "";
var request = RequestAdapter.Create(new TokenAuthProvider(new TokenProvider(token)));
var gitHubClient = new GitHubClient(request);

var pullRequests = await gitHubClient.Repos["octokit"]["octokit.net"].Pulls.GetAsync();

foreach (var pullRequest in pullRequests)
{
    Console.WriteLine($"#{pullRequest.Number} {pullRequest.Title}");
}

[!IMPORTANT] This SDK is not yet stable. Breaking changes may occur at any time.

Authentication

This SDK supports Personal Access Tokens (classic), fine-grained Personal Access Tokens, and GitHub Apps authentication.

In order to use either type of Personal Access token, you can use the TokenAuthProvider constructor option when constructing a new token provider, like so:

    var tokenProvider = new TokenProvider(Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? "");
    var adapter = RequestAdapter.Create(new TokenAuthProvider(tokenProvider));
    var gitHubClient = new GitHubClient(adapter);

In order to authenticate as a GitHub App, you can use the AppInstallationAuthProvider constructor option:

    var githubAppTokenProvider = new GitHubAppTokenProvider();
    var rsa = RSA.Create();
    rsa.ImportFromPem(PRIVATE_KEY_PATH);

    var aiAccessTokenProvider = new AppInstallationTokenProvider(CLIENT_ID, rsa, INSTALLATION_ID, githubAppTokenProvider);
    var aiAdapter = RequestAdapter.Create(new AppInstallationAuthProvider(aiAccessTokenProvider));
    var aiGitHubClient = new GitHubClient(aiAdapter);

To see more detailed examples, view the cli/ directory in this repo.

⚠️ Note: There are three types of GitHub App authentication:

  1. As the App itself (meta endpoints)
  2. As an App installation
  3. On behalf of a user

Authenticating on behalf of a user is not supported in an SDK, as it requires a UI authentication flow with redirects. This SDK supports authenticating as the App itself and as an App installation.

Note that the SDK does not yet support authenticating as the App itself and as an App installation using the same client transparently to the user. Authenticating as the App itself requires creating a JSON Web Token (JWT) and using that as token authentication. For helpers to create and sign a JWT in .NET, you may use the helpers in the GitHubAppTokenProvider class.

Why a generated SDK?

We want to...

  1. provide 100% coverage of the API in our SDK
  2. use this as a building block for future SDK tooling

Why .NET?

We have a substantial userbase that uses .NET and we wanted them to get access to our generated SDK as early as possible.

How can I report on my experience or issues with the SDK?

Please use this project's issues!

Source organization

Currently this project is fairly simple (we hope it can stay that way). All of the package based source is contained in the GitHub folder.

Testing

More details on this SDK and repo