1

我有一个类 GitHub,其中一种方法应该返回 GitHub 上特定用户名和 repo 的所有提交列表:

using System;
using Octokit;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ReadRepo
{
    public class GitHub
    {
        public GitHub()
        {
        }

        public async Task<List<GitHubCommit>> getAllCommits()
        {            
            string username = "lukalopusina";
            string repo = "flask-microservices-main";

            var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp"));
            var repository = await github.Repository.Get(username, repo);
            var commits = await github.Repository.Commit.GetAll(repository.Id);

            List<GitHubCommit> commitList = new List<GitHubCommit>();

            foreach(GitHubCommit commit in commits) {
                commitList.Add(commit);
            }

            return commitList;
        }

    }
}

我有调用 getAllCommits 方法的主函数:

using System;
using Octokit;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ReadRepo
{
    class MainClass
    {

        public static void Main(string[] args)
        {            

            GitHub github = new GitHub();

            Task<List<GitHubCommit>> commits = github.getAllCommits();
            commits.Wait(10000);

            foreach(GitHubCommit commit in commits.Result) {                
                foreach (GitHubCommitFile file in commit.Files)
                    Console.WriteLine(file.Filename);    
            }

        }
    }
}

当我运行它时,我收到以下错误:

在此处输入图像描述

问题是因为这个变量 commit.Files 是 Null 可能是因为异步调用,但我不知道如何解决它。请帮忙?

4

1 回答 1

3

我的猜测是,如果您需要获取所有提交的文件列表,则需要通过使用分别获取每个提交

foreach(GitHubCommit commit in commits) 
{
    var commitDetails = github.Repository.Commit.Get(commit.Sha);
    var files = commitDetails.Files;
}

看看这个。还描述了另一种实现目标的方法 - 首先获取存储库中所有文件的列表,然后获取每个文件的提交列表。

于 2017-08-09T10:08:20.267 回答