-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHub.cs
50 lines (40 loc) · 1.96 KB
/
GitHub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Octokit;
using System;
using System.Threading.Tasks;
namespace MastodonGitHubBot;
internal static class GitHub
{
public static async Task<GitHubClient> GetClientAsync(Log log, Server server)
{
ArgumentException.ThrowIfNullOrEmpty(server.GitHubClientId);
ArgumentException.ThrowIfNullOrEmpty(server.GitHubSecret);
ArgumentException.ThrowIfNullOrEmpty(server.GitHubUserName);
GitHubClient github = new(new ProductHeaderValue(server.AppName));
string accessToken = await GetAccessTokenAsync(log, server, github);
github.Credentials = new Credentials(accessToken);
return github;
}
private static async Task<string> GetAccessTokenAsync(Log log, Server server, GitHubClient github)
{
if (string.IsNullOrWhiteSpace(server.GitHubAccessToken))
{
OauthLoginRequest oauthLoginRequest = new(server.GitHubClientId);
oauthLoginRequest.Scopes.Add(server.GitHubUserName);
oauthLoginRequest.Scopes.Add("public_repo");
string authCode = GetGitHubOAuthCode(log, github, oauthLoginRequest);
OauthTokenRequest oauthTokenRequest = new(server.GitHubClientId, server.GitHubSecret, authCode);
OauthToken accessToken = await github.Oauth.CreateAccessToken(oauthTokenRequest);
server.GitHubAccessToken = accessToken.AccessToken;
}
return server.GitHubAccessToken;
}
private static string GetGitHubOAuthCode(Log log, GitHubClient github, OauthLoginRequest oauthLoginRequest)
{
Uri url = github.Oauth.GetGitHubLoginUrl(oauthLoginRequest);
log.WriteWarning($"Go to the authorization page '{url}' then paste the authentication code in the console.");
Console.Write("Paste the authentication code: ");
string authCode = Console.ReadLine() ?? throw new NullReferenceException(nameof(authCode));
ArgumentException.ThrowIfNullOrEmpty(authCode);
return authCode;
}
}