LoginSignup
0
0

More than 3 years have passed since last update.

GitHub の Organization の private レポジトリ一覧取得

Last updated at Posted at 2019-06-27

ちょっとそのような用事があったので、備忘録を残します📝

ASP.NET Core の TargetFramework は netcoreapp2.2 にします。

Refit をインストール

dotnet add package refit

API を定義

namespace GitRepo.Interfaces
{
    [Headers(
        "User-Agent: GitRepo/0.1",
        "Accept: application/vnd.github.v3+json",
        "Authorization: token"
    )]
    public interface IGitHubApi
    {
        [Get("/orgs/{org}/repos")]
        Task<FoundRepos[]> ListOrgRepos(string org, string page);
    }
}

Note: User-Agent は必須項目です。ないと 403 エラーになりました。

モデルを定義

namespace GitRepo.Models
{
    public class FoundRepos
    {
        public string name { get; set; }
        public string full_name { get; set; }
    }
}

Organization の private レポジトリ一覧を取得します。

    var api = RestService.For<IGitHubApi>("https://api.github.com", new RefitSettings
    {
        AuthorizationHeaderValueGetter = () => Task.FromResult("TOKEN"),
    });

    var allRepos = new List<FoundRepos>();
    for (int page = 1; ; page++)
    {
        var reposList = await api.ListOrgRepos(
            org: "ORGNAME",
            page: page.ToString()
        );
        if (reposList.Length == 0)
        {
            break;
        }
        allRepos.AddRange(reposList);
    }

TOKEN は Edit personal access token で取得してください。

private レポジトリ一覧化に必要な scope は repo です。

image.png

今回のポイントは、繰り返し取得が必要な点です (ページネーションされているので)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0