0

我希望能够显示我的 Github 个人资料中的所有提交,我确实设法列出了一个 repo 的 repos 和提交,但不能全部列出。

错误消息 下面是我的代码:

    public IReadOnlyList<Repository> Repositories { get; set; }

    public IReadOnlyList<GitHubCommit> Commits = new List<GitHubCommit>();

    public async Task OnGetAsync()
    {
        if (User.Identity.IsAuthenticated)
        {
            GitHubName = User.FindFirst(c => c.Type == ClaimTypes.Name)?.Value;
            GitHubLogin = User.FindFirst(c => c.Type == "urn:github:login")?.Value;
            GitHubUrl = User.FindFirst(c => c.Type == "urn:github:url")?.Value;
            GitHubAvatar = User.FindFirst(c => c.Type == "urn:github:avatar")?.Value;

            string accessToken = await HttpContext.GetTokenAsync("access_token");

            var github = new GitHubClient(new ProductHeaderValue("CommitView"), new InMemoryCredentialStore(new Credentials(accessToken)));

            Repositories = await github.Repository.GetAllForCurrent();

            foreach (var reppo in Repositories)
            {

                var repoCommits = await github.Repository.Commit.GetAll(reppo.Id);
                Commits.Append(repoCommits);

            }


        }
    }
4

1 回答 1

1

Octokit.net 维护者在这里。

IReadOnlyList<T>Octokit.net 中每个基于集合的 API 返回的是一个接口,它是 .NET 框架的一部分,我们使用它来表示从 GitHub API 返回的响应是不可变的,并且它缺少像 or 这样的可变API AddAppend即为什么您的示例无法编译。

特别是这一行:

public IReadOnlyList<GitHubCommit> Commits = new List<GitHubCommit>();

IReadOnlyList<T>可以像其他集合类型一样枚举,因此让您的示例工作的最快方法是使用List<T>支持添加一系列元素的 API:

public List<Repository> Repositories { get; set; }
public List<GitHubCommit> Commits = new List<GitHubCommit>();

public async Task OnGetAsync()
{
  if (User.Identity.IsAuthenticated)
  {
    GitHubName = User.FindFirst(c => c.Type == ClaimTypes.Name)?.Value;
    GitHubLogin = User.FindFirst(c => c.Type == "urn:github:login")?.Value;
    GitHubUrl = User.FindFirst(c => c.Type == "urn:github:url")?.Value;
    GitHubAvatar = User.FindFirst(c => c.Type == "urn:github:avatar")?.Value;

    string accessToken = await HttpContext.GetTokenAsync("access_token");

    var github = new GitHubClient(new ProductHeaderValue("CommitView"), new InMemoryCredentialStore(new Credentials(accessToken)));

    var repositories = await github.Repository.GetAllForCurrent();
    Repositories = new List<Repository>(repositories);

    foreach (var repo in Repositories)
    {
      var repoCommits = await github.Repository.Commit.GetAll(repo.Id);
      Commits.AddRange(repoCommits);
    }
  }
}
于 2020-02-15T16:29:28.150 回答