如何使用octokit和c#

时间:2017-08-09 08:24:56

标签: c# .net github-api octokit.net

我有一个类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);    
            }

        }
    }
}

当我运行此操作时,我收到以下错误:

enter image description here

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

1 个答案:

答案 0 :(得分:2)

我的猜测是,如果您需要获取所有提交的文件列表,您需要使用

分别获取每个提交
foreach(GitHubCommit commit in commits) 
{
    var commitDetails = github.Repository.Commit.Get(commit.Sha);
    var files = commitDetails.Files;
}

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