C# - REST API 대신 github 클라이언트 라이브러리를 통해 프로그래밍으로 접근
예를 들어, 예전에 github README에 badge를 달아본 적이 있는데요,
애저듣보잡 - Github Workflow/Actions 소개
; https://www.sysnet.pe.kr/2/0/12541#badge
이와 함께
Shields IO를 사용하면 아래와 같이 더욱 다양한 배지를 달 수 있습니다.
[그림:
https://github.com/stjeong/Utilities README의 배지]
그런데 위에서 보면 "tag"의 값으로 "v1.1.3"을 가져오는데, 예상할 수 있듯이 이런 것들은 github 측에서 제공하는 REST API를 통해 알아낼 수 있는 값입니다. 실제로 간단하게 curl을 이용해 다음과 같이 가장 최근에 릴리스된 태그 버전을 구할 수 있는데요,
C:\temp> curl --silent https://api.github.com/repos/stjeong/Utilities/releases/latest | findstr tag_name
"tag_name": "v1.1.3",
그리고 당연히 누군가는 이에 대한 C# 전용 클라이언트를 만들어 두었을 것입니다. ^^
octokit/octokit.net
; https://github.com/octokit/octokit.net
// 최소 .NET 4.6, .NET Standard 2.0
Install-Package Octokit
게다가 도움말도 잘 되어 있어서,
Octokit.net
; https://octokitnet.readthedocs.io/en/latest/
다음과 같이 특정 사용자의 repo에 대한 최신 태그 이름을 가져오도록 간단하게 코딩할 수 있습니다.
using Octokit;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// https://octokitnet.readthedocs.io/en/latest/getting-started/
GitHubClient github = new GitHubClient(new ProductHeaderValue("octokit-client"));
// https://octokitnet.readthedocs.io/en/latest/releases/
Release latest = await github.Repository.Release.GetLatest("stjeong", "Utilities");
Console.WriteLine(
"The latest release is tagged at {0} and is named {1}",
latest.TagName,
latest.Name);
}
}
참고로, github API 사용에는, 인증을 요구하는 것과 그렇지 않은 것이 있습니다. 위에서 했던 tagname을 가져오는 것은 인증이 필요없었지만 해당 repo에 대해 권한이 요구되는 것들은 반드시 사용 전에 Access token을 설정해야 정상적인 사용이 가능합니다. 이를 위해 우선 PAT를 구하고,
Creating a personal access token
; https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
아래의 예제에서 보여주듯이,
Docs » Samples » Exploring Pull Requests
; https://octokitnet.readthedocs.io/en/latest/demos/exploring-pull-requests/
InMemoryCredentialStore에 PAT 값을 담아 GitHubClient를 초기화하면 됩니다.
InMemoryCredentialStore credentials = new InMemoryCredentialStore(new Credentials("...your-token-here..."));
GitHubClient client = new GitHubClient(new ProductHeaderValue("octokit-client"), credentials);
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]