Search code examples
asp.netgithuboctokit

Use Octokit to list all commits in all repos on Github using Asp.net Core Razor


I want to be able to show all commits from my Github profile, i did manage to list repos and commits of one repo but can t for all.

Error msg Below is my code :

    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);

            }


        }
    }

Solution

  • 👋Octokit.net maintainer here.

    The IReadOnlyList<T> returned from each collection-based API in Octokit.net is an interface that's part of the .NET Framework which we use to signal that the response returned from the GitHub API is not mutable, and it lacks the mutable APIs like Add or Append, which is why your sample doesn't compile.

    In particular it's this line:

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

    IReadOnlyList<T> can be enumerated like other collection types, so the quickest way to get your sample working would be to use the List<T> APIs instead that support adding a range of elements:

    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);
        }
      }
    }