我希望能够显示我的 Github 配置文件中的所有提交,我确实设法列出了一个存储库的存储库和提交,但不能全部提交。
错误消息 以下是我的代码:
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);
}
}
}
👋这里的维护者 Octokit.net。
Octokit.net 中每个基于集合的 API 返回的IReadOnlyList<T>
是一个接口,它是 .NET Framework 的一部分,我们用它来指示从 GitHub API 返回的响应不可变,并且它缺少Add
或Append
等可变 API,这就是您的示例无法编译的原因。
特别是这一行:
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);
}
}
}